Revision 447

View differences:

org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/MANIFEST.MF
1
Manifest-Version: 1.0
2
Ant-Version: Apache Ant 1.7.1
3
Created-By: 20.1-b02 (Sun Microsystems Inc.)
4
Implementation-Version: 0.7
5
Built-Date: 2013-02-23 00:34:19
6

  
0 7

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/hydrology/isocrones/IsocronesAlgorithm.java
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
}
0 192

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/hydrology/burnStreams/BurnStreamsAlgorithm.java
1

  
2

  
3
package es.unex.sextante.hydrology.burnStreams;
4

  
5
import com.vividsolutions.jts.geom.Coordinate;
6
import com.vividsolutions.jts.geom.Geometry;
7

  
8
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
9
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
10
import es.unex.sextante.core.AnalysisExtent;
11
import es.unex.sextante.core.GeoAlgorithm;
12
import es.unex.sextante.core.Sextante;
13
import es.unex.sextante.dataObjects.IFeatureIterator;
14
import es.unex.sextante.dataObjects.IRasterLayer;
15
import es.unex.sextante.dataObjects.IVectorLayer;
16
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
17
import es.unex.sextante.exceptions.RepeatedParameterNameException;
18
import es.unex.sextante.rasterWrappers.GridCell;
19

  
20

  
21
public class BurnStreamsAlgorithm
22
         extends
23
            GeoAlgorithm {
24

  
25
   public static final String  STREAMS          = "STREAMS";
26
   public static final String  DEM              = "DEM";
27
   public static final String  RESULT           = "RESULT";
28
   public static final String  DEPTH            = "DEPTH";
29

  
30
   private static final double SMALL_DIFFERENCE = 0.05;
31

  
32
   private IRasterLayer        m_DEM;
33
   private IRasterLayer        m_Result;
34
   private double              m_dLastZ;
35
   private double              m_dDepth;
36
   private boolean             m_bFoundValidCell;
37

  
38

  
39
   @Override
40
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
41

  
42
      m_dDepth = m_Parameters.getParameterValueAsDouble(DEPTH);
43

  
44
      final IVectorLayer lines = m_Parameters.getParameterValueAsVectorLayer(STREAMS);
45

  
46
      m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
47
      m_DEM.setFullExtent();
48
      final AnalysisExtent extent = m_DEM.getWindowGridExtent();
49

  
50
      m_Result = getNewRasterLayer(RESULT, Sextante.getText("Modified_DEM"), IRasterLayer.RASTER_DATA_TYPE_DOUBLE, extent);
51

  
52
      m_Result.assign(m_DEM);
53

  
54
      int iShape = 0;
55
      final int iShapeCount = lines.getShapesCount();
56
      final IFeatureIterator iterator = lines.iterator();
57
      while (iterator.hasNext() && setProgress(iShape, iShapeCount)) {
58
         final Geometry line = iterator.next().getGeometry();
59
         for (int i = 0; i < line.getNumGeometries(); i++) {
60
            processLine(line.getGeometryN(i));
61
         }
62
         iShape++;
63
      }
64
      iterator.close();
65

  
66
      return !m_Task.isCanceled();
67

  
68
   }
69

  
70

  
71
   @Override
72
   public void defineCharacteristics() {
73

  
74
      setName(Sextante.getText("Burn_streams"));
75
      setGroup(Sextante.getText("Basic_hydrological_analysis"));
76
      setUserCanDefineAnalysisExtent(false);
77
      try {
78
         m_Parameters.addInputVectorLayer(STREAMS, Sextante.getText("Channel_network"),
79
                  AdditionalInfoVectorLayer.SHAPE_TYPE_LINE, true);
80
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
81
         m_Parameters.addNumericalValue(DEPTH, Sextante.getText("Depth"), AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE,
82
                  10, 0, Double.MAX_VALUE);
83
         addOutputRasterLayer(RESULT, Sextante.getText("Modified_DEM"));
84
      }
85
      catch (final RepeatedParameterNameException e) {
86
         Sextante.addErrorToLog(e);
87
      }
88

  
89
   }
90

  
91

  
92
   private void processLine(final Geometry line) {
93

  
94
      double x, y, x2, y2;
95
      final Coordinate[] coords = line.getCoordinates();
96

  
97
      m_bFoundValidCell = false;
98
      for (int i = 0; i < coords.length - 1; i++) {
99
         x = coords[i].x;
100
         y = coords[i].y;
101
         x2 = coords[i + 1].x;
102
         y2 = coords[i + 1].y;
103
         processSegment(x, y, x2, y2);
104
      }
105

  
106
   }
107

  
108

  
109
   private void processSegment(double x,
110
                               double y,
111
                               final double x2,
112
                               final double y2) {
113

  
114

  
115
      double dx, dy, d, n;
116

  
117
      dx = Math.abs(x2 - x);
118
      dy = Math.abs(y2 - y);
119

  
120
      if ((dx > 0.0) || (dy > 0.0)) {
121
         if (dx > dy) {
122
            dx /= m_DEM.getWindowCellSize();
123
            n = dx;
124
            dy /= dx;
125
            dx = m_DEM.getWindowCellSize();
126
         }
127
         else {
128
            dy /= m_DEM.getWindowCellSize();
129
            n = dy;
130
            dx /= dy;
131
            dy = m_DEM.getWindowCellSize();
132
         }
133

  
134
         if (x2 < x) {
135
            dx = -dx;
136
         }
137

  
138
         if (y2 < y) {
139
            dy = -dy;
140
         }
141

  
142
         for (d = 0.0; d <= n; d++, x += dx, y += dy) {
143
            addPoint(x, y);
144
         }
145
      }
146

  
147
   }
148

  
149

  
150
   private void addPoint(final double x,
151
                         final double y) {
152

  
153

  
154
      final GridCell cell = m_DEM.getWindowGridExtent().getGridCoordsFromWorldCoords(x, y);
155
      final int iX = cell.getX();
156
      final int iY = cell.getY();
157
      final double z = m_DEM.getCellValueAsDouble(iX, iY) /*- m_dDepth*/;
158
      if (m_DEM.isNoDataValue(z)) {
159
         return;
160
      }
161
      if (!m_bFoundValidCell) {
162
         m_dLastZ = z - m_dDepth;
163
         m_bFoundValidCell = true;
164
      }
165
      //if (z >= m_dLastZ) {
166
      m_dLastZ -= SMALL_DIFFERENCE;
167
      //}
168

  
169
      m_Result.setCellValue(iX, iY, m_dLastZ);
170

  
171
   }
172
}
0 173

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/hydrology/hydroModel/HydroModelAlgorithm.java
1
package es.unex.sextante.hydrology.hydroModel;
2

  
3
import java.awt.geom.Point2D;
4
import java.util.ArrayList;
5

  
6
import com.vividsolutions.jts.geom.Coordinate;
7

  
8
import es.unex.sextante.additionalInfo.AdditionalInfoMultipleInput;
9
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
10
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
11
import es.unex.sextante.core.GeoAlgorithm;
12
import es.unex.sextante.core.AnalysisExtent;
13
import es.unex.sextante.core.Sextante;
14
import es.unex.sextante.dataObjects.IFeature;
15
import es.unex.sextante.dataObjects.IFeatureIterator;
16
import es.unex.sextante.dataObjects.IRasterLayer;
17
import es.unex.sextante.dataObjects.IRecord;
18
import es.unex.sextante.dataObjects.IRecordsetIterator;
19
import es.unex.sextante.dataObjects.ITable;
20
import es.unex.sextante.dataObjects.IVectorLayer;
21
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
22
import es.unex.sextante.exceptions.OptionalParentParameterException;
23
import es.unex.sextante.exceptions.RepeatedParameterNameException;
24
import es.unex.sextante.exceptions.UndefinedParentParameterNameException;
25
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
26
import es.unex.sextante.hydrology.modelling.Hydrograph;
27
import es.unex.sextante.hydrology.modelling.Hyetograph;
28

  
29
public class HydroModelAlgorithm
30
         extends
31
            GeoAlgorithm {
32

  
33
   public static final String TABLES               = "TABLES";
34
   public static final String TIMEOUT              = "TIMEOUT";
35
   public static final String CN                   = "CN";
36
   public static final String INTERVALHYDRO        = "INTERVALHYDRO";
37
   public static final String INTERVALHYETO        = "INTERVALHYETO";
38
   public static final String FIELD                = "FIELD";
39
   public static final String STATIONS             = "STATIONS";
40

  
41
   private int                m_iNX, m_iNY;
42
   private int                m_iIntervalHydro;
43
   private int                m_iIntervalHyeto;
44
   private IRasterLayer       m_TimeOut            = null;
45
   private IRasterLayer       m_CN                 = null;
46
   private ArrayList          m_Hydrographs;
47
   private Hyetograph         m_Hyetogram[][];
48
   private ArrayList          m_Tables;
49
   private int                m_iTableNameField;
50
   private boolean            m_bHyetoNamesCreated = false;
51
   private String[]           m_sHyetoNames;
52

  
53

  
54
   @Override
55
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
56

  
57

  
58
      m_TimeOut = m_Parameters.getParameterValueAsRasterLayer(TIMEOUT);
59
      m_CN = m_Parameters.getParameterValueAsRasterLayer(CN);
60
      m_iIntervalHydro = m_Parameters.getParameterValueAsInt(INTERVALHYDRO) * 60;
61
      m_iIntervalHyeto = m_Parameters.getParameterValueAsInt(INTERVALHYETO) * 60;
62
      m_iTableNameField = m_Parameters.getParameterValueAsInt(FIELD);
63
      m_TimeOut.setFullExtent();
64
      m_CN.setWindowExtent(m_TimeOut.getWindowGridExtent());
65

  
66
      m_iNX = m_TimeOut.getNX();
67
      m_iNY = m_TimeOut.getNY();
68

  
69
      if (createHyetographs()) {
70
         calculateHydrographs();
71
         documentHydrographs();
72
      }
73
      else {
74
         throw new GeoAlgorithmExecutionException(Sextante.getText("Hyetographs_are_not_consistent"));
75
      }
76

  
77
      return !m_Task.isCanceled();
78

  
79
   }
80

  
81

  
82
   @Override
83
   public void defineCharacteristics() {
84

  
85
      setName(Sextante.getText("A_simple_hydrological_model"));
86
      setGroup(Sextante.getText("Indices_and_other_hydrological_parameters"));
87
      setUserCanDefineAnalysisExtent(false);
88

  
89
      try {
90
         m_Parameters.addInputRasterLayer(TIMEOUT, Sextante.getText("Time_to_outlet"), true);
91
         m_Parameters.addInputRasterLayer(CN, Sextante.getText("N\u00famero_de_Curva"), true);
92
         m_Parameters.addInputVectorLayer(STATIONS, Sextante.getText("Estaciones"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT,
93
                  true);
94
         m_Parameters.addTableField(FIELD, Sextante.getText("Nombre_de_tabla_con_hietograma"), STATIONS);
95
         m_Parameters.addMultipleInput(TABLES, Sextante.getText("Tables"), AdditionalInfoMultipleInput.DATA_TYPE_TABLE, true);
96
         m_Parameters.addNumericalValue(INTERVALHYDRO, Sextante.getText("Intervalo_en_hidrogramas__minutos"),
97
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 10, 1, Integer.MAX_VALUE);
98
         m_Parameters.addNumericalValue(INTERVALHYETO, Sextante.getText("Intervalo_en_hietogramas__minutos"),
99
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 30, 1, Integer.MAX_VALUE);
100
      }
101
      catch (final RepeatedParameterNameException e) {
102
         Sextante.addErrorToLog(e);
103
      }
104
      catch (final UndefinedParentParameterNameException e) {
105
         Sextante.addErrorToLog(e);
106
      }
107
      catch (final OptionalParentParameterException e) {
108
         Sextante.addErrorToLog(e);
109
      }
110

  
111
   }
112

  
113

  
114
   private boolean createHyetographs() {
115

  
116
      int iPoint = 0;
117

  
118
      try {
119
         final IVectorLayer stations = m_Parameters.getParameterValueAsVectorLayer(STATIONS);
120
         m_Hyetogram = new Hyetograph[stations.getShapesCount()][];
121
         final IFeatureIterator iter = stations.iterator();
122
         while (iter.hasNext()) {
123
            final IFeature feature = iter.next();
124
            final String sTableName = feature.getRecord().getValue(m_iTableNameField).toString();
125
            final Coordinate c = feature.getGeometry().getCoordinate();
126
            m_Hyetogram[iPoint] = getHyetogramsFromTable(c.x, c.y, sTableName);
127
            iPoint++;
128
         }
129
         iter.close();
130

  
131
         return checkHyetogramsConsistency();
132
      }
133
      catch (final Exception e) {
134
         Sextante.addErrorToLog(e);
135
         return false;
136
      }
137

  
138
   }
139

  
140

  
141
   private boolean checkHyetogramsConsistency() {
142

  
143
      try {
144
         final int iIntervals = m_Hyetogram[0][0].getIntervals();
145
         for (int i = 0; i < m_Hyetogram.length; i++) {
146
            if (m_Hyetogram[i].length != m_Hyetogram[0].length) {
147
               return false;
148
            }
149
            if (iIntervals != m_Hyetogram[i][0].getIntervals()) {
150
               return false;
151
            }
152
            for (final Hyetograph[] element : m_Hyetogram) {
153
               if (element[i] == null) {
154
                  return false;
155
               }
156
            }
157
         }
158
         return true;
159
      }
160
      catch (final Exception e) {
161
         return false;
162
      }
163

  
164
   }
165

  
166

  
167
   private Hyetograph[] getHyetogramsFromTable(final double x,
168
                                               final double y,
169
                                               final String sName) {
170

  
171
      try {
172
         final ITable table = getTableFromName(sName);
173
         if (table != null) {
174
            final int iCount = table.getFieldCount();
175

  
176
            if (iCount <= 0) {
177
               return null;
178
            }
179

  
180
            if (!m_bHyetoNamesCreated) {
181
               m_bHyetoNamesCreated = true;
182
               m_sHyetoNames = new String[iCount];
183
               for (int i = 0; i < iCount; i++) {
184
                  m_sHyetoNames[i] = table.getFieldName(i);
185
               }
186
            }
187

  
188
            final Hyetograph hyetos[] = new Hyetograph[iCount];
189
            for (int i = 0; i < iCount; i++) {
190
               final double dHyeto[] = new double[(int) table.getRecordCount()];
191
               final IRecordsetIterator iter = table.iterator();
192
               int j = 0;
193
               while (iter.hasNext()) {
194
                  final IRecord record = iter.next();
195
                  String s = record.getValue(i).toString();
196
                  s = s.replaceAll("'", "");
197
                  try {
198
                     dHyeto[j] = Double.parseDouble(s);
199
                  }
200
                  catch (final NumberFormatException nfe) {
201
                     dHyeto[j] = 0;
202
                  }
203
                  j++;
204
               }
205
               hyetos[i] = new Hyetograph(dHyeto, this.m_iIntervalHyeto);
206
               hyetos[i].setName(sName + table.getFieldName(i));
207
               hyetos[i].setCoords(new Point2D.Double(x, y));
208
            }
209

  
210
            return hyetos;
211
         }
212
         else {
213
            return null;
214
         }
215
      }
216
      catch (final Exception e) {
217
         return null;
218
      }
219

  
220
   }
221

  
222

  
223
   private ITable getTableFromName(final String sName) {
224

  
225
      for (int i = 0; i < m_Tables.size(); i++) {
226
         final ITable table = (ITable) m_Tables.get(i);
227
         if (table.getName().equals(sName)) {
228
            return table;
229
         }
230
      }
231

  
232
      return null;
233

  
234
   }
235

  
236

  
237
   private void calculateHydrographs() throws UnsupportedOutputChannelException {
238

  
239
      int i, j, k;
240
      int x, y;
241
      int iLength;
242
      int iRatio;
243
      int iIndex;
244
      int iIntervalsHyeto, iIntervalsHydro;
245
      double hydData[];
246
      double dRain;
247
      double dTimeOut;
248
      double dRunoff;
249
      double dAccRunoff;
250
      final double dRunoff2Volume = m_TimeOut.getWindowCellSize() * m_TimeOut.getWindowCellSize() / 1000.0;
251
      IRasterLayer swap;
252
      IRasterLayer accRainGrid;
253
      IRasterLayer accRunoffGrid;
254
      IRasterLayer runoffGrid;
255

  
256
      m_Hydrographs = new ArrayList();
257

  
258
      final AnalysisExtent extent = m_TimeOut.getWindowGridExtent();
259

  
260
      accRainGrid = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_FLOAT, extent);
261
      accRunoffGrid = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_FLOAT, extent);
262
      runoffGrid = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_FLOAT, extent);
263

  
264
      iLength = m_Hyetogram[0][0].getDuration();
265
      iIntervalsHyeto = m_Hyetogram[0][0].getIntervals();
266
      iRatio = m_iIntervalHyeto / m_iIntervalHydro;
267
      iIntervalsHydro = (int) ((iLength + m_TimeOut.getMaxValue() * 3600) / m_iIntervalHydro) + 1;
268

  
269

  
270
      for (i = 0; i < m_Hyetogram[0].length; i++) {//for each return period
271

  
272
         accRainGrid.assign(0.0);
273
         accRunoffGrid.assign(0.0);
274
         runoffGrid.assign(0.0);
275

  
276
         hydData = new double[iIntervalsHydro];
277
         for (j = 0; j < iIntervalsHydro; j++) {
278
            hydData[j] = 0;
279
         }
280

  
281
         this.setProgressText(Sextante.getText("Calculando_hidrograma") + "(" + m_sHyetoNames[i] + ")");
282
         for (j = 0; j < iIntervalsHyeto; j++) {
283
            for (y = 0; y < m_iNY; y++) {
284
               for (x = 0; x < m_iNX; x++) {
285
                  dTimeOut = m_TimeOut.getCellValueAsDouble(x, y);
286
                  if (!m_TimeOut.isNoDataValue(dTimeOut) && (dTimeOut > 0)) {
287
                     dRain = getRainfall(i, extent.getWorldCoordsFromGridCoords(x, y), j * m_iIntervalHyeto, m_iIntervalHyeto);
288
                     if (dRain > 0) {
289
                        accRainGrid.addToCellValue(x, y, dRain);
290
                     }
291
                  }
292
               }
293
            }
294

  
295
            calculateRunoffGrid(runoffGrid, accRainGrid);
296

  
297
            for (y = 1; y < m_iNY; y++) {
298
               for (x = 0; x < m_iNX; x++) {
299
                  dTimeOut = m_TimeOut.getCellValueAsDouble(x, y);
300
                  if (!m_TimeOut.isNoDataValue(dTimeOut) && (dTimeOut > 0)) {
301
                     iIndex = (int) ((dTimeOut * 3600 + j * m_iIntervalHyeto) / m_iIntervalHydro);
302
                     dRunoff = runoffGrid.getCellValueAsDouble(x, y);
303
                     dAccRunoff = accRunoffGrid.getCellValueAsDouble(x, y);
304
                     dRunoff -= dAccRunoff;
305
                     if (dRunoff > 0) {
306
                        for (k = 0; k < iRatio; k++) {
307
                           hydData[iIndex] += dRunoff;
308
                        }
309
                     }
310
                  }
311
               }
312
            }
313

  
314
            swap = runoffGrid;
315
            runoffGrid = accRunoffGrid;
316
            accRunoffGrid = swap;
317

  
318
            setProgress(j, iIntervalsHyeto);
319

  
320
         }
321

  
322
         for (j = 0; j < iIntervalsHydro; j++) {
323
            hydData[j] = hydData[j] * dRunoff2Volume / iRatio / m_iIntervalHydro;
324
         }
325

  
326
         final Hydrograph hyd = new Hydrograph(hydData, m_iIntervalHydro);
327
         hyd.setName(m_Hyetogram[0][i].getName());
328
         m_Hydrographs.add(hyd);
329

  
330
      }
331

  
332
   }
333

  
334

  
335
   private double getRainfall(final int iSerie,
336
                              final Point2D pt,
337
                              final int iInitTime,
338
                              final int iInterval) {
339

  
340
      double dTotalRainfall = 0;
341
      double dTotalWeight = 0;
342

  
343
      for (final Hyetograph[] element : m_Hyetogram) {
344
         final Point2D coords = element[iSerie].getCoords();
345
         final double dWeight = 1. / Math.pow(coords.distance(pt), 2.);
346
         final double dRainfall = element[iSerie].getRainfall(iInitTime, iInterval) * dWeight;
347
         dTotalRainfall += dRainfall;
348
         dTotalWeight += dWeight;
349
      }
350

  
351
      return dTotalRainfall / dTotalWeight;
352

  
353
   }
354

  
355

  
356
   private void documentHydrographs() throws UnsupportedOutputChannelException {
357

  
358
      int i, j;
359
      double flow[];
360
      Hydrograph hydro;
361
      String sTableDescription;
362
      String sTableName;
363
      final String sFields[] = { "T", "Q" };
364
      final Object[] values = new Object[2];
365
      final Class[] types = { Integer.class, Double.class };
366
      ITable driver;
367

  
368
      for (i = 0; i < m_Hydrographs.size(); i++) {
369
         hydro = (Hydrograph) m_Hydrographs.get(i);
370
         sTableName = "HYDROGRAPH" + Integer.toString(i);
371
         sTableDescription = "Hidrograma (" + m_sHyetoNames[i] + ")";
372
         driver = getNewTable(sTableName, sTableDescription, types, sFields);
373
         flow = hydro.getFlowArray();
374
         for (j = 0; j < hydro.getLengthInIntervals(); j++) {
375
            values[0] = new Integer(j * hydro.getTimeInterval());
376
            values[1] = new Double(flow[j]);
377
            driver.addRecord(values);
378
         }
379
         setProgress(i, m_Hydrographs.size());
380
      }
381

  
382
   }
383

  
384

  
385
   private double getRunoff(final double dRainfall, //in mm
386
                            final double dCN) {
387

  
388
      double dS;
389
      double dRunoff;
390

  
391
      dS = (25400.0 / dCN) - 254.0;
392

  
393
      if (dRainfall < (0.2 * dS)) {
394
         return 0;
395
      }
396

  
397
      dRunoff = Math.pow(dRainfall - 0.2 * dS, 2.0) / (dRainfall + 0.8 * dS);
398

  
399
      return dRunoff;
400

  
401
   }
402

  
403

  
404
   private void calculateRunoffGrid(final IRasterLayer runoff,
405
                                    final IRasterLayer rainfall) {
406

  
407
      int x, y;
408
      double dCN;
409
      double dRunoff;
410
      double dRainfall;
411

  
412
      double dMean = 0;
413
      int i = 0;
414

  
415
      for (y = 0; y < m_iNY; y++) {
416
         for (x = 0; x < m_iNX; x++) {
417
            dCN = m_CN.getCellValueAsDouble(x, y);
418
            dRainfall = rainfall.getCellValueAsDouble(x, y);
419
            if (!m_CN.isNoDataValue(dCN) && (dRainfall != 0.0)) {
420
               i++;
421
               dRunoff = getRunoff(dRainfall, dCN);
422
               runoff.setCellValue(x, y, dRunoff);
423
               dMean += dRunoff;
424
            }
425
         }
426
      }
427
      dMean /= (i);
428

  
429
      dMean += 1;
430

  
431
   }
432

  
433

  
434
   @Override
435
   public boolean isSuitableForModelling() {
436

  
437
      return false;
438

  
439
   }
440

  
441

  
442
}
0 443

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/hydrology/heightOverChannelNetwork/HeightOverChannelNetworkAlgorithm.java
1
package es.unex.sextante.hydrology.heightOverChannelNetwork;
2

  
3
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
4
import es.unex.sextante.core.GeoAlgorithm;
5
import es.unex.sextante.core.AnalysisExtent;
6
import es.unex.sextante.core.OutputObjectsSet;
7
import es.unex.sextante.core.ParametersSet;
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.gridTools.closeGaps.CloseGapsAlgorithm;
13
import es.unex.sextante.rasterWrappers.GridWrapper;
14

  
15
public class HeightOverChannelNetworkAlgorithm
16
         extends
17
            GeoAlgorithm {
18

  
19
   public static final String DEM        = "DEM";
20
   public static final String NETWORK    = "NETWORK";
21
   public static final String HEIGHTOVER = "HEIGHTOVER";
22
   public static final String THRESHOLD  = "THRESHOLD";
23

  
24
   private int                m_iNX, m_iNY;
25

  
26
   private IRasterLayer       m_DEM      = null;
27
   private IRasterLayer       m_Network  = null;
28
   private IRasterLayer       m_Diff;
29
   private double             m_dThreshold;
30

  
31

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

  
35
      m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
36
      m_Network = m_Parameters.getParameterValueAsRasterLayer(NETWORK);
37
      m_dThreshold = m_Parameters.getParameterValueAsDouble(THRESHOLD);
38

  
39
      m_Diff = getNewRasterLayer(HEIGHTOVER, Sextante.getText("Elevation_over_channel_network"),
40
               IRasterLayer.RASTER_DATA_TYPE_FLOAT);
41

  
42
      m_Diff.setNoDataValue(-1.0);
43
      //m_Diff.assignNoData();
44

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

  
50
      m_iNX = m_DEM.getNX();
51
      m_iNY = m_DEM.getNY();
52

  
53
      return interpolateSurfaceAndSubstract();
54

  
55
   }
56

  
57

  
58
   @Override
59
   public void defineCharacteristics() {
60

  
61
      setName(Sextante.getText("Elevation_over_channel_network"));
62
      setGroup(Sextante.getText("Indices_and_other_hydrological_parameters"));
63
      setUserCanDefineAnalysisExtent(true);
64

  
65
      try {
66
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
67
         m_Parameters.addInputRasterLayer(NETWORK, Sextante.getText("Channel_network"), true);
68
         m_Parameters.addNumericalValue(THRESHOLD, Sextante.getText("Tension_threshold"), 0.1,
69
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
70
         addOutputRasterLayer(HEIGHTOVER, Sextante.getText("Elevation_over_channel_network"));
71
      }
72
      catch (final RepeatedParameterNameException e) {
73
         Sextante.addErrorToLog(e);
74
      }
75

  
76
   }
77

  
78

  
79
   private boolean interpolateSurfaceAndSubstract() throws GeoAlgorithmExecutionException {
80

  
81
      int x, y;
82
      double dValue, dValue2;
83

  
84
      for (y = 0; y < m_iNY; y++) {
85
         for (x = 0; x < m_iNX; x++) {
86
            dValue = m_Network.getCellValueAsDouble(x, y);
87
            if ((dValue != 0) && !m_Network.isNoDataValue(dValue)) {
88
               dValue = m_DEM.getCellValueAsDouble(x, y);
89
               m_Diff.setCellValue(x, y, dValue);
90
            }
91
            else {
92
               m_Diff.setNoData(x, y);
93
            }
94
         }
95
      }
96

  
97
      final CloseGapsAlgorithm alg = new CloseGapsAlgorithm();
98
      final ParametersSet ps = alg.getParameters();
99

  
100
      //IRasterLayer layer = m_Diff.getRasterLayer(" ",  getAlgorithmProjection());
101
      ps.getParameter(CloseGapsAlgorithm.INPUT).setParameterValue(m_Diff);
102
      ps.getParameter(CloseGapsAlgorithm.THRESHOLD).setParameterValue(new Double(m_dThreshold));
103
      if (alg.execute(this.m_Task, this.m_OutputFactory)) {
104
         final OutputObjectsSet output = alg.getOutputObjects();
105
         final IRasterLayer networkHeight = (IRasterLayer) output.getOutput("RESULT").getOutputObject();
106
         networkHeight.open();
107
         for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
108
            for (x = 0; x < m_iNX; x++) {
109
               dValue = m_DEM.getCellValueAsDouble(x, y);
110
               dValue2 = networkHeight.getCellValueAsDouble(x, y);
111
               if (m_DEM.isNoDataValue(dValue) || networkHeight.isNoDataValue(dValue2)) {
112
                  m_Diff.setNoData(x, y);
113
               }
114
               else {
115
                  m_Diff.setCellValue(x, y, dValue - dValue2);
116
               }
117
            }
118
         }
119
         networkHeight.close();
120
         return !m_Task.isCanceled();
121
      }
122
      else {
123
         return false;
124
      }
125

  
126
   }
127

  
128
}
0 129

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/hydrology/watersheds/WatershedsAlgorithm.java
1
package es.unex.sextante.hydrology.watersheds;
2

  
3
import java.util.ArrayList;
4
import java.util.Arrays;
5

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

  
15
public class WatershedsAlgorithm
16
         extends
17
            GeoAlgorithm {
18

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

  
23
   public static final String DEM          = "DEM";
24
   public static final String NETWORK      = "NETWORK";
25
   public static final String MINSIZE      = "MINSIZE";
26
   public static final String WATERSHEDS   = "WATERSHEDS";
27

  
28
   private int                m_iBasins;
29
   private int                m_iNX, m_iNY;
30
   private int                m_iMinSize;
31

  
32
   private IRasterLayer       m_DEM        = null;
33
   private IRasterLayer       m_Network    = null;
34
   private IRasterLayer       m_Basins;
35
   private IRasterLayer       m_Directions;
36

  
37

  
38
   @Override
39
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
40

  
41
      m_DEM = m_Parameters.getParameterValueAsRasterLayer(DEM);
42
      m_Network = m_Parameters.getParameterValueAsRasterLayer(NETWORK);
43
      m_iMinSize = m_Parameters.getParameterValueAsInt(MINSIZE);
44

  
45
      m_Basins = getNewRasterLayer(WATERSHEDS, Sextante.getText("Watersheds"), IRasterLayer.RASTER_DATA_TYPE_INT);
46

  
47
      m_Basins.setNoDataValue(NO_BASIN);
48
      m_Basins.assignNoData();
49

  
50
      final AnalysisExtent extent = m_Basins.getWindowGridExtent();
51
      m_DEM.setFullExtent();
52
      m_Network.setWindowExtent(extent);
53

  
54
      m_Directions = getTempRasterLayer(IRasterLayer.RASTER_DATA_TYPE_INT, extent);
55

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

  
59
      calculateBasins();
60

  
61
      return !m_Task.isCanceled();
62

  
63
   }
64

  
65

  
66
   @Override
67
   public void defineCharacteristics() {
68

  
69
      setName(Sextante.getText("Watersheds"));
70
      setGroup(Sextante.getText("Basic_hydrological_analysis"));
71
      setUserCanDefineAnalysisExtent(true);
72

  
73
      try {
74
         m_Parameters.addInputRasterLayer(DEM, Sextante.getText("Elevation"), true);
75
         m_Parameters.addInputRasterLayer(NETWORK, Sextante.getText("Channel_network"), true);
76
         m_Parameters.addNumericalValue(MINSIZE, Sextante.getText("Minimum_watershed_size__cells"), 0,
77
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER);
78
         addOutputRasterLayer(WATERSHEDS, Sextante.getText("Watersheds"));
79
      }
80
      catch (final RepeatedParameterNameException e) {
81
         Sextante.addErrorToLog(e);
82
      }
83

  
84
   }
85

  
86

  
87
   private void calculateBasins() {
88

  
89
      int i;
90
      int x, y;
91
      int iBasins;
92
      prepareDirectionsLayer();
93
      final ArrayList outletsArrayList = getOutlets();
94
      final Object[] outlets = outletsArrayList.toArray();
95
      Arrays.sort(outlets);
96

  
97
      m_iBasins = 0;
98
      for (i = outlets.length - 1; (i >= 0) && setProgress(outlets.length - i, outlets.length); i--) {
99
         m_iBasins++;
100
         x = ((GridCell) outlets[i]).getX();
101
         y = ((GridCell) outlets[i]).getY();
102
         if (getBasin(x, y) < m_iMinSize) {
103
            iBasins = m_iBasins - 1;
104
            m_iBasins = NO_BASIN;
105
            getBasin(x, y);
106
            m_iBasins = iBasins;
107
         }
108
      }
109

  
110
   }
111

  
112

  
113
   private void prepareDirectionsLayer() {
114

  
115
      int x, y;
116
      int iDir;
117

  
118
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
119
         for (x = 0; x < m_iNX; x++) {
120
            iDir = m_DEM.getDirToNextDownslopeCell(x, y, false);
121
            if (iDir < 0) {
122
               m_Directions.setCellValue(x, y, -1.0);
123
            }
124
            else {
125
               m_Directions.setCellValue(x, y, ((iDir + 4) % 8));
126
            }
127
         }
128
      }
129

  
130
   }
131

  
132

  
133
   private ArrayList getOutlets() {
134

  
135
      int x, y;
136
      final ArrayList outlets = new ArrayList();
137

  
138
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
139
         for (x = 0; x < m_iNX; x++) {
140
            addOutlet(x, y, outlets);
141
         }
142

  
143
      }
144

  
145
      return outlets;
146

  
147
   }
148

  
149

  
150
   private void addOutlet(final int x,
151
                          final int y,
152
                          final ArrayList outlets) {
153

  
154
      int i;
155
      int ix, iy;
156
      int iUpslopeX = 0, iUpslopeY = 0;
157
      int iUpslopeRiverCells = 0;
158
      double dUpslopeZ = 0;
159
      int iNetwork = m_Network.getCellValueAsInt(x, y);
160
      final int iDir = m_Directions.getCellValueAsInt(x, y);
161
      final double dZ = m_DEM.getCellValueAsDouble(x, y);
162
      final int iNextCellX = x + m_iOffsetX[(iDir + 4) % 8];
163
      final int iNextCellY = y + m_iOffsetY[(iDir + 4) % 8];
164
      final int iNextCellNetwork = m_Network.getCellValueAsInt(iNextCellX, iNextCellY);
165

  
166
      if (m_Network.isNoDataValue(iNetwork) || (iNetwork == 0)) { //not a river cell
167
         return;
168
      }
169
      if (m_Network.isNoDataValue(iNextCellNetwork) || (iNextCellNetwork == 0)) { //border river cell
170
         outlets.add(new GridCell(x, y, dZ));
171
      }
172
      else if (iNetwork < 0) { // user defined outlet
173
         outlets.add(new GridCell(x, y, dZ));
174
         return;
175
      }
176
      else if ((iDir == -1) && !m_DEM.isNoDataValue(dZ)) { //border cell, limits with no data or with grid boundaries
177
         outlets.add(new GridCell(x, y, dZ));
178
      }
179
      else {
180
         for (i = 0; i < 8; i++) {
181
            ix = x + m_iOffsetX[i];
182
            iy = y + m_iOffsetY[i];
183
            if (m_Directions.getCellValueAsInt(ix, iy) == i) {
184
               iNetwork = m_Network.getCellValueAsInt(ix, iy);
185
               if ((iNetwork > 0) && !m_Network.isNoDataValue(iNetwork)) {
186
                  if (iUpslopeRiverCells > 0) {
187
                     if (iUpslopeRiverCells == 1) {
188
                        outlets.add(new GridCell(iUpslopeX, iUpslopeY, dUpslopeZ));
189
                     }
190
                     outlets.add(new GridCell(ix, iy, m_DEM.getCellValueAsDouble(ix, iy)));
191
                  }
192
                  else {
193
                     iUpslopeX = ix;
194
                     iUpslopeY = iy;
195
                     dUpslopeZ = m_DEM.getCellValueAsDouble(x, y);
196
                  }
197
                  iUpslopeRiverCells++;
198
               }
199
            }
200
         }
201
      }
202

  
203
   }
204

  
205

  
206
   private int getBasin(final int x,
207
                        final int y) {
208

  
209
      int i, ix, iy, nCells = 1;
210

  
211
      final int iBasin = m_Basins.getCellValueAsInt(x, y);
212
      final int iDir = m_Directions.getCellValueAsInt(x, y);
213
      if ((iBasin == NO_BASIN) && !m_Directions.isNoDataValue(iDir)) {
214

  
215
         m_Basins.setCellValue(x, y, m_iBasins);
216

  
217
         for (i = 0, nCells = 1; i < 8; i++) {
218
            ix = x + m_iOffsetX[i];
219
            iy = y + m_iOffsetY[i];
220
            if ((m_Directions.getCellValueAsInt(ix, iy) == i) && (m_Basins.getCellValueAsInt(ix, iy) == NO_BASIN)) {
221
               nCells += getBasin(ix, iy);
222
            }
223
         }
224
         return nCells;
225
      }
226

  
227
      return 0;
228
   }
229

  
230
}
0 231

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/hydrology/createExclusionAreas/CreateExclusionAreasAlgorithm.java
1

  
2

  
3
package es.unex.sextante.hydrology.createExclusionAreas;
4

  
5
import java.awt.Point;
6
import java.awt.geom.Point2D;
7
import java.util.ArrayList;
8

  
9
import es.unex.sextante.core.GeoAlgorithm;
10
import es.unex.sextante.core.Sextante;
11
import es.unex.sextante.dataObjects.IRasterLayer;
12
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
13
import es.unex.sextante.exceptions.RepeatedParameterNameException;
14
import es.unex.sextante.rasterWrappers.GridCell;
15

  
16

  
17
public class CreateExclusionAreasAlgorithm
18
         extends
19
            GeoAlgorithm {
20

  
21
   private final static int   m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
22
   private final static int   m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
23

  
24
   public static final String RESULT       = "RESULT";
25
   public static final String OUTLET_POINT = "OUTLET_POINT";
26
   public static final String INPUT        = "INPUT";
27

  
28
   int                        m_iNX, m_iNY;
29
   IRasterLayer               m_Grid;
30
   boolean                    m_IsCellAlreadyVisited[][];
31
   private IRasterLayer       m_ExclusionAreas;
32

  
33

  
34
   @Override
35
   public void defineCharacteristics() {
36

  
37
      setName(Sextante.getText("CreateExclusionAreas"));
38
      setGroup(Sextante.getText("Basic_hydrological_analysis"));
39
      setUserCanDefineAnalysisExtent(false);
40

  
41
      try {
42
         m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true);
43
         m_Parameters.addPoint(OUTLET_POINT, Sextante.getText("Outlet_point"));
44
         addOutputRasterLayer(RESULT, Sextante.getText("Result"), 0);
45
      }
46
      catch (final RepeatedParameterNameException e) {
47
         Sextante.addErrorToLog(e);
48
      }
49

  
50
   }
51

  
52

  
53
   @Override
54
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
55

  
56
      m_Grid = m_Parameters.getParameterValueAsRasterLayer(INPUT);
57

  
58
      m_Grid.setFullExtent();
59
      m_Grid.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
60

  
61
      m_iNX = m_Grid.getNX();
62
      m_iNY = m_Grid.getNY();
63

  
64
      m_IsCellAlreadyVisited = new boolean[m_iNX][m_iNY];
65

  
66
      m_ExclusionAreas = getNewRasterLayer(RESULT, Sextante.getText("ExclusionAreas"), IRasterLayer.RASTER_DATA_TYPE_INT,
67
               m_Grid.getLayerGridExtent());
68

  
69
      m_ExclusionAreas.assign(m_Grid);
70
      m_ExclusionAreas.setNoDataValue(0.0);
71

  
72
      final Point2D pt = m_Parameters.getParameterValueAsPoint(OUTLET_POINT);
73
      final GridCell cell = m_Grid.getLayerGridExtent().getGridCoordsFromWorldCoords(pt);
74

  
75
      exclude(cell.getX(), cell.getY());
76

  
77
      m_ExclusionAreas.setNoData(cell.getX(), cell.getY());
78

  
79
      return !m_Task.isCanceled();
80

  
81
   }
82

  
83

  
84
   private void exclude(int x,
85
                        int y) {
86

  
87
      int x2, y2;
88
      int iInitClass;
89
      int iPt;
90
      int n;
91
      int iClass;
92
      double dArea = 0;
93
      ArrayList centralPoints = new ArrayList();
94
      ArrayList adjPoints = new ArrayList();
95
      Point point;
96

  
97
      iInitClass = m_Grid.getCellValueAsInt(x, y);
98

  
99
      centralPoints.add(new Point(x, y));
100
      m_IsCellAlreadyVisited[x][y] = true;
101

  
102
      while ((centralPoints.size() != 0) && !m_Task.isCanceled()) {
103
         for (iPt = 0; iPt < centralPoints.size(); iPt++) {
104
            dArea += m_Grid.getWindowCellSize() * m_Grid.getWindowCellSize();
105
            point = (Point) centralPoints.get(iPt);
106
            x = point.x;
107
            y = point.y;
108
            double dClass = m_Grid.getCellValueAsInt(x, y);
109
            if (!m_Grid.isNoDataValue(dClass)) {
110
               for (n = 0; n < 8; n++) {
111
                  x2 = x + m_iOffsetX[n];
112
                  y2 = y + m_iOffsetY[n];
113
                  dClass = m_Grid.getCellValueAsDouble(x2, y2);
114
                  if (!m_Grid.isNoDataValue(dClass)) {
115
                     iClass = (int) dClass;
116
                     if (m_IsCellAlreadyVisited[x2][y2] == false) {
117
                        if (iInitClass == iClass) {
118
                           m_IsCellAlreadyVisited[x2][y2] = true;
119
                           adjPoints.add(new Point(x2, y2));
120
                           m_ExclusionAreas.setNoData(x2, y2);
121
                        }
122
                     }
123
                  }
124
               }
125
            }
126
         }
127

  
128
         centralPoints = adjPoints;
129
         System.out.println(centralPoints.size());
130
         adjPoints = new ArrayList();
131

  
132
      }
133

  
134
      m_ExclusionAreas.setNoData(x, y);
135

  
136
   }
137

  
138
}
0 139

  
org.gvsig.toolbox/tags/org.gvsig.toolbox-1.0.83/org.gvsig.toolbox.algorithm/src/main/java/es/unex/sextante/hydrology/distToChannelNetwork/DistToChannelNetworkAlgorithm.java
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

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff