1 /**********************************************************
2 * Version $Id$
3 *********************************************************/
4 /*******************************************************************************
5 CombineGrids.cpp
6 Copyright (C) Victor Olaya
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA
21 *******************************************************************************/
22
23 #include "Grid_CombineGrids.h"
24
CCombineGrids(void)25 CCombineGrids::CCombineGrids(void){
26
27 CSG_Table *pLookup;
28 CSG_Table_Record *pRecord;
29
30 Parameters.Set_Name(_TL("Combine Grids"));
31 Parameters.Set_Description(_TW(
32 "(c) 2005 by Victor Olaya."));
33
34 Parameters.Add_Grid(NULL,
35 "GRID1",
36 _TL("Grid 1"),
37 _TL(""),
38 PARAMETER_INPUT);
39
40 Parameters.Add_Grid(NULL,
41 "GRID2",
42 _TL("Grid 2"),
43 _TL(""),
44 PARAMETER_INPUT);
45
46 Parameters.Add_Grid(NULL,
47 "RESULT",
48 _TL("Result"),
49 _TL(""),
50 PARAMETER_OUTPUT);
51
52 pLookup = Parameters.Add_FixedTable(NULL,
53 "LOOKUP",
54 _TL("LookUp Table"),
55 _TL(""))->asTable();
56
57 pLookup->Add_Field(_TL("Value in Grid 1"), SG_DATATYPE_Double);
58 pLookup->Add_Field(_TL("Value in Grid 2"), SG_DATATYPE_Double);
59 pLookup->Add_Field(_TL("Resulting Value"), SG_DATATYPE_Double);
60
61 pRecord = pLookup->Add_Record();
62 pRecord->Set_Value(0, 0.0);
63 pRecord->Set_Value(1, 0.0);
64 pRecord->Set_Value(2, 0.0);
65
66
67 }//constructor
68
69
~CCombineGrids(void)70 CCombineGrids::~CCombineGrids(void)
71 {}
72
On_Execute(void)73 bool CCombineGrids::On_Execute(void){
74
75 int x,y;
76 int i;
77 int iCellValue1, iCellValue2;
78 int iTableValue1, iTableValue2;
79 int iResultValue;
80
81 CSG_Grid *pGrid1 = Parameters("GRID1")->asGrid();
82 CSG_Grid *pGrid2 = Parameters("GRID2")->asGrid();
83 CSG_Grid *pResult = Parameters("RESULT")->asGrid(); ;
84 CSG_Table *pLookup;
85 CSG_Table_Record *pRecord;
86
87 pLookup = Parameters("LOOKUP")->asTable();
88
89 for(y=0; y<Get_NY() && Set_Progress(y); y++){
90 for(x=0; x<Get_NX(); x++){
91 iCellValue1 = pGrid1->asInt(x,y);
92 iCellValue2 = pGrid2->asInt(x,y);
93 for (i = 0; i < pLookup->Get_Record_Count(); i++){
94 pRecord = pLookup->Get_Record(i);
95 iTableValue1 = pRecord->asInt(0);
96 iTableValue2 = pRecord->asInt(1);
97 if (iTableValue1 == iCellValue1){
98 if (iTableValue2 == iCellValue2){
99 iResultValue = pRecord->asInt(2);
100 pResult->Set_Value(x,y,iResultValue);
101 break;
102 }//if
103 }//if
104 }//for
105 if (i >= pLookup->Get_Record_Count()){
106 pResult->Set_NoData(x,y);
107 }//if
108 }//for
109 }//for
110 return true;
111
112 }//method
113