root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridAnalysis / owa / OWAAlgorithm.java @ 59
History | View | Annotate | Download (3.76 KB)
1 |
package es.unex.sextante.gridAnalysis.owa; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
|
6 |
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput; |
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 |
import es.unex.sextante.parameters.FixedTableModel; |
13 |
import es.unex.sextante.rasterWrappers.GridWrapper; |
14 |
|
15 |
public class OWAAlgorithm |
16 |
extends
|
17 |
GeoAlgorithm { |
18 |
|
19 |
public static final String INPUT = "INPUT"; |
20 |
public static final String WEIGHTS = "WEIGHTS"; |
21 |
public static final String RESULT = "RESULT"; |
22 |
|
23 |
|
24 |
@Override
|
25 |
public void defineCharacteristics() { |
26 |
|
27 |
setName(Sextante.getText("Ordered_Weighted_Averaging__OWA"));
|
28 |
setGroup(Sextante.getText("Raster_layer_analysis"));
|
29 |
setUserCanDefineAnalysisExtent(true);
|
30 |
|
31 |
try {
|
32 |
m_Parameters.addMultipleInput(INPUT, Sextante.getText("Layers"), AdditionalInfoMultipleInput.DATA_TYPE_RASTER, true); |
33 |
m_Parameters.addFixedTable(WEIGHTS, Sextante.getText("Weights"), new String[] { Sextante.getText("Pesos") }, 3, false); |
34 |
addOutputRasterLayer(RESULT, Sextante.getText("Result"));
|
35 |
} |
36 |
catch (final RepeatedParameterNameException e) { |
37 |
Sextante.addErrorToLog(e); |
38 |
} |
39 |
|
40 |
} |
41 |
|
42 |
|
43 |
@Override
|
44 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
45 |
|
46 |
int i;
|
47 |
int x, y;
|
48 |
double dResult;
|
49 |
double dValues[]; |
50 |
double dWeights[]; |
51 |
boolean bNoDataValue;
|
52 |
IRasterLayer windows[];
|
53 |
|
54 |
final ArrayList layers = m_Parameters.getParameterValueAsArrayList(INPUT); |
55 |
final FixedTableModel weights = (FixedTableModel) m_Parameters.getParameterValueAsObject(WEIGHTS);
|
56 |
|
57 |
if ((layers.size() == 0) || (weights.getRowCount() == 0) || (weights.getRowCount() < layers.size())) { |
58 |
return false; |
59 |
} |
60 |
|
61 |
final IRasterLayer result = getNewRasterLayer(RESULT, Sextante.getText("OWA"), IRasterLayer.RASTER_DATA_TYPE_DOUBLE); |
62 |
|
63 |
dWeights = new double[layers.size()]; |
64 |
windows = new IRasterLayer[layers.size()];
|
65 |
|
66 |
for (i = 0; i < layers.size(); i++) { |
67 |
windows[i] = (IRasterLayer) layers.get(i); |
68 |
windows[i].setWindowExtent(result.getWindowGridExtent()); |
69 |
windows[i].setInterpolationMethod(GridWrapper.INTERPOLATION_BSpline); |
70 |
try {
|
71 |
dWeights[i] = Double.parseDouble(weights.getValueAt(i, 0).toString()); |
72 |
} |
73 |
catch (final NumberFormatException e) { |
74 |
throw new GeoAlgorithmExecutionException(Sextante.getText("OWA_Invalid_weight")); |
75 |
} |
76 |
} |
77 |
|
78 |
final int iNX = result.getWindowGridExtent().getNX(); |
79 |
final int iNY = result.getWindowGridExtent().getNY(); |
80 |
|
81 |
dValues = new double[layers.size()]; |
82 |
|
83 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
84 |
for (x = 0; x < iNX; x++) { |
85 |
bNoDataValue = false;
|
86 |
for (i = 0; i < dValues.length; i++) { |
87 |
dValues[i] = windows[i].getCellValueAsDouble(x, y); |
88 |
if (windows[i].isNoDataValue(dValues[i])) {
|
89 |
bNoDataValue = true;
|
90 |
break;
|
91 |
} |
92 |
} |
93 |
if (bNoDataValue) {
|
94 |
result.setNoData(x, y); |
95 |
} |
96 |
else {
|
97 |
Arrays.sort(dValues);
|
98 |
dResult = 0;
|
99 |
for (i = 0; i < dValues.length; i++) { |
100 |
dResult += (dValues[i] * dWeights[i]); |
101 |
} |
102 |
result.setCellValue(x, y, dResult); |
103 |
} |
104 |
} |
105 |
} |
106 |
|
107 |
return !m_Task.isCanceled();
|
108 |
|
109 |
} |
110 |
|
111 |
} |