Statistics
| Revision:

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

History | View | Annotate | Download (4.48 KB)

1

    
2

    
3
package es.unex.sextante.gridStatistics.base;
4

    
5
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
6
import es.unex.sextante.core.GeoAlgorithm;
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

    
12

    
13
public abstract class NeighborhoodStatsExtendedBaseAlgorithm
14
         extends
15
            GeoAlgorithm {
16

    
17
   public static final String RADIUS       = "RADIUS";
18
   public static final String LAYER        = "LAYER";
19
   public static final String VALUE        = "VALUE";
20
   public static final String FORCE_NODATA = "NODATA";
21
   public static final String RESULT       = "RESULT";
22

    
23
   protected double           NO_DATA;
24

    
25
   private int                m_iRadius;
26
   private boolean            m_bForceNoData;
27
   private boolean            m_bIsValidCell[][];
28
   private IRasterLayer       m_window;
29
   protected double           m_dValues[];
30
   protected double           m_dValue;
31

    
32

    
33
   @Override
34
   public void defineCharacteristics() {
35

    
36
      setUserCanDefineAnalysisExtent(false);
37

    
38
      try {
39
         m_Parameters.addInputRasterLayer(LAYER, Sextante.getText("Layer"), true);
40
         m_Parameters.addNumericalValue(RADIUS, Sextante.getText("Radius"), AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER,
41
                  1, 1, 20);
42
         m_Parameters.addNumericalValue(VALUE, Sextante.getText("Value"), 0, AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE);
43
         m_Parameters.addBoolean(FORCE_NODATA, Sextante.getText("Force_no-data_value"), true);
44
         addOutputRasterLayer(RESULT, this.getName());
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
      int x, y;
57
      int iNX, iNY;
58
      int iValidCells = 0;
59

    
60
      NO_DATA = m_OutputFactory.getDefaultNoDataValue();
61

    
62
      m_iRadius = m_Parameters.getParameterValueAsInt(RADIUS);
63
      m_window = m_Parameters.getParameterValueAsRasterLayer(LAYER);
64
      m_dValue = m_Parameters.getParameterValueAsDouble(VALUE);
65
      m_bForceNoData = m_Parameters.getParameterValueAsBoolean(FORCE_NODATA);
66

    
67
      m_window.setFullExtent();
68

    
69
      final IRasterLayer result = getNewRasterLayer(RESULT, this.getName(), IRasterLayer.RASTER_DATA_TYPE_DOUBLE,
70
               m_window.getWindowGridExtent());
71

    
72
      result.setNoDataValue(NO_DATA);
73
      result.assignNoData();
74

    
75
      iNX = m_window.getNX();
76
      iNY = m_window.getNY();
77

    
78
      m_bIsValidCell = new boolean[2 * m_iRadius + 1][2 * m_iRadius + 1];
79

    
80
      for (y = -m_iRadius; y < m_iRadius + 1; y++) {
81
         for (x = -m_iRadius; x < m_iRadius + 1; x++) {
82
            final double dDist = Math.sqrt(x * x + y * y);
83
            if (dDist < m_iRadius) {
84
               m_bIsValidCell[x + m_iRadius][y + m_iRadius] = true;
85
               iValidCells++;
86
            }
87
            else {
88
               m_bIsValidCell[x + m_iRadius][y + m_iRadius] = false;
89
            }
90
         }
91
      }
92

    
93
      m_dValues = new double[iValidCells];
94

    
95
      for (y = 0; (y < iNY) && setProgress(y, iNY); y++) {
96
         for (x = 0; x < iNX; x++) {
97
            if (setNeighborhoodValues(x, y)) {
98
               result.setCellValue(x, y, processValues());
99
            }
100
            else {
101
               result.setNoData(x, y);
102
            }
103
         }
104

    
105
      }
106

    
107
      return !m_Task.isCanceled();
108

    
109
   }
110

    
111

    
112
   private boolean setNeighborhoodValues(final int iX,
113
                                         final int iY) {
114

    
115
      int x, y;
116
      int iCell = 0;
117
      double dValue;
118

    
119

    
120
      for (y = -m_iRadius; y < m_iRadius + 1; y++) {
121
         for (x = -m_iRadius; x < m_iRadius + 1; x++) {
122
            if (m_bIsValidCell[x + m_iRadius][y + m_iRadius]) {
123
               dValue = m_window.getCellValueAsDouble(iX + x, iY + y);
124
               if (!m_window.isNoDataValue(dValue)) {
125
                  m_dValues[iCell] = dValue;
126
               }
127
               else {
128
                  if (m_bForceNoData) {
129
                     return false;
130
                  }
131
                  else {
132
                     m_dValues[iCell] = NO_DATA;
133
                  }
134
               }
135
               iCell++;
136
            }
137
         }
138
      }
139

    
140
      return true;
141

    
142
   }
143

    
144

    
145
   protected abstract double processValues();
146

    
147
}