Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / vectorTools / clip / ClipAlgorithm.java @ 59

History | View | Annotate | Download (5.13 KB)

1

    
2

    
3
package es.unex.sextante.vectorTools.clip;
4

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

    
8
import com.vividsolutions.jts.geom.Envelope;
9
import com.vividsolutions.jts.geom.Geometry;
10
import com.vividsolutions.jts.geom.prep.PreparedGeometry;
11
import com.vividsolutions.jts.geom.prep.PreparedGeometryFactory;
12
import com.vividsolutions.jts.index.strtree.STRtree;
13

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

    
25

    
26
public class ClipAlgorithm
27
         extends
28
            GeoAlgorithm {
29

    
30
   public static final String LAYER     = "LAYER";
31
   public static final String CLIPLAYER = "CLIPLAYER";
32
   public static final String RESULT    = "RESULT";
33

    
34
   private IVectorLayer       m_Output;
35

    
36

    
37
   @Override
38
   public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
39

    
40
      final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER);
41
      final IVectorLayer layerClip = m_Parameters.getParameterValueAsVectorLayer(CLIPLAYER);
42
      if (!m_bIsAutoExtent) {
43
         layerIn.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
44
         layerClip.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
45
      }
46

    
47
      final STRtree tree = buildClipTree(layerClip);
48

    
49
      m_Output = getNewVectorLayer(RESULT, Sextante.getText("Clipped_layer"), layerIn.getShapeType(), layerIn.getFieldTypes(),
50
               layerIn.getFieldNames());
51

    
52
      final IFeatureIterator iter = layerIn.iterator();
53

    
54
      int i = 0;
55
      final int iShapeCount = layerIn.getShapesCount();
56
      while (iter.hasNext() && setProgress(i, iShapeCount)) {
57
         final IFeature feature = iter.next();
58
         final Geometry g = clipGeometry(feature.getGeometry(), tree);
59
         if (g != null) {
60
            m_Output.addFeature(g, feature.getRecord().getValues());
61
         }
62
         i++;
63
      }
64
      iter.close();
65

    
66
      return !m_Task.isCanceled();
67

    
68
   }
69

    
70

    
71
   @Override
72
   public void defineCharacteristics() {
73

    
74
      setName(Sextante.getText("Clip"));
75
      setGroup(Sextante.getText("Tools_for_vector_layers"));
76

    
77
      try {
78
         m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer_to_clip"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY,
79
                  true);
80
         m_Parameters.addInputVectorLayer(CLIPLAYER, Sextante.getText("Clipping_layer"),
81
                  AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON, true);
82
         addOutputVectorLayer(RESULT, Sextante.getText("Clipped_Layer"));
83
      }
84
      catch (final RepeatedParameterNameException e) {
85
         Sextante.addErrorToLog(e);
86
      }
87

    
88
   }
89

    
90

    
91
   public Geometry clipGeometry(final Geometry g,
92
                                final STRtree tree) throws GeoAlgorithmExecutionException {
93

    
94
      if (g == null) {
95
         return null;
96
      }
97

    
98
      final Envelope env = g.getEnvelopeInternal();
99
      if (env == null) {
100
         return null;
101
      }
102

    
103
      final List candidates = tree.query(env);
104
      Geometry clipGeometry = null;
105
      Geometry result = g;
106
      boolean intersected = false;
107
      if ((candidates == null) || (candidates.size() == 0)) {
108
         return null;
109
      }
110
      else {
111
         try {
112
            for (final Iterator it = candidates.iterator(); it.hasNext();) {
113
               final PreparedGeometry pg = (PreparedGeometry) it.next();
114
               if (pg.intersects(result)) {
115
                  intersected = true;
116
                  clipGeometry = pg.getGeometry();
117
                  result = clipGeometry.intersection(result);
118
               }
119
            }
120
            if (!intersected) {
121
               return null;
122
            }
123
            if (result.getNumGeometries() == 0) {
124
               return null;
125
            }
126
            return result;
127
         }
128
         catch (final com.vividsolutions.jts.geom.TopologyException e) {
129
            if (!g.isValid()) {
130
               throw new GeoAlgorithmExecutionException("Wrong input geometry");
131
            }
132
            if (!clipGeometry.isValid()) {
133
               throw new GeoAlgorithmExecutionException("Wrong clipping geometry");
134
            }
135
         }
136

    
137
      }
138
      return null;
139
   }
140

    
141

    
142
   private STRtree buildClipTree(final IVectorLayer layer) throws IteratorException {
143

    
144
      final STRtree tree = new STRtree();
145

    
146
      final IFeatureIterator iter = layer.iterator();
147
      while (iter.hasNext()) {
148
         final IFeature feature = iter.next();
149
         final Geometry geom = feature.getGeometry();
150
         final PreparedGeometry pg = PreparedGeometryFactory.prepare(geom);
151
         tree.insert(pg.getGeometry().getEnvelopeInternal(), pg);
152
      }
153
      iter.close();
154

    
155
      return tree;
156

    
157
   }
158

    
159
}