Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.union / src / main / java / org / gvsig / sextante / app / algorithm / union / UnionAlgorithm.java @ 172

History | View | Annotate | Download (10.3 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.sextante.app.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.core.gvGeoAlgorithm;
39
import org.gvsig.geoprocess.core.gvVectorLayer;
40
import org.gvsig.sextante.app.algorithm.base.core.GeometryOperation;
41
import org.gvsig.sextante.app.algorithm.base.core.ScalableUnionOperation;
42
import org.gvsig.sextante.app.algorithm.difference.DifferenceOperation;
43
import org.gvsig.sextante.app.algorithm.intersection.IntersectionOperation;
44

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

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

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

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