1 /**********************************************************
2 * Version $Id$
3 *********************************************************/
4
5 ///////////////////////////////////////////////////////////
6 // //
7 // SAGA //
8 // //
9 // System for Automated Geoscientific Analyses //
10 // //
11 // Tool Library //
12 // Grid_Tools //
13 // //
14 //-------------------------------------------------------//
15 // //
16 // Grid_Tiling.cpp //
17 // //
18 // Copyright (C) 2010 by //
19 // Olaf Conrad //
20 // //
21 //-------------------------------------------------------//
22 // //
23 // This file is part of 'SAGA - System for Automated //
24 // Geoscientific Analyses'. SAGA is free software; you //
25 // can redistribute it and/or modify it under the terms //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the //
28 // License, or (at your option) any later version. //
29 // //
30 // SAGA is distributed in the hope that it will be //
31 // useful, but WITHOUT ANY WARRANTY; without even the //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
33 // PARTICULAR PURPOSE. See the GNU General Public //
34 // License for more details. //
35 // //
36 // You should have received a copy of the GNU General //
37 // Public License along with this program; if not, see //
38 // <http://www.gnu.org/licenses/>. //
39 // //
40 //-------------------------------------------------------//
41 // //
42 // e-mail: oconrad@saga-gis.org //
43 // //
44 // contact: Olaf Conrad //
45 // Institute of Geography //
46 // University of Hamburg //
47 // Germany //
48 // //
49 ///////////////////////////////////////////////////////////
50
51 //---------------------------------------------------------
52
53
54 ///////////////////////////////////////////////////////////
55 // //
56 // //
57 // //
58 ///////////////////////////////////////////////////////////
59
60 //---------------------------------------------------------
61 #include "Grid_Tiling.h"
62
63
64 ///////////////////////////////////////////////////////////
65 // //
66 // //
67 // //
68 ///////////////////////////////////////////////////////////
69
70 //---------------------------------------------------------
CGrid_Tiling(void)71 CGrid_Tiling::CGrid_Tiling(void)
72 {
73 //-----------------------------------------------------
74 Set_Name (_TL("Tiling"));
75
76 Set_Author ("O.Conrad (c) 2010");
77
78 Set_Description (_TW(
79 ""
80 ));
81
82 //-----------------------------------------------------
83 Parameters.Add_Grid(NULL,
84 "GRID" , _TL("Grid"),
85 _TL(""),
86 PARAMETER_INPUT
87 );
88
89 Parameters.Add_Grid_List(NULL,
90 "TILES" , _TL("Tiles"),
91 _TL(""),
92 PARAMETER_OUTPUT, false
93 );
94
95 //-----------------------------------------------------
96 Parameters.Add_Bool(NULL,
97 "TILES_SAVE" , _TL("Save Tiles to Disk"),
98 _TL("Save tiles to disk individually"),
99 false
100 );
101
102 Parameters.Add_FilePath(Parameters("TILES_SAVE"),
103 "TILES_PATH" , _TL("Output Directory"),
104 _TL(""),
105 NULL, NULL, true, true
106 );
107
108 Parameters.Add_String(Parameters("TILES_SAVE"),
109 "TILES_NAME" , _TL("Base Name"),
110 _TL("The base name of the tiles"),
111 "tile"
112 );
113
114 //-----------------------------------------------------
115 Parameters.Add_Int(NULL,
116 "OVERLAP" , _TL("Overlapping Cells"),
117 _TL(""),
118 0, 0, true
119 );
120
121 Parameters.Add_Choice(Parameters("OVERLAP"),
122 "OVERLAP_SYM" , _TL("Add Cells"),
123 _TL(""),
124 CSG_String::Format("%s|%s|%s|",
125 _TL("symmetric"),
126 _TL("bottom / left"),
127 _TL("top / right")
128 ), 0
129 );
130
131 //-----------------------------------------------------
132 Parameters.Add_Choice(NULL,
133 "METHOD" , _TL("Tile Size Definition"),
134 _TL(""),
135 CSG_String::Format("%s|%s|",
136 _TL("number of grid cells per tile"),
137 _TL("coordinates (offset, range, cell size, tile size)")
138 ), 0
139 );
140
141 //-----------------------------------------------------
142 Parameters.Add_Int(Parameters("METHOD"),
143 "NX" , _TL("Number of Column Cells"),
144 _TL(""),
145 100, 1, true
146 );
147
148 Parameters.Add_Int(Parameters("METHOD"),
149 "NY" , _TL("Number of Row Cells"),
150 _TL(""),
151 100, 1, true
152 );
153
154 //-----------------------------------------------------
155 Parameters.Add_Range(Parameters("METHOD"),
156 "XRANGE" , _TL("Offset and Range (X)"),
157 _TL(""),
158 0.0, 1000.0
159 );
160
161 Parameters.Add_Range(Parameters("METHOD"),
162 "YRANGE" , _TL("Offset and Range (Y)"),
163 _TL(""),
164 0.0, 1000.0
165 );
166
167 Parameters.Add_Double(Parameters("METHOD"),
168 "DCELL" , _TL("Cell Size"),
169 _TL(""),
170 1.0, 0.0, true
171 );
172
173 Parameters.Add_Double(Parameters("METHOD"),
174 "DX" , _TL("Tile Size (X)"),
175 _TL(""),
176 100.0, 0.0, true
177 );
178
179 Parameters.Add_Double(Parameters("METHOD"),
180 "DY" , _TL("Tile Size (Y)"),
181 _TL(""),
182 100.0, 0.0, true
183 );
184 }
185
186
187 ///////////////////////////////////////////////////////////
188 // //
189 ///////////////////////////////////////////////////////////
190
191 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)192 int CGrid_Tiling::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
193 {
194 if( pParameter == pParameters->Get_Grid_System_Parameter() )
195 {
196 CSG_Grid_System System(1.0, 0.0, 0.0, 101, 101);
197
198 if( pParameter->asGrid_System() && pParameter->asGrid_System()->is_Valid() )
199 {
200 System = *pParameter->asGrid_System();
201 }
202
203 pParameters->Get_Parameter("NX" )->Set_Value(System.Get_NX() / 2);
204 pParameters->Get_Parameter("NY" )->Set_Value(System.Get_NX() / 2);
205
206 pParameters->Get_Parameter("XRANGE")->asRange()->Set_Range(System.Get_XMin(), System.Get_XMax());
207 pParameters->Get_Parameter("YRANGE")->asRange()->Set_Range(System.Get_YMin(), System.Get_YMax());
208 pParameters->Get_Parameter("DCELL" )->Set_Value(System.Get_Cellsize());
209 pParameters->Get_Parameter("DX" )->Set_Value(System.Get_XRange() / 2.0);
210 pParameters->Get_Parameter("DY" )->Set_Value(System.Get_YRange() / 2.0);
211 }
212
213 //-----------------------------------------------------
214 return( CSG_Tool_Grid::On_Parameter_Changed(pParameters, pParameter) );
215 }
216
217 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)218 int CGrid_Tiling::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
219 {
220 if( pParameter->Cmp_Identifier("TILES_SAVE") )
221 {
222 pParameters->Set_Enabled("TILES_PATH", pParameter->asBool());
223 pParameters->Set_Enabled("TILES_NAME", pParameter->asBool());
224 }
225
226 if( pParameter->Cmp_Identifier("OVERLAP") )
227 {
228 pParameters->Set_Enabled("OVERLAP_SYM", pParameter->asInt() > 0);
229 }
230
231 if( pParameter->Cmp_Identifier("METHOD") )
232 {
233 pParameters->Set_Enabled("NX" , pParameter->asInt() == 0);
234 pParameters->Set_Enabled("NY" , pParameter->asInt() == 0);
235
236 pParameters->Set_Enabled("XRANGE", pParameter->asInt() == 1);
237 pParameters->Set_Enabled("YRANGE", pParameter->asInt() == 1);
238 pParameters->Set_Enabled("DCELL" , pParameter->asInt() == 1);
239 pParameters->Set_Enabled("DX" , pParameter->asInt() == 1);
240 pParameters->Set_Enabled("DY" , pParameter->asInt() == 1);
241 }
242
243 //-----------------------------------------------------
244 return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
245 }
246
247
248 ///////////////////////////////////////////////////////////
249 // //
250 ///////////////////////////////////////////////////////////
251
252 //---------------------------------------------------------
On_Execute(void)253 bool CGrid_Tiling::On_Execute(void)
254 {
255 //-----------------------------------------------------
256 CSG_Grid *pGrid = Parameters("GRID")->asGrid();
257
258 CSG_Parameter_Grid_List *pTiles = Parameters("TILES")->asGridList();
259
260 pTiles->Del_Items();
261
262 //-----------------------------------------------------
263 int nx, ny;
264 double dx, dy, dCell;
265 TSG_Rect Extent;
266 TSG_Grid_Resampling Resampling;
267
268 switch( Parameters("METHOD")->asInt() )
269 {
270 default: // number of grid cells per tile
271 Extent.xMin = pGrid->Get_XMin();
272 Extent.xMax = pGrid->Get_XMax();
273 Extent.yMin = pGrid->Get_YMin();
274 Extent.yMax = pGrid->Get_YMax();
275 dCell = pGrid->Get_Cellsize();
276 nx = Parameters("NX")->asInt();
277 ny = Parameters("NY")->asInt();
278 dx = dCell * nx;
279 dy = dCell * ny;
280 Resampling = GRID_RESAMPLING_NearestNeighbour;
281 break;
282
283 case 1: // coordinates (offset, range, cell size, tile size)
284 Extent.xMin = Parameters("XRANGE")->asRange()->Get_Min();
285 Extent.xMax = Parameters("XRANGE")->asRange()->Get_Max();
286 Extent.yMin = Parameters("YRANGE")->asRange()->Get_Min();
287 Extent.yMax = Parameters("YRANGE")->asRange()->Get_Max();
288 dCell = Parameters("DCELL" )->asDouble();
289 dx = Parameters("DX" )->asDouble();
290 dy = Parameters("DY" )->asDouble();
291 nx = (int)(dx / dCell);
292 ny = (int)(dy / dCell);
293 Resampling = GRID_RESAMPLING_Undefined;
294 break;
295 }
296
297 //-----------------------------------------------------
298 if( dx <= 0.0 || dy <= 0.0 || dCell <= 0.0 )
299 {
300 Message_Add(_TL("no intersection with mask grid."));
301
302 return( false );
303 }
304
305 //-----------------------------------------------------
306 int Overlap = Parameters("OVERLAP")->asInt();
307
308 switch( Parameters("OVERLAP_SYM")->asInt() )
309 {
310 default: // symetric
311 nx += Overlap * 2;
312 ny += Overlap * 2;
313 break;
314
315 case 1: // bottom / left
316 nx += Overlap;
317 ny += Overlap;
318 break;
319
320 case 2: // top / right
321 nx += Overlap;
322 ny += Overlap;
323 Overlap = 0;
324 break;
325 }
326
327 //-----------------------------------------------------
328 CSG_String Tiles_Name, Tiles_Path;
329
330 bool Tiles_bSave = Parameters("TILES_SAVE")->asBool();
331
332 if( Tiles_bSave )
333 {
334 Tiles_Name = Parameters("TILES_NAME")->asString();
335
336 if( Tiles_Name.is_Empty() )
337 {
338 SG_UI_Msg_Add_Error(_TL("Please provide a valid base name for the output files!"));
339
340 return( false );
341 }
342
343 Tiles_Path = Parameters("TILES_PATH")->asString();
344
345 if( !SG_Dir_Exists(SG_File_Get_Path(Tiles_Path)) )
346 {
347 SG_UI_Msg_Add_Error(_TL("Please provide a valid output directory for the output files!"));
348
349 return( false );
350 }
351 }
352
353
354 //-----------------------------------------------------
355 int iy = 1, nTiles = 0;
356
357 for(double y=Extent.yMin; y<Extent.yMax && Process_Get_Okay(); y+=dy, iy++)
358 {
359 int ix = 1;
360
361 for(double x=Extent.xMin; x<Extent.xMax; x+=dx, ix++)
362 {
363 CSG_Grid *pTile = SG_Create_Grid(pGrid->Get_Type(), nx, ny, dCell,
364 x - dCell * Overlap,
365 y - dCell * Overlap
366 );
367
368 pTile->Assign(pGrid, Resampling);
369
370 if( pTile->Get_NoData_Count() == pTile->Get_NCells() )
371 {
372 delete(pTile);
373 }
374 else
375 {
376 if( Tiles_bSave )
377 {
378 CSG_String FileName = CSG_String::Format("%s/%s_%d_%d.sgrd", Tiles_Path.c_str(), Tiles_Name.c_str(), iy, ix);
379
380 pTile->Save(FileName);
381
382 delete(pTile);
383 }
384 else
385 {
386 pTile->Fmt_Name("%s [%d, %d]", pGrid->Get_Name(), iy, ix);
387
388 pTiles->Add_Item(pTile);
389 }
390
391 nTiles++;
392 }
393 }
394 }
395
396 SG_UI_Msg_Add(CSG_String::Format("%s: %d", _TL("Number of tiles"), nTiles), true);
397
398 return( nTiles > 0 );
399 }
400
401
402 ///////////////////////////////////////////////////////////
403 // //
404 // //
405 // //
406 ///////////////////////////////////////////////////////////
407
408 //---------------------------------------------------------
409