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

History | View | Annotate | Download (8.65 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 javax.swing.JOptionPane;
27

    
28
import org.gvsig.fmap.dal.exception.DataException;
29
import org.gvsig.fmap.dal.feature.FeatureStore;
30
import org.gvsig.fmap.dal.feature.FeatureType;
31
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
32
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
33

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

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

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

    
156
                FeatureType featureType1 = storeLayer.getDefaultFeatureType();
157
                FeatureType featureType2 = storeOverlay.getDefaultFeatureType();
158

    
159
                FeatureStore outFeatStorePol = null;
160
                FeatureStore outFeatStoreLine = null;
161
                FeatureStore outFeatStorePoint = null;
162
                
163
                IntersectionOperation operation = new IntersectionOperation(storeOverlay, this);
164
                operation.setTaskStatus(getStatus());
165
                 
166
                //La de puntos se genera siempre
167
                outFeatStorePoint =
168
                        buildOutPutStoreFromUnion(featureType1, featureType2,
169
                                        IVectorLayer.SHAPE_TYPE_POINT,
170
                                        getTranslation("Intersection_point"), RESULT_POINT);
171
                
172
                getStatus().setTitle("Point");
173
                operation.computesGeometryOperation(storeLayer, outFeatStorePoint, attrNames, 
174
                                selectedGeomInput, selectedGeomOverlay, true);
175

    
176
                //La de pol?gonos solo si es intersecci?n entre pol?gonos
177
                if (isPolygon(storeLayer) && isPolygon(storeOverlay)) {
178
                        outFeatStorePol =
179
              buildOutPutStoreFromUnion(featureType1, featureType2,
180
                              IVectorLayer.SHAPE_TYPE_POLYGON, getTranslation("Intersection_polygon"), RESULT_POL);
181
                        getStatus().setTitle("Polygon");
182
                        operation.computesGeometryOperation(storeLayer, outFeatStorePol, attrNames, 
183
                                        selectedGeomInput, selectedGeomOverlay, true);
184
                }
185
                
186
                //La capa de l?neas se genera cuando ning?na de las dos es de puntos
187
                if (!isPoint(storeLayer) && !isPoint(storeOverlay)) {
188
                        outFeatStoreLine =
189
              buildOutPutStoreFromUnion(featureType1, featureType2,
190
                              IVectorLayer.SHAPE_TYPE_LINE, getTranslation("Intersection_line"), RESULT_LINE);
191
                        getStatus().setTitle("Line");
192
                        operation.computesGeometryOperation(storeLayer, outFeatStoreLine, attrNames, 
193
                                        selectedGeomInput, selectedGeomOverlay, true);
194
                }
195

    
196
                if(outFeatStorePol == null) {
197
                        getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"),
198
                    OutputVectorLayer.SHAPE_TYPE_POLYGON, new Class[]{Integer.class}, new String[]{""});
199
                }
200
                
201
                if(outFeatStoreLine == null) {
202
                        getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"),
203
                    OutputVectorLayer.SHAPE_TYPE_LINE, new Class[]{Integer.class}, new String[]{""});
204
                }
205
                
206
                if(outFeatStorePoint == null) {
207
                        getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"),
208
                    OutputVectorLayer.SHAPE_TYPE_POINT, new Class[]{Integer.class}, new String[]{""});
209
                }
210
                
211
                return operation.getErrorInfo();
212
        }
213
        
214
}