root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridCalculus / kernelFilter / KernelFilterAlgorithm.java @ 59
History | View | Annotate | Download (3.5 KB)
1 |
package es.unex.sextante.gridCalculus.kernelFilter; |
---|---|
2 |
|
3 |
import es.unex.sextante.core.GeoAlgorithm; |
4 |
import es.unex.sextante.core.Sextante; |
5 |
import es.unex.sextante.dataObjects.IRasterLayer; |
6 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
7 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
8 |
import es.unex.sextante.parameters.FixedTableModel; |
9 |
|
10 |
public class KernelFilterAlgorithm |
11 |
extends
|
12 |
GeoAlgorithm { |
13 |
|
14 |
public static final String RESULT = "RESULT"; |
15 |
public static final String KERNEL = "KERNEL"; |
16 |
public static final String LAYER = "LAYER"; |
17 |
|
18 |
private IRasterLayer m_Window;
|
19 |
protected double m_dValues[]; |
20 |
private double m_dNoData; |
21 |
private double m_dCoeffs[]; |
22 |
|
23 |
|
24 |
@Override
|
25 |
public void defineCharacteristics() { |
26 |
|
27 |
final String sColumnNames[] = { "1", "2", "3" }; |
28 |
setUserCanDefineAnalysisExtent(false);
|
29 |
setName(Sextante.getText("User-defined_3_X_3_filter"));
|
30 |
setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
|
31 |
try {
|
32 |
m_Parameters.addInputRasterLayer(LAYER, Sextante.getText("Layer"), true); |
33 |
m_Parameters.addFixedTable(KERNEL, Sextante.getText("Filter_kernel"), sColumnNames, 3, true); |
34 |
addOutputRasterLayer(RESULT, this.getName());
|
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 x, y;
|
47 |
int iNX, iNY;
|
48 |
|
49 |
m_Window = m_Parameters.getParameterValueAsRasterLayer(LAYER); |
50 |
final FixedTableModel kernel = (FixedTableModel) m_Parameters.getParameterValueAsObject(KERNEL);
|
51 |
m_Window.setFullExtent(); |
52 |
|
53 |
final IRasterLayer result = getNewRasterLayer(RESULT, this.getName(), IRasterLayer.RASTER_DATA_TYPE_DOUBLE, |
54 |
m_Window.getLayerGridExtent()); |
55 |
|
56 |
iNX = m_Window.getNX(); |
57 |
iNY = m_Window.getNY(); |
58 |
|
59 |
m_dValues = new double[9]; |
60 |
m_dCoeffs = new double[9]; |
61 |
m_dNoData = m_Window.getNoDataValue(); |
62 |
|
63 |
result.setNoDataValue(m_dNoData); |
64 |
|
65 |
int i, j;
|
66 |
int iCell = 0; |
67 |
|
68 |
for (i = 0; i < 3; i++) { |
69 |
for (j = 0; j < 3; j++) { |
70 |
try {
|
71 |
m_dCoeffs[iCell] = Double.parseDouble(kernel.getValueAt(i, j).toString());
|
72 |
} |
73 |
catch (final NumberFormatException e) { |
74 |
m_dCoeffs[iCell] = 1;
|
75 |
} |
76 |
iCell++; |
77 |
} |
78 |
} |
79 |
|
80 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
81 |
for (x = 0; x < iNX; x++) { |
82 |
setNeighborhoodValues(x, y); |
83 |
result.setCellValue(x, y, processValues()); |
84 |
} |
85 |
setProgress(y, iNY); |
86 |
} |
87 |
|
88 |
return !m_Task.isCanceled();
|
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
private void setNeighborhoodValues(final int iX, |
94 |
final int iY) { |
95 |
|
96 |
int x, y;
|
97 |
int iCell = 0; |
98 |
|
99 |
for (y = -1; y < 2; y++) { |
100 |
for (x = -1; x < 2; x++) { |
101 |
m_dValues[iCell] = m_Window.getCellValueAsDouble(iX + x, iY + y); |
102 |
iCell++; |
103 |
} |
104 |
} |
105 |
|
106 |
} |
107 |
|
108 |
|
109 |
protected double processValues() { |
110 |
|
111 |
int i = 0; |
112 |
double dResult = 0; |
113 |
|
114 |
for (i = 0; i < m_dValues.length; i++) { |
115 |
if (m_dValues[i] != m_dNoData) {
|
116 |
dResult += (m_dCoeffs[i] * m_dValues[i]); |
117 |
} |
118 |
else {
|
119 |
return m_dNoData;
|
120 |
} |
121 |
} |
122 |
|
123 |
return dResult;
|
124 |
|
125 |
} |
126 |
|
127 |
|
128 |
} |