root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridCategorical / filterClumps / FilterClumpsAlgorithm.java @ 59
History | View | Annotate | Download (5.98 KB)
1 |
package es.unex.sextante.gridCategorical.filterClumps; |
---|---|
2 |
|
3 |
import java.awt.Point; |
4 |
import java.util.ArrayList; |
5 |
|
6 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
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.exceptions.GeoAlgorithmExecutionException; |
11 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
12 |
|
13 |
public class FilterClumpsAlgorithm |
14 |
extends
|
15 |
GeoAlgorithm { |
16 |
|
17 |
private final static int m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 }; |
18 |
private final static int m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 }; |
19 |
|
20 |
public static final String INPUT = "INPUT"; |
21 |
public static final String MINAREA = "MINAREA"; |
22 |
public static final String RESULT = "RESULT"; |
23 |
|
24 |
int m_iArea;
|
25 |
int m_iNX, m_iNY;
|
26 |
IRasterLayer m_Grid; |
27 |
IRasterLayer m_Result; |
28 |
boolean m_IsCellAlreadyVisited[][]; |
29 |
boolean m_IsCellAlreadyVisitedArea[][]; |
30 |
|
31 |
|
32 |
@Override
|
33 |
public void defineCharacteristics() { |
34 |
|
35 |
setName(Sextante.getText("Filter_clumps"));
|
36 |
setGroup(Sextante.getText("Raster_categories_analysis"));
|
37 |
setUserCanDefineAnalysisExtent(true);
|
38 |
|
39 |
try {
|
40 |
m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true); |
41 |
m_Parameters.addNumericalValue(MINAREA, Sextante.getText("Min_area_[cells]"),
|
42 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 100, 1, Integer.MAX_VALUE); |
43 |
addOutputRasterLayer(RESULT, Sextante.getText("Filtered_layer"));
|
44 |
} |
45 |
catch (final RepeatedParameterNameException e) { |
46 |
Sextante.addErrorToLog(e); |
47 |
} |
48 |
|
49 |
} |
50 |
|
51 |
|
52 |
@Override
|
53 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
54 |
|
55 |
int x, y;
|
56 |
|
57 |
m_Grid = m_Parameters.getParameterValueAsRasterLayer(INPUT); |
58 |
m_iArea = m_Parameters.getParameterValueAsInt(MINAREA); |
59 |
|
60 |
m_Result = getNewRasterLayer(RESULT, m_Grid.getName() + "[filt]", IRasterLayer.RASTER_DATA_TYPE_INT);
|
61 |
|
62 |
m_Grid.setWindowExtent(m_Result); |
63 |
m_Grid.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour); |
64 |
|
65 |
m_iNX = m_Grid.getNX(); |
66 |
m_iNY = m_Grid.getNY(); |
67 |
|
68 |
m_IsCellAlreadyVisited = new boolean[m_iNX][m_iNY]; |
69 |
m_IsCellAlreadyVisitedArea = new boolean[m_iNX][m_iNY]; |
70 |
|
71 |
m_Result.assign(m_Grid); |
72 |
|
73 |
for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) { |
74 |
for (x = 0; x < m_iNX; x++) { |
75 |
if (!m_IsCellAlreadyVisited[x][y]) {
|
76 |
int iArea = getArea(x, y);
|
77 |
if (iArea < m_iArea) {
|
78 |
setToNoData(x, y); |
79 |
} |
80 |
else {
|
81 |
iArea = iArea + 1;
|
82 |
} |
83 |
} |
84 |
} |
85 |
} |
86 |
|
87 |
return !m_Task.isCanceled();
|
88 |
|
89 |
} |
90 |
|
91 |
|
92 |
private int getArea(int x, |
93 |
int y) {
|
94 |
|
95 |
int x2, y2;
|
96 |
int iInitClass;
|
97 |
int iPt;
|
98 |
int n;
|
99 |
int iClass;
|
100 |
int iArea = 1; |
101 |
ArrayList centralPoints = new ArrayList(); |
102 |
ArrayList adjPoints = new ArrayList(); |
103 |
Point point;
|
104 |
|
105 |
if (m_IsCellAlreadyVisitedArea[x][y]) {
|
106 |
return Integer.MAX_VALUE; |
107 |
} |
108 |
|
109 |
iInitClass = m_Grid.getCellValueAsInt(x, y); |
110 |
|
111 |
centralPoints.add(new Point(x, y)); |
112 |
m_IsCellAlreadyVisitedArea[x][y] = true;
|
113 |
|
114 |
while (centralPoints.size() != 0) { |
115 |
for (iPt = 0; iPt < centralPoints.size(); iPt++) { |
116 |
point = (Point) centralPoints.get(iPt);
|
117 |
x = point.x; |
118 |
y = point.y; |
119 |
iClass = m_Grid.getCellValueAsInt(x, y); |
120 |
if (!m_Grid.isNoDataValue(iClass)) {
|
121 |
for (n = 0; n < 8; n++) { |
122 |
x2 = x + m_iOffsetX[n]; |
123 |
y2 = y + m_iOffsetY[n]; |
124 |
iClass = m_Grid.getCellValueAsInt(x2, y2); |
125 |
if (!m_Grid.isNoDataValue(iClass)) {
|
126 |
if (m_IsCellAlreadyVisitedArea[x2][y2] == false) { |
127 |
if (iInitClass == iClass) {
|
128 |
m_IsCellAlreadyVisitedArea[x2][y2] = true;
|
129 |
adjPoints.add(new Point(x2, y2)); |
130 |
iArea += 1;
|
131 |
} |
132 |
} |
133 |
} |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
centralPoints = adjPoints; |
139 |
adjPoints = new ArrayList(); |
140 |
|
141 |
if (m_Task.isCanceled()) {
|
142 |
return Integer.MAX_VALUE; |
143 |
} |
144 |
|
145 |
} |
146 |
|
147 |
return iArea;
|
148 |
|
149 |
} |
150 |
|
151 |
|
152 |
private void setToNoData(int x, |
153 |
int y) {
|
154 |
|
155 |
int x2, y2;
|
156 |
int iInitClass;
|
157 |
int iPt;
|
158 |
int n;
|
159 |
int iClass;
|
160 |
ArrayList centralPoints = new ArrayList(); |
161 |
ArrayList adjPoints = new ArrayList(); |
162 |
Point point;
|
163 |
|
164 |
iInitClass = m_Result.getCellValueAsInt(x, y); |
165 |
|
166 |
centralPoints.add(new Point(x, y)); |
167 |
|
168 |
while (centralPoints.size() != 0) { |
169 |
for (iPt = 0; iPt < centralPoints.size(); iPt++) { |
170 |
point = (Point) centralPoints.get(iPt);
|
171 |
x = point.x; |
172 |
y = point.y; |
173 |
iClass = m_Result.getCellValueAsInt(x, y); |
174 |
if (!m_Result.isNoDataValue(iClass)) {
|
175 |
m_Result.setNoData(x, y); |
176 |
for (n = 0; n < 8; n++) { |
177 |
x2 = x + m_iOffsetX[n]; |
178 |
y2 = y + m_iOffsetY[n]; |
179 |
iClass = m_Result.getCellValueAsInt(x2, y2); |
180 |
if (!m_Result.isNoDataValue(iClass)) {
|
181 |
if (iInitClass == iClass) {
|
182 |
adjPoints.add(new Point(x2, y2)); |
183 |
} |
184 |
} |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
centralPoints = adjPoints; |
190 |
adjPoints = new ArrayList(); |
191 |
|
192 |
if (m_Task.isCanceled()) {
|
193 |
return;
|
194 |
} |
195 |
|
196 |
} |
197 |
|
198 |
} |
199 |
|
200 |
} |