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

History | View | Annotate | Download (9.16 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
import org.gvsig.tools.namestranslator.NamesTranslator;
40

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

    
55
        public static final String  RESULT_POL            = "RESULT_POL";
56
        public static final String  RESULT_POINT          = "RESULT_POINT";
57
        public static final String  RESULT_LINE           = "RESULT_LINE";
58
        public static final String  LAYER                 = "LAYER";
59
        public static final String  INTER                 = "INTER";
60
        public static final String  SELECTGEOM_INPUT      = "SELECTGEOM_INPUT";
61
        public static final String  SELECTGEOM_OVERLAY    = "SELECTGEOM_OVERLAY";
62
    private NamesTranslator nameTranslator;
63

    
64
        /*
65
         * (non-Javadoc)
66
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
67
         */
68
        public void defineCharacteristics() {
69
        setName(getTranslation("Intersection"));
70
        setGroup(getTranslation("basic_vect_algorithms"));
71
        // setGeneratesUserDefinedRasterOutput(false);
72

    
73
                try {
74
                        m_Parameters.addInputVectorLayer(LAYER,
75
                getTranslation("Input_layer"),
76
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG,
77
                                                                                                true);
78
                        m_Parameters.addInputVectorLayer(INTER,
79
                getTranslation("Overlays_layer"),
80
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG,
81
                                                                                                true);
82
            m_Parameters.addBoolean(SELECTGEOM_INPUT,
83
                            getTranslation("Selected_geometries_input_layer_inters"), false);
84
            m_Parameters.addBoolean(SELECTGEOM_OVERLAY,
85
                            getTranslation("Selected_geometries_overlay_layer_inters"), false);
86
                } catch (RepeatedParameterNameException e) {
87
                        Sextante.addErrorToLog(e);
88
                }
89
                addOutputVectorLayer(RESULT_POL, getTranslation("Intersection_polygon"),
90
                                                                OutputVectorLayer.SHAPE_TYPE_POLYGON);
91
                addOutputVectorLayer(RESULT_LINE, getTranslation("Intersection_line"),
92
                                                                OutputVectorLayer.SHAPE_TYPE_LINE);
93
                addOutputVectorLayer(RESULT_POINT, getTranslation("Intersection_point"),
94
                                                                OutputVectorLayer.SHAPE_TYPE_POINT);
95
        }
96

    
97
        /*
98
         * (non-Javadoc)
99
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
100
         */
101
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
102
                if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
103
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
104
            }
105
                if(existsOutPutFile(IntersectionAlgorithm.RESULT_POINT, 0)) {
106
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
107
            }
108
                if(existsOutPutFile(IntersectionAlgorithm.RESULT_LINE, 0)) {
109
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
110
            }
111
                IVectorLayer inter = m_Parameters.getParameterValueAsVectorLayer(INTER);
112
                IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
113
                boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
114
                boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
115
                boolean error = false;
116

    
117
                try {
118
                        error = computesIntersection(layer, inter, layer.getShapeType(), selectedGeomInput, selectedGeomOverlay);
119
                } catch (DataException e) {
120
                        Sextante.addErrorToLog(e);
121
                        return false;
122
                }
123

    
124
                if(getTaskMonitor().isCanceled())
125
                        return false;
126
                if(error)
127
                        JOptionPane.showMessageDialog(null,
128
                        getTranslation("problems_with_some_geometries"), "Error",
129
                        JOptionPane.WARNING_MESSAGE);
130
                return true;
131
        }
132

    
133
        /**
134
         * Builds a layer with the intersection between the input layer and the templateGeometry
135
         * @param layer
136
         *        Input layer
137
         * @param templateGeometry
138
         * @param shapeType
139
         *        Output shape type
140
         * @param selectedGeom
141
         *        If it's true only selected geometries will be computed
142
         * @throws GeoAlgorithmExecutionException
143
         */
144
        private boolean computesIntersection(IVectorLayer layer,
145
                                                                IVectorLayer overlay,
146
                                                                int shapeType,
147
                                                                boolean selectedGeomInput,
148
                                                                boolean selectedGeomOverlay) throws DataException, GeoAlgorithmExecutionException {
149
                FeatureStore storeLayer = null;
150
                FeatureStore storeOverlay = null;
151
                if(layer instanceof FlyrVectIVectorLayer &&
152
                        overlay instanceof FlyrVectIVectorLayer) {
153
                        storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore();
154
                        storeOverlay = ((FlyrVectIVectorLayer)overlay).getFeatureStore();
155
                } else
156
                        return false;
157

    
158
                FeatureType featureType1 = storeLayer.getDefaultFeatureType();
159
                FeatureType featureType2 = storeOverlay.getDefaultFeatureType();
160

    
161
                FeatureStore outFeatStorePol = null;
162
                FeatureStore outFeatStoreLine = null;
163
                FeatureStore outFeatStorePoint = null;
164

    
165
                IntersectionOperation operation = new IntersectionOperation(storeOverlay, this);
166
                operation.setTaskStatus(getStatus());
167
                this.nameTranslator = NamesTranslator.createTrimTranslator(10);
168

    
169
                //La de puntos se genera siempre
170
                outFeatStorePoint =
171
                        buildOutPutStoreFromUnion(featureType1, featureType2,
172
                                        IVectorLayer.SHAPE_TYPE_POINT,
173
                                        getTranslation("Intersection_point"), RESULT_POINT,nameTranslator);
174

    
175
        if (outFeatStorePoint != null) {
176
            getStatus().setTitle("Point");
177
            operation.computesGeometryOperation(storeLayer, outFeatStorePoint,
178
                attrNames, selectedGeomInput, selectedGeomOverlay, true);
179
        }
180
                //La de pol?gonos solo si es intersecci?n entre pol?gonos
181
                if (isPolygon(storeLayer) && isPolygon(storeOverlay)) {
182
                        outFeatStorePol =
183
              buildOutPutStoreFromUnion(featureType1, featureType2,
184
                              IVectorLayer.SHAPE_TYPE_POLYGON, getTranslation("Intersection_polygon"), RESULT_POL, nameTranslator);
185
            if (outFeatStorePol != null) {
186
                getStatus().setTitle("Polygon");
187
                operation.computesGeometryOperation(storeLayer,
188
                    outFeatStorePol, attrNames, selectedGeomInput,
189
                    selectedGeomOverlay, true);
190
            }
191
                }
192

    
193
                //La capa de l?neas se genera cuando ning?na de las dos es de puntos
194
                if (!isPoint(storeLayer) && !isPoint(storeOverlay)) {
195
                        outFeatStoreLine =
196
              buildOutPutStoreFromUnion(featureType1, featureType2,
197
                              IVectorLayer.SHAPE_TYPE_LINE, getTranslation("Intersection_line"), RESULT_LINE, nameTranslator);
198
            if (outFeatStoreLine != null) {
199
                getStatus().setTitle("Line");
200
                operation.computesGeometryOperation(storeLayer,
201
                    outFeatStoreLine, attrNames, selectedGeomInput,
202
                    selectedGeomOverlay, true);
203
            }
204
                }
205

    
206
                if(outFeatStorePol == null) {
207
                        getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"),
208
                    OutputVectorLayer.SHAPE_TYPE_POLYGON, new Class[]{Integer.class}, new String[]{""});
209
                }
210

    
211
                if(outFeatStoreLine == null) {
212
                        getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"),
213
                    OutputVectorLayer.SHAPE_TYPE_LINE, new Class[]{Integer.class}, new String[]{""});
214
                }
215

    
216
                if(outFeatStorePoint == null) {
217
                        getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"),
218
                    OutputVectorLayer.SHAPE_TYPE_POINT, new Class[]{Integer.class}, new String[]{""});
219
                }
220

    
221
                return operation.getErrorInfo();
222
        }
223

    
224
}