Statistics
| Revision:

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

History | View | Annotate | Download (3.9 KB)

1
package es.unex.sextante.hydrology.upslopeAreaFromArea;
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
import es.unex.sextante.rasterWrappers.GridWrapper;
10

    
11
public class UpslopeAreaFromAreaAlgorithm
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  = 0.0;
18
   private static final double VISITED      = 1.0;
19

    
20
   public static final String  DEM          = "DEM";
21
   public static final String  INITZONES    = "INITZONES";
22
   public static final String  RESULT       = "RESULT";
23

    
24
   private int                 m_iNX, m_iNY;
25

    
26
   private IRasterLayer        m_DEM        = null;
27
   private IRasterLayer        m_InitZone   = null;
28
   private IRasterLayer        m_Result;
29

    
30

    
31
   @Override
32
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
33

    
34
      m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
35
      m_InitZone = m_Parameters.getParameterValueAsRasterLayer(INITZONES);
36

    
37

    
38
      m_DEM.setFullExtent();
39
      final AnalysisExtent extent = m_DEM.getWindowGridExtent();
40
      m_InitZone.setWindowExtent(extent);
41
      m_InitZone.setInterpolationMethod(GridWrapper.INTERPOLATION_NearestNeighbour);
42

    
43
      m_Result = getNewRasterLayer(RESULT, Sextante.getText("Upslope_area"), IRasterLayer.RASTER_DATA_TYPE_BYTE, extent);
44
      m_Result.assign(NOT_VISITED);
45

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

    
49
      calculateUpslopeArea();
50

    
51
      m_Result.setNoDataValue(NOT_VISITED);
52

    
53
      return !m_Task.isCanceled();
54

    
55
   }
56

    
57

    
58
   @Override
59
   public void defineCharacteristics() {
60

    
61

    
62
      setName(Sextante.getText("Upslope_area_from_outlet_zone"));
63
      setGroup(Sextante.getText("Basic_hydrological_analysis"));
64
      setUserCanDefineAnalysisExtent(false);
65
      setIsDeterminatedProcess(false);
66

    
67
      try {
68
         m_Parameters.addInputRasterLayer(DEM,
69

    
70
         Sextante.getText("Elevation"), true);
71
         m_Parameters.addInputRasterLayer(INITZONES, Sextante.getText("Outlet_zone"), true);
72
         addOutputRasterLayer(RESULT, Sextante.getText("Upslope_area"));
73
      }
74
      catch (final RepeatedParameterNameException e) {
75
         Sextante.addErrorToLog(e);
76
      }
77

    
78
   }
79

    
80

    
81
   private void calculateUpslopeArea() {
82

    
83
      int x, y;
84
      double dValue;
85

    
86
      for (y = 0; y < m_iNY; y++) {
87
         for (x = 0; x < m_iNX; x++) {
88
            dValue = m_InitZone.getCellValueAsDouble(x, y);
89
            if (!m_InitZone.isNoDataValue(dValue)) {
90
               calculateUpslopeAreaFromPoint(x, y);
91
            }
92
         }
93

    
94
      }
95

    
96
   }
97

    
98

    
99
   private void calculateUpslopeAreaFromPoint(final int x,
100
                                              final int y) {
101

    
102
      int i, ix, iy;
103
      double dValue;
104
      int iDirection;
105

    
106
      if ((m_Result.getCellValueAsDouble(x, y) != NOT_VISITED) || m_Task.isCanceled()) {
107
         return;
108
      }
109

    
110
      dValue = m_DEM.getCellValueAsDouble(x, y);
111
      if (m_DEM.isNoDataValue(dValue)) {
112
         return;
113
      }
114

    
115
      m_Result.setCellValue(x, y, VISITED);
116
      for (i = 0; i < 8; i++) {
117
         ix = x + m_iOffsetX[i];
118
         iy = y + m_iOffsetY[i];
119
         dValue = m_DEM.getCellValueAsDouble(ix, iy);
120
         if (!m_DEM.isNoDataValue(dValue)) {
121
            iDirection = m_DEM.getDirToNextDownslopeCell(ix, iy);
122
            if (iDirection >= 0) {
123
               if ((i + 4) % 8 == iDirection) {
124
                  calculateUpslopeAreaFromPoint(ix, iy);
125
               }
126
            }
127
         }
128
      }
129

    
130
   }
131

    
132
}