root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridCategorical / reclassifyEqualArea / ReclassifyEqualAreaAlgorithm.java @ 59
History | View | Annotate | Download (3.14 KB)
1 |
package es.unex.sextante.gridCategorical.reclassifyEqualArea; |
---|---|
2 |
|
3 |
import java.util.Arrays; |
4 |
|
5 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
6 |
import es.unex.sextante.core.GeoAlgorithm; |
7 |
import es.unex.sextante.core.Sextante; |
8 |
import es.unex.sextante.dataObjects.IRasterLayer; |
9 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
10 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
11 |
import es.unex.sextante.rasterWrappers.GridCell; |
12 |
|
13 |
public class ReclassifyEqualAreaAlgorithm |
14 |
extends
|
15 |
GeoAlgorithm { |
16 |
|
17 |
public static final String INPUT = "INPUT"; |
18 |
public static final String CLASSAREA = "CLASSAREA"; |
19 |
public static final String RECLASS = "RECLASS"; |
20 |
|
21 |
|
22 |
@Override
|
23 |
public void defineCharacteristics() { |
24 |
|
25 |
setName(Sextante.getText("Divide_into_n_classes_of_equal_area"));
|
26 |
setGroup(Sextante.getText("Reclassify_raster_layers"));
|
27 |
setUserCanDefineAnalysisExtent(true);
|
28 |
|
29 |
try {
|
30 |
m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true); |
31 |
m_Parameters.addNumericalValue(CLASSAREA, Sextante.getText("Class_area_[cells]"),
|
32 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 100, 2, Integer.MAX_VALUE); |
33 |
addOutputRasterLayer(RECLASS, Sextante.getText("Reclassify"));
|
34 |
} |
35 |
catch (final RepeatedParameterNameException e) { |
36 |
Sextante.addErrorToLog(e); |
37 |
} |
38 |
|
39 |
} |
40 |
|
41 |
|
42 |
@Override
|
43 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
44 |
|
45 |
int i;
|
46 |
int iCell;
|
47 |
int iClassArea;
|
48 |
GridCell cell; |
49 |
|
50 |
final IRasterLayer grid = m_Parameters.getParameterValueAsRasterLayer(INPUT);
|
51 |
iClassArea = m_Parameters.getParameterValueAsInt(CLASSAREA); |
52 |
|
53 |
final IRasterLayer result = getNewRasterLayer(RECLASS, grid.getName() + Sextante.getText("[reclassified]"), |
54 |
IRasterLayer.RASTER_DATA_TYPE_INT); |
55 |
|
56 |
grid.setWindowExtent(getAnalysisExtent()); |
57 |
|
58 |
final GridCell[] cells = getSortedArrayOfCells(grid); |
59 |
|
60 |
result.assignNoData(); |
61 |
|
62 |
iCell = 0;
|
63 |
|
64 |
for (i = 0; i < cells.length; i++) { |
65 |
cell = cells[i]; |
66 |
if (!grid.isNoDataValue(cell.getValue())) {
|
67 |
final int iValue = (iCell / iClassArea); |
68 |
result.setCellValue(cell.getX(), cell.getY(), iValue); |
69 |
if (!setProgress(iCell, cells.length)) {
|
70 |
return false; |
71 |
} |
72 |
iCell++; |
73 |
} |
74 |
else {
|
75 |
result.setNoData(cell.getX(), cell.getY()); |
76 |
} |
77 |
} |
78 |
|
79 |
return !m_Task.isCanceled();
|
80 |
|
81 |
} |
82 |
|
83 |
|
84 |
public GridCell[] getSortedArrayOfCells(final IRasterLayer layer) { |
85 |
|
86 |
int i;
|
87 |
int iX, iY;
|
88 |
final int iNX = layer.getNX(); |
89 |
final int iCells = layer.getNX() * layer.getNY(); |
90 |
GridCell[] cells;
|
91 |
GridCell cell; |
92 |
|
93 |
cells = new GridCell[iCells];
|
94 |
|
95 |
for (i = 0; i < iCells; i++) { |
96 |
iX = i % iNX; |
97 |
iY = i / iNX; |
98 |
cell = new GridCell(iX, iY, layer.getCellValueAsDouble(iX, iY));
|
99 |
cells[i] = cell; |
100 |
} |
101 |
|
102 |
Arrays.sort(cells);
|
103 |
|
104 |
return cells;
|
105 |
|
106 |
} |
107 |
|
108 |
|
109 |
} |