Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / topology / extractNodes / ExtractNodesAlgorithm.java @ 59

History | View | Annotate | Download (4.11 KB)

1

    
2

    
3
package es.unex.sextante.topology.extractNodes;
4

    
5
import java.util.HashMap;
6
import java.util.Iterator;
7

    
8
import com.vividsolutions.jts.geom.Coordinate;
9
import com.vividsolutions.jts.geom.Geometry;
10
import com.vividsolutions.jts.geom.GeometryFactory;
11
import com.vividsolutions.jts.geom.Point;
12

    
13
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
14
import es.unex.sextante.core.GeoAlgorithm;
15
import es.unex.sextante.core.Sextante;
16
import es.unex.sextante.dataObjects.IFeature;
17
import es.unex.sextante.dataObjects.IFeatureIterator;
18
import es.unex.sextante.dataObjects.IVectorLayer;
19
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter;
20
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
21
import es.unex.sextante.exceptions.RepeatedParameterNameException;
22
import es.unex.sextante.outputs.OutputVectorLayer;
23

    
24

    
25
public class ExtractNodesAlgorithm
26
         extends
27
            GeoAlgorithm {
28

    
29
   public static final String LAYER  = "LAYER";
30
   public static final String RESULT = "RESULT";
31

    
32

    
33
   @Override
34
   public void defineCharacteristics() {
35

    
36
      setName(Sextante.getText("Extract_Nodes"));
37
      setGroup(Sextante.getText("Topology"));
38
      setUserCanDefineAnalysisExtent(true);
39

    
40
      try {
41
         m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Capa de entrada"), AdditionalInfoVectorLayer.SHAPE_TYPE_LINE,
42
                  true);
43

    
44
         addOutputVectorLayer(RESULT, Sextante.getText("Nodes"), OutputVectorLayer.SHAPE_TYPE_POINT);
45

    
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 i = 0;
58
      final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(ExtractNodesAlgorithm.LAYER);
59
      if (!m_bIsAutoExtent) {
60
         layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
61
      }
62

    
63
      final Class[] out_ftypes = new Class[1];
64
      out_ftypes[0] = Integer.class;
65

    
66
      final String[] out_fnames = new String[1];
67
      out_fnames[0] = "DEGREE";
68

    
69
      final IVectorLayer driver = getNewVectorLayer(ExtractNodesAlgorithm.RESULT, Sextante.getText("Nodes"),
70
               OutputVectorLayer.SHAPE_TYPE_POINT, out_ftypes, out_fnames);
71

    
72

    
73
      final HashMap nodesHashMap = new HashMap();
74
      final GeometryFactory gf = new GeometryFactory();
75

    
76
      // STEP 01 - Get Nodes
77
      int count = 0;
78
      int idx = 0;
79
      final int iTotal = layerIn.getShapesCount();
80
      final IFeatureIterator iter = layerIn.iterator();
81

    
82
      idx = 0;
83
      while (iter.hasNext() && setProgress(count, iTotal)) {
84
         final IFeature feature = iter.next();
85
         final Coordinate[] coords = feature.getGeometry().getCoordinates();
86

    
87
         Point point = gf.createPoint(coords[0]);
88
         processNode(nodesHashMap, point, count, feature, idx);
89
         count++;
90
         point = gf.createPoint(coords[coords.length - 1]);
91
         processNode(nodesHashMap, point, count, feature, idx);
92

    
93
         setProgress(count, iTotal);
94
         idx++;
95
      }
96
      iter.close();
97

    
98
      final Iterator nodesIter = nodesHashMap.values().iterator();
99
      while (nodesIter.hasNext() && setProgress(i, iTotal)) {
100
         final Node n = (Node) nodesIter.next();
101
         final Geometry g = n.getGeometry();
102
         final Object[] values = { n.getDegree() };
103
         driver.addFeature(g, values);
104
         setProgress(i, iTotal);
105
         i++;
106
      }
107
      iter.close();
108

    
109
      return !m_Task.isCanceled();
110

    
111
   }
112

    
113

    
114
   public void processNode(final HashMap endpoints,
115
                           final Geometry point,
116
                           final int count,
117
                           final IFeature feat,
118
                           final int featIdx) {
119

    
120
      if (!endpoints.containsKey(point.toText())) {
121
         final Node node = new Node(count, point, feat);
122
         node.addConnectedLine(feat, featIdx);
123
         endpoints.put(point.toText(), node);
124

    
125
      }
126
      else {
127
         //TODO What about SELF-LINE-NODES?
128
         final Node ep = (Node) endpoints.get(point.toText());
129
         ep.incrementDegree();
130
         ep.addConnectedLine(feat, featIdx);
131
      }
132
   }
133

    
134
}