1
2 /*******************************************************************************
3 Grid_Pattern.cpp
4 Copyright (C) Victor Olaya
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA
19 *******************************************************************************/
20
21 ///////////////////////////////////////////////////////////
22 // //
23 // //
24 // //
25 ///////////////////////////////////////////////////////////
26
27 //---------------------------------------------------------
28 #include "Grid_Pattern.h"
29
30
31 ///////////////////////////////////////////////////////////
32 // //
33 // //
34 // //
35 ///////////////////////////////////////////////////////////
36
37 //---------------------------------------------------------
CGrid_Pattern(void)38 CGrid_Pattern::CGrid_Pattern(void)
39 {
40 Set_Name (_TL("Pattern Analysis"));
41
42 Set_Author ("Victor Olaya (c) 2004");
43
44 Set_Description (_TW(
45 "Pattern Analysis"
46 ));
47
48 //-----------------------------------------------------
49 Parameters.Add_Grid("",
50 "INPUT" , _TL("Input Grid"),
51 _TL(""),
52 PARAMETER_INPUT
53 );
54
55 Parameters.Add_Grid("",
56 "NDC" , _TL("Number of Classes"),
57 _TL(""),
58 PARAMETER_OUTPUT, true, SG_DATATYPE_Int
59 );
60
61 Parameters.Add_Grid("",
62 "RELATIVE" , _TL("Relative Richness"),
63 _TL(""),
64 PARAMETER_OUTPUT_OPTIONAL
65 );
66
67 Parameters.Add_Grid("",
68 "FRAGMENTATION" , _TL("Fragmentation"),
69 _TL(""),
70 PARAMETER_OUTPUT_OPTIONAL
71 );
72
73 Parameters.Add_Grid("",
74 "CVN" , _TL("Center vs. Neighbours"),
75 _TL(""),
76 PARAMETER_OUTPUT, true, SG_DATATYPE_Int
77 );
78
79 Parameters.Add_Grid("",
80 "DIVERSITY" , _TL("Diversity"),
81 _TL(""),
82 PARAMETER_OUTPUT
83 );
84
85 Parameters.Add_Grid("",
86 "DOMINANCE" , _TL("Dominance"),
87 _TL(""),
88 PARAMETER_OUTPUT
89 );
90
91 Parameters.Add_Int("",
92 "MAXNUMCLASS" , _TL("Max. Number of Classes"),
93 _TL("Maximum number of classes in entire grid."),
94 10
95 );
96
97 CSG_Grid_Cell_Addressor::Add_Parameters(Parameters);
98 }
99
100
101 ///////////////////////////////////////////////////////////
102 // //
103 ///////////////////////////////////////////////////////////
104
105 //---------------------------------------------------------
On_Execute(void)106 bool CGrid_Pattern::On_Execute(void)
107 {
108 m_pInput = Parameters("INPUT")->asGrid();
109
110 CSG_Grid *pnClasses = Parameters("NDC" )->asGrid();
111 CSG_Grid *pDiversity = Parameters("DIVERSITY" )->asGrid();
112 CSG_Grid *pRelative = Parameters("RELATIVE" )->asGrid();
113 CSG_Grid *pDominance = Parameters("DOMINANCE" )->asGrid();
114 CSG_Grid *pFragmentation = Parameters("FRAGMENTATION")->asGrid();
115 CSG_Grid *pCVN = Parameters("CVN" )->asGrid();
116
117 if( !m_Kernel.Set_Parameters(Parameters) )
118 {
119 Error_Set(_TL("could not initialize kernel"));
120
121 return( false );
122 }
123
124 int maxClasses = Parameters("MAXNUMCLASS")->asInt();
125
126 //-----------------------------------------------------
127 for(int y=0; y<Get_NY() && Set_Progress(y); y++)
128 {
129 #ifndef _DEBUG
130 #pragma omp parallel for
131 #endif
132 for(int x=0; x<Get_NX(); x++)
133 {
134 int nCells, nClasses, nCVN; double Diversity;
135
136 if( Get_Pattern(x, y, nCells, nClasses, nCVN, Diversity) )
137 {
138 SG_GRID_PTR_SAFE_SET_VALUE (pnClasses , x, y, nClasses);
139 SG_GRID_PTR_SAFE_SET_VALUE (pRelative , x, y, 100. * nClasses / (double)maxClasses);
140 SG_GRID_PTR_SAFE_SET_VALUE (pFragmentation, x, y, nClasses / (double)nCells);
141 SG_GRID_PTR_SAFE_SET_VALUE (pCVN , x, y, nCVN);
142 SG_GRID_PTR_SAFE_SET_VALUE (pDiversity , x, y, Diversity);
143 SG_GRID_PTR_SAFE_SET_VALUE (pDominance , x, y, log((double)nClasses) - Diversity);
144 }
145 else
146 {
147 SG_GRID_PTR_SAFE_SET_NODATA(pnClasses , x, y);
148 SG_GRID_PTR_SAFE_SET_NODATA(pRelative , x, y);
149 SG_GRID_PTR_SAFE_SET_NODATA(pDiversity , x, y);
150 SG_GRID_PTR_SAFE_SET_NODATA(pDominance , x, y);
151 SG_GRID_PTR_SAFE_SET_NODATA(pFragmentation, x, y);
152 SG_GRID_PTR_SAFE_SET_NODATA(pCVN , x, y);
153 }
154 }
155 }
156
157 //-----------------------------------------------------
158 m_Kernel.Destroy();
159
160 return( true );
161 }
162
163
164 ///////////////////////////////////////////////////////////
165 // //
166 ///////////////////////////////////////////////////////////
167
168 //---------------------------------------------------------
Get_Pattern(int x,int y,int & nCells,int & nClasses,int & nCVN,double & Diversity)169 bool CGrid_Pattern::Get_Pattern(int x, int y, int &nCells, int &nClasses, int &nCVN, double &Diversity)
170 {
171 if( m_pInput->is_NoData(x, y) )
172 {
173 return( false );
174 }
175
176 nCells = nCVN = 0;
177
178 CSG_Unique_Number_Statistics Classes;
179
180 double iz, z = m_pInput->asDouble(x, y);
181
182 for(int i=0; i<m_Kernel.Get_Count(); i++)
183 {
184 int ix = m_Kernel.Get_X(i, x);
185 int iy = m_Kernel.Get_Y(i, y);
186
187 if( m_pInput->is_InGrid(ix, iy) )
188 {
189 nCells++;
190
191 Classes += (iz = m_pInput->asDouble(ix, iy));
192
193 if( z != iz )
194 {
195 nCVN++;
196 }
197 }
198 }
199
200 nClasses = Classes.Get_Count();
201
202 Diversity = 0.; // the Shannon index !!!
203
204 for(int i=0; i<Classes.Get_Count(); i++)
205 {
206 double p = Classes.Get_Count(i) / (double)nCells;
207
208 Diversity -= p * log(p);
209 }
210
211 return( true );
212 }
213
214
215 ///////////////////////////////////////////////////////////
216 // //
217 // //
218 // //
219 ///////////////////////////////////////////////////////////
220
221 //---------------------------------------------------------
222