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

History | View | Annotate | Download (11.4 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

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

    
54
        public static final String  RESULT1       = "RESULT1";
55
        public static final String  RESULT2       = "RESULT2";
56
        public static final String  LAYER1        = "LAYER1";
57
        public static final String  LAYER2        = "LAYER2";
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("Union"));
66
        setGroup(getTranslation("basic_vect_algorithms"));
67
        // setGeneratesUserDefinedRasterOutput(false);
68
                
69
                try {
70
                        m_Parameters.addInputVectorLayer(LAYER1, 
71
                getTranslation("Input_layer") + " 1",
72
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
73
                                                                                                true);
74
                        m_Parameters.addInputVectorLayer(LAYER2, 
75
                getTranslation("Input_layer") + " 2",
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(RESULT1, getTranslation("Union_l1"),
84
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
85
                addOutputVectorLayer(RESULT2, getTranslation("Union_l2"),
86
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
87
        }
88
        
89
        /*
90
         * (non-Javadoc)
91
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
92
         */
93
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
94
                if(existsOutPutFile(UnionAlgorithm.RESULT1, 0)) {
95
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
96
            }
97
                if(existsOutPutFile(UnionAlgorithm.RESULT2, 0)) {
98
                    throw new GeoAlgorithmExecutionException(getTranslation("file_exists"));
99
            }
100
                
101
                IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1);
102
                IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2);
103
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
104
                
105
                FeatureStore storeLayer1 = null;
106
                FeatureStore storeLayer2 = null;
107
                if(layer1 instanceof FlyrVectIVectorLayer && 
108
                        layer2 instanceof FlyrVectIVectorLayer) {
109
                        storeLayer1 = ((FlyrVectIVectorLayer)layer1).getFeatureStore();
110
                        storeLayer2 = ((FlyrVectIVectorLayer)layer2).getFeatureStore();
111
                } else
112
                        return false;
113

    
114
                try {
115
                        FeatureSet features1 = null;
116
                        features1 = storeLayer1.getFeatureSet();
117
                        FeatureType featureType1 = features1.getDefaultFeatureType();
118

    
119
                        FeatureSet features2 = null;
120
                        features2 = storeLayer2.getFeatureSet();
121
                        FeatureType featureType2 = features2.getDefaultFeatureType();
122

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