Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / pointAnalysis / ripleysK / RipleysKAlgorithm.java @ 59

History | View | Annotate | Download (5.15 KB)

1

    
2

    
3
package es.unex.sextante.pointAnalysis.ripleysK;
4

    
5
import java.awt.geom.Rectangle2D;
6

    
7
import com.vividsolutions.jts.geom.Coordinate;
8
import com.vividsolutions.jts.geom.Geometry;
9

    
10
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
11
import es.unex.sextante.core.GeoAlgorithm;
12
import es.unex.sextante.core.Sextante;
13
import es.unex.sextante.dataObjects.IFeature;
14
import es.unex.sextante.dataObjects.IFeatureIterator;
15
import es.unex.sextante.dataObjects.ITable;
16
import es.unex.sextante.dataObjects.IVectorLayer;
17
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter;
18
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
19
import es.unex.sextante.exceptions.RepeatedParameterNameException;
20
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
21

    
22

    
23
public class RipleysKAlgorithm
24
         extends
25
            GeoAlgorithm {
26

    
27
   private static final String POINTS     = "POINTS";
28
   private static final String RESULT     = "RESULT";
29

    
30
   private final int           CLASSES    = 30;
31

    
32
   private int                 m_iCount;
33
   private double              m_dMaxDist = 0;
34
   private double              m_dInterval;
35
   private double              m_dArea;
36
   private double              m_dK[];
37
   private IVectorLayer        m_Layer;
38

    
39

    
40
   @Override
41
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
42

    
43
      int i, j;
44
      double x1, x2, y1, y2;
45
      double dDifX, dDifY;
46
      Rectangle2D rect;
47

    
48
      m_Layer = m_Parameters.getParameterValueAsVectorLayer(POINTS);
49
      if (!m_bIsAutoExtent) {
50
         m_Layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
51
      }
52
      m_iCount = m_Layer.getShapesCount();
53
      if (m_iCount == 0) {
54
         throw new GeoAlgorithmExecutionException("0 points in layer");
55
      }
56
      rect = m_Layer.getFullExtent();
57
      m_dArea = rect.getHeight() * rect.getWidth();
58
      m_dMaxDist = Math.min(rect.getHeight(), rect.getWidth()) / 3;
59
      m_dInterval = m_dMaxDist / CLASSES;
60
      final double[][] d = new double[m_iCount][m_iCount];
61

    
62
      i = 0;
63
      final IFeatureIterator iter = m_Layer.iterator();
64
      while (iter.hasNext() && setProgress(i, m_iCount)) {
65
         final IFeature feature = iter.next();
66
         final Geometry geom = feature.getGeometry();
67
         final Coordinate coord = geom.getCoordinate();
68
         x1 = coord.x;
69
         y1 = coord.y;
70
         j = 0;
71
         final IFeatureIterator iter2 = m_Layer.iterator();
72
         while (iter2.hasNext()) {
73
            final IFeature feature2 = iter2.next();
74
            final Geometry geom2 = feature2.getGeometry();
75
            final Coordinate coord2 = geom2.getCoordinate();
76
            x2 = coord2.x;
77
            y2 = coord2.y;
78
            dDifX = x2 - x1;
79
            dDifY = y2 - y1;
80
            d[i][j] = Math.sqrt(dDifX * dDifX + dDifY * dDifY);
81
            j++;
82
         }
83
         i++;
84
      }
85
      iter.close();
86

    
87
      calculate(d);
88
      createTables();
89

    
90
      return !m_Task.isCanceled();
91

    
92
   }
93

    
94

    
95
   private void createTables() throws UnsupportedOutputChannelException {
96

    
97
      int i;
98
      double dKPoisson;
99
      double dL;
100
      final String[] sFields = { Sextante.getText("Distance"), Sextante.getText("K"), Sextante.getText("K_Poisson"),
101
               Sextante.getText("L") };
102
      final Class[] types = { Double.class, Double.class, Double.class, Double.class };
103
      final String sTableName = Sextante.getText("Ripley_K") + "[" + m_Layer.getName() + "]";
104
      final Object[] values = new Object[4];
105
      final ITable driver = getNewTable(RESULT, sTableName, types, sFields);
106

    
107
      for (i = 0; i < CLASSES; i++) {
108
         values[0] = new Double(m_dInterval * i);
109
         values[1] = new Double(m_dK[i]);
110
         dKPoisson = Math.PI * Math.pow(i * m_dInterval, 2.);
111
         values[2] = new Double(dKPoisson);
112
         dL = Math.sqrt(m_dK[i] / Math.PI) - (i * m_dInterval);
113
         values[3] = new Double(dL);
114
         driver.addRecord(values);
115
      }
116

    
117
   }
118

    
119

    
120
   private void calculate(final double[][] dist) {
121

    
122
      int i, j;
123
      int iCount;
124
      int iClass;
125
      double dDist;
126

    
127
      m_dK = new double[CLASSES];
128

    
129
      for (iClass = 1; iClass < CLASSES; iClass++) {
130
         dDist = iClass * m_dInterval;
131
         iCount = 0;
132
         for (i = 0; i < dist.length; i++) {
133
            for (j = 0; j < dist.length; j++) {
134
               if (dist[i][j] < dDist) {
135
                  iCount++;
136
               }
137
            }
138
            setProgress(i, dist.length);
139
         }
140
         m_dK[iClass] = m_dArea / (m_iCount * m_iCount) * iCount;
141
      }
142

    
143
   }
144

    
145

    
146
   @Override
147
   public void defineCharacteristics() {
148

    
149
      setName(Sextante.getText("Ripley_K"));
150
      setGroup(Sextante.getText("Tools_for_point_layers"));
151
      setUserCanDefineAnalysisExtent(true);
152
      try {
153
         m_Parameters.addInputVectorLayer(POINTS, Sextante.getText("Points"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT, true);
154
         addOutputTable(RESULT, Sextante.getText("Ripley_K"));
155
      }
156
      catch (final RepeatedParameterNameException e) {
157
         Sextante.addErrorToLog(e);
158
      }
159

    
160
   }
161

    
162
}