1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 /*******************************************************************************
5     CrossClassification.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 #include "CrossClassification.h"
23 
CCrossClassification(void)24 CCrossClassification::CCrossClassification(void){
25 
26 	Parameters.Set_Name(_TL("Cross-Classification and Tabulation"));
27 	Parameters.Set_Description(_TW(
28 		"(c) 2004 by Victor Olaya. Cross-Classification and Tabulation"));
29 
30 	Parameters.Add_Grid(NULL,
31 						"INPUT",
32 						_TL("Input Grid 1"),
33 						_TL(""),
34 						PARAMETER_INPUT);
35 
36 	Parameters.Add_Grid(NULL,
37 						"INPUT2",
38 						_TL("Input Grid 2"),
39 						_TL(""),
40 						PARAMETER_INPUT);
41 
42 	Parameters.Add_Grid(NULL,
43 						"RESULTGRID",
44 						_TL("Cross-Classification Grid"),
45 						_TL(""),
46 						PARAMETER_OUTPUT);
47 
48 	Parameters.Add_Table(NULL,
49 						"RESULTTABLE",
50 						_TL("Cross-Tabulation Table"),
51 						_TL(""),
52 						PARAMETER_OUTPUT);
53 
54 	Parameters.Add_Value(NULL,
55 						"MAXNUMCLASS",
56 						_TL("Max. Number of Classes"),
57 						_TL("Maximum number of classes in the entire grid."),
58 						PARAMETER_TYPE_Int,
59 						10);
60 
61 }//constructor
62 
~CCrossClassification(void)63 CCrossClassification::~CCrossClassification(void){}
64 
On_Execute(void)65 bool CCrossClassification::On_Execute(void){
66 
67 	CSG_Grid* pInput = Parameters("INPUT")->asGrid();
68 	CSG_Grid* pInput2 = Parameters("INPUT2")->asGrid();
69 	CSG_Grid* pOutput = Parameters("RESULTGRID")->asGrid();
70 	CSG_Table* pTable = Parameters("RESULTTABLE")->asTable();
71 	CSG_Table_Record* pRecord;
72 	int iNumClasses = Parameters("MAXNUMCLASS")->asInt();
73 	int iClass, iClass2;
74 	int iNewClass;
75 	int i,j;
76 	int x,y;
77 	int **pTabulation = new int* [iNumClasses];
78 	int *pTotal, iTotal;
79 
80 	pTable->Create((CSG_Table*)NULL);
81 	pTable->Set_Name(_TL("Cross-Tabulation"));
82 
83 	for (i = 0; i < iNumClasses; i++){
84 		pTable->Add_Field(SG_Get_String(i+1).c_str(), SG_DATATYPE_Int);
85 		pTabulation[i] = new int[iNumClasses];
86 		for(j=0; j < iNumClasses; j++){
87 			pTabulation[i][j] = 0;
88 		}//for
89 	}//for
90 	pTable->Add_Field(_TL("Total"), SG_DATATYPE_Int);
91 
92 	for(y=0; y<Get_NY() && Set_Progress(y); y++){
93 		for(x=0; x<Get_NX(); x++){
94 			if (!pInput->is_NoData(x,y) && !pInput2->is_NoData(x,y)){
95 				iClass = pInput->asInt(x,y)-1;
96 				iClass2 = pInput2->asInt(x,y)-1;
97 				if (iClass < iNumClasses && iClass >= 0
98 						&& iClass2 < iNumClasses && iClass2 >= 0){
99 					pTabulation[iClass][iClass2]++;
100 				}//if
101 				iNewClass = iClass * iNumClasses + iClass2;
102 				pOutput->Set_Value(x,y,iNewClass);
103 			}//if
104 		}//for
105 	}//for
106 
107 	pTotal = new int[iNumClasses];
108 	for(i=0; i<iNumClasses; i++){
109 		pTotal[i] = 0;
110 	}//for
111 	for(i=0; i<iNumClasses; i++){
112 		pRecord	= pTable->Add_Record();
113 		iTotal = 0;
114 	 	for(j=0; j<iNumClasses; j++){
115 			pRecord->Set_Value(j, pTabulation[i][j]);
116 			iTotal += pTabulation[i][j];
117 			pTotal[j] += pTabulation[i][j];
118 		}//for
119 		pRecord->Set_Value(j, iTotal);
120 	}//for
121 	pRecord	= pTable->Add_Record();
122 	for(i=0; i<iNumClasses; i++){
123 		pRecord->Set_Value(i, pTotal[i]);
124 	}//for
125 
126 	for (i = 0; i < iNumClasses; i++){
127 		delete [] pTabulation[i];
128 	}//for
129 
130 	delete [] pTabulation;
131 	delete [] pTotal;
132 
133 	return true;
134 
135 }//method