root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / hydrology / meanValueUphill / MeanValueUphillAlgorithm.java @ 59
History | View | Annotate | Download (4.87 KB)
1 | 59 | nbrodin | package es.unex.sextante.hydrology.meanValueUphill; |
---|---|---|---|
2 | |||
3 | import es.unex.sextante.core.GeoAlgorithm; |
||
4 | import es.unex.sextante.core.AnalysisExtent; |
||
5 | import es.unex.sextante.core.Sextante; |
||
6 | import es.unex.sextante.dataObjects.IRasterLayer; |
||
7 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
8 | import es.unex.sextante.exceptions.RepeatedParameterNameException; |
||
9 | |||
10 | public class MeanValueUphillAlgorithm |
||
11 | extends
|
||
12 | GeoAlgorithm { |
||
13 | |||
14 | private final static int m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 }; |
||
15 | private final static int m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 }; |
||
16 | private static final double NOT_VISITED = -1.0; |
||
17 | |||
18 | public static final String DEM = "DEM"; |
||
19 | public static final String PARAM = "PARAM"; |
||
20 | public static final String RESULT = "RESULT"; |
||
21 | |||
22 | private int m_iNX, m_iNY; |
||
23 | private IRasterLayer m_DEM = null; |
||
24 | private IRasterLayer m_Param = null; |
||
25 | private IRasterLayer m_AccFlow;
|
||
26 | private IRasterLayer m_MeanValue;
|
||
27 | |||
28 | |||
29 | @Override
|
||
30 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
31 | |||
32 | int x, y;
|
||
33 | int iCells;
|
||
34 | double dValue;
|
||
35 | |||
36 | m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM); |
||
37 | m_Param = m_Parameters.getParameterValueAsRasterLayer(PARAM); |
||
38 | |||
39 | m_MeanValue = getNewRasterLayer(RESULT, Sextante.getText("Mean_value_uphill"), IRasterLayer.RASTER_DATA_TYPE_FLOAT);
|
||
40 | m_AccFlow = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_INT, m_MeanValue.getWindowGridExtent()); |
||
41 | |||
42 | m_MeanValue.assign(NOT_VISITED); |
||
43 | m_AccFlow.assign(NOT_VISITED); |
||
44 | |||
45 | final AnalysisExtent extent = m_MeanValue.getWindowGridExtent();
|
||
46 | |||
47 | m_DEM.setWindowExtent(extent); |
||
48 | m_Param.setWindowExtent(extent); |
||
49 | |||
50 | m_iNX = m_DEM.getNX(); |
||
51 | m_iNY = m_DEM.getNY(); |
||
52 | |||
53 | calculateAccFlows(); |
||
54 | |||
55 | m_MeanValue.setNoDataValue(NOT_VISITED); |
||
56 | |||
57 | for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) { |
||
58 | for (x = 0; x < m_iNX; x++) { |
||
59 | dValue = m_MeanValue.getCellValueAsDouble(x, y); |
||
60 | iCells = m_AccFlow.getCellValueAsInt(x, y); |
||
61 | if (!m_MeanValue.isNoDataValue(dValue) && (iCells != 0)) { |
||
62 | m_MeanValue.setCellValue(x, y, dValue / iCells); |
||
63 | } |
||
64 | else {
|
||
65 | m_MeanValue.setNoData(x, y); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | |||
70 | |||
71 | return !m_Task.isCanceled();
|
||
72 | } |
||
73 | |||
74 | |||
75 | @Override
|
||
76 | public void defineCharacteristics() { |
||
77 | |||
78 | setName(Sextante.getText("Mean_value_uphill"));
|
||
79 | setGroup(Sextante.getText("Indices_and_other_hydrological_parameters"));
|
||
80 | setUserCanDefineAnalysisExtent(true);
|
||
81 | |||
82 | try {
|
||
83 | m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true); |
||
84 | m_Parameters.addInputRasterLayer(PARAM, Sextante.getText("Parameter"), true); |
||
85 | addOutputRasterLayer(RESULT, Sextante.getText("Mean_value_uphill"));
|
||
86 | } |
||
87 | catch (final RepeatedParameterNameException e) { |
||
88 | Sextante.addErrorToLog(e); |
||
89 | } |
||
90 | |||
91 | } |
||
92 | |||
93 | |||
94 | private void calculateAccFlows() { |
||
95 | |||
96 | int x, y;
|
||
97 | |||
98 | for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) { |
||
99 | for (x = 0; x < m_iNX; x++) { |
||
100 | getFlow(x, y); |
||
101 | } |
||
102 | |||
103 | } |
||
104 | |||
105 | } |
||
106 | |||
107 | |||
108 | private void getFlow(final int x, |
||
109 | final int y) { |
||
110 | |||
111 | int i, ix, iy;
|
||
112 | int iDirection;
|
||
113 | double dValue;
|
||
114 | double dParamValue;
|
||
115 | |||
116 | if (m_AccFlow.getCellValueAsDouble(x, y) != NOT_VISITED) {
|
||
117 | return;
|
||
118 | } |
||
119 | |||
120 | dParamValue = m_Param.getCellValueAsDouble(x, y); |
||
121 | dValue = m_DEM.getCellValueAsDouble(x, y); |
||
122 | if (!m_DEM.isNoDataValue(dValue) /*&& !m_Param.isNoDataValue(dParamValue)*/) { |
||
123 | if (!m_Param.isNoDataValue(dParamValue)) {
|
||
124 | m_AccFlow.setCellValue(x, y, 1);
|
||
125 | m_MeanValue.setCellValue(x, y, dParamValue); |
||
126 | } |
||
127 | else {
|
||
128 | m_AccFlow.setCellValue(x, y, 0);
|
||
129 | m_MeanValue.setCellValue(x, y, 0);
|
||
130 | } |
||
131 | for (i = 0; i < 8; i++) { |
||
132 | ix = x + m_iOffsetX[i]; |
||
133 | iy = y + m_iOffsetY[i]; |
||
134 | dValue = m_DEM.getCellValueAsDouble(ix, iy); |
||
135 | if (!m_DEM.isNoDataValue(dValue)) {
|
||
136 | iDirection = m_DEM.getDirToNextDownslopeCell(ix, iy); |
||
137 | if (iDirection >= 0) { |
||
138 | if ((i + 4) % 8 == iDirection) { |
||
139 | getFlow(ix, iy); |
||
140 | //dParamValue = m_Param.getCellValueAsDouble(ix, iy);
|
||
141 | //if (!m_Param.isNoDataValue(dParamValue)){
|
||
142 | m_AccFlow.addToCellValue(x, y, m_AccFlow.getCellValueAsDouble(ix, iy)); |
||
143 | m_MeanValue.addToCellValue(x, y, m_MeanValue.getCellValueAsDouble(ix, iy)); |
||
144 | //}
|
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | |||
152 | } |
||
153 | |||
154 | } |