Statistics
| Revision:

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

History | View | Annotate | Download (4.78 KB)

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

    
3
import java.util.ArrayList;
4

    
5
import es.unex.sextante.core.GeoAlgorithm;
6
import es.unex.sextante.core.AnalysisExtent;
7
import es.unex.sextante.core.Sextante;
8
import es.unex.sextante.dataObjects.IRasterLayer;
9
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
10
import es.unex.sextante.exceptions.RepeatedParameterNameException;
11
import es.unex.sextante.rasterWrappers.GridCell;
12
import es.unex.sextante.rasterWrappers.GridWrapper;
13

    
14
public class DistToChannelNetworkAlgorithm
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 DIST         = "DIST";
24

    
25
   private int                m_iNX, m_iNY;
26

    
27
   private IRasterLayer       m_DEM        = null;
28
   private IRasterLayer       m_Network    = null;
29
   private IRasterLayer       m_Dist;
30
   private IRasterLayer       m_Directions;
31
   private ArrayList          m_AdjPoints;
32
   private ArrayList          m_CentralPoints;
33

    
34

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

    
38
      m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
39
      m_Network = m_Parameters.getParameterValueAsRasterLayer(NETWORK);
40

    
41
      m_Dist = getNewRasterLayer(DIST, Sextante.getText("Distance_to_channel_network"), IRasterLayer.RASTER_DATA_TYPE_DOUBLE);
42

    
43
      m_Dist.setNoDataValue(-1.0);
44
      m_Dist.assignNoData();
45

    
46
      final AnalysisExtent extent = m_Dist.getWindowGridExtent();
47
      m_DEM.setWindowExtent(extent);
48
      m_Network.setWindowExtent(extent);
49
      m_Network.setInterpolationMethod(GridWrapper.INTERPOLATION_NearestNeighbour);
50

    
51
      m_Directions = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_INT, extent);
52

    
53
      m_iNX = m_DEM.getNX();
54
      m_iNY = m_DEM.getNY();
55

    
56
      prepareInitData();
57

    
58
      calculateDistance();
59

    
60
      return !m_Task.isCanceled();
61
   }
62

    
63

    
64
   @Override
65
   public void defineCharacteristics() {
66

    
67
      setName(Sextante.getText("Distance_to_channel_network"));
68
      setGroup(Sextante.getText("Indices_and_other_hydrological_parameters"));
69
      setUserCanDefineAnalysisExtent(true);
70
      setIsDeterminatedProcess(false);
71

    
72
      try {
73
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
74
         m_Parameters.addInputRasterLayer(NETWORK, Sextante.getText("Channel_network"), true);
75
         addOutputRasterLayer(DIST, Sextante.getText("Distance_to_channel_network"));
76
      }
77
      catch (final RepeatedParameterNameException e) {
78
         Sextante.addErrorToLog(e);
79
      }
80

    
81
   }
82

    
83

    
84
   private void prepareInitData() {
85

    
86
      int x, y;
87
      int iDir;
88
      int iChannel;
89

    
90
      m_AdjPoints = new ArrayList();
91
      m_CentralPoints = new ArrayList();
92

    
93
      for (y = 0; y < m_iNY; y++) {
94
         for (x = 0; x < m_iNX; x++) {
95
            iDir = m_DEM.getDirToNextDownslopeCell(x, y, false);
96
            iChannel = m_Network.getCellValueAsInt(x, y);
97
            if (iDir < 0) {
98
               m_Directions.setCellValue(x, y, -1.0);
99
            }
100
            else {
101
               m_Directions.setCellValue(x, y, ((iDir + 4) % 8));
102
            }
103
            if ((iChannel != 0) && !m_Network.isNoDataValue(iChannel)) {
104
               m_Dist.setCellValue(x, y, 0.0);
105
               m_CentralPoints.add(new GridCell(x, y, 0));
106
            }
107
         }
108
      }
109

    
110
   }
111

    
112

    
113
   private void calculateDistance() {
114

    
115
      int i;
116
      int iPt;
117
      int x, y, x2, y2;
118
      double dDist, dDist2, dAccDist;
119
      GridCell cell;
120

    
121
      while (m_CentralPoints.size() != 0) {
122
         for (iPt = 0; iPt < m_CentralPoints.size(); iPt++) {
123
            cell = (GridCell) m_CentralPoints.get(iPt);
124
            x = cell.getX();
125
            y = cell.getY();
126
            dDist = m_Dist.getCellValueAsDouble(x, y);
127
            for (i = 0; i < 8; i++) {
128
               x2 = x + m_iOffsetX[i];
129
               y2 = y + m_iOffsetY[i];
130
               if (m_Directions.getCellValueAsInt(x2, y2) == i) {
131
                  dAccDist = dDist + m_DEM.getDistToNeighborInDir(i);
132
                  dDist2 = m_Dist.getCellValueAsDouble(x2, y2);
133
                  if (m_Dist.isNoDataValue(dDist2) || (dDist2 > dAccDist)) {
134
                     m_Dist.setCellValue(x2, y2, dAccDist);
135
                     m_AdjPoints.add(new GridCell(x2, y2, 0));
136
                  }
137
               }
138
            }
139
         }
140

    
141
         m_CentralPoints = m_AdjPoints;
142
         m_AdjPoints = new ArrayList();
143

    
144
         if (m_Task.isCanceled()) {
145
            return;
146
         }
147

    
148
      }
149

    
150
   }
151

    
152
}