Statistics
| Revision:

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

History | View | Annotate | Download (3.55 KB)

1

    
2

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

    
5
import com.vividsolutions.jts.geom.Coordinate;
6
import com.vividsolutions.jts.geom.Geometry;
7

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

    
19

    
20
public class DistanceMatrixAlgorithm
21
         extends
22
            GeoAlgorithm {
23

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

    
27

    
28
   @Override
29
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
30

    
31
      int i = 0, j = 0;
32
      double x1, x2, y1, y2;
33
      double dDifX, dDifY;
34
      int iCount;
35
      String[] sFields;
36
      Class[] types;
37
      Object[] values;
38

    
39
      final IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(POINTS);
40
      if (!m_bIsAutoExtent) {
41
         layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
42
      }
43

    
44
      iCount = layer.getShapesCount();
45
      final double[][] d = new double[iCount][iCount];
46
      sFields = new String[iCount + 1];
47
      types = new Class[iCount + 1];
48
      values = new Object[iCount + 1];
49
      sFields[0] = "ID";
50
      types[0] = Integer.class;
51
      final IFeatureIterator iter = layer.iterator();
52
      while (iter.hasNext() && setProgress(i, iCount)) {
53
         IFeature feature = iter.next();
54
         sFields[i + 1] = Integer.toString(i + 1);
55
         types[i + 1] = Double.class;
56
         Geometry geom = feature.getGeometry();
57
         Coordinate coord = geom.getCoordinate();
58
         x1 = coord.x;
59
         y1 = coord.y;
60
         final IFeatureIterator iter2 = layer.iterator();
61
         j = 0;
62
         while (iter2.hasNext()) {
63
            feature = iter2.next();
64
            geom = feature.getGeometry();
65
            coord = geom.getCoordinate();
66
            x2 = coord.x;
67
            y2 = coord.y;
68
            dDifX = x2 - x1;
69
            dDifY = y2 - y1;
70
            d[i][j] = Math.sqrt(dDifX * dDifX + dDifY * dDifY);
71
            j++;
72
         }
73
         iter2.close();
74
         i++;
75
      }
76
      iter.close();
77

    
78
      final String sTableName = Sextante.getText("Distance_matrix_[") + layer.getName() + "]";
79

    
80
      final ITable driver = getNewTable(RESULT, sTableName, types, sFields);
81

    
82
      for (i = 0; (i < iCount) && setProgress(i, iCount); i++) {
83
         values[0] = new Integer(i + 1);
84
         for (j = 0; j < iCount; j++) {
85
            final double dDist = d[i][j];
86
            values[j + 1] = new Double(dDist);
87
         }
88
         driver.addRecord(values);
89

    
90
      }
91

    
92
      return !m_Task.isCanceled();
93
   }
94

    
95

    
96
   @Override
97
   public void defineCharacteristics() {
98

    
99
      setName(Sextante.getText("Distance_matrix"));
100
      setGroup(Sextante.getText("Tools_for_point_layers"));
101
      setUserCanDefineAnalysisExtent(true);
102

    
103
      try {
104
         m_Parameters.addInputVectorLayer(POINTS, Sextante.getText("Points"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT, true);
105
         addOutputTable(RESULT, Sextante.getText("Distance_matrix"));
106
      }
107
      catch (final RepeatedParameterNameException e) {
108
         Sextante.addErrorToLog(e);
109
      }
110

    
111
   }
112

    
113
}