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

History | View | Annotate | Download (10.4 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
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.gvVectorLayer;
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(Sextante.getText("Union"));
63
                setGroup(Sextante.getText("gvSIG_Algorithms"));
64
        // setGeneratesUserDefinedRasterOutput(false);
65
                
66
                try {
67
                        m_Parameters.addInputVectorLayer(LAYER1, 
68
                                                                                                Sextante.getText("Input_layer") + " 1", 
69
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
70
                                                                                                true);
71
                        m_Parameters.addInputVectorLayer(LAYER2, 
72
                                                                                                Sextante.getText("Input_layer") + " 2", 
73
                                                                                                IVectorLayer.SHAPE_TYPE_WRONG, 
74
                                                                                                true);
75
                        m_Parameters.addBoolean(CHECK, Sextante.getText("Selected_geometries"), false);
76
                } catch (RepeatedParameterNameException e) {
77
                        Sextante.addErrorToLog(e);
78
                }
79
                addOutputVectorLayer(RESULT1,
80
                                                                Sextante.getText("Union_l1"),
81
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
82
                addOutputVectorLayer(RESULT2,
83
                                                                Sextante.getText( "Union_l2"),
84
                                                                OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
85
        }
86
        
87
        /*
88
         * (non-Javadoc)
89
         * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
90
         */
91
        public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
92
                IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1);
93
                IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2);
94
                boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
95
                
96
                FeatureStore storeLayer1 = null;
97
                FeatureStore storeLayer2 = null;
98
                if(layer1 instanceof gvVectorLayer && 
99
                        layer2 instanceof gvVectorLayer) {
100
                        storeLayer1 = ((gvVectorLayer)layer1).getFeatureStore();
101
                        storeLayer2 = ((gvVectorLayer)layer2).getFeatureStore();
102
                } else
103
                        return false;
104

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

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

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