Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridCategorical / classStatistics / ClassStatisticsAlgorithm.java @ 59

History | View | Annotate | Download (5.9 KB)

1
package es.unex.sextante.gridCategorical.classStatistics;
2

    
3
import java.awt.Point;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.Set;
8

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

    
17
public class ClassStatisticsAlgorithm
18
         extends
19
            GeoAlgorithm {
20

    
21
   private final static int   m_iOffsetX[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
22
   private final static int   m_iOffsetY[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
23

    
24
   public static final String RESULT       = "RESULT";
25

    
26
   public static final String INPUT        = "INPUT";
27

    
28
   int                        m_iNX, m_iNY;
29
   IRasterLayer               m_Grid;
30
   HashMap                    m_Map;
31
   boolean                    m_IsCellAlreadyVisited[][];
32

    
33

    
34
   @Override
35
   public void defineCharacteristics() {
36

    
37
      setName(Sextante.getText("Class_statistics"));
38
      setGroup(Sextante.getText("Raster_categories_analysis"));
39
      setUserCanDefineAnalysisExtent(true);
40

    
41
      try {
42
         m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer"), true);
43
         addOutputTable(RESULT, Sextante.getText("Class_statistics"));
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 iClass;
57
      double dArea;
58
      ClassStatistics stats = null;
59

    
60
      m_Grid = m_Parameters.getParameterValueAsRasterLayer(INPUT);
61

    
62
      m_Grid.setWindowExtent(m_AnalysisExtent);
63
      m_Grid.setInterpolationMethod(IRasterLayer.INTERPOLATION_NearestNeighbour);
64

    
65
      m_iNX = m_Grid.getNX();
66
      m_iNY = m_Grid.getNY();
67

    
68
      m_IsCellAlreadyVisited = new boolean[m_iNX][m_iNY];
69
      m_Map = new HashMap();
70

    
71
      for (y = 0; (y < m_iNY) && setProgress(y, m_iNY); y++) {
72
         for (x = 0; x < m_iNX; x++) {
73
            if (!m_IsCellAlreadyVisited[x][y]) {
74
               iClass = m_Grid.getCellValueAsInt(x, y);
75
               dArea = getClassArea(x, y);
76
               stats = (ClassStatistics) m_Map.get(new Integer(iClass));
77
               if (stats != null) {
78
                  stats.add(dArea);
79
               }
80
               else {
81
                  stats = new ClassStatistics(iClass);
82
                  stats.add(dArea);
83
                  m_Map.put(new Integer(iClass), stats);
84
               }
85
            }
86
         }
87

    
88
      }
89

    
90
      if (!m_Task.isCanceled()) {
91
         createTable();
92
         return true;
93
      }
94
      else {
95
         return false;
96
      }
97

    
98
   }
99

    
100

    
101
   private void createTable() throws UnsupportedOutputChannelException {
102

    
103
      Object[] values;
104
      ClassStatistics stats;
105
      final Set set = m_Map.keySet();
106
      final Iterator iter = set.iterator();
107

    
108
      final String sFields[] = { Sextante.getText("Class_ID"), Sextante.getText("total_area_Total"),
109
               Sextante.getText("Number_of_patches"), Sextante.getText("Mean_area"), Sextante.getText("Variance_of_area"),
110
               Sextante.getText("Max_area"), Sextante.getText("Min_area") };
111
      final Class types[] = { Integer.class, Double.class, Integer.class, Double.class, Double.class, Double.class, Double.class };
112
      final String sTableName = Sextante.getText("Class_statistics_[") + m_Grid.getName() + "]";
113

    
114
      final ITable table = getNewTable(RESULT, sTableName, types, sFields);
115
      values = new Object[7];
116

    
117
      while (iter.hasNext()) {
118
         stats = (ClassStatistics) m_Map.get(iter.next());
119
         values[0] = new Integer(stats.getClassID());
120
         values[1] = new Double(stats.getTotalArea());
121
         values[2] = new Integer(stats.getZonesCount());
122
         values[3] = new Double(stats.getMeanArea());
123
         values[4] = new Double(stats.getVarianceArea());
124
         values[5] = new Double(stats.getMaxArea());
125
         values[6] = new Double(stats.getMinArea());
126
         table.addRecord(values);
127
      }
128

    
129
   }
130

    
131

    
132
   private double getClassArea(int x,
133
                               int y) {
134

    
135
      int x2, y2;
136
      int iInitClass;
137
      int iPt;
138
      int n;
139
      int iClass;
140
      double dArea = 0;
141
      ArrayList centralPoints = new ArrayList();
142
      ArrayList adjPoints = new ArrayList();
143
      Point point;
144

    
145
      iInitClass = m_Grid.getCellValueAsInt(x, y);
146

    
147
      centralPoints.add(new Point(x, y));
148
      m_IsCellAlreadyVisited[x][y] = true;
149

    
150
      while (centralPoints.size() != 0) {
151
         for (iPt = 0; iPt < centralPoints.size(); iPt++) {
152
            dArea += m_Grid.getWindowCellSize() * m_Grid.getWindowCellSize();
153
            point = (Point) centralPoints.get(iPt);
154
            x = point.x;
155
            y = point.y;
156
            double dClass = m_Grid.getCellValueAsInt(x, y);
157
            if (!m_Grid.isNoDataValue(dClass)) {
158
               for (n = 0; n < 8; n++) {
159
                  x2 = x + m_iOffsetX[n];
160
                  y2 = y + m_iOffsetY[n];
161
                  dClass = m_Grid.getCellValueAsDouble(x2, y2);
162
                  if (!m_Grid.isNoDataValue(dClass)) {
163
                     iClass = (int) dClass;
164
                     if (m_IsCellAlreadyVisited[x2][y2] == false) {
165
                        if (iInitClass == iClass) {
166
                           m_IsCellAlreadyVisited[x2][y2] = true;
167
                           adjPoints.add(new Point(x2, y2));
168
                        }
169
                     }
170
                  }
171
               }
172
            }
173
         }
174

    
175
         centralPoints = adjPoints;
176
         adjPoints = new ArrayList();
177

    
178
      }
179

    
180
      return dArea;
181

    
182
   }
183

    
184
}