Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.clip / src / main / java / org / gvsig / sextante / app / algorithm / clip / ClipAlgorithm.java @ 25

History | View | Annotate | Download (2.98 KB)

1 20 nbrodin
package org.gvsig.sextante.app.algorithm.clip;
2
3 25 nbrodin
import java.awt.geom.Rectangle2D;
4
5 20 nbrodin
import es.unex.sextante.core.GeoAlgorithm;
6
import es.unex.sextante.core.Sextante;
7 25 nbrodin
import es.unex.sextante.dataObjects.IVectorLayer;
8 20 nbrodin
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
9
import es.unex.sextante.exceptions.RepeatedParameterNameException;
10 25 nbrodin
import es.unex.sextante.outputs.OutputVectorLayer;
11
import com.vividsolutions.jts.geom.Geometry;
12 20 nbrodin
13 21 nbrodin
public class ClipAlgorithm extends GeoAlgorithm {
14 25 nbrodin
        public static final String  RESULT   = "RESULT";
15
        public static final String  LAYER    = "LAYER";
16
17
        /**
18
         * processes input layer's features to clip its geometries with clipping
19
         * layer bounding box
20
         */
21
        private ClipVisitor visitor;
22
23
        /*
24
         * (non-Javadoc)
25
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
26
         */
27 20 nbrodin
        public void defineCharacteristics(){
28 21 nbrodin
                setName(Sextante.getText("Clip"));
29
                setGroup(Sextante.getText("gvSIG_Algorithms"));
30 20 nbrodin
                setGeneratesUserDefinedRasterOutput(true);
31 25 nbrodin
32
                try {
33
                        m_Parameters.addInputVectorLayer(LAYER,
34
                                                                                                Sextante.getText( "Input_layer"),
35
                                                                                                IVectorLayer.SHAPE_TYPE_MIXED,
36
                                                                                                true);
37
                } catch (RepeatedParameterNameException e) {
38
                        Sextante.addErrorToLog(e);
39
                }
40
                addOutputVectorLayer(RESULT,
41
                                                                Sextante.getText( "Clip"),
42
                                                                OutputVectorLayer.SHAPE_TYPE_LINE);
43
44 20 nbrodin
        }
45 25 nbrodin
46
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException{
47
                Geometry clippingGeometry = null;
48
                try {
49
                        clippingGeometry = computeJtsClippingPoly();
50
                } catch (Exception e) {
51
                        e.printStackTrace();
52
                }
53
54
                visitor = new ClipVisitor(clippingGeometry, resultLayerDefinition,
55
                                schemaManager, writer);
56
                Strategy strategy = StrategyManager.getStrategy(firstLayer);
57
                Rectangle2D clippingRect = FConverter.
58
                        convertEnvelopeToRectangle2D(clippingGeometry.
59
                                                                        getEnvelopeInternal());
60
                try {
61
                        if (onlyFirstLayerSelection) {
62
                                visitor.setSelection(firstLayer.getRecordset().getSelection());
63
                        }
64
                        strategy.process(visitor,
65
                                        clippingRect,
66
                                        cancelMonitor);
67 20 nbrodin
68 25 nbrodin
69
                } catch (Exception e) {
70
                        e.printStackTrace();
71
                }
72
                finished = true;
73
                return true;
74
        }
75
76
        /**
77
         * Computes union of all geometries of the clipping layer
78
         *
79
         * @return
80
         * @throws com.iver.cit.gvsig.fmap.DriverException
81
         * @throws ReadDriverException
82
         * @throws VisitorException
83
         * @throws ExpansionFileReadException
84
         */
85
86
        //TODO Esto lo vamos a quitar, y lo vamos a hacer para cada
87
        //feature individual
88
        private Geometry computeJtsClippingPoly()
89
                        throws ReadDriverException, ExpansionFileReadException, VisitorException {
90
                ScalableUnionVisitor visitor = new ScalableUnionVisitor(overlayLayer.getShapeType());
91
92
                Strategy strategy = StrategyManager.getStrategy(overlayLayer);
93
                if (onlyClipLayerSelection) {
94
                        strategy.process(visitor, overlayLayer.getRecordset()
95
                                        .getSelection());
96
                } else {
97
                        strategy.process(visitor);
98
                }
99
                return visitor.getJtsConvexHull();
100
        }
101 20 nbrodin
}