Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / hydrology / maxValueUphill / MaxValueUphillAlgorithm.java @ 59

History | View | Annotate | Download (4.26 KB)

1
package es.unex.sextante.hydrology.maxValueUphill;
2

    
3

    
4
import es.unex.sextante.core.GeoAlgorithm;
5
import es.unex.sextante.core.AnalysisExtent;
6
import es.unex.sextante.core.Sextante;
7
import es.unex.sextante.dataObjects.IRasterLayer;
8
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
9
import es.unex.sextante.exceptions.RepeatedParameterNameException;
10

    
11
public class MaxValueUphillAlgorithm
12
         extends
13
            GeoAlgorithm {
14

    
15
   private final static int    m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
16
   private final static int    m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
17
   private static final double NOT_VISITED  = -1.0;
18

    
19
   public static final String  DEM          = "DEM";
20
   public static final String  PARAM        = "PARAM";
21
   public static final String  RESULT       = "RESULT";
22

    
23
   private int                 m_iNX, m_iNY;
24
   private IRasterLayer        m_DEM        = null;
25
   private IRasterLayer        m_Param      = null;
26
   private IRasterLayer        m_MaxValue;
27

    
28

    
29
   @Override
30
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
31

    
32
      m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
33
      m_Param = m_Parameters.getParameterValueAsRasterLayer(PARAM);
34

    
35
      m_MaxValue = getNewRasterLayer(RESULT, Sextante.getText("Max_value_uphill"), IRasterLayer.RASTER_DATA_TYPE_FLOAT);
36

    
37

    
38
      m_MaxValue.assign(NOT_VISITED);
39

    
40
      final AnalysisExtent extent = m_MaxValue.getWindowGridExtent();
41

    
42
      m_DEM.setWindowExtent(extent);
43
      m_Param.setWindowExtent(extent);
44

    
45
      m_MaxValue.setNoDataValue(m_Param.getNoDataValue());
46

    
47
      m_iNX = m_DEM.getNX();
48
      m_iNY = m_DEM.getNY();
49

    
50
      calculateMaxValues();
51

    
52
      return !m_Task.isCanceled();
53

    
54
   }
55

    
56

    
57
   @Override
58
   public void defineCharacteristics() {
59

    
60
      setName(Sextante.getText("Max_value_uphill"));
61
      setGroup(Sextante.getText("Indices_and_other_hydrological_parameters"));
62
      setUserCanDefineAnalysisExtent(true);
63

    
64
      try {
65
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
66
         m_Parameters.addInputRasterLayer(PARAM, Sextante.getText("Parameter"), true);
67
         addOutputRasterLayer(RESULT, Sextante.getText("Max_value_uphill"));
68
      }
69
      catch (final RepeatedParameterNameException e) {
70
         Sextante.addErrorToLog(e);
71
      }
72

    
73
   }
74

    
75

    
76
   private void calculateMaxValues() {
77

    
78
      int x, y;
79

    
80
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
81
         for (x = 0; x < m_iNX; x++) {
82
            getMaxValue(x, y);
83
         }
84

    
85
      }
86

    
87
   }
88

    
89

    
90
   private void getMaxValue(final int x,
91
                            final int y) {
92

    
93
      int i, ix, iy;
94
      int iDirection;
95
      double dValue;
96
      double dParamValue;
97
      double dMaxValue;
98

    
99

    
100
      if (m_Task.isCanceled()) {
101
         return;
102
      }
103

    
104

    
105
      if (m_MaxValue.getCellValueAsDouble(x, y) != NOT_VISITED) {
106
         return;
107
      }
108

    
109
      dValue = m_DEM.getCellValueAsDouble(x, y);
110
      if (!m_DEM.isNoDataValue(dValue)) {
111
         dParamValue = m_Param.getCellValueAsDouble(x, y);
112
         if (!m_Param.isNoDataValue(dParamValue)) {
113
            m_MaxValue.setCellValue(x, y, dParamValue);
114
         }
115
         for (i = 0; i < 8; i++) {
116
            ix = x + m_iOffsetX[i];
117
            iy = y + m_iOffsetY[i];
118
            dValue = m_DEM.getCellValueAsDouble(ix, iy);
119
            if (!m_DEM.isNoDataValue(dValue)) {
120
               iDirection = m_DEM.getDirToNextDownslopeCell(ix, iy);
121
               if (iDirection >= 0) {
122
                  if ((i + 4) % 8 == iDirection) {
123
                     getMaxValue(ix, iy);
124
                     dMaxValue = m_MaxValue.getCellValueAsDouble(x, y);
125
                     if (m_MaxValue.isNoDataValue(dMaxValue)) {
126
                        m_MaxValue.setCellValue(x, y, m_MaxValue.getCellValueAsDouble(ix, iy));
127
                     }
128
                     else {
129
                        m_MaxValue.setCellValue(x, y, Math.max(m_MaxValue.getCellValueAsDouble(x, y),
130
                                 m_MaxValue.getCellValueAsDouble(ix, iy)));
131
                     }
132
                  }
133
               }
134
            }
135
         }
136
      }
137
      else {
138
         m_MaxValue.setNoData(x, y);
139
      }
140

    
141

    
142
   }
143

    
144
}