Statistics
| Revision:

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

History | View | Annotate | Download (4.2 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
;
14

    
15
public abstract class NeighborhoodStatsBaseAlgorithm
16
         extends
17
            GeoAlgorithm {
18

    
19
   private static final String LAYER        = "LAYER";
20
   private static final String RADIUS       = "RADIUS";
21
   private static final String FORCE_NODATA = "NODATA";
22
   private static final String RESULT       = "RESULT";
23

    
24
   protected double            NO_DATA;
25

    
26
   private int                 m_iRadius;
27
   private boolean             m_bForceNoData;
28
   private boolean             m_bIsValidCell[][];
29
   private IRasterLayer        m_window;
30
   protected double            m_dValues[];
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.addBoolean(FORCE_NODATA, Sextante.getText("Force_no-data_value"), true);
43
         addOutputRasterLayer(RESULT, this.getName());
44
      }
45
      catch (final RepeatedParameterNameException e) {
46
         Sextante.addErrorToLog(e);
47
      }
48

    
49
   }
50

    
51

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

    
55
      int x, y;
56
      int iNX, iNY;
57
      int iValidCells = 0;
58

    
59
      NO_DATA = m_OutputFactory.getDefaultNoDataValue();
60

    
61
      m_iRadius = m_Parameters.getParameterValueAsInt(RADIUS);
62
      m_window = m_Parameters.getParameterValueAsRasterLayer(LAYER);
63
      m_bForceNoData = m_Parameters.getParameterValueAsBoolean(FORCE_NODATA);
64

    
65
      m_window.setFullExtent();
66

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

    
70
      result.setNoDataValue(NO_DATA);
71
      result.assignNoData();
72

    
73
      iNX = m_window.getNX();
74
      iNY = m_window.getNY();
75

    
76
      m_bIsValidCell = new boolean[2 * m_iRadius + 1][2 * m_iRadius + 1];
77

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

    
91
      m_dValues = new double[iValidCells];
92

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

    
104
      return !m_Task.isCanceled();
105

    
106
   }
107

    
108

    
109
   private boolean setNeighborhoodValues(final int iX,
110
                                         final int iY) {
111

    
112
      int x, y;
113
      int iCell = 0;
114
      double dValue;
115

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

    
136
      return true;
137

    
138
   }
139

    
140

    
141
   protected abstract double processValues();
142

    
143

    
144
}