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 @ 1293
History | View | Annotate | Download (13.7 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 |
*
|
52 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
53 |
*/
|
54 |
public class UnionAlgorithm extends AbstractSextanteGeoProcess { |
55 |
|
56 |
public static final String RESULT1 = "RESULT1"; |
57 |
public static final String RESULT2 = "RESULT2"; |
58 |
public static final String LAYER1 = "LAYER1"; |
59 |
public static final String LAYER2 = "LAYER2"; |
60 |
public static final String SELECTGEOM_INPUT = "SELECTGEOM_INPUT"; |
61 |
public static final String SELECTGEOM_OVERLAY = "SELECTGEOM_OVERLAY"; |
62 |
private NamesTranslator nameTranslator;
|
63 |
|
64 |
/*
|
65 |
* (non-Javadoc)
|
66 |
* @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
67 |
*/
|
68 |
public void defineCharacteristics() { |
69 |
setName(getTranslation("Union"));
|
70 |
setGroup(getTranslation("basic_vect_algorithms"));
|
71 |
// setGeneratesUserDefinedRasterOutput(false);
|
72 |
|
73 |
try {
|
74 |
m_Parameters.addInputVectorLayer(LAYER1, |
75 |
getTranslation("Input_layer") + " 1", |
76 |
IVectorLayer.SHAPE_TYPE_WRONG, |
77 |
true);
|
78 |
m_Parameters.addInputVectorLayer(LAYER2, |
79 |
getTranslation("Input_layer") + " 2", |
80 |
IVectorLayer.SHAPE_TYPE_WRONG, |
81 |
true);
|
82 |
m_Parameters.addBoolean(SELECTGEOM_INPUT, |
83 |
getTranslation("Selected_geometries_input_layer_union"), false); |
84 |
m_Parameters.addBoolean(SELECTGEOM_OVERLAY, |
85 |
getTranslation("Selected_geometries_overlay_layer_union"), false); |
86 |
} catch (RepeatedParameterNameException e) {
|
87 |
Sextante.addErrorToLog(e); |
88 |
} |
89 |
addOutputVectorLayer(RESULT1, getTranslation("Union_l1"),
|
90 |
OutputVectorLayer.SHAPE_TYPE_UNDEFINED); |
91 |
addOutputVectorLayer(RESULT2, getTranslation("Union_l2"),
|
92 |
OutputVectorLayer.SHAPE_TYPE_UNDEFINED); |
93 |
} |
94 |
|
95 |
/*
|
96 |
* (non-Javadoc)
|
97 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
98 |
*/
|
99 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
100 |
if (existsOutPutFile(UnionAlgorithm.RESULT1, 0)) { |
101 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
102 |
} |
103 |
if (existsOutPutFile(UnionAlgorithm.RESULT2, 0)) { |
104 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
105 |
} |
106 |
|
107 |
IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1); |
108 |
IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2); |
109 |
boolean selectedGeomInput = m_Parameters.getParameter(SELECTGEOM_INPUT).getParameterValueAsBoolean();
|
110 |
boolean selectedGeomOverlay = m_Parameters.getParameter(SELECTGEOM_OVERLAY).getParameterValueAsBoolean();
|
111 |
this.nameTranslator = NamesTranslator.createTrimTranslator(11); |
112 |
|
113 |
FeatureStore storeLayer1 = null;
|
114 |
FeatureStore storeLayer2 = null;
|
115 |
if (layer1 instanceof FlyrVectIVectorLayer |
116 |
&& layer2 instanceof FlyrVectIVectorLayer) {
|
117 |
storeLayer1 = ((FlyrVectIVectorLayer) layer1).getFeatureStore(); |
118 |
storeLayer2 = ((FlyrVectIVectorLayer) layer2).getFeatureStore(); |
119 |
} else {
|
120 |
return false; |
121 |
} |
122 |
|
123 |
try {
|
124 |
FeatureType featureType1 = storeLayer1.getDefaultFeatureType(); |
125 |
|
126 |
FeatureSet features2 = null;
|
127 |
features2 = storeLayer2.getFeatureSet(); |
128 |
FeatureType featureType2 = features2.getDefaultFeatureType(); |
129 |
|
130 |
FeatureStore featStoreOut1 = null;
|
131 |
FeatureStore featStoreOut2 = null;
|
132 |
|
133 |
//POL-POL
|
134 |
if (isPolygon(storeLayer1) && isPolygon(storeLayer2)) {
|
135 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, |
136 |
featureType2, |
137 |
layer1.getShapeType(), |
138 |
getTranslation("Union_polygon"),
|
139 |
RESULT1); |
140 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay); |
141 |
} else //PTO-PTO |
142 |
if (isPoint(storeLayer1) && isPoint(storeLayer2)) {
|
143 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, |
144 |
featureType2, |
145 |
layer1.getShapeType(), |
146 |
getTranslation("Union_point"),
|
147 |
RESULT1); |
148 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay); |
149 |
} else //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); |
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); |
162 |
computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
|
163 |
computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
|
164 |
} else //POL-LIN |
165 |
if ((isPolygon(storeLayer1) && isLine(storeLayer2))
|
166 |
|| (isLine(storeLayer1) && isPolygon(storeLayer2))) { |
167 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, |
168 |
featureType2, |
169 |
IVectorLayer.SHAPE_TYPE_LINE, |
170 |
getTranslation("Union_line"), RESULT1);
|
171 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay); |
172 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, |
173 |
featureType2, |
174 |
IVectorLayer.SHAPE_TYPE_POLYGON, |
175 |
getTranslation("Union_polygon"),
|
176 |
RESULT2); |
177 |
computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay, false);
|
178 |
computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeomInput, selectedGeomOverlay, true);
|
179 |
} else //POL-PTO |
180 |
if ((isPolygon(storeLayer1) && isPoint(storeLayer2))
|
181 |
|| (isPoint(storeLayer1) && isPolygon(storeLayer2))) { |
182 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, |
183 |
getTranslation("Union_point"), RESULT1);
|
184 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay); |
185 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_MULTIPOLYGON, |
186 |
getTranslation("Union_polygon"), RESULT2);
|
187 |
computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay); |
188 |
} else //PTO-LIN |
189 |
if ((isPoint(storeLayer1) && isLine(storeLayer2))
|
190 |
|| (isLine(storeLayer1) && isPoint(storeLayer2))) { |
191 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, |
192 |
getTranslation("Union_point"), RESULT1);
|
193 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeomInput, selectedGeomOverlay); |
194 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_MULTILINE, |
195 |
getTranslation("Union_line"), RESULT2);
|
196 |
computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeomInput, selectedGeomOverlay); |
197 |
} |
198 |
|
199 |
if (featStoreOut2 == null) { |
200 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_WRONG, |
201 |
getTranslation("Null_Layer"), RESULT2);
|
202 |
} |
203 |
|
204 |
} catch (ReadException e) {
|
205 |
throw new GeoAlgorithmExecutionException(e.getMessage()); |
206 |
} catch (DataException e) {
|
207 |
throw new GeoAlgorithmExecutionException(e.getMessage()); |
208 |
} |
209 |
|
210 |
if (getTaskMonitor().isCanceled()) {
|
211 |
return false; |
212 |
} |
213 |
|
214 |
return true; |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Computes union
|
219 |
*
|
220 |
* @param storeLayer1
|
221 |
* @param storeLayer2
|
222 |
* @param featStoreOut
|
223 |
* @param selectedGeom
|
224 |
* @throws DataException
|
225 |
* @throws WrongParameterTypeException
|
226 |
* @throws WrongParameterIDException
|
227 |
* @throws NullParameterValueException
|
228 |
*/
|
229 |
private void computesUnion(FeatureStore storeLayer1, FeatureStore storeLayer2, |
230 |
FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
231 |
computesIntersection(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
|
232 |
computesDifference(storeLayer1, storeLayer2, featStoreOut, selectedGeomInput, selectGeomOverlay, false);
|
233 |
computesDifference(storeLayer2, storeLayer1, featStoreOut, selectedGeomInput, selectGeomOverlay, true);
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* Computes intersection
|
238 |
*
|
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 |
*
|
260 |
* @param storeLayer1
|
261 |
* @param storeLayer2
|
262 |
* @param featStoreOut
|
263 |
* @param selectedGeom
|
264 |
* @param close
|
265 |
* @throws DataException
|
266 |
* @throws WrongParameterTypeException
|
267 |
* @throws WrongParameterIDException
|
268 |
* @throws NullParameterValueException
|
269 |
*/
|
270 |
private void computesDifference(FeatureStore storeLayer1, FeatureStore storeLayer2, |
271 |
FeatureStore featStoreOut, boolean selectedGeomInput, boolean selectGeomOverlay, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
272 |
Geometry unionGeom1 = ScalableUnionOperation.joinLayerGeometries(storeLayer2, selectGeomOverlay); |
273 |
GeometryOperation differenceL1_L2 = new DifferenceOperation(unionGeom1, this); |
274 |
differenceL1_L2.setTaskStatus(getStatus()); |
275 |
differenceL1_L2.computesGeometryOperation(storeLayer1, featStoreOut, attrNames, |
276 |
selectedGeomInput, selectGeomOverlay, close); |
277 |
} |
278 |
|
279 |
} |