Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.union / src / main / java / org / gvsig / geoprocess / algorithm / union / UnionAlgorithm.java @ 1259

History | View | Annotate | Download (12.6 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.union;
25

    
26
import org.gvsig.fmap.dal.exception.DataException;
27
import org.gvsig.fmap.dal.exception.ReadException;
28
import org.gvsig.fmap.dal.feature.FeatureSet;
29
import org.gvsig.fmap.dal.feature.FeatureStore;
30
import org.gvsig.fmap.dal.feature.FeatureType;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
33
import org.gvsig.geoprocess.algorithm.base.core.ScalableUnionOperation;
34
import org.gvsig.geoprocess.algorithm.difference.DifferenceOperation;
35
import org.gvsig.geoprocess.algorithm.intersection.IntersectionOperation;
36
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
37
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
38

    
39
import es.unex.sextante.core.Sextante;
40
import es.unex.sextante.dataObjects.IVectorLayer;
41
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
42
import es.unex.sextante.exceptions.NullParameterValueException;
43
import es.unex.sextante.exceptions.RepeatedParameterNameException;
44
import es.unex.sextante.exceptions.WrongParameterIDException;
45
import es.unex.sextante.exceptions.WrongParameterTypeException;
46
import es.unex.sextante.outputs.OutputVectorLayer;
47
import org.gvsig.tools.namestranslator.NamesTranslator;
48

    
49
/**
50
 * Union algorithm
51
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52
 */
53
public class UnionAlgorithm extends AbstractSextanteGeoProcess {
54

    
55
        public static final String  RESULT1               = "RESULT1";
56
        public static final String  RESULT2               = "RESULT2";
57
        public static final String  LAYER1                = "LAYER1";
58
        public static final String  LAYER2                = "LAYER2";
59
        public static final String  SELECTGEOM_INPUT      = "SELECTGEOM_INPUT";
60
        public static final String  SELECTGEOM_OVERLAY    = "SELECTGEOM_OVERLAY";
61
    private NamesTranslator nameTranslator;
62
        
63
        /*
64
         * (non-Javadoc)
65
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
66
         */
67
        public void defineCharacteristics() {
68
        setName(getTranslation("Union"));
69
        setGroup(getTranslation("basic_vect_algorithms"));
70
        // setGeneratesUserDefinedRasterOutput(false);
71
                
72
                try {
73
                        m_Parameters.addInputVectorLayer(LAYER1, 
74
                getTranslation("Input_layer") + " 1",
75
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
76
                                                                                                true);
77
                        m_Parameters.addInputVectorLayer(LAYER2, 
78
                getTranslation("Input_layer") + " 2",
79
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
80
                                                                                                true);
81
            m_Parameters.addBoolean(SELECTGEOM_INPUT, 
82
                            getTranslation("Selected_geometries_input_layer_union"), false);
83
            m_Parameters.addBoolean(SELECTGEOM_OVERLAY, 
84
                            getTranslation("Selected_geometries_overlay_layer_union"), false);
85
                } catch (RepeatedParameterNameException e) {
86
                        Sextante.addErrorToLog(e);
87
                }
88
                addOutputVectorLayer(RESULT1, getTranslation("Union_l1"),
89
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
90
                addOutputVectorLayer(RESULT2, getTranslation("Union_l2"),
91
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
92
        }
93
        
94
        /*
95
         * (non-Javadoc)
96
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
97
         */
98
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
99
                if(existsOutPutFile(UnionAlgorithm.RESULT1, 0)) {
100
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
101
            }
102
                if(existsOutPutFile(UnionAlgorithm.RESULT2, 0)) {
103
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
104
            }
105
                
106
                IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1);
107
                IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2);
108
                boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
109
                boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
110
                this.nameTranslator = NamesTranslator.createTrimTranslator(10);
111
                
112
                FeatureStore storeLayer1 = null;
113
                FeatureStore storeLayer2 = null;
114
                if(layer1 instanceof FlyrVectIVectorLayer && 
115
                        layer2 instanceof FlyrVectIVectorLayer) {
116
                        storeLayer1 = ((FlyrVectIVectorLayer)layer1).getFeatureStore();
117
                        storeLayer2 = ((FlyrVectIVectorLayer)layer2).getFeatureStore();
118
                } else
119
                        return false;
120

    
121
                try {
122
                        FeatureType featureType1 = storeLayer1.getDefaultFeatureType();
123

    
124
                        FeatureSet features2 = null;
125
                        features2 = storeLayer2.getFeatureSet();
126
                        FeatureType featureType2 = features2.getDefaultFeatureType();
127

    
128
                        FeatureStore featStoreOut1 = null;
129
                        FeatureStore featStoreOut2 = null;
130
                        
131
                        //POL-POL
132
                        if(isPolygon(storeLayer1) && isPolygon(storeLayer2)) { 
133
                                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
134
                                                featureType2, 
135
                                                layer1.getShapeType(), 
136
                                                getTranslation("Union_polygon"),
137
                        RESULT1, nameTranslator);
138
                                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
139
                        } else
140
                        //PTO-PTO
141
                        if(isPoint(storeLayer1) && isPoint(storeLayer2)) { 
142
                                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
143
                                                featureType2, 
144
                                                layer1.getShapeType(), 
145
                                                getTranslation("Union_point"),
146
                        RESULT1, nameTranslator);
147
                                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
148
                        } else
149
                        //LIN-LIN        
150
                        if(isLine(storeLayer1) && isLine(storeLayer2)) {
151
                                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
152
                                                featureType2, 
153
                                                IVectorLayer.SHAPE_TYPE_POINT, 
154
                        getTranslation("Union_point"), 
155
                        RESULT1, nameTranslator);
156
                                computesIntersection(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay, true);
157
                                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, 
158
                                                featureType2, 
159
                                                IVectorLayer.SHAPE_TYPE_LINE, 
160
                        getTranslation("Union_line"), 
161
                        RESULT2, nameTranslator);
162
                                computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
163
                                computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
164
                        } else
165
                        //POL-LIN
166
                        if( (isPolygon(storeLayer1) && isLine(storeLayer2)) || 
167
                                (isLine(storeLayer1) && isPolygon(storeLayer2))) { 
168
                                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, 
169
                                                featureType2, 
170
                                                IVectorLayer.SHAPE_TYPE_LINE, 
171
                        getTranslation("Union_line"), RESULT1, nameTranslator);
172
                                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
173
                                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, 
174
                                                featureType2, 
175
                                                IVectorLayer.SHAPE_TYPE_POLYGON, 
176
                        getTranslation("Union_polygon"), 
177
                        RESULT2, nameTranslator);
178
                                computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
179
                                computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
180
                        } else
181
                        //POL-PTO
182
                        if( (isPolygon(storeLayer1) && isPoint(storeLayer2)) || 
183
                                (isPoint(storeLayer1) && isPolygon(storeLayer2))) { 
184
                                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, 
185
                        getTranslation("Union_point"), RESULT1, nameTranslator);
186
                                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
187
                                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POLYGON, 
188
                        getTranslation("Union_polygon"), RESULT2, nameTranslator);
189
                                computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay);
190
                        } else
191
                        //PTO-LIN
192
                        if( (isPoint(storeLayer1) && isLine(storeLayer2)) || 
193
                                (isLine(storeLayer1) && isPoint(storeLayer2))) { 
194
                                featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, 
195
                        getTranslation("Union_point"), RESULT1, nameTranslator);
196
                                computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay);
197
                                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_LINE, 
198
                        getTranslation("Union_line"), RESULT2, nameTranslator);
199
                                computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay);
200
                        }
201
                        
202
                        if(featStoreOut2 == null) {
203
                                featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_WRONG, 
204
                        getTranslation("Null_Layer"), RESULT2, nameTranslator);
205
                        }
206
                        
207
                } catch (ReadException e) {
208
                        throw new GeoAlgorithmExecutionException(e.getMessage());
209
                } catch (DataException e) {
210
                        throw new GeoAlgorithmExecutionException(e.getMessage());
211
                }
212
                
213
                if(getTaskMonitor().isCanceled())
214
                        return false;
215
                
216
                return true;
217
        }
218
        
219
        /**
220
         * Computes union
221
         * @param storeLayer1
222
         * @param storeLayer2
223
         * @param featStoreOut
224
         * @param selectedGeom
225
         * @throws DataException
226
         * @throws WrongParameterTypeException
227
         * @throws WrongParameterIDException
228
         * @throws NullParameterValueException
229
         */
230
        private void computesUnion(FeatureStore storeLayer1, FeatureStore storeLayer2, 
231
                        FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
232
                computesIntersection(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
233
                computesDifference(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
234
                computesDifference(storeLayer2, storeLayer1, featStoreOut, selectedGeomInput, selectGeomOverlay, true);
235
        }
236
        
237
        /**
238
         * Computes intersection
239
         * @param storeLayer1
240
         * @param storeLayer2
241
         * @param featStoreOut
242
         * @param selectedGeom
243
         * @param close
244
         * @throws DataException
245
         * @throws WrongParameterTypeException
246
         * @throws WrongParameterIDException
247
         * @throws NullParameterValueException
248
         */
249
        private void computesIntersection(FeatureStore storeLayer1, FeatureStore storeLayer2, 
250
                        FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {
251
                GeometryOperation intersection = new IntersectionOperation(storeLayer2, this);
252
        intersection.setTaskStatus(getStatus());
253
                intersection.computesGeometryOperation(storeLayer1, featStoreOut, attrNames, 
254
                                selectedGeomInput, selectGeomOverlay, close);
255
        }
256
        
257
        /**
258
         * Computes difference
259
         * @param storeLayer1
260
         * @param storeLayer2
261
         * @param featStoreOut
262
         * @param selectedGeom
263
         * @param close
264
         * @throws DataException
265
         * @throws WrongParameterTypeException
266
         * @throws WrongParameterIDException
267
         * @throws NullParameterValueException
268
         */
269
        private void computesDifference(FeatureStore storeLayer1, FeatureStore storeLayer2, 
270
                        FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException {                
271
                Geometry unionGeom1 = ScalableUnionOperation.joinLayerGeometries(storeLayer2, selectGeomOverlay);
272
                GeometryOperation differenceL1_L2 = new DifferenceOperation(unionGeom1, this);
273
        differenceL1_L2.setTaskStatus(getStatus());
274
                differenceL1_L2.computesGeometryOperation(storeLayer1, featStoreOut, attrNames, 
275
                                selectedGeomInput, selectGeomOverlay, close);
276
        }
277
        
278
}