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

History | View | Annotate | Download (9.75 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
 *
52
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
53
 */
54
public class IntersectionAlgorithm extends AbstractSextanteGeoProcess {
55

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

    
64

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

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

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

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

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

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

    
162
        FeatureType featureType1 = storeLayer.getDefaultFeatureType();
163
        FeatureType featureType2 = storeOverlay.getDefaultFeatureType();
164

    
165
        FeatureStore outFeatStorePol = null;
166
        FeatureStore outFeatStoreLine = null;
167
        FeatureStore outFeatStorePoint = null;
168

    
169
        IntersectionOperation operation = new IntersectionOperation(storeOverlay, this);
170
        operation.setTaskStatus(getStatus());
171
        this.namesTranslator = NamesTranslator.createTrimTranslator(11);
172

    
173
        //La de puntos se genera siempre
174
        outFeatStorePoint
175
                = buildOutPutStoreFromUnion(featureType1, featureType2,
176
                        IVectorLayer.SHAPE_TYPE_POINT,
177
                        getTranslation("Intersection_point"), RESULT_POINT);
178

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

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

    
210
        if (outFeatStorePol == null) {
211
            getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"),
212
                    OutputVectorLayer.SHAPE_TYPE_POLYGON, new Class[]{Integer.class}, new String[]{""});
213
        }
214

    
215
        if (outFeatStoreLine == null) {
216
            getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"),
217
                    OutputVectorLayer.SHAPE_TYPE_LINE, new Class[]{Integer.class}, new String[]{""});
218
        }
219

    
220
        if (outFeatStorePoint == null) {
221
            getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"),
222
                    OutputVectorLayer.SHAPE_TYPE_POINT, new Class[]{Integer.class}, new String[]{""});
223
        }
224

    
225
        return operation.getErrorInfo();
226
    }
227

    
228
}