root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridTools / closeGapsNN / CloseGapsNNAlgorithm.java @ 59
History | View | Annotate | Download (4.28 KB)
1 | 59 | nbrodin | |
---|---|---|---|
2 | |||
3 | package es.unex.sextante.gridTools.closeGapsNN; |
||
4 | |||
5 | import java.util.ArrayList; |
||
6 | |||
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.rasterWrappers.GridCell; |
||
13 | |||
14 | |||
15 | public class CloseGapsNNAlgorithm |
||
16 | extends
|
||
17 | GeoAlgorithm { |
||
18 | |||
19 | private static final int NO_DATA = -1; |
||
20 | |||
21 | public static final String LAYER = "LAYER"; |
||
22 | public static final String RESULT = "RESULT"; |
||
23 | |||
24 | int m_iNX, m_iNY;
|
||
25 | IRasterLayer m_Window; |
||
26 | IRasterLayer m_Result; |
||
27 | IRasterLayer m_Filled; |
||
28 | ArrayList m_CentralPoints, m_AdjPoints;
|
||
29 | |||
30 | |||
31 | @Override
|
||
32 | public void defineCharacteristics() { |
||
33 | |||
34 | setName(Sextante.getText("Void_filling_[nearest_neighbour]"));
|
||
35 | setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
|
||
36 | setUserCanDefineAnalysisExtent(false);
|
||
37 | setIsDeterminatedProcess(false);
|
||
38 | |||
39 | try {
|
||
40 | m_Parameters.addInputRasterLayer(LAYER, Sextante.getText("Layer"), true); |
||
41 | addOutputRasterLayer(RESULT, Sextante.getText("Filled_layer"));
|
||
42 | } |
||
43 | catch (final RepeatedParameterNameException e) { |
||
44 | Sextante.addErrorToLog(e); |
||
45 | } |
||
46 | |||
47 | } |
||
48 | |||
49 | |||
50 | @Override
|
||
51 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
52 | |||
53 | int x, y;
|
||
54 | double dValue;
|
||
55 | |||
56 | m_CentralPoints = new ArrayList(); |
||
57 | m_AdjPoints = new ArrayList(); |
||
58 | |||
59 | m_Window = m_Parameters.getParameterValueAsRasterLayer(LAYER); |
||
60 | m_Window.setFullExtent(); |
||
61 | m_Result = getNewRasterLayer(RESULT, m_Window.getName() + Sextante.getText("[filled]"), m_Window.getDataType(),
|
||
62 | m_Window.getWindowGridExtent()); |
||
63 | m_Filled = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_INT, m_Window.getWindowGridExtent()); |
||
64 | |||
65 | |||
66 | m_iNX = m_Window.getNX(); |
||
67 | m_iNY = m_Window.getNY(); |
||
68 | |||
69 | m_Filled.setNoDataValue(NO_DATA); |
||
70 | m_Filled.assignNoData(); |
||
71 | |||
72 | m_Result.setNoDataValue(NO_DATA); |
||
73 | m_Result.assignNoData(); |
||
74 | |||
75 | for (y = 0; y < m_iNY; y++) { |
||
76 | for (x = 0; x < m_iNX; x++) { |
||
77 | dValue = m_Window.getCellValueAsDouble(x, y); |
||
78 | if (!m_Window.isNoDataValue(dValue)) {
|
||
79 | m_CentralPoints.add(new GridCell(x, y, dValue));
|
||
80 | m_Filled.setCellValue(x, y, 0.0);
|
||
81 | m_Result.setCellValue(x, y, dValue); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | |||
86 | closeGaps(); |
||
87 | |||
88 | return !m_Task.isCanceled();
|
||
89 | |||
90 | } |
||
91 | |||
92 | |||
93 | private void closeGaps() { |
||
94 | |||
95 | int i, j;
|
||
96 | int iPt;
|
||
97 | int x, y, x2, y2;
|
||
98 | double dValue;
|
||
99 | double dAccCost;
|
||
100 | double dPrevAccCost;
|
||
101 | GridCell cell; |
||
102 | |||
103 | final double dDist[][] = new double[3][3]; |
||
104 | |||
105 | for (i = -1; i < 2; i++) { |
||
106 | for (j = -1; j < 2; j++) { |
||
107 | dDist[i + 1][j + 1] = Math.sqrt(i * i + j * j); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | while ((m_CentralPoints.size() != 0) && !m_Task.isCanceled()) { |
||
112 | for (iPt = 0; iPt < m_CentralPoints.size(); iPt++) { |
||
113 | cell = (GridCell) m_CentralPoints.get(iPt); |
||
114 | x = cell.getX(); |
||
115 | y = cell.getY(); |
||
116 | dValue = cell.getValue(); |
||
117 | for (i = -1; i < 2; i++) { |
||
118 | for (j = -1; j < 2; j++) { |
||
119 | x2 = x + i; |
||
120 | y2 = y + j; |
||
121 | if (m_Window.isInWindow(x2, y2)) {
|
||
122 | dAccCost = m_Filled.getCellValueAsDouble(x, y); |
||
123 | dAccCost += dDist[i + 1][j + 1]; |
||
124 | dPrevAccCost = m_Filled.getCellValueAsDouble(x2, y2); |
||
125 | if (m_Filled.isNoDataValue(dPrevAccCost) || (dPrevAccCost > dAccCost)) {
|
||
126 | m_Filled.setCellValue(x2, y2, dAccCost); |
||
127 | m_Result.setCellValue(x2, y2, dValue); |
||
128 | m_AdjPoints.add(new GridCell(x2, y2, dValue));
|
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | m_CentralPoints = m_AdjPoints; |
||
136 | m_AdjPoints = new ArrayList(); |
||
137 | |||
138 | if (m_Task.isCanceled()) {
|
||
139 | return;
|
||
140 | } |
||
141 | |||
142 | } |
||
143 | |||
144 | } |
||
145 | |||
146 | } |