Statistics
| Revision:

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

History | View | Annotate | Download (5.34 KB)

1

    
2

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

    
5
import java.util.Arrays;
6

    
7
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
8
import es.unex.sextante.core.GeoAlgorithm;
9
import es.unex.sextante.core.Sextante;
10
import es.unex.sextante.dataObjects.IRasterLayer;
11
import es.unex.sextante.dataObjects.ITable;
12
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
13
import es.unex.sextante.exceptions.RepeatedParameterNameException;
14
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
15

    
16

    
17
public class SemivariancesAlgorithm
18
         extends
19
            GeoAlgorithm {
20

    
21
   public static final String RESULT  = "RESULT";
22
   public static final String LAYER   = "LAYER";
23
   public static final String MAXDIST = "MAXDIST";
24

    
25
   private int                m_iNX, m_iNY;
26
   private int                m_iMaxDist;
27
   private double             m_dSemivarHorz[];
28
   private double             m_dSemivarVert[];
29
   private int                m_iValidCellsHorz[];
30
   private int                m_iValidCellsVert[];
31
   private IRasterLayer       m_Layer;
32

    
33

    
34
   @Override
35
   public void defineCharacteristics() {
36

    
37
      setUserCanDefineAnalysisExtent(false);
38
      setName(Sextante.getText("Semivariances__raster"));
39
      setGroup(Sextante.getText("Geostatistics"));
40

    
41
      try {
42
         m_Parameters.addInputRasterLayer(LAYER, Sextante.getText("Layer"), true);
43
         m_Parameters.addNumericalValue(MAXDIST, Sextante.getText("Maximum_distance__pixels"),
44
                  AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER, 20, 1, Integer.MAX_VALUE);
45
         addOutputRasterLayer(RESULT, Sextante.getText("Semivariances"));
46
      }
47
      catch (final RepeatedParameterNameException e) {
48
         Sextante.addErrorToLog(e);
49
      }
50

    
51
   }
52

    
53

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

    
57
      int x, y;
58

    
59
      m_iMaxDist = m_Parameters.getParameterValueAsInt(MAXDIST);
60
      m_Layer = m_Parameters.getParameterValueAsRasterLayer(LAYER);
61

    
62
      m_Layer.setFullExtent();
63

    
64
      m_iNX = m_Layer.getNX();
65
      m_iNY = m_Layer.getNY();
66

    
67
      m_iMaxDist = Math.min(m_iMaxDist, m_iNX);
68
      m_iMaxDist = Math.min(m_iMaxDist, m_iNY);
69

    
70
      m_dSemivarHorz = new double[m_iMaxDist];
71
      m_dSemivarVert = new double[m_iMaxDist];
72
      m_iValidCellsHorz = new int[m_iMaxDist];
73
      m_iValidCellsVert = new int[m_iMaxDist];
74

    
75
      Arrays.fill(m_dSemivarHorz, 0.0);
76
      Arrays.fill(m_dSemivarVert, 0.0);
77
      Arrays.fill(m_iValidCellsHorz, 0);
78
      Arrays.fill(m_iValidCellsVert, 0);
79

    
80
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
81
         for (x = 0; x < m_iNX; x++) {
82
            calculateSemivariances(x, y);
83
         }
84
      }
85

    
86
      if (!m_Task.isCanceled()) {
87
         postProcessValues();
88
         createTable();
89
      }
90
      return !m_Task.isCanceled();
91

    
92
   }
93

    
94

    
95
   private void createTable() throws UnsupportedOutputChannelException {
96

    
97
      int i;
98
      final String[] sFields = { Sextante.getText("Distance"), Sextante.getText("Semivar__Horz"),
99
               Sextante.getText("Semivar__Vert") };
100
      final Class types[] = { Double.class, Double.class, Double.class };
101
      final String sTableName = Sextante.getText("Semivariances[") + m_Layer.getName() + "]";
102
      final Object[] values = new Object[3];
103
      final ITable driver = getNewTable(RESULT, sTableName, types, sFields);
104

    
105
      for (i = 0; i < m_iMaxDist; i++) {
106
         values[0] = new Double(m_Layer.getWindowCellSize() * i);
107
         values[1] = new Double(m_dSemivarHorz[i]);
108
         values[2] = new Double(m_dSemivarVert[i]);
109
         driver.addRecord(values);
110
      }
111

    
112
   }
113

    
114

    
115
   private void postProcessValues() {
116

    
117
      for (int i = 0; i < m_iMaxDist; i++) {
118
         if (m_iValidCellsHorz[i] != 0) {
119
            m_dSemivarHorz[i] /= (2. * m_iValidCellsHorz[i]);
120
         }
121
         if (m_iValidCellsVert[i] != 0) {
122
            m_dSemivarVert[i] /= (2. * m_iValidCellsVert[i]);
123
         }
124
      }
125

    
126
   }
127

    
128

    
129
   private void calculateSemivariances(final int iInitX,
130
                                       final int iInitY) {
131

    
132
      int iDist;
133
      int x, y;
134
      int x1, x2, y1, y2;
135
      double dValue;
136
      final double dCenterValue = m_Layer.getCellValueAsDouble(iInitX, iInitY);
137

    
138
      if (m_Layer.isNoDataValue(dCenterValue)) {
139
         return;
140
      }
141

    
142
      x1 = Math.max(iInitX - m_iMaxDist, 0);
143
      x2 = Math.min(iInitX + m_iMaxDist, m_iNX - 1);
144
      y1 = Math.max(iInitY - m_iMaxDist, 0);
145
      y2 = Math.min(iInitY + m_iMaxDist, m_iNY - 1);
146

    
147
      for (x = x1; x < x2 + 1; x++) {
148
         iDist = Math.abs(x - iInitX) - 1;
149
         if (iDist > 0) {
150
            dValue = m_Layer.getCellValueAsDouble(x, iInitY);
151
            if (!m_Layer.isNoDataValue(dValue)) {
152
               m_dSemivarHorz[iDist] += Math.pow(dValue - dCenterValue, 2);
153
               m_iValidCellsHorz[iDist]++;
154
            }
155
         }
156
      }
157

    
158
      for (y = y1; y < y2 + 1; y++) {
159
         iDist = Math.abs(y - iInitY) - 1;
160
         if (iDist > 0) {
161
            dValue = m_Layer.getCellValueAsDouble(iInitX, y);
162
            if (!m_Layer.isNoDataValue(dValue)) {
163
               m_dSemivarVert[iDist] += Math.pow(dValue - dCenterValue, 2);
164
               m_iValidCellsVert[iDist]++;
165
            }
166
         }
167
      }
168

    
169
   }
170

    
171

    
172
}