Statistics
| Revision:

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

History | View | Annotate | Download (6.09 KB)

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

    
3
import java.awt.geom.Point2D;
4

    
5
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
6
import es.unex.sextante.core.GeoAlgorithm;
7
import es.unex.sextante.core.AnalysisExtent;
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
public class IsocronesAlgorithm
15
         extends
16
            GeoAlgorithm {
17

    
18
   private final static int   m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
19
   private final static int   m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
20

    
21
   public static final String DEM          = "DEM";
22
   public static final String NETWORK      = "NETWORK";
23
   public static final String OUTLET       = "OUTLET";
24
   public static final String TIME         = "TIME";
25
   public static final String RATIO        = "RATIO";
26

    
27
   private int                m_iNX, m_iNY;
28
   private double             m_dSpeed;
29
   private double             m_dRatio;
30
   private IRasterLayer       m_DEM        = null;
31
   private IRasterLayer       m_Network    = null;
32
   private IRasterLayer       m_TimeOut;
33
   private GridCell           m_Outlet;
34

    
35

    
36
   @Override
37
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
38

    
39
      m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
40
      m_Network = m_Parameters.getParameterValueAsRasterLayer(NETWORK);
41
      final Point2D pt = m_Parameters.getParameterValueAsPoint(OUTLET);
42

    
43
      final AnalysisExtent gridExtent = new AnalysisExtent(m_DEM);
44
      m_DEM.setWindowExtent(gridExtent);
45
      m_TimeOut = getNewRasterLayer(TIME, Sextante.getText("Time_to_outlet__h"), IRasterLayer.RASTER_DATA_TYPE_FLOAT, gridExtent);
46

    
47
      m_TimeOut.assign(0.0);
48

    
49
      m_Outlet = gridExtent.getGridCoordsFromWorldCoords(pt);
50

    
51
      if (m_Network != null) {
52
         m_Network.setWindowExtent(gridExtent);
53
      }
54

    
55
      m_iNX = m_DEM.getNX();
56
      m_iNY = m_DEM.getNY();
57

    
58
      m_dSpeed = 1.0;
59
      m_dRatio = 1.0;
60
      calculateTimeOfConcentration();
61

    
62
      m_dRatio = m_Parameters.getParameterValueAsDouble(RATIO);
63
      calculateTimeOut();
64

    
65
      m_TimeOut.setNoDataValue(0.0);
66

    
67
      return !m_Task.isCanceled();
68

    
69
   }
70

    
71

    
72
   @Override
73
   public void defineCharacteristics() {
74

    
75
      setName(Sextante.getText("Time_to_outlet"));
76
      setGroup(Sextante.getText("Basic_hydrological_analysis"));
77
      setUserCanDefineAnalysisExtent(false);
78
      setIsDeterminatedProcess(false);
79

    
80
      try {
81
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
82
         m_Parameters.addInputRasterLayer(NETWORK, Sextante.getText("Channel_network"), false);
83
         m_Parameters.addNumericalValue(RATIO, Sextante.getText("speed_ratio__channel_-_overland"), 10,
84
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
85
         m_Parameters.addPoint(OUTLET, Sextante.getText("Outlet_point"));
86
         addOutputRasterLayer(TIME, Sextante.getText("Time_to_outlet"));
87
      }
88
      catch (final RepeatedParameterNameException e) {
89
         Sextante.addErrorToLog(e);
90
      }
91

    
92
   }
93

    
94

    
95
   private void calculateTimeOfConcentration() {
96

    
97
      int x, y;
98
      double dValue;
99
      final GridCell highestCell = new GridCell(0, 0, Double.NEGATIVE_INFINITY);
100

    
101
      writeTimeOut(m_Outlet.getX(), m_Outlet.getY(), m_Outlet.getX(), m_Outlet.getY());
102

    
103
      for (y = 0; y < m_iNY; y++) {
104
         for (x = 0; x < m_iNX; x++) {
105
            dValue = m_TimeOut.getCellValueAsDouble(x, y);
106
            if (!m_TimeOut.isNoDataValue(dValue)) {
107
               if (dValue > highestCell.getValue()) {
108
                  highestCell.setX(x);
109
                  highestCell.setY(y);
110
                  highestCell.setValue(dValue);
111
               }
112
            }
113
         }
114
      }
115

    
116
      final double dH1 = m_DEM.getCellValueAsDouble(m_Outlet.getX(), m_Outlet.getY());
117
      final double dH2 = m_DEM.getCellValueAsDouble(highestCell.getX(), highestCell.getY());
118
      final double dConcTime = Math.pow(0.87 * Math.pow(highestCell.getValue() / 1000., 3) / (dH2 - dH1), 0.385);
119
      m_dSpeed = highestCell.getValue() / dConcTime;
120

    
121
   }
122

    
123

    
124
   private void calculateTimeOut() {
125

    
126
      m_TimeOut.assign(0.0);
127

    
128
      writeTimeOut(m_Outlet.getX(), m_Outlet.getY(), m_Outlet.getX(), m_Outlet.getY());
129

    
130
   }
131

    
132

    
133
   private void writeTimeOut(final int iX1,
134
                             final int iY1,
135
                             final int iX2,
136
                             final int iY2) {
137

    
138
      int i;
139
      int ix, iy;
140
      int iDirection;
141
      double dDist = 1;
142
      double dTime;
143
      double dValue;
144

    
145

    
146
      if (m_Task.isCanceled()) {
147
         return;
148
      }
149

    
150
      dValue = m_DEM.getCellValueAsDouble(iX1, iY1);
151

    
152
      if (!m_DEM.isNoDataValue(dValue)) {
153
         if ((iX1 == iX2) && (iY1 == iY2)) {
154
            dDist = 0;
155
         }
156
         else if (Math.abs(iX1 - iX2 + iY1 - iY2) == 1) {
157
            dDist = m_DEM.getDistToNeighborInDir(0);
158
         }
159
         else {
160
            dDist = m_DEM.getDistToNeighborInDir(1);
161
         }
162
         dTime = dDist / m_dSpeed;
163

    
164
         if (m_Network != null) {
165
            dValue = m_Network.getCellValueAsDouble(iX1, iY1);
166
            if (m_Network.isNoDataValue(dValue) || (dValue == 0)) {
167
               dTime *= m_dRatio;
168
            }
169
         }
170

    
171
         dTime += m_TimeOut.getCellValueAsDouble(iX2, iY2);
172
         m_TimeOut.setCellValue(iX1, iY1, dTime);
173

    
174
         for (i = 0; i < 8; i++) {
175
            ix = iX1 + m_iOffsetX[i];
176
            iy = iY1 + m_iOffsetY[i];
177
            dValue = m_DEM.getCellValueAsDouble(ix, iy);
178
            if (!m_DEM.isNoDataValue(dValue)) {
179
               iDirection = m_DEM.getDirToNextDownslopeCell(ix, iy);
180
               if (iDirection >= 0) {
181
                  if ((i + 4) % 8 == iDirection) {
182
                     writeTimeOut(ix, iy, iX1, iY1);
183
                  }
184
               }
185
            }
186
         }
187

    
188
      }
189

    
190
   }
191
}