svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / difference / fmap / DifferenceVisitor.java @ 9183
History | View | Annotate | Download (10.3 KB)
1 |
/*
|
---|---|
2 |
* Created on 22-feb-2006
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id: DifferenceVisitor.java 9183 2006-12-04 19:44:25Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.2 2006-12-04 19:44:25 azabala
|
49 |
* comments removed
|
50 |
*
|
51 |
* Revision 1.1 2006/06/20 18:20:45 azabala
|
52 |
* first version in cvs
|
53 |
*
|
54 |
* Revision 1.3 2006/06/08 18:24:23 azabala
|
55 |
* modificaciones para admitir capas de shapeType MULTI
|
56 |
*
|
57 |
* Revision 1.2 2006/06/02 18:21:28 azabala
|
58 |
* *** empty log message ***
|
59 |
*
|
60 |
* Revision 1.1 2006/05/24 21:11:38 azabala
|
61 |
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
|
62 |
*
|
63 |
* Revision 1.4 2006/05/01 19:15:18 azabala
|
64 |
* *** empty log message ***
|
65 |
*
|
66 |
* Revision 1.3 2006/03/26 20:02:25 azabala
|
67 |
* *** empty log message ***
|
68 |
*
|
69 |
* Revision 1.2 2006/03/07 21:01:33 azabala
|
70 |
* *** empty log message ***
|
71 |
*
|
72 |
* Revision 1.1 2006/03/06 19:48:39 azabala
|
73 |
* *** empty log message ***
|
74 |
*
|
75 |
* Revision 1.2 2006/03/05 19:58:10 azabala
|
76 |
* *** empty log message ***
|
77 |
*
|
78 |
* Revision 1.1 2006/02/26 20:53:28 azabala
|
79 |
* *** empty log message ***
|
80 |
*
|
81 |
*
|
82 |
*/
|
83 |
package com.iver.cit.gvsig.geoprocess.impl.difference.fmap; |
84 |
|
85 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
86 |
import com.hardcode.gdbms.engine.values.Value; |
87 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
88 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
89 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
90 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
91 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
92 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
93 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
94 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
95 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
96 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
97 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
98 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData; |
99 |
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
100 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
101 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
102 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory; |
103 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
104 |
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes; |
105 |
import com.vividsolutions.jts.geom.Geometry; |
106 |
import com.vividsolutions.jts.geom.MultiPolygon; |
107 |
import com.vividsolutions.jts.geom.Polygon; |
108 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
109 |
|
110 |
public class DifferenceVisitor implements FeatureVisitor { |
111 |
|
112 |
/**
|
113 |
* Allows to get attributes of first layer features which is being
|
114 |
* differenced
|
115 |
*/
|
116 |
SelectableDataSource firstRs; |
117 |
|
118 |
/**
|
119 |
* Number of fields of first layer recordset
|
120 |
*/
|
121 |
int numFieldsA;
|
122 |
|
123 |
/**
|
124 |
* looks for overlay features of the processed feauture by spatial criteria
|
125 |
* (queryByEnvelope)
|
126 |
*/
|
127 |
FLyrVect overlayLayer; |
128 |
|
129 |
/**
|
130 |
* Strategy to process overlaylayer, allowing cancelations
|
131 |
*/
|
132 |
Strategy strategy; |
133 |
|
134 |
/**
|
135 |
* this flag marks if visitor only must process overlay layer selection
|
136 |
*/
|
137 |
boolean onlyOverlayLayerSelected;
|
138 |
|
139 |
/**
|
140 |
* It processes features resulting of intersetions. It could saves them in
|
141 |
* persistent datastore, caching them, reprocess them, reprojects them, etc.
|
142 |
*/
|
143 |
FeatureProcessor featureProcessor; |
144 |
|
145 |
/**
|
146 |
* Schema of result layer
|
147 |
*/
|
148 |
ILayerDefinition layerDefinition; |
149 |
|
150 |
/**
|
151 |
* Constructor
|
152 |
*
|
153 |
* @param overlayLayer
|
154 |
* @param processor
|
155 |
* @throws DriverException
|
156 |
*/
|
157 |
public DifferenceVisitor(FLyrVect overlayLayer, FeatureProcessor processor,
|
158 |
Strategy strategy, boolean onlySelection) throws DriverException { |
159 |
this.overlayLayer = overlayLayer;
|
160 |
this.featureProcessor = processor;
|
161 |
this.strategy = strategy;
|
162 |
this.onlyOverlayLayerSelected = onlySelection;
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Inner class to process with a given strategy all geometries of overlay layer
|
167 |
* that overlays with a given geometry of input layer.
|
168 |
* @author azabala
|
169 |
*
|
170 |
*/
|
171 |
class UnionOverlaysVisitor implements FeatureVisitor { |
172 |
/**
|
173 |
* Result of the strategy process (union of overlays of a IGeometry)
|
174 |
*/
|
175 |
Geometry overlayGeometry; |
176 |
|
177 |
/**
|
178 |
* Flag to process or not selections of overlay layer
|
179 |
*/
|
180 |
boolean overlayLayerSelected = true; |
181 |
|
182 |
Geometry getUnionOfOverlays() { |
183 |
return overlayGeometry;
|
184 |
} |
185 |
|
186 |
public void visit(IGeometry g, int index) throws VisitException { |
187 |
if(g == null) |
188 |
return;
|
189 |
if (overlayLayerSelected) {
|
190 |
try {
|
191 |
if (!overlayLayer.getRecordset().getSelection().get(index))
|
192 |
return;
|
193 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
194 |
throw new VisitException( |
195 |
"Error en diferencia: verificando si un posible overlay esta seleccionado",
|
196 |
e); |
197 |
}// geometry g is not selected
|
198 |
} |
199 |
|
200 |
if(g.getGeometryType() != XTypes.POLYGON &&
|
201 |
g.getGeometryType() != XTypes.MULTI) |
202 |
return;
|
203 |
|
204 |
Geometry actualGeometry = g.toJTSGeometry(); |
205 |
if (overlayGeometry == null) { |
206 |
overlayGeometry = actualGeometry; |
207 |
} else {
|
208 |
overlayGeometry = actualGeometry.union(overlayGeometry); |
209 |
}// if
|
210 |
|
211 |
}// visit
|
212 |
|
213 |
public String getProcessDescription() { |
214 |
return ""; |
215 |
} |
216 |
|
217 |
public void stop(FLayer layer) { |
218 |
} |
219 |
|
220 |
public boolean start(FLayer layer) { |
221 |
return true; |
222 |
} |
223 |
}//UnionOverlaysVisitor
|
224 |
|
225 |
|
226 |
public void visit(IGeometry g, final int index) throws VisitException { |
227 |
if(g == null) |
228 |
return;
|
229 |
if(g.getGeometryType() != XTypes.POLYGON &&
|
230 |
g.getGeometryType() != XTypes.MULTI) |
231 |
return;
|
232 |
|
233 |
Geometry firstJts = g.toJTSGeometry(); |
234 |
Geometry solution = null;
|
235 |
try {
|
236 |
UnionOverlaysVisitor unionVisitor = new UnionOverlaysVisitor();
|
237 |
unionVisitor.overlayLayerSelected = onlyOverlayLayerSelected; |
238 |
strategy.process(unionVisitor, g.getBounds2D()); |
239 |
// now we compute difference of firstJts and overlaysUnion
|
240 |
Geometry overlays = unionVisitor.getUnionOfOverlays(); |
241 |
if (overlays != null) { |
242 |
solution = EnhancedPrecisionOp.difference(firstJts, overlays); |
243 |
} else {
|
244 |
solution = firstJts; |
245 |
} |
246 |
if (!(solution instanceof Polygon)) { |
247 |
if (!(solution instanceof MultiPolygon)) { |
248 |
// intersection of adjacent polygons is a linestring
|
249 |
// but we are not interested in it
|
250 |
return;
|
251 |
} |
252 |
} |
253 |
featureProcessor.processFeature(createFeature(solution, index)); |
254 |
|
255 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
256 |
throw new VisitException( |
257 |
"Error buscando los overlays que intersectan con un feature");
|
258 |
} catch (DriverException e) {
|
259 |
throw new VisitException( |
260 |
"Error al guardar feature en el geoproceso diferencia");
|
261 |
} |
262 |
} |
263 |
|
264 |
public void stop(FLayer layer) { |
265 |
featureProcessor.finish(); |
266 |
} |
267 |
|
268 |
public boolean start(FLayer layer) { |
269 |
if (layer instanceof AlphanumericData && layer instanceof VectorialData) { |
270 |
try {
|
271 |
this.firstRs = ((AlphanumericData) layer).getRecordset();
|
272 |
numFieldsA = firstRs.getFieldCount(); |
273 |
this.featureProcessor.start();
|
274 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
275 |
return false; |
276 |
} catch (DriverException e) {
|
277 |
return false; |
278 |
} catch (EditionException e) {
|
279 |
return false; |
280 |
} |
281 |
|
282 |
return true; |
283 |
} |
284 |
return false; |
285 |
} |
286 |
|
287 |
/**
|
288 |
*
|
289 |
* @param jtsGeometry
|
290 |
* @param firstLayerIndex
|
291 |
* @return
|
292 |
* @throws DriverException
|
293 |
*
|
294 |
* FIXME Revisar. Para el caso de la Union, hay que hacer dos pasadas:
|
295 |
* diferencia A-B y diferencia B-A. Pero los features tienen que tener el
|
296 |
* esquema de la intersecci?n. ?Como saber el lugar que ocupa cada atributo
|
297 |
* en el esquema ILayerDefinition, en el caso de que dos capas tuviesen
|
298 |
* atributos con el mismo nombre?) De momento, se buscar? en
|
299 |
* ILayerDefinition la posicion que ocupa un campo a partir de su nombre.
|
300 |
* Esto obliga a que 2 campos no tomen el mismo nombre
|
301 |
*/
|
302 |
private IFeature createFeature(Geometry jtsGeometry, int firstLayerIndex) |
303 |
throws DriverException {
|
304 |
IFeature solution = null;
|
305 |
IGeometry diffGeometry = FConverter.jts_to_igeometry(jtsGeometry); |
306 |
FieldDescription[] fields = layerDefinition.getFieldsDesc();
|
307 |
Value[] featureAttr = new Value[fields.length]; |
308 |
for (int indexField = 0; indexField < numFieldsA; indexField++) { |
309 |
// for each field of firstRs
|
310 |
String fieldName = firstRs.getFieldName(indexField);
|
311 |
for (int j = 0; j < fields.length; j++) { |
312 |
if (fieldName.equalsIgnoreCase(fields[j].getFieldName())) {
|
313 |
featureAttr[j] = firstRs.getFieldValue(firstLayerIndex, |
314 |
indexField); |
315 |
break;
|
316 |
}// if
|
317 |
}// for
|
318 |
}// for
|
319 |
// now we put null values
|
320 |
for (int i = 0; i < featureAttr.length; i++) { |
321 |
if (featureAttr[i] == null) |
322 |
featureAttr[i] = ValueFactory.createNullValue(); |
323 |
} |
324 |
solution = FeatureFactory.createFeature(featureAttr, diffGeometry); |
325 |
return solution;
|
326 |
} |
327 |
|
328 |
/*
|
329 |
* Value[] featureAttr = new Value[numFieldsA]; for (int indexField = 0;
|
330 |
* indexField < numFieldsA; indexField++) { featureAttr[indexField] =
|
331 |
* firstRs.getFieldValue(firstLayerIndex, indexField); } solution =
|
332 |
* FeatureFactory.createFeature(featureAttr, diffGeometry); return solution;
|
333 |
*/
|
334 |
|
335 |
public void setFeatureProcessor(FeatureProcessor featureProcessor) { |
336 |
this.featureProcessor = featureProcessor;
|
337 |
} |
338 |
|
339 |
public String getProcessDescription() { |
340 |
return "Computing differences between two layers"; |
341 |
} |
342 |
|
343 |
public ILayerDefinition getLayerDefinition() {
|
344 |
return layerDefinition;
|
345 |
} |
346 |
|
347 |
public void setLayerDefinition(ILayerDefinition layerDefinition) { |
348 |
this.layerDefinition = layerDefinition;
|
349 |
} |
350 |
|
351 |
} |