Statistics
| Revision:

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

History | View | Annotate | Download (4.71 KB)

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
}