root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridCategorical / aggregationIndex / AggregationIndexAlgorithm.java @ 59
History | View | Annotate | Download (5.21 KB)
1 |
package es.unex.sextante.gridCategorical.aggregationIndex; |
---|---|
2 |
|
3 |
import java.util.HashMap; |
4 |
import java.util.Iterator; |
5 |
import java.util.Set; |
6 |
|
7 |
import es.unex.sextante.core.GeoAlgorithm; |
8 |
import es.unex.sextante.core.Sextante; |
9 |
import es.unex.sextante.dataObjects.IRasterLayer; |
10 |
import es.unex.sextante.dataObjects.ITable; |
11 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
12 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
13 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
14 |
|
15 |
public class AggregationIndexAlgorithm |
16 |
extends
|
17 |
GeoAlgorithm { |
18 |
|
19 |
public static final String RESULT = "RESULT"; |
20 |
public static final String INPUT = "INPUT"; |
21 |
|
22 |
private final int m_iOffsetX[] = { -1, 0, 0, 1 }; |
23 |
private final int m_iOffsetY[] = { 0, -1, 1, 0 }; |
24 |
|
25 |
int m_iNX, m_iNY;
|
26 |
int m_iTotalArea;
|
27 |
IRasterLayer m_Window; |
28 |
HashMap m_Map;
|
29 |
boolean m_IsCellAlreadyVisited[][]; |
30 |
|
31 |
|
32 |
@Override
|
33 |
public void defineCharacteristics() { |
34 |
|
35 |
this.setName(Sextante.getText("Aggregation_index")); |
36 |
setGroup(Sextante.getText("Raster_categories_analysis"));
|
37 |
setUserCanDefineAnalysisExtent(false);
|
38 |
|
39 |
try {
|
40 |
m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true); |
41 |
addOutputTable(RESULT, Sextante.getText("Aggregation_index"));
|
42 |
} |
43 |
catch (final RepeatedParameterNameException e) { |
44 |
Sextante.addErrorToLog(e); |
45 |
} |
46 |
|
47 |
} |
48 |
|
49 |
|
50 |
@Override
|
51 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
52 |
|
53 |
m_Window = m_Parameters.getParameterValueAsRasterLayer(INPUT); |
54 |
|
55 |
m_Window.setFullExtent(); |
56 |
|
57 |
m_iNX = m_Window.getNX(); |
58 |
m_iNY = m_Window.getNY(); |
59 |
|
60 |
m_Map = new HashMap(); |
61 |
|
62 |
if (calculateIndex()) {
|
63 |
createTable(); |
64 |
return true; |
65 |
} |
66 |
else {
|
67 |
return false; |
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
|
73 |
private boolean calculateIndex() { |
74 |
|
75 |
int i;
|
76 |
int x, y;
|
77 |
int iClass, iClass2;
|
78 |
AggregationInfo info; |
79 |
|
80 |
m_iTotalArea = 0;
|
81 |
|
82 |
for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) { |
83 |
for (x = 0; x < m_iNX; x++) { |
84 |
iClass = m_Window.getCellValueAsInt(x, y); |
85 |
info = (AggregationInfo) m_Map.get(new Integer(iClass)); |
86 |
if (info != null) { |
87 |
m_iTotalArea++; |
88 |
info.iArea++; |
89 |
for (i = 0; i < 4; i++) { |
90 |
iClass2 = m_Window.getCellValueAsInt(x + m_iOffsetX[i], y + m_iOffsetY[i]); |
91 |
if (iClass2 == iClass) {
|
92 |
info.iAggregation++; |
93 |
} |
94 |
} |
95 |
} |
96 |
else {
|
97 |
info = new AggregationInfo(iClass);
|
98 |
m_iTotalArea++; |
99 |
info.iArea++; |
100 |
for (i = 0; i < 4; i++) { |
101 |
iClass2 = m_Window.getCellValueAsInt(x + m_iOffsetX[i], y + m_iOffsetY[i]); |
102 |
if (iClass2 == iClass) {
|
103 |
info.iAggregation++; |
104 |
} |
105 |
} |
106 |
m_Map.put(new Integer(iClass), info); |
107 |
} |
108 |
} |
109 |
} |
110 |
|
111 |
return !m_Task.isCanceled();
|
112 |
|
113 |
} |
114 |
|
115 |
|
116 |
private void createTable() throws UnsupportedOutputChannelException { |
117 |
|
118 |
int iLargestInt;
|
119 |
int iRemainder;
|
120 |
int iMaxEii;
|
121 |
Object[] values; |
122 |
AggregationInfo info; |
123 |
final Set set = m_Map.keySet(); |
124 |
final Iterator iter = set.iterator(); |
125 |
|
126 |
final String sFields[] = { Sextante.getText("Class"), Sextante.getText("Area_cells"), Sextante.getText("Area[%]"), |
127 |
Sextante.getText("Aggregation_index") };
|
128 |
final Class types[] = { Integer.class, Integer.class, Double.class, Double.class }; |
129 |
final String sTableName = Sextante.getText("Aggregation_index_[") + m_Window.getName() + "]"; |
130 |
|
131 |
final ITable table = getNewTable(RESULT, sTableName, types, sFields);
|
132 |
values = new Object[4]; |
133 |
|
134 |
while (iter.hasNext()) {
|
135 |
info = (AggregationInfo) m_Map.get(iter.next()); |
136 |
values[0] = new Integer(info.iClass); |
137 |
values[1] = new Integer(info.iArea); |
138 |
values[2] = new Double((double) info.iArea / (double) m_iTotalArea * 100.); |
139 |
iLargestInt = (int) Math.floor(Math.sqrt(info.iArea)); |
140 |
iRemainder = (info.iArea - (iLargestInt * iLargestInt)); |
141 |
if (iRemainder != 0) { |
142 |
if (iRemainder < iLargestInt) {
|
143 |
iMaxEii = 2 * iLargestInt * (iLargestInt - 1) + 2 * iRemainder - 1; |
144 |
} |
145 |
else {
|
146 |
iMaxEii = 2 * iLargestInt * (iLargestInt - 1) + 2 * iRemainder - 2; |
147 |
} |
148 |
} |
149 |
else {
|
150 |
iMaxEii = 2 * iLargestInt * (iLargestInt - 1); |
151 |
} |
152 |
values[3] = new Double((double) info.iAggregation / (double) iMaxEii / 2.0); |
153 |
table.addRecord(values); |
154 |
} |
155 |
|
156 |
} |
157 |
|
158 |
private class AggregationInfo { |
159 |
|
160 |
public int iClass; |
161 |
public int iArea; |
162 |
public int iAggregation; |
163 |
|
164 |
|
165 |
public AggregationInfo(final int i) { |
166 |
|
167 |
iClass = i; |
168 |
iArea = 0;
|
169 |
iAggregation = 0;
|
170 |
|
171 |
} |
172 |
|
173 |
} |
174 |
|
175 |
} |