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 @ 38
History | View | Annotate | Download (10.5 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 org.gvsig.fmap.dal.exception.DataException; |
24 |
import org.gvsig.fmap.dal.exception.ReadException; |
25 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
26 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
27 |
import org.gvsig.fmap.dal.feature.FeatureType; |
28 |
import org.gvsig.fmap.geom.Geometry; |
29 |
import org.gvsig.sextante.app.algorithm.base.core.GeometryOperation; |
30 |
import org.gvsig.sextante.app.algorithm.base.core.ScalableUnionOperation; |
31 |
import org.gvsig.sextante.app.algorithm.base.panel.AlgorithmOutputPanel; |
32 |
import org.gvsig.sextante.app.algorithm.difference.DifferenceOperation; |
33 |
import org.gvsig.sextante.app.algorithm.intersection.IntersectionOperation; |
34 |
import org.gvsig.sextante.app.extension.core.gvGeoAlgorithm; |
35 |
import org.gvsig.sextante.app.extension.core.gvVectorLayer; |
36 |
|
37 |
import es.unex.sextante.core.Sextante; |
38 |
import es.unex.sextante.dataObjects.IVectorLayer; |
39 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
40 |
import es.unex.sextante.exceptions.NullParameterValueException; |
41 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
42 |
import es.unex.sextante.exceptions.WrongParameterIDException; |
43 |
import es.unex.sextante.exceptions.WrongParameterTypeException; |
44 |
import es.unex.sextante.outputs.OutputVectorLayer; |
45 |
|
46 |
/**
|
47 |
* Union algorithm
|
48 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
49 |
*/
|
50 |
public class UnionAlgorithm extends gvGeoAlgorithm { |
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_layer1"),
|
69 |
IVectorLayer.SHAPE_TYPE_WRONG, |
70 |
true);
|
71 |
m_Parameters.addInputVectorLayer(LAYER2, |
72 |
Sextante.getText( "Input_layer2"),
|
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 |
setExternalParameters((Object)new AlgorithmOutputPanel()); |
86 |
setExternalParameters((Object)new AlgorithmOutputPanel()); |
87 |
} |
88 |
|
89 |
/*
|
90 |
* (non-Javadoc)
|
91 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
92 |
*/
|
93 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
94 |
IVectorLayer layer1 = m_Parameters.getParameterValueAsVectorLayer(LAYER1); |
95 |
IVectorLayer layer2 = m_Parameters.getParameterValueAsVectorLayer(LAYER2); |
96 |
boolean selectedGeom = m_Parameters.getParameter(CHECK).getParameterValueAsBoolean();
|
97 |
|
98 |
FeatureStore storeLayer1 = null;
|
99 |
FeatureStore storeLayer2 = null;
|
100 |
if(layer1 instanceof gvVectorLayer && |
101 |
layer2 instanceof gvVectorLayer) {
|
102 |
storeLayer1 = ((gvVectorLayer)layer1).getFeatureStore(); |
103 |
storeLayer2 = ((gvVectorLayer)layer2).getFeatureStore(); |
104 |
} else
|
105 |
return false; |
106 |
|
107 |
try {
|
108 |
FeatureSet features1 = null;
|
109 |
features1 = storeLayer1.getFeatureSet(); |
110 |
FeatureType featureType1 = features1.getDefaultFeatureType(); |
111 |
|
112 |
FeatureSet features2 = null;
|
113 |
features2 = storeLayer2.getFeatureSet(); |
114 |
FeatureType featureType2 = features2.getDefaultFeatureType(); |
115 |
|
116 |
FeatureStore featStoreOut1 = null;
|
117 |
FeatureStore featStoreOut2 = null;
|
118 |
|
119 |
//POL-POL
|
120 |
if(isPolygon(storeLayer1) && isPolygon(storeLayer2)) {
|
121 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, layer1.getShapeType(), |
122 |
Sextante.getText("Union_l1"), 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 |
Sextante.getText("Union_l1"), RESULT1);
|
129 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeom); |
130 |
} else
|
131 |
//LIN-LIN
|
132 |
if(isLine(storeLayer1) && isLine(storeLayer2)) {
|
133 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, |
134 |
Sextante.getText("Union_l1"), RESULT1);
|
135 |
computesIntersection(storeLayer1, storeLayer2, featStoreOut1, selectedGeom, true);
|
136 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_LINE, |
137 |
Sextante.getText("Union_l2"), RESULT2);
|
138 |
computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeom, false);
|
139 |
computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeom, true);
|
140 |
} else
|
141 |
//POL-LIN
|
142 |
if( (isPolygon(storeLayer1) && isLine(storeLayer2)) ||
|
143 |
(isLine(storeLayer1) && isPolygon(storeLayer2))) { |
144 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_LINE, |
145 |
Sextante.getText("Union_l1"), RESULT1);
|
146 |
computesIntersection(storeLayer1, storeLayer2, featStoreOut1, selectedGeom, true);
|
147 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POLYGON, |
148 |
Sextante.getText("Union_l2"), RESULT2);
|
149 |
computesDifference(storeLayer1, storeLayer2, featStoreOut2, selectedGeom, false);
|
150 |
computesDifference(storeLayer2, storeLayer1, featStoreOut2, selectedGeom, true);
|
151 |
} else
|
152 |
//POL-PTO
|
153 |
if( (isPolygon(storeLayer1) && isPoint(storeLayer2)) ||
|
154 |
(isPoint(storeLayer1) && isPolygon(storeLayer2))) { |
155 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, |
156 |
Sextante.getText("Union_l1"), RESULT1);
|
157 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeom); |
158 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POLYGON, |
159 |
Sextante.getText("Union_l2"), RESULT2);
|
160 |
computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeom); |
161 |
} else
|
162 |
//PTO-LIN
|
163 |
if( (isPoint(storeLayer1) && isLine(storeLayer2)) ||
|
164 |
(isLine(storeLayer1) && isPoint(storeLayer2))) { |
165 |
featStoreOut1 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_POINT, |
166 |
Sextante.getText("Union_l1"), RESULT1);
|
167 |
computesUnion(storeLayer1, storeLayer2, featStoreOut1, selectedGeom); |
168 |
featStoreOut2 = buildOutPutStoreFromUnion(featureType1, featureType2, IVectorLayer.SHAPE_TYPE_LINE, |
169 |
Sextante.getText("Union_l2"), RESULT2);
|
170 |
computesUnion(storeLayer1, storeLayer2, featStoreOut2, selectedGeom); |
171 |
} |
172 |
|
173 |
} catch (ReadException e) {
|
174 |
throw new GeoAlgorithmExecutionException(e.getMessage()); |
175 |
} catch (DataException e) {
|
176 |
throw new GeoAlgorithmExecutionException(e.getMessage()); |
177 |
} |
178 |
|
179 |
return true; |
180 |
} |
181 |
|
182 |
/**
|
183 |
* Computes union
|
184 |
* @param storeLayer1
|
185 |
* @param storeLayer2
|
186 |
* @param featStoreOut
|
187 |
* @param selectedGeom
|
188 |
* @throws DataException
|
189 |
* @throws WrongParameterTypeException
|
190 |
* @throws WrongParameterIDException
|
191 |
* @throws NullParameterValueException
|
192 |
*/
|
193 |
private void computesUnion(FeatureStore storeLayer1, FeatureStore storeLayer2, FeatureStore featStoreOut, boolean selectedGeom) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
194 |
computesIntersection(storeLayer1, storeLayer2, featStoreOut, selectedGeom, false);
|
195 |
computesDifference(storeLayer1, storeLayer2, featStoreOut, selectedGeom, false);
|
196 |
computesDifference(storeLayer2, storeLayer1, featStoreOut, selectedGeom, true);
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Computes intersection
|
201 |
* @param storeLayer1
|
202 |
* @param storeLayer2
|
203 |
* @param featStoreOut
|
204 |
* @param selectedGeom
|
205 |
* @param close
|
206 |
* @throws DataException
|
207 |
* @throws WrongParameterTypeException
|
208 |
* @throws WrongParameterIDException
|
209 |
* @throws NullParameterValueException
|
210 |
*/
|
211 |
private void computesIntersection(FeatureStore storeLayer1, FeatureStore storeLayer2, FeatureStore featStoreOut, boolean selectedGeom, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
212 |
GeometryOperation intersection = new IntersectionOperation(storeLayer2);
|
213 |
intersection.computesGeometryOperation(storeLayer1, featStoreOut, attrNames, selectedGeom, close); |
214 |
} |
215 |
|
216 |
/**
|
217 |
* Computes difference
|
218 |
* @param storeLayer1
|
219 |
* @param storeLayer2
|
220 |
* @param featStoreOut
|
221 |
* @param selectedGeom
|
222 |
* @param close
|
223 |
* @throws DataException
|
224 |
* @throws WrongParameterTypeException
|
225 |
* @throws WrongParameterIDException
|
226 |
* @throws NullParameterValueException
|
227 |
*/
|
228 |
private void computesDifference(FeatureStore storeLayer1, FeatureStore storeLayer2, FeatureStore featStoreOut, boolean selectedGeom, boolean close) throws DataException, WrongParameterTypeException, WrongParameterIDException, NullParameterValueException { |
229 |
Geometry unionGeom1 = ScalableUnionOperation.joinLayerGeometries(storeLayer2); |
230 |
GeometryOperation differenceL1_L2 = new DifferenceOperation(unionGeom1);
|
231 |
differenceL1_L2.computesGeometryOperation(storeLayer1, featStoreOut, attrNames, selectedGeom, close); |
232 |
} |
233 |
} |