Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.intersection / src / main / java / org / gvsig / geoprocess / algorithm / intersection / IntersectionAlgorithm.java @ 245

History | View | Annotate | Download (5.69 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2012 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geoprocess.algorithm.intersection;
25

    
26
import es.unex.sextante.core.Sextante;
27
import es.unex.sextante.dataObjects.IVectorLayer;
28
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
29
import es.unex.sextante.exceptions.RepeatedParameterNameException;
30
import es.unex.sextante.outputs.OutputVectorLayer;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.feature.FeatureSet;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
37
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
38

    
39
/**
40
 * Intersection algorithm
41
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
42
 */
43
public class IntersectionAlgorithm extends AbstractSextanteGeoProcess {
44

    
45
        public static final String  RESULT    = "RESULT";
46
        public static final String  LAYER     = "LAYER";
47
        public static final String  INTER     = "INTER";
48
        public static final String  CHECK     = "CHECK";
49
        
50
        /*
51
         * (non-Javadoc)
52
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
53
         */
54
        public void defineCharacteristics() {
55
        setName(getTranslation("Intersection"));
56
        setGroup(getTranslation("basic_vect_algorithms"));
57
        // setGeneratesUserDefinedRasterOutput(false);
58
                
59
                try {
60
                        m_Parameters.addInputVectorLayer(LAYER, 
61
                getTranslation("Input_layer"),
62
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
63
                                                                                                true);
64
                        m_Parameters.addInputVectorLayer(INTER, 
65
                getTranslation("Overlays_layer"),
66
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
67
                                                                                                true);
68
            m_Parameters.addBoolean(CHECK,
69
                getTranslation("Selected_geometries"), false);
70
                } catch (RepeatedParameterNameException e) {
71
                        Sextante.addErrorToLog(e);
72
                }
73
                addOutputVectorLayer(RESULT,
74
 getTranslation("Intersection"),
75
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
76
        }
77
        
78
        /*
79
         * (non-Javadoc)
80
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
81
         */
82
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
83
                IVectorLayer inter = m_Parameters.getParameterValueAsVectorLayer(INTER);
84
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
85
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
86
                
87
                try {
88
                        computesIntersection(layer, inter, layer.getShapeType(), selectedGeom);
89
                } catch (DataException e) {
90
                        Sextante.addErrorToLog(e);
91
                        return false;
92
                }
93
                return true;
94
        }
95
        
96
        /**
97
         * Builds a layer with the intersection between the input layer and the templateGeometry
98
         * @param layer
99
         *        Input layer
100
         * @param templateGeometry
101
         * @param shapeType
102
         *        Output shape type
103
         * @param selectedGeom
104
         *        If it's true only selected geometries will be computed
105
         */
106
        private void computesIntersection(IVectorLayer layer,
107
                                                                IVectorLayer overlay,
108
                                                                int shapeType, 
109
                                                                boolean selectedGeom) throws DataException {
110
                FeatureStore storeLayer = null;
111
                FeatureStore storeOverlay = null;
112
                if(layer instanceof FlyrVectIVectorLayer && 
113
                        overlay instanceof FlyrVectIVectorLayer) {
114
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
115
                        storeOverlay = ((FlyrVectIVectorLayer)overlay).getFeatureStore();
116
                } else
117
                        return;
118

    
119
                FeatureSet features1 = null;
120
                features1 = storeLayer.getFeatureSet();
121
                FeatureType featureType1 = features1.getDefaultFeatureType();
122
                
123
                FeatureSet features2 = null;
124
                features2 = storeOverlay.getFeatureSet();
125
                FeatureType featureType2 = features2.getDefaultFeatureType();
126
                
127
                FeatureStore outFeatStore = null;
128
                //Si las dos capas son de lineas la resultante es de l?neas
129
        if (isLine(storeLayer) && isLine(storeOverlay))
130
            outFeatStore =
131
                buildOutPutStoreFromUnion(featureType1, featureType2,
132
                    IVectorLayer.SHAPE_TYPE_POINT,
133
                    getTranslation("Intersection"), RESULT);
134
        else
135
        // Si alguna de las dos capas es de puntos la resultante tambi?n lo es
136
        if (isPoint(storeLayer) || isPoint(storeOverlay))
137
            outFeatStore =
138
                buildOutPutStoreFromUnion(featureType1, featureType2,
139
                    IVectorLayer.SHAPE_TYPE_POINT,
140
                    getTranslation("Intersection"), RESULT);
141
        else
142
            outFeatStore =
143
                buildOutPutStoreFromUnion(featureType1, featureType2,
144
                    shapeType, getTranslation("Intersection"), RESULT);
145
                
146
                IntersectionOperation operation = new IntersectionOperation(storeOverlay);
147
                operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
148
        }
149
        
150
}