1
2 ///////////////////////////////////////////////////////////
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // Tool Library //
9 // grids_tools //
10 // //
11 //-------------------------------------------------------//
12 // //
13 // grid_collection.cpp //
14 // //
15 // Copyright (C) 2017 by //
16 // Olaf 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.org //
40 // //
41 // contact: Olaf Conrad //
42 // Institute of Geography //
43 // University of Hamburg //
44 // Germany //
45 // //
46 ///////////////////////////////////////////////////////////
47
48 //---------------------------------------------------------
49 #include "grid_collection.h"
50
51
52 ///////////////////////////////////////////////////////////
53 // //
54 // //
55 // //
56 ///////////////////////////////////////////////////////////
57
58 //---------------------------------------------------------
59 #define GET_ID_NAME(i) CSG_String::Format("NAME%d", i)
60 #define GET_ID_TYPE(i) CSG_String::Format("TYPE%d", i)
61
62
63 ///////////////////////////////////////////////////////////
64 // //
65 ///////////////////////////////////////////////////////////
66
67 //---------------------------------------------------------
CGrids_Create(void)68 CGrids_Create::CGrids_Create(void)
69 {
70 Set_Name (_TL("Create a Grid Collection"));
71
72 Set_Author ("O.Conrad (c) 2017");
73
74 Set_Description (_TW(
75 "Create a new grid collection from existing grids."
76 ));
77
78 //-----------------------------------------------------
79 Parameters.Add_Grid_List("",
80 "LIST" , _TL("Single Grids"),
81 _TL(""),
82 PARAMETER_INPUT
83 );
84
85 Parameters.Add_Grids("",
86 "GRIDS" , _TL("Grid Collection"),
87 _TL(""),
88 PARAMETER_OUTPUT
89 );
90
91 Parameters.Add_String("",
92 "NAME" , _TL("Name"),
93 _TL(""),
94 ""
95 );
96
97 Parameters.Add_Bool("LIST",
98 "DELETE" , _TL("Delete"),
99 _TL(""),
100 true
101 );
102
103 //-----------------------------------------------------
104 Parameters.Add_Choice("",
105 "ATTRIBUTES", _TL("Attribute Definition"),
106 _TL(""),
107 CSG_String::Format("%s|%s|%s|%s",
108 _TL("index and name"),
109 _TL("user defined structure"),
110 _TL("table with values"),
111 _TL("copy from other grid collection")
112 ), 0
113 );
114
115 Parameters.Add_Table("ATTRIBUTES",
116 "TABLE" , _TL("Attributes"),
117 _TL(""),
118 PARAMETER_INPUT
119 );
120
121 Parameters.Add_Table_Field("TABLE",
122 "TABLE_Z" , _TL("Z Attribute"),
123 _TL("")
124 );
125
126 Parameters.Add_Int("ATTRIBUTES",
127 "NFIELDS" , _TL("Number of Attributes"),
128 _TL(""),
129 2, 0, true
130 );
131
132 Parameters.Add_Int("NFIELDS",
133 "ZFIELD" , _TL("Z Attribute"),
134 _TL(""),
135 1, 1, true
136 );
137
138 Parameters.Add_Parameters("NFIELDS",
139 "FIELDS" , _TL("Attributes"),
140 _TL("")
141 );
142
143 Set_Field_Count(Parameters("FIELDS")->asParameters(), Parameters("NFIELDS")->asInt());
144
145 Parameters("FIELDS")->asParameters()->Get_Parameter(GET_ID_NAME(0))->Set_Value("ID");
146 Parameters("FIELDS")->asParameters()->Get_Parameter(GET_ID_TYPE(0))->Set_Value( 8 ); // int
147
148 Parameters.Add_Grids("ATTRIBUTES",
149 "COPY" , _TL("Copy from Grid Collection"),
150 _TL(""),
151 PARAMETER_INPUT, false
152 )->ignore_Projection(true);
153
154 Parameters.Add_Bool("COPY",
155 "COPY_SET" , _TL("Copy Settings"),
156 _TL(""),
157 true
158 )->Set_UseInCMD(false);
159 }
160
161
162 ///////////////////////////////////////////////////////////
163 // //
164 ///////////////////////////////////////////////////////////
165
166 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)167 int CGrids_Create::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
168 {
169 if( pParameter->Cmp_Identifier("LIST") )
170 {
171 if( pParameter->asList()->Get_Item_Count() > 0 )
172 {
173 pParameters->Set_Parameter("NAME", pParameter->asList()->Get_Item(0)->Get_Name());
174 }
175 }
176
177 if( pParameter->Cmp_Identifier("NFIELDS") && pParameter->asInt() > 0 )
178 {
179 Set_Field_Count((*pParameters)("FIELDS")->asParameters(), pParameter->asInt());
180
181 return( true );
182 }
183
184 return( CSG_Tool_Grid::On_Parameter_Changed(pParameters, pParameter) );
185 }
186
187 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)188 int CGrids_Create::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
189 {
190 if( pParameter->Cmp_Identifier("ATTRIBUTES") )
191 {
192 pParameters->Set_Enabled("NFIELDS", pParameter->asInt() == 1);
193 pParameters->Set_Enabled("TABLE" , pParameter->asInt() == 2);
194
195 pParameters->Set_Enabled((*pParameters)("COPY")->Get_Parent()->Get_Identifier(), pParameter->asInt() == 3);
196 }
197
198 if( pParameter->Cmp_Identifier("NFIELDS") )
199 {
200 pParameters->Set_Enabled("ZFIELD" , pParameter->asInt() > 0);
201 pParameters->Set_Enabled( "FIELDS", pParameter->asInt() > 0);
202 }
203
204 return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
205 }
206
207
208 ///////////////////////////////////////////////////////////
209 // //
210 ///////////////////////////////////////////////////////////
211
212 //---------------------------------------------------------
Set_Field_Count(CSG_Parameters * pFields,int nFields)213 void CGrids_Create::Set_Field_Count(CSG_Parameters *pFields, int nFields)
214 {
215 static const CSG_String Types(
216 SG_Data_Type_Get_Name(SG_DATATYPE_String) + "|" + // 0
217 SG_Data_Type_Get_Name(SG_DATATYPE_Date ) + "|" + // 1
218 SG_Data_Type_Get_Name(SG_DATATYPE_Color ) + "|" + // 2
219 SG_Data_Type_Get_Name(SG_DATATYPE_Byte ) + "|" + // 3
220 SG_Data_Type_Get_Name(SG_DATATYPE_Char ) + "|" + // 4
221 SG_Data_Type_Get_Name(SG_DATATYPE_Word ) + "|" + // 5
222 SG_Data_Type_Get_Name(SG_DATATYPE_Short ) + "|" + // 6
223 SG_Data_Type_Get_Name(SG_DATATYPE_DWord ) + "|" + // 7
224 SG_Data_Type_Get_Name(SG_DATATYPE_Int ) + "|" + // 8
225 SG_Data_Type_Get_Name(SG_DATATYPE_ULong ) + "|" + // 9
226 SG_Data_Type_Get_Name(SG_DATATYPE_Long ) + "|" + // 10
227 SG_Data_Type_Get_Name(SG_DATATYPE_Float ) + "|" + // 11
228 SG_Data_Type_Get_Name(SG_DATATYPE_Double) + "|" + // 12
229 SG_Data_Type_Get_Name(SG_DATATYPE_Binary) + "|" // 13
230 );
231
232 if( pFields && nFields >= 0 )
233 {
234 int nCurrent = pFields->Get_Count() / 2;
235
236 if( nCurrent < nFields )
237 {
238 for(int i=nCurrent; i<nFields; i++)
239 {
240 pFields->Add_String("" , GET_ID_NAME(i), _TL("Name"), _TL(""), _TL("Value"));
241 pFields->Add_Choice(GET_ID_NAME(i), GET_ID_TYPE(i), _TL("Type"), _TL(""), Types, 11); // float
242 }
243 }
244 else if( nCurrent > nFields )
245 {
246 for(int i=nCurrent-1; i>=nFields; i--)
247 {
248 pFields->Del_Parameter(i);
249 }
250 }
251 }
252 }
253
254 //---------------------------------------------------------
Get_Field_Name(int iField)255 CSG_String CGrids_Create::Get_Field_Name(int iField)
256 {
257 return( Parameters("FIELDS")->asParameters()->Get_Parameter(GET_ID_NAME(iField))->asString() );
258 }
259
260 //---------------------------------------------------------
Get_Field_Type(int iField)261 TSG_Data_Type CGrids_Create::Get_Field_Type(int iField)
262 {
263 switch( Parameters("FIELDS")->asParameters()->Get_Parameter(GET_ID_TYPE(iField))->asInt() )
264 {
265 default: return( SG_DATATYPE_String );
266 case 1: return( SG_DATATYPE_Date );
267 case 2: return( SG_DATATYPE_Color );
268 case 3: return( SG_DATATYPE_Byte );
269 case 4: return( SG_DATATYPE_Char );
270 case 5: return( SG_DATATYPE_Word );
271 case 6: return( SG_DATATYPE_Short );
272 case 7: return( SG_DATATYPE_DWord );
273 case 8: return( SG_DATATYPE_Int );
274 case 9: return( SG_DATATYPE_ULong );
275 case 10: return( SG_DATATYPE_Long );
276 case 11: return( SG_DATATYPE_Float );
277 case 12: return( SG_DATATYPE_Double );
278 case 13: return( SG_DATATYPE_Binary );
279 }
280 }
281
282
283 ///////////////////////////////////////////////////////////
284 // //
285 ///////////////////////////////////////////////////////////
286
287 //---------------------------------------------------------
On_Execute(void)288 bool CGrids_Create::On_Execute(void)
289 {
290 CSG_Parameter_Grid_List *pList = Parameters("LIST")->asGridList();
291
292 if( pList->Get_Grid_Count() <= 0 )
293 {
294 Error_Set(_TL("empty input grid list"));
295
296 return( false );
297 }
298
299 //-----------------------------------------------------
300 CSG_Table *pTable, Table; int zField;
301
302 switch( Parameters("ATTRIBUTES")->asInt() )
303 {
304 default: // index and name
305 {
306 pTable = &Table;
307 zField = 0;
308
309 Table.Add_Field("ID" , SG_DATATYPE_Int );
310 Table.Add_Field("NAME", SG_DATATYPE_String);
311
312 for(int i=0; i<pList->Get_Grid_Count(); i++)
313 {
314 CSG_Table_Record *pRecord = Table.Add_Record();
315
316 pRecord->Set_Value(0, i + 1);
317 pRecord->Set_Value(1, pList->Get_Grid(i)->Get_Name());
318 }
319 }
320 break;
321
322 case 1: // user defined structure
323 {
324 pTable = &Table;
325 zField = Parameters("ZFIELD")->asInt();
326
327 for(int i=0; i<Parameters("NFIELDS")->asInt(); i++)
328 {
329 Table.Add_Field(Get_Field_Name(i), Get_Field_Type(i));
330 }
331 }
332 break;
333
334 case 2: // table with values
335 {
336 pTable = Parameters("TABLE" )->asTable();
337 zField = Parameters("TABLE_Z")->asInt ();
338 }
339 break;
340
341 case 3: // copy from other grid collection
342 {
343 Table.Create(Parameters("COPY")->asGrids()->Get_Attributes());
344 pTable = &Table;
345 zField = Parameters("COPY")->asGrids()->Get_Z_Attribute();
346 }
347 break;
348 }
349
350 //-----------------------------------------------------
351 CSG_Grids *pGrids = Parameters("GRIDS")->asGrids();
352
353 if( pTable->Get_Count() == pList->Get_Grid_Count() )
354 {
355 pGrids->Create(pList->Get_Grid(0)->Get_System(), *pTable, zField, pList->Get_Grid(0)->Get_Type());
356 }
357 else
358 {
359 pGrids->Create(pList->Get_Grid(0)->Get_System());
360 }
361
362 if( !pGrids->is_Valid() )
363 {
364 Error_Set(_TL("failed to create grid collection"));
365
366 return( false );
367 }
368
369 pGrids->Set_Name(Parameters("NAME")->asString());
370
371 //-----------------------------------------------------
372 bool bDelete = Parameters("DELETE")->asBool();
373
374 if( bDelete )
375 {
376 for(int i=0; i<pList->Get_Item_Count(); i++)
377 {
378 CSG_Data_Object *pItem = pList->Get_Item(i);
379
380 if( (pItem->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grid && ((CSG_Grid *)pItem)->Get_Type() != pGrids->Get_Type())
381 || (pItem->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grids && ((CSG_Grids *)pItem)->Get_Type() != pGrids->Get_Type()) )
382 {
383 continue;
384 }
385
386 if( Parameters.Get_Manager() )
387 {
388 Parameters.Get_Manager()->Delete(pItem, true);
389
390 DataObject_Update(pItem);
391 }
392
393 if( pItem->Get_ObjectType() == SG_DATAOBJECT_TYPE_Grids )
394 {
395 ((CSG_Grids *)pItem)->Del_Grids(true);
396
397 delete(pList->Get_Item(i));
398 }
399 }
400 }
401
402 for(int i=0; i<pList->Get_Grid_Count() && Set_Progress(i, pList->Get_Grid_Count()); i++)
403 {
404 if( pList->Get_Grid(i)->Get_Type() != pGrids->Get_Type() )
405 {
406 Message_Fmt("\n%s: %s [%s]", _TL("Warning"), _TL("data type mismatch, grid will be skipped"), pList->Get_Grid(i)->Get_Name());
407
408 continue;
409 }
410
411 if( pTable && i < pTable->Get_Count() )
412 {
413 pGrids->Add_Grid(*pTable->Get_Record_byIndex(i), pList->Get_Grid(i), bDelete);
414 }
415 else
416 {
417 pGrids->Add_Grid(i, pList->Get_Grid(i), bDelete);
418 }
419 }
420
421 if( bDelete )
422 {
423 pList->Del_Items();
424 }
425
426 //-----------------------------------------------------
427 if( Parameters("ATTRIBUTES")->asInt() == 3 && Parameters("COPY_SET")->asBool() )
428 {
429 DataObject_Set_Parameters(pGrids, Parameters("COPY")->asGrids());
430 }
431 else
432 {
433 DataObject_Set_Parameter(pGrids, "BAND_R", 0);
434 DataObject_Set_Parameter(pGrids, "BAND_G", 1);
435 DataObject_Set_Parameter(pGrids, "BAND_B", 2);
436 }
437
438 pGrids->Set_Name(Parameters("NAME")->asString());
439
440 //-----------------------------------------------------
441 return( true );
442 }
443
444
445 ///////////////////////////////////////////////////////////
446 // //
447 // //
448 // //
449 ///////////////////////////////////////////////////////////
450
451 //---------------------------------------------------------
CGrids_Add_Grid(void)452 CGrids_Add_Grid::CGrids_Add_Grid(void)
453 {
454 Set_Name (_TL("Add a Grid to a Grid Collection"));
455
456 Set_Author ("O.Conrad (c) 2018");
457
458 Set_Description (_TW(
459 "Adds a grid at the specified z-level to an existing grid collection. "
460 "If no grid collection is supplied it will be created according to the "
461 "input grid's grid system and data type. "
462 ));
463
464 //-----------------------------------------------------
465 Parameters.Add_Grid("",
466 "GRID" , _TL("Grid"),
467 _TL(""),
468 PARAMETER_INPUT
469 );
470
471 Parameters.Add_Grids("",
472 "GRIDS" , _TL("Grid Collection"),
473 _TL(""),
474 PARAMETER_OUTPUT_OPTIONAL
475 );
476
477 Parameters.Add_Double("",
478 "Z_LEVEL" , _TL("Z"),
479 _TL(""),
480 0.0
481 );
482
483 Parameters.Add_Bool("GRID",
484 "DELETE" , _TL("Delete"),
485 _TL(""),
486 false
487 );
488 }
489
490
491 ///////////////////////////////////////////////////////////
492 // //
493 ///////////////////////////////////////////////////////////
494
495 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)496 int CGrids_Add_Grid::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
497 {
498 return( CSG_Tool_Grid::On_Parameter_Changed(pParameters, pParameter) );
499 }
500
501 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)502 int CGrids_Add_Grid::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
503 {
504 if( pParameter->Cmp_Identifier("GRID") )
505 {
506 pParameters->Set_Enabled("DELETE", pParameter->asGrid() && !pParameter->asGrid()->Get_Owner());
507 }
508
509 return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
510 }
511
512
513 ///////////////////////////////////////////////////////////
514 // //
515 ///////////////////////////////////////////////////////////
516
517 //---------------------------------------------------------
On_Execute(void)518 bool CGrids_Add_Grid::On_Execute(void)
519 {
520 //-----------------------------------------------------
521 CSG_Grid *pGrid = Parameters("GRID" )->asGrid ();
522 CSG_Grids *pGrids = Parameters("GRIDS")->asGrids();
523
524 //-----------------------------------------------------
525 if( pGrids == NULL )
526 {
527 pGrids = SG_Create_Grids(pGrid->Get_System(), 0, 0.0, pGrid->Get_Type());
528
529 pGrids->Set_Name(pGrid->Get_Name());
530 pGrids->Set_Unit(pGrid->Get_Unit());
531 pGrids->Set_NoData_Value_Range(pGrid->Get_NoData_Value(), pGrid->Get_NoData_Value(true));
532
533 Parameters("GRIDS")->Set_Value(pGrids);
534 }
535
536 //-----------------------------------------------------
537 if( pGrid->Get_Type() != pGrids->Get_Type() )
538 {
539 Error_Fmt("%s\n%s > %s", _TL("Data types of grid and grid collection must not differ."),
540 SG_Data_Type_Get_Name(pGrid ->Get_Type()).c_str(),
541 SG_Data_Type_Get_Name(pGrids->Get_Type()).c_str()
542 );
543
544 return( false );
545 }
546
547 //-----------------------------------------------------
548 double Z = Parameters("Z_LEVEL")->asDouble();
549
550 bool bDelete = !pGrid->Get_Owner() && Parameters("DELETE")->asBool();
551
552 if( bDelete && Parameters.Get_Manager() )
553 {
554 Parameters.Get_Manager()->Delete(pGrid, true);
555
556 DataObject_Update(pGrid);
557 }
558
559 return( pGrids->Add_Grid(Z, pGrid, bDelete) );
560 }
561
562
563 ///////////////////////////////////////////////////////////
564 // //
565 // //
566 // //
567 ///////////////////////////////////////////////////////////
568
569 //---------------------------------------------------------
CGrids_Extract(void)570 CGrids_Extract::CGrids_Extract(void)
571 {
572 Set_Name (_TL("Extract Grids from a Grid Collection"));
573
574 Set_Author ("O.Conrad (c) 2017");
575
576 Set_Description (_TW(
577 "Extracts selected z-level grids from a grid collection."
578 ));
579
580 //-----------------------------------------------------
581 Parameters.Add_Grids("",
582 "GRIDS" , _TL("Grid Collection"),
583 _TL(""),
584 PARAMETER_INPUT
585 );
586
587 Parameters.Add_Grid_List("",
588 "LIST" , _TL("Single Grids"),
589 _TL(""),
590 PARAMETER_OUTPUT
591 );
592
593 Parameters.Add_Choices("",
594 "SELECTION" , _TL("Selection"),
595 _TL(""),
596 ""
597 );
598 }
599
600
601 ///////////////////////////////////////////////////////////
602 // //
603 ///////////////////////////////////////////////////////////
604
605 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)606 int CGrids_Extract::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
607 {
608 if( pParameter->Cmp_Identifier("GRIDS") )
609 {
610 CSG_Parameter_Choices *pChoices = (*pParameters)("SELECTION")->asChoices();
611
612 pChoices->Del_Items();
613
614 for(int i=0; pParameter->asGrids() && i<pParameter->asGrids()->Get_Grid_Count(); i++)
615 {
616 pChoices->Add_Item(pParameter->asGrids()->Get_Grid_Name(i));
617 }
618 }
619
620 return( CSG_Tool_Grid::On_Parameter_Changed(pParameters, pParameter) );
621 }
622
623 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)624 int CGrids_Extract::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
625 {
626 return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
627 }
628
629
630 ///////////////////////////////////////////////////////////
631 // //
632 ///////////////////////////////////////////////////////////
633
634 //---------------------------------------------------------
On_Execute(void)635 bool CGrids_Extract::On_Execute(void)
636 {
637 //-----------------------------------------------------
638 CSG_Parameter_Grid_List *pList = Parameters("LIST")->asGridList();
639
640 pList->Del_Items();
641
642 CSG_Grids *pGrids = Parameters("GRIDS")->asGrids();
643
644 CSG_Parameter_Choices *pSelection = Parameters("SELECTION")->asChoices();
645
646 if( pSelection->Get_Selection_Count() > 0 )
647 {
648 for(int i=0; i<pSelection->Get_Selection_Count() && Set_Progress(i, pSelection->Get_Selection_Count()); i++)
649 {
650 CSG_Grid *pGrid = SG_Create_Grid(pGrids->Get_Grid(pSelection->Get_Selection_Index(i)));
651
652 pGrid->Set_Name(pGrids->Get_Grid_Name(pSelection->Get_Selection_Index(i)));
653
654 pList->Add_Item(pGrid);
655 }
656 }
657 else
658 {
659 for(int i=0; i<pGrids->Get_Grid_Count() && Set_Progress(i, pGrids->Get_Grid_Count()); i++)
660 {
661 CSG_Grid *pGrid = SG_Create_Grid(pGrids->Get_Grid(i));
662
663 pGrid->Set_Name(pGrids->Get_Grid_Name(i));
664
665 pList->Add_Item(pGrid);
666 }
667 }
668
669 //-----------------------------------------------------
670 return( pList->Get_Grid_Count() > 0 );
671 }
672
673
674 ///////////////////////////////////////////////////////////
675 // //
676 // //
677 // //
678 ///////////////////////////////////////////////////////////
679
680 //---------------------------------------------------------
CGrids_Extract_Grid(void)681 CGrids_Extract_Grid::CGrids_Extract_Grid(void)
682 {
683 Set_Name (_TL("Extract a Grid from a Grid Collection"));
684
685 Set_Author ("O.Conrad (c) 2018");
686
687 Set_Description (_TW(
688 "Extracts grid values from the input grid collection using "
689 "the chosen interpolation either for a constant or a variable "
690 "z-level as defined by the z-level input grid."
691 ));
692
693 //-----------------------------------------------------
694 Parameters.Add_Grids("",
695 "GRIDS" , _TL("Grid Collection"),
696 _TL(""),
697 PARAMETER_INPUT
698 );
699
700 Parameters.Add_Grid("",
701 "GRID" , _TL("Grid"),
702 _TL(""),
703 PARAMETER_OUTPUT
704 );
705
706 Parameters.Add_Grid_or_Const("",
707 "Z_LEVEL" , _TL("Z"),
708 _TL(""),
709 0.0
710 );
711
712 Parameters.Add_Choice("",
713 "RESAMPLING", _TL("Resampling"),
714 _TL(""),
715 CSG_String::Format("%s|%s|%s",
716 _TL("Nearest Neigbhour" ),
717 _TL("Linear Interpolation"),
718 _TL("Spline Interpolation")
719 ), 1
720 );
721 }
722
723
724 ///////////////////////////////////////////////////////////
725 // //
726 ///////////////////////////////////////////////////////////
727
728 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)729 int CGrids_Extract_Grid::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
730 {
731 return( CSG_Tool_Grid::On_Parameter_Changed(pParameters, pParameter) );
732 }
733
734 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)735 int CGrids_Extract_Grid::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
736 {
737 return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
738 }
739
740
741 ///////////////////////////////////////////////////////////
742 // //
743 ///////////////////////////////////////////////////////////
744
745 //---------------------------------------------------------
On_Execute(void)746 bool CGrids_Extract_Grid::On_Execute(void)
747 {
748 //-----------------------------------------------------
749 CSG_Grid *pZ = Parameters("Z_LEVEL")->asGrid();
750 double Z = Parameters("Z_LEVEL")->asDouble();
751
752 CSG_Grids *pGrids = Parameters("GRIDS")->asGrids();
753
754 if( pZ == NULL && Z < pGrids->Get_ZMin() )
755 {
756 Message_Fmt("%s: %s (%f < %f)", _TL("Warning"), _TL("z-level is out of grid collection's range"), Z, pGrids->Get_ZMin());
757 }
758
759 if( pZ == NULL && Z > pGrids->Get_ZMax() )
760 {
761 Message_Fmt("%s: %s (%f > %f)", _TL("Warning"), _TL("z-level is out of grid collection's range"), Z, pGrids->Get_ZMax());
762 }
763
764 //-----------------------------------------------------
765 CSG_Grid *pGrid = Parameters("GRID")->asGrid();
766
767 pGrid->Create(pGrids->Get_System(), pGrids->Get_Type());
768
769 pGrid->Set_NoData_Value_Range(pGrids->Get_NoData_Value(), pGrids->Get_NoData_Value(true));
770
771 if( pZ )
772 {
773 pGrid->Fmt_Name("%s [%s]" , pGrids->Get_Name(), pZ->Get_Name());
774 }
775 else
776 {
777 pGrid->Fmt_Name("%s [%.*f]", pGrids->Get_Name(), SG_Get_Significant_Decimals(Z), Z);
778 }
779
780 TSG_Grid_Resampling Resampling;
781
782 switch( Parameters("RESAMPLING")->asInt() )
783 {
784 default: Resampling = GRID_RESAMPLING_NearestNeighbour; break;
785 case 1: Resampling = GRID_RESAMPLING_Bilinear ; break;
786 case 2: Resampling = GRID_RESAMPLING_BSpline ; break;
787 }
788
789 //-----------------------------------------------------
790 for(int y=0; y<Get_NY() && Set_Progress(y); y++)
791 {
792 double py = Get_YMin() + y * Get_Cellsize();
793
794 #ifndef _DEBUG
795 #pragma omp parallel for
796 #endif
797 for(int x=0; x<Get_NX(); x++)
798 {
799 double Value, px = Get_XMin() + x * Get_Cellsize();
800
801 if( (!pZ || !pZ->is_NoData(x, y)) && pGrids->Get_Value(px, py, !pZ ? Z : pZ->asDouble(x, y), Value, GRID_RESAMPLING_NearestNeighbour, Resampling) )
802 {
803 pGrid->Set_Value(x, y, Value);
804 }
805 else
806 {
807 pGrid->Set_NoData(x, y);
808 }
809 }
810 }
811
812 //-----------------------------------------------------
813 return( true );
814 }
815
816
817 ///////////////////////////////////////////////////////////
818 // //
819 // //
820 // //
821 ///////////////////////////////////////////////////////////
822
823 //---------------------------------------------------------
CGrids_Delete(void)824 CGrids_Delete::CGrids_Delete(void)
825 {
826 Set_Name (_TL("Delete Grids from a Grid Collection"));
827
828 Set_Author ("O.Conrad (c) 2017");
829
830 Set_Description (_TW(
831 ""
832 ));
833
834 //-----------------------------------------------------
835 Parameters.Add_Grids("",
836 "GRIDS" , _TL("Grid Collection"),
837 _TL(""),
838 PARAMETER_INPUT
839 );
840
841 Parameters.Add_Choices("",
842 "SELECTION" , _TL("Selection"),
843 _TL(""),
844 ""
845 );
846 }
847
848
849 ///////////////////////////////////////////////////////////
850 // //
851 ///////////////////////////////////////////////////////////
852
853 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)854 int CGrids_Delete::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
855 {
856 if( pParameter->Cmp_Identifier("GRIDS") )
857 {
858 CSG_Parameter_Choices *pChoices = (*pParameters)("SELECTION")->asChoices();
859
860 pChoices->Del_Items();
861
862 for(int i=0; pParameter->asGrids() && i<pParameter->asGrids()->Get_Grid_Count(); i++)
863 {
864 pChoices->Add_Item(pParameter->asGrids()->Get_Grid_Name(i));
865 }
866 }
867
868 return( CSG_Tool_Grid::On_Parameter_Changed(pParameters, pParameter) );
869 }
870
871 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)872 int CGrids_Delete::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
873 {
874 return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
875 }
876
877
878 ///////////////////////////////////////////////////////////
879 // //
880 ///////////////////////////////////////////////////////////
881
882 //---------------------------------------------------------
On_Execute(void)883 bool CGrids_Delete::On_Execute(void)
884 {
885 //-----------------------------------------------------
886 CSG_Parameter_Choices *pSelection = Parameters("SELECTION")->asChoices();
887
888 if( pSelection->Get_Selection_Count() <= 0 )
889 {
890 Error_Set(_TL("No grids in selection"));
891
892 return( false );
893 }
894
895 if( pSelection->Get_Selection_Count() >= pSelection->Get_Item_Count() )
896 {
897 Error_Set(_TL("It is not allowed to remove all grids from a grid collection."));
898
899 return( false );
900 }
901
902 //-----------------------------------------------------
903 CSG_Grids *pGrids = Parameters("GRIDS")->asGrids();
904
905 for(int i=pGrids->Get_Grid_Count()-1; i>=0 && Process_Get_Okay(); i--)
906 {
907 if( pSelection->is_Selected(i) )
908 {
909 pGrids->Del_Grid(i);
910 }
911 }
912
913 On_Parameter_Changed(&Parameters, Parameters("GRIDS"));
914
915 DataObject_Update(pGrids);
916
917 //-----------------------------------------------------
918 return( true );
919 }
920
921
922 ///////////////////////////////////////////////////////////
923 // //
924 // //
925 // //
926 ///////////////////////////////////////////////////////////
927
928 //---------------------------------------------------------
929