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

History | View | Annotate | Download (10.5 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
package org.gvsig.geoprocess.algorithm.union;
22

    
23
import es.unex.sextante.core.Sextante;
24
import es.unex.sextante.dataObjects.IVectorLayer;
25
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
26
import es.unex.sextante.exceptions.NullParameterValueException;
27
import es.unex.sextante.exceptions.RepeatedParameterNameException;
28
import es.unex.sextante.exceptions.WrongParameterIDException;
29
import es.unex.sextante.exceptions.WrongParameterTypeException;
30
import es.unex.sextante.outputs.OutputVectorLayer;
31

    
32
import org.gvsig.fmap.dal.exception.DataException;
33
import org.gvsig.fmap.dal.exception.ReadException;
34
import org.gvsig.fmap.dal.feature.FeatureSet;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.dal.feature.FeatureType;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
39
import org.gvsig.geoprocess.algorithm.base.core.ScalableUnionOperation;
40
import org.gvsig.geoprocess.algorithm.difference.DifferenceOperation;
41
import org.gvsig.geoprocess.algorithm.intersection.IntersectionOperation;
42
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
43
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
44

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

    
51
        public static final String  RESULT1       = "RESULT1";
52
        public static final String  RESULT2       = "RESULT2";
53
        public static final String  LAYER1        = "LAYER1";
54
        public static final String  LAYER2        = "LAYER2";
55
        public static final String  CHECK         = "CHECK";
56
        
57
        /*
58
         * (non-Javadoc)
59
         * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
60
         */
61
        public void defineCharacteristics() {
62
        setName(getTranslation("Union"));
63
        setGroup(getTranslation("basic_vect_algorithms"));
64
        // setGeneratesUserDefinedRasterOutput(false);
65
                
66
                try {
67
                        m_Parameters.addInputVectorLayer(LAYER1, 
68
                getTranslation("Input_layer") + " 1",
69
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
70
                                                                                                true);
71
                        m_Parameters.addInputVectorLayer(LAYER2, 
72
                getTranslation("Input_layer") + " 2",
73
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
74
                                                                                                true);
75
            m_Parameters.addBoolean(CHECK,
76
                getTranslation("Selected_geometries"), false);
77
                } catch (RepeatedParameterNameException e) {
78
                        Sextante.addErrorToLog(e);
79
                }
80
                addOutputVectorLayer(RESULT1,
81
 getTranslation("Union_l1"),
82
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
83
                addOutputVectorLayer(RESULT2,
84
 getTranslation("Union_l2"),
85
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
86
        }
87
        
88
        /*
89
         * (non-Javadoc)
90
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
91
         */
92
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
93
                IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1);
94
                IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2);
95
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
96
                
97
                FeatureStore storeLayer1 = null;
98
                FeatureStore storeLayer2 = null;
99
                if(layer1 instanceof FlyrVectIVectorLayer && 
100
                        layer2 instanceof FlyrVectIVectorLayer) {
101
                        storeLayer1 = ((FlyrVectIVectorLayer)layer1).getFeatureStore();
102
                        storeLayer2 = ((FlyrVectIVectorLayer)layer2).getFeatureStore();
103
                } else
104
                        return false;
105

    
106
                try {
107
                        FeatureSet features1 = null;
108
                        features1 = storeLayer1.getFeatureSet();
109
                        FeatureType featureType1 = features1.getDefaultFeatureType();
110

    
111
                        FeatureSet features2 = null;
112
                        features2 = storeLayer2.getFeatureSet();
113
                        FeatureType featureType2 = features2.getDefaultFeatureType();
114

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