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 @ 307

History | View | Annotate | Download (7.93 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 org.gvsig.fmap.dal.exception.DataException;
27
import org.gvsig.fmap.dal.feature.FeatureSet;
28
import org.gvsig.fmap.dal.feature.FeatureStore;
29
import org.gvsig.fmap.dal.feature.FeatureType;
30
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
31
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
32

    
33
import es.unex.sextante.core.Sextante;
34
import es.unex.sextante.dataObjects.IVectorLayer;
35
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
36
import es.unex.sextante.exceptions.RepeatedParameterNameException;
37
import es.unex.sextante.outputs.OutputVectorLayer;
38

    
39
/**
40
 * Intersection algorithm
41
 * <UL>
42
 * <LI>Pol-Pol: 3 layers (polygon, point, line)</LI>
43
 * <LI>Pol-Line: 2 layers (point, line)</LI>
44
 * <LI>Pol-Point: 1 layer (point)</LI>
45
 * <LI>Line-Point: 1 layer (point)</LI>
46
 * <LI>Line-Line: 2 layers (point, line)</LI>
47
 * <LI>Point-Point: 1 layer (point)</LI>
48
 * </UL>
49
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
50
 */
51
public class IntersectionAlgorithm extends AbstractSextanteGeoProcess {
52

    
53
        public static final String  RESULT_POL      = "RESULT_POL";
54
        public static final String  RESULT_POINT    = "RESULT_POINT";
55
        public static final String  RESULT_LINE     = "RESULT_LINE";
56
        public static final String  LAYER           = "LAYER";
57
        public static final String  INTER           = "INTER";
58
        public static final String  CHECK           = "CHECK";
59
        
60
        /*
61
         * (non-Javadoc)
62
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
63
         */
64
        public void defineCharacteristics() {
65
        setName(getTranslation("Intersection"));
66
        setGroup(getTranslation("basic_vect_algorithms"));
67
        // setGeneratesUserDefinedRasterOutput(false);
68
                
69
                try {
70
                        m_Parameters.addInputVectorLayer(LAYER, 
71
                getTranslation("Input_layer"),
72
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
73
                                                                                                true);
74
                        m_Parameters.addInputVectorLayer(INTER, 
75
                getTranslation("Overlays_layer"),
76
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
77
                                                                                                true);
78
            m_Parameters.addBoolean(CHECK,
79
                getTranslation("Selected_geometries"), false);
80
                } catch (RepeatedParameterNameException e) {
81
                        Sextante.addErrorToLog(e);
82
                }
83
                addOutputVectorLayer(RESULT_POL, getTranslation("Intersection_polygon"),
84
                                                                OutputVectorLayer.SHAPE_TYPE_POLYGON);
85
                addOutputVectorLayer(RESULT_LINE, getTranslation("Intersection_line"),
86
                                                                OutputVectorLayer.SHAPE_TYPE_LINE);
87
                addOutputVectorLayer(RESULT_POINT, getTranslation("Intersection_point"),
88
                                                                OutputVectorLayer.SHAPE_TYPE_POINT);
89
        }
90
        
91
        /*
92
         * (non-Javadoc)
93
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
94
         */
95
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
96
                if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
97
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
98
            }
99
                if(existsOutPutFile(IntersectionAlgorithm.RESULT_POINT, 0)) {
100
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
101
            }
102
                if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
103
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
104
            }
105
                IVectorLayer inter = m_Parameters.getParameterValueAsVectorLayer(INTER);
106
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
107
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
108
                
109
                try {
110
                        computesIntersection(layer, inter, layer.getShapeType(), selectedGeom);
111
                } catch (DataException e) {
112
                        Sextante.addErrorToLog(e);
113
                        return false;
114
                }
115
                return true;
116
        }
117
        
118
        /**
119
         * Builds a layer with the intersection between the input layer and the templateGeometry
120
         * @param layer
121
         *        Input layer
122
         * @param templateGeometry
123
         * @param shapeType
124
         *        Output shape type
125
         * @param selectedGeom
126
         *        If it's true only selected geometries will be computed
127
         * @throws GeoAlgorithmExecutionException 
128
         */
129
        private void computesIntersection(IVectorLayer layer,
130
                                                                IVectorLayer overlay,
131
                                                                int shapeType, 
132
                                                                boolean selectedGeom) throws DataException, GeoAlgorithmExecutionException {
133
                FeatureStore storeLayer = null;
134
                FeatureStore storeOverlay = null;
135
                if(layer instanceof FlyrVectIVectorLayer && 
136
                        overlay instanceof FlyrVectIVectorLayer) {
137
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
138
                        storeOverlay = ((FlyrVectIVectorLayer)overlay).getFeatureStore();
139
                } else
140
                        return;
141

    
142
                FeatureSet features1 = null;
143
                features1 = storeLayer.getFeatureSet();
144
                FeatureType featureType1 = features1.getDefaultFeatureType();
145
                
146
                FeatureSet features2 = null;
147
                features2 = storeOverlay.getFeatureSet();
148
                FeatureType featureType2 = features2.getDefaultFeatureType();
149

    
150
                FeatureStore outFeatStorePol = null;
151
                FeatureStore outFeatStoreLine = null;
152
                FeatureStore outFeatStorePoint = null;
153
                
154
                IntersectionOperation operation = new IntersectionOperation(storeOverlay, this);
155
                operation.setTaskStatus(getStatus());
156
                 
157
                //La de puntos se genera siempre
158
                outFeatStorePoint =
159
                        buildOutPutStoreFromUnion(featureType1, featureType2,
160
                                        IVectorLayer.SHAPE_TYPE_POINT,
161
                                        getTranslation("Intersection_point"), RESULT_POINT);
162
                
163
                getStatus().setTitle("Point");
164
                operation.computesGeometryOperation(storeLayer, outFeatStorePoint, attrNames, selectedGeom, true);
165

    
166
                //La de pol?gonos solo si es intersecci?n entre pol?gonos
167
                if (isPolygon(storeLayer) && isPolygon(storeOverlay)) {
168
                        outFeatStorePol =
169
              buildOutPutStoreFromUnion(featureType1, featureType2,
170
                              IVectorLayer.SHAPE_TYPE_POLYGON, getTranslation("Intersection_polygon"), RESULT_POL);
171
                        getStatus().setTitle("Polygon");
172
                        operation.computesGeometryOperation(storeLayer, outFeatStorePol, attrNames, selectedGeom, true);
173
                }
174
                
175
                //La capa de l?neas se genera cuando ning?na de las dos es de puntos
176
                if (!isPoint(storeLayer) && !isPoint(storeOverlay)) {
177
                        outFeatStoreLine =
178
              buildOutPutStoreFromUnion(featureType1, featureType2,
179
                              IVectorLayer.SHAPE_TYPE_LINE, getTranslation("Intersection_line"), RESULT_LINE);
180
                        getStatus().setTitle("Line");
181
                        operation.computesGeometryOperation(storeLayer, outFeatStoreLine, attrNames, selectedGeom, true);
182
                }
183

    
184
                if(outFeatStorePol == null) {
185
                        getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"),
186
                    OutputVectorLayer.SHAPE_TYPE_POLYGON, new Class[]{Integer.class}, new String[]{""});
187
                }
188
                
189
                if(outFeatStoreLine == null) {
190
                        getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"),
191
                    OutputVectorLayer.SHAPE_TYPE_LINE, new Class[]{Integer.class}, new String[]{""});
192
                }
193
                
194
                if(outFeatStorePoint == null) {
195
                        getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"),
196
                    OutputVectorLayer.SHAPE_TYPE_POINT, new Class[]{Integer.class}, new String[]{""});
197
                }
198
        }
199
        
200
}