root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridTools / mergeGrids / MergeGridsAlgorithm.java @ 59
History | View | Annotate | Download (3.83 KB)
1 |
package es.unex.sextante.gridTools.mergeGrids; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput; |
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 |
|
12 |
public class MergeGridsAlgorithm |
13 |
extends
|
14 |
GeoAlgorithm { |
15 |
|
16 |
public static final String METHOD = "METHOD"; |
17 |
public static final String INPUT = "INPUT"; |
18 |
public static final String RESULT = "RESULT"; |
19 |
|
20 |
|
21 |
@Override
|
22 |
public void defineCharacteristics() { |
23 |
|
24 |
|
25 |
final String[] sMethod = { Sextante.getText("Nearest_neighbour"), Sextante.getText("Bilinear"), |
26 |
Sextante.getText("Inverse_distance"), Sextante.getText("Cubic_spline"), Sextante.getText("B-spline") }; |
27 |
|
28 |
setName(Sextante.getText("Merge_grids"));
|
29 |
setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
|
30 |
setUserCanDefineAnalysisExtent(true);
|
31 |
|
32 |
try {
|
33 |
m_Parameters.addMultipleInput(INPUT, Sextante.getText("Layers"), AdditionalInfoMultipleInput.DATA_TYPE_RASTER, true); |
34 |
m_Parameters.addSelection(METHOD, Sextante.getText("Interpolation_method"), sMethod);
|
35 |
addOutputRasterLayer(RESULT, Sextante.getText("Merged_grids"));
|
36 |
} |
37 |
catch (final RepeatedParameterNameException e) { |
38 |
Sextante.addErrorToLog(e); |
39 |
} |
40 |
|
41 |
} |
42 |
|
43 |
|
44 |
@Override
|
45 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
46 |
|
47 |
int i;
|
48 |
int x, y;
|
49 |
int iMethod;
|
50 |
int iNX, iNY;
|
51 |
double dValue, dValueSum;
|
52 |
double dValidValues;
|
53 |
IRasterLayer[] window;
|
54 |
|
55 |
iMethod = m_Parameters.getParameterValueAsInt(METHOD); |
56 |
final ArrayList input = m_Parameters.getParameterValueAsArrayList(INPUT); |
57 |
|
58 |
if (input.size() == 0) { |
59 |
return false; |
60 |
} |
61 |
|
62 |
boolean bAddWarning = false; |
63 |
int iBands = ((IRasterLayer) input.get(0)).getBandsCount(); |
64 |
for (i = 0; i < input.size(); i++) { |
65 |
final IRasterLayer layer = ((IRasterLayer) input.get(i));
|
66 |
if (layer.getBandsCount() != iBands) {
|
67 |
iBands = Math.max(layer.getBandsCount(), iBands);
|
68 |
bAddWarning = true;
|
69 |
} |
70 |
} |
71 |
|
72 |
if (bAddWarning) {
|
73 |
Sextante.addWarningToLog(Sextante.getText("Layers_to_be_merged_have_different_number_of_bands"));
|
74 |
} |
75 |
|
76 |
final IRasterLayer result = getNewRasterLayer(RESULT, Sextante.getText("Merged_grids"), |
77 |
((IRasterLayer) input.get(0)).getDataType(), iBands);
|
78 |
|
79 |
result.assignNoData(); |
80 |
|
81 |
window = new IRasterLayer[input.size()];
|
82 |
for (i = 0; i < input.size(); i++) { |
83 |
window[i] = ((IRasterLayer) input.get(i)); |
84 |
window[i].setWindowExtent(result.getWindowGridExtent()); |
85 |
window[i].setInterpolationMethod(iMethod); |
86 |
} |
87 |
|
88 |
iNX = window[0].getNX();
|
89 |
iNY = window[0].getNY();
|
90 |
|
91 |
for (int iBand = 0; iBand < iBands; iBand++) { |
92 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
93 |
for (x = 0; x < iNX; x++) { |
94 |
dValueSum = 0;
|
95 |
dValidValues = 0;
|
96 |
for (i = 0; i < window.length; i++) { |
97 |
dValue = window[i].getCellValueAsDouble(x, y, iBand); |
98 |
if (!window[i].isNoDataValue(dValue)) {
|
99 |
dValidValues++; |
100 |
dValueSum += dValue; |
101 |
} |
102 |
} |
103 |
if (dValidValues == 0.0) { |
104 |
result.setNoData(x, y); |
105 |
} |
106 |
else {
|
107 |
result.setCellValue(x, y, iBand, dValueSum / dValidValues); |
108 |
} |
109 |
} |
110 |
} |
111 |
} |
112 |
|
113 |
return !m_Task.isCanceled();
|
114 |
|
115 |
} |
116 |
|
117 |
} |