Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vegetationIndices / base / DistanceBasedAlgorithm.java @ 59

History | View | Annotate | Download (4.32 KB)

1
package es.unex.sextante.vegetationIndices.base;
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.Sextante;
7
import es.unex.sextante.dataObjects.IRasterLayer;
8
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
9
import es.unex.sextante.exceptions.OptionalParentParameterException;
10
import es.unex.sextante.exceptions.RepeatedParameterNameException;
11
import es.unex.sextante.exceptions.UndefinedParentParameterNameException;
12

    
13
public abstract class DistanceBasedAlgorithm
14
         extends
15
            GeoAlgorithm {
16

    
17
   public static final String LAYERRED  = "LAYERRED";
18
   public static final String BANDRED   = "BANDRED";
19
   public static final String LAYERNIR  = "LAYERNIR";
20
   public static final String BANDNIR   = "BANDNIR";
21
   public static final String SLOPE     = "SLOPE";
22
   public static final String INTERCEPT = "INTERCEPT";
23
   public static final String RESULT    = "RESULT";
24

    
25
   private IRasterLayer       m_LayerRed, m_LayerNIR;
26

    
27
   protected double           m_dNoData;
28
   protected double           m_dSlope, m_dIntercept;
29

    
30

    
31
   @Override
32
   public void defineCharacteristics() {
33

    
34
      setUserCanDefineAnalysisExtent(true);
35
      setGroup(Sextante.getText("Vegetation_indices"));
36
      try {
37
         m_Parameters.addInputRasterLayer(LAYERRED, Sextante.getText("Red_layer"), true);
38
         m_Parameters.addBand(BANDRED, Sextante.getText("Red_band"), LAYERRED);
39
         m_Parameters.addInputRasterLayer(LAYERNIR, Sextante.getText("Near_infrared_layer"), true);
40
         m_Parameters.addBand(BANDNIR, Sextante.getText("Near_infrared_band"), LAYERNIR);
41
         m_Parameters.addNumericalValue(SLOPE, Sextante.getText("Soil_line_slope"),
42
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 1, 0, Double.MAX_VALUE);
43
         m_Parameters.addNumericalValue(INTERCEPT, Sextante.getText("Soil_line_intercept"),
44
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 0, 0, Double.MAX_VALUE);
45
         addOutputRasterLayer(RESULT, Sextante.getText("Result"));
46
      }
47
      catch (final RepeatedParameterNameException e) {
48
         Sextante.addErrorToLog(e);
49
      }
50
      catch (final UndefinedParentParameterNameException e) {
51
         Sextante.addErrorToLog(e);
52
      }
53
      catch (final OptionalParentParameterException e) {
54
         Sextante.addErrorToLog(e);
55
      }
56

    
57
   }
58

    
59

    
60
   @Override
61
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
62

    
63
      int x, y;
64
      int iNX, iNY;
65
      double dRed;
66
      double dNIR;
67
      double dIndex;
68
      AnalysisExtent extent;
69

    
70
      m_LayerRed = m_Parameters.getParameterValueAsRasterLayer(LAYERRED);
71
      final int iBandRed = m_Parameters.getParameterValueAsInt(BANDRED);
72
      m_LayerNIR = m_Parameters.getParameterValueAsRasterLayer(LAYERNIR);
73
      final int iBandNIR = m_Parameters.getParameterValueAsInt(BANDNIR);
74

    
75
      m_dIntercept = m_Parameters.getParameterValueAsDouble(INTERCEPT);
76
      m_dSlope = m_Parameters.getParameterValueAsDouble(SLOPE);
77

    
78
      final IRasterLayer result = getNewRasterLayer(RESULT, getName(), IRasterLayer.RASTER_DATA_TYPE_DOUBLE);
79

    
80
      extent = result.getWindowGridExtent();
81

    
82
      m_LayerRed.setWindowExtent(extent);
83
      m_LayerNIR.setWindowExtent(extent);
84
      m_LayerRed.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
85
      m_LayerNIR.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
86

    
87
      iNX = m_LayerRed.getNX();
88
      iNY = m_LayerRed.getNY();
89

    
90
      m_dNoData = result.getNoDataValue();
91

    
92
      for (y = 0; (y < iNY) && setProgress(y, iNY); y++) {
93
         for (x = 0; x < iNX; x++) {
94
            dRed = m_LayerRed.getCellValueAsDouble(x, y, iBandRed);
95
            dNIR = m_LayerNIR.getCellValueAsDouble(x, y, iBandNIR);
96
            if (!m_LayerRed.isNoDataValue(dRed) && !m_LayerNIR.isNoDataValue(dNIR)) {
97
               dIndex = getIndex(dRed, dNIR);
98
               result.setCellValue(x, y, dIndex);
99
            }
100
            else {
101
               result.setNoData(x, y);
102
            }
103
         }
104
      }
105

    
106
      return !m_Task.isCanceled();
107

    
108
   }
109

    
110

    
111
   protected abstract double getIndex(double dRed,
112
                                      double dNIR);
113

    
114
}