1
2 ///////////////////////////////////////////////////////////
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Tool Library //
9 // //
10 // io_gdal //
11 // //
12 //-------------------------------------------------------//
13 // //
14 // gdal_export.cpp //
15 // //
16 // Copyright (C) 2007 O. Conrad //
17 // //
18 //-------------------------------------------------------//
19 // //
20 // This file is part of 'SAGA - System for Automated //
21 // Geoscientific Analyses'. SAGA is free software; you //
22 // can redistribute it and/or modify it under the terms //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the //
25 // License, or (at your option) any later version. //
26 // //
27 // SAGA is distributed in the hope that it will be //
28 // useful, but WITHOUT ANY WARRANTY; without even the //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
30 // PARTICULAR PURPOSE. See the GNU General Public //
31 // License for more details. //
32 // //
33 // You should have received a copy of the GNU General //
34 // Public License along with this program; if not, see //
35 // <http://www.gnu.org/licenses/>. //
36 // //
37 //-------------------------------------------------------//
38 // //
39 // e-mail: oconrad@saga-gis.de //
40 // //
41 // contact: Olaf Conrad //
42 // Bundesstr. 55 //
43 // D-20146 Hamburg //
44 // Germany //
45 // //
46 ///////////////////////////////////////////////////////////
47
48 //---------------------------------------------------------
49 #include "gdal_export.h"
50
51
52 ///////////////////////////////////////////////////////////
53 // //
54 // //
55 // //
56 ///////////////////////////////////////////////////////////
57
58 //---------------------------------------------------------
CGDAL_Export(void)59 CGDAL_Export::CGDAL_Export(void)
60 {
61 Set_Name (_TL("Export Raster"));
62
63 Set_Author ("O.Conrad (c) 2007");
64
65 Add_Reference("GDAL/OGR contributors", "2019",
66 "GDAL/OGR Geospatial Data Abstraction software Library",
67 "A translator library for raster and vector geospatial data formats. Open Source Geospatial Foundation.",
68 SG_T("https://gdal.org"), SG_T("Link")
69 );
70
71 CSG_String Description, Formats, Filter;
72
73 Description = _TW(
74 "The \"GDAL Raster Export\" tool exports one or more grids to various file formats using the "
75 "\"Geospatial Data Abstraction Library\" (GDAL) by Frank Warmerdam. "
76 );
77
78 Description += CSG_String::Format("\nGDAL %s:%s\n\n", _TL("Version"), SG_Get_GDAL_Drivers().Get_Version().c_str());
79
80 Description += _TL("Following raster formats are currently supported:");
81
82 Description += CSG_String::Format("\n<table border=\"1\"><tr><th>%s</th><th>%s</th><th>%s</th></tr>\n",
83 _TL("ID"), _TL("Name"), _TL("Extension")
84 );
85
86 Filter.Printf("%s|*.*", _TL("All Files"));
87
88 for(int i=0; i<SG_Get_GDAL_Drivers().Get_Count(); i++)
89 {
90 if( SG_Get_GDAL_Drivers().is_Raster(i) && SG_Get_GDAL_Drivers().Can_Write(i) )
91 {
92 CSG_String ID = SG_Get_GDAL_Drivers().Get_Description(i).c_str();
93 CSG_String Name = SG_Get_GDAL_Drivers().Get_Name (i).c_str();
94 CSG_String Ext = SG_Get_GDAL_Drivers().Get_Extension (i).c_str();
95
96 Description += "<tr><td>" + ID + "</td><td>" + Name + "</td><td>" + Ext + "</td></tr>";
97 Formats += "{" + ID + "}" + Name + "|";
98
99 if( !Ext.is_Empty() )
100 {
101 Ext.Replace("/", ";");
102
103 Filter += "|" + Name + "|*." + Ext;
104 }
105 }
106 }
107
108 Description += "</table>";
109
110 Set_Description(Description);
111
112 //-----------------------------------------------------
113 Parameters.Add_Grid_List("",
114 "GRIDS" , _TL("Grid(s)"),
115 _TL(""),
116 PARAMETER_INPUT
117 );
118
119 Parameters.Add_FilePath("",
120 "FILE" , _TL("File"),
121 _TL("The GDAL dataset to be created."),
122 Filter, NULL, true
123 );
124
125 Parameters.Add_Choice("",
126 "FORMAT" , _TL("Format"),
127 _TL("The GDAL raster format (driver) to be used."),
128 Formats
129 );
130
131 Parameters.Add_Choice("",
132 "TYPE" , _TL("Data Type"),
133 _TL("The GDAL datatype of the created dataset."),
134 CSG_String::Format("%s|%s|%s|%s|%s|%s|%s|%s",
135 _TL("match input data"),
136 _TL("8 bit unsigned integer"),
137 _TL("16 bit unsigned integer"),
138 _TL("16 bit signed integer"),
139 _TL("32 bit unsigned integer"),
140 _TL("32 bit signed integer"),
141 _TL("32 bit floating point"),
142 _TL("64 bit floating point")
143 ), 0
144 );
145
146 Parameters.Add_Bool("",
147 "SET_NODATA", _TL("Set Custom NoData"),
148 _TL(""),
149 false
150 );
151
152 Parameters.Add_Double("SET_NODATA",
153 "NODATA" , _TL("NoData Value"),
154 _TL("")
155 );
156
157 Parameters.Add_String("",
158 "OPTIONS" , _TL("Creation Options"),
159 _TL("A space separated list of key-value pairs (K=V)."), _TL("")
160 );
161 }
162
163
164 ///////////////////////////////////////////////////////////
165 // //
166 ///////////////////////////////////////////////////////////
167
168 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)169 int CGDAL_Export::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
170 {
171 if( pParameter->Cmp_Identifier("SET_NODATA") )
172 {
173 pParameters->Set_Enabled("NODATA", pParameter->asBool());
174 }
175
176 return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
177 }
178
179
180 ///////////////////////////////////////////////////////////
181 // //
182 ///////////////////////////////////////////////////////////
183
184 //---------------------------------------------------------
On_Execute(void)185 bool CGDAL_Export::On_Execute(void)
186 {
187 //-----------------------------------------------------
188 CSG_Parameter_Grid_List *pGrids = Parameters("GRIDS")->asGridList();
189
190 //-----------------------------------------------------
191 TSG_Data_Type Type;
192
193 switch( Parameters("TYPE")->asInt() )
194 {
195 default: Type = SG_Get_Grid_Type(pGrids); break; // match input data
196 case 1: Type = SG_DATATYPE_Byte ; break; // Eight bit unsigned integer
197 case 2: Type = SG_DATATYPE_Word ; break; // Sixteen bit unsigned integer
198 case 3: Type = SG_DATATYPE_Short ; break; // Sixteen bit signed integer
199 case 4: Type = SG_DATATYPE_DWord ; break; // Thirty two bit unsigned integer
200 case 5: Type = SG_DATATYPE_Int ; break; // Thirty two bit signed integer
201 case 6: Type = SG_DATATYPE_Float ; break; // Thirty two bit floating point
202 case 7: Type = SG_DATATYPE_Double ; break; // Sixty four bit floating point
203 }
204
205 //-----------------------------------------------------
206 CSG_Projection Projection;
207
208 Get_Projection(Projection);
209
210 //-----------------------------------------------------
211 CSG_String Driver;
212
213 if( !Parameters("FORMAT")->asChoice()->Get_Data(Driver) )
214 {
215 return( false );
216 }
217
218 //-----------------------------------------------------
219 CSG_GDAL_DataSet DataSet;
220
221 if( !DataSet.Open_Write(Parameters("FILE")->asString(), Driver, Parameters("OPTIONS")->asString(), Type, pGrids->Get_Grid_Count(), Get_System(), Projection) )
222 {
223 return( false );
224 }
225
226 //-----------------------------------------------------
227 for(int i=0; i<pGrids->Get_Grid_Count(); i++)
228 {
229 Process_Set_Text("%s %d", _TL("Band"), i + 1);
230
231 if( Parameters("SET_NODATA")->asBool() )
232 {
233 DataSet.Write(i, pGrids->Get_Grid(i), Parameters("NODATA")->asDouble());
234 }
235 else
236 {
237 DataSet.Write(i, pGrids->Get_Grid(i));
238 }
239 }
240
241 if( !DataSet.Close() )
242 {
243 return( false );
244 }
245
246 return( true );
247 }
248
249
250 ///////////////////////////////////////////////////////////
251 // //
252 // //
253 // //
254 ///////////////////////////////////////////////////////////
255
256 //---------------------------------------------------------
257