svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / difference / fmap / DifferenceVisitor.java @ 13874
History | View | Annotate | Download (11.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 13874 2007-09-19 16:12:18Z jaume $
|
47 |
* $Log$
|
48 |
* Revision 1.5 2007-09-19 16:05:53 jaume
|
49 |
* ReadExpansionFileException removed from this context
|
50 |
*
|
51 |
* Revision 1.4 2007/08/07 15:42:19 azabala
|
52 |
* centrilizing JTS in JTSFacade
|
53 |
*
|
54 |
* Revision 1.3 2007/03/06 16:47:58 caballero
|
55 |
* Exceptions
|
56 |
*
|
57 |
* Revision 1.2 2006/12/04 19:44:25 azabala
|
58 |
* comments removed
|
59 |
*
|
60 |
* Revision 1.1 2006/06/20 18:20:45 azabala
|
61 |
* first version in cvs
|
62 |
*
|
63 |
* Revision 1.3 2006/06/08 18:24:23 azabala
|
64 |
* modificaciones para admitir capas de shapeType MULTI
|
65 |
*
|
66 |
* Revision 1.2 2006/06/02 18:21:28 azabala
|
67 |
* *** empty log message ***
|
68 |
*
|
69 |
* Revision 1.1 2006/05/24 21:11:38 azabala
|
70 |
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
|
71 |
*
|
72 |
* Revision 1.4 2006/05/01 19:15:18 azabala
|
73 |
* *** empty log message ***
|
74 |
*
|
75 |
* Revision 1.3 2006/03/26 20:02:25 azabala
|
76 |
* *** empty log message ***
|
77 |
*
|
78 |
* Revision 1.2 2006/03/07 21:01:33 azabala
|
79 |
* *** empty log message ***
|
80 |
*
|
81 |
* Revision 1.1 2006/03/06 19:48:39 azabala
|
82 |
* *** empty log message ***
|
83 |
*
|
84 |
* Revision 1.2 2006/03/05 19:58:10 azabala
|
85 |
* *** empty log message ***
|
86 |
*
|
87 |
* Revision 1.1 2006/02/26 20:53:28 azabala
|
88 |
* *** empty log message ***
|
89 |
*
|
90 |
*
|
91 |
*/
|
92 |
package com.iver.cit.gvsig.geoprocess.impl.difference.fmap; |
93 |
|
94 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
95 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
96 |
import com.hardcode.gdbms.engine.values.Value; |
97 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
98 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
99 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException; |
100 |
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException; |
101 |
import com.iver.cit.gvsig.exceptions.visitors.VisitorException; |
102 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
103 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
104 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
105 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
106 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
107 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
108 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
109 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
110 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
111 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData; |
112 |
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
113 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
114 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory; |
115 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
116 |
import com.iver.cit.gvsig.geoprocess.core.util.JTSFacade; |
117 |
import com.vividsolutions.jts.geom.Geometry; |
118 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
119 |
|
120 |
public class DifferenceVisitor implements FeatureVisitor { |
121 |
|
122 |
/**
|
123 |
* Allows to get attributes of first layer features which is being
|
124 |
* differenced
|
125 |
*/
|
126 |
SelectableDataSource firstRs; |
127 |
|
128 |
/**
|
129 |
* Number of fields of first layer recordset
|
130 |
*/
|
131 |
int numFieldsA;
|
132 |
|
133 |
/**
|
134 |
* looks for overlay features of the processed feauture by spatial criteria
|
135 |
* (queryByEnvelope)
|
136 |
*/
|
137 |
FLyrVect overlayLayer; |
138 |
|
139 |
/**
|
140 |
* Strategy to process overlaylayer, allowing cancelations
|
141 |
*/
|
142 |
Strategy strategy; |
143 |
|
144 |
/**
|
145 |
* this flag marks if visitor only must process overlay layer selection
|
146 |
*/
|
147 |
boolean onlyOverlayLayerSelected;
|
148 |
|
149 |
/**
|
150 |
* It processes features resulting of intersetions. It could saves them in
|
151 |
* persistent datastore, caching them, reprocess them, reprojects them, etc.
|
152 |
*/
|
153 |
FeatureProcessor featureProcessor; |
154 |
|
155 |
/**
|
156 |
* Schema of result layer
|
157 |
*/
|
158 |
ILayerDefinition layerDefinition; |
159 |
|
160 |
/**
|
161 |
* Constructor
|
162 |
*
|
163 |
* @param overlayLayer
|
164 |
* @param processor
|
165 |
* @throws DriverException
|
166 |
*/
|
167 |
public DifferenceVisitor(FLyrVect overlayLayer, FeatureProcessor processor,
|
168 |
Strategy strategy, boolean onlySelection) {
|
169 |
this.overlayLayer = overlayLayer;
|
170 |
this.featureProcessor = processor;
|
171 |
this.strategy = strategy;
|
172 |
this.onlyOverlayLayerSelected = onlySelection;
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Inner class to process with a given strategy all geometries of overlay layer
|
177 |
* that overlays with a given geometry of input layer.
|
178 |
* @author azabala
|
179 |
*
|
180 |
*/
|
181 |
|
182 |
/*
|
183 |
* TODO Dado un feature, es factible pensar que los de la otra capa que lo cubran pueden
|
184 |
* estar en memoria (aunque esto no sea siempre as?). Podemos hacer un
|
185 |
* EnhancedMemoryOverlay, que primero lo haga todo en memoria, capture un OutOfMemoryException
|
186 |
* y lo haga de modo incremental
|
187 |
*
|
188 |
* */
|
189 |
class UnionOverlaysVisitor implements FeatureVisitor { |
190 |
/**
|
191 |
* Result of the strategy process (union of overlays of a IGeometry)
|
192 |
*/
|
193 |
Geometry overlayGeometry; |
194 |
|
195 |
/**
|
196 |
* Flag to process or not selections of overlay layer
|
197 |
*/
|
198 |
boolean overlayLayerSelected = true; |
199 |
|
200 |
Geometry getUnionOfOverlays() { |
201 |
return overlayGeometry;
|
202 |
} |
203 |
|
204 |
public void visit(IGeometry g, int index) throws VisitorException, ProcessVisitorException { |
205 |
if(g == null) |
206 |
return;
|
207 |
|
208 |
/*
|
209 |
* TODO
|
210 |
* Cuando hagamos uso de los iteradores en geoprocessing, meter
|
211 |
* un readableVectorial.getFeatureIterator(IFeatureFilter), que pueda
|
212 |
* englobar varias opciones de filtrado.
|
213 |
* As?, por ejemplo, puedo tener en cuenta las SELECCIONES.
|
214 |
*
|
215 |
* */
|
216 |
if (overlayLayerSelected) {
|
217 |
try {
|
218 |
if (!overlayLayer.getRecordset().getSelection().get(index))
|
219 |
return;
|
220 |
} catch (ReadDriverException e) {
|
221 |
throw new ProcessVisitorException(overlayLayer.getName(),e, |
222 |
"Error en diferencia: verificando si un posible overlay esta seleccionado");
|
223 |
}// geometry g is not selected
|
224 |
} |
225 |
|
226 |
/*
|
227 |
Prueba para hacer diferencia entre cualquier tipo de geometria
|
228 |
if(g.getGeometryType() != XTypes.POLYGON &&
|
229 |
g.getGeometryType() != XTypes.MULTI)
|
230 |
return;
|
231 |
*/
|
232 |
|
233 |
Geometry actualGeometry = g.toJTSGeometry(); |
234 |
if (overlayGeometry == null) { |
235 |
overlayGeometry = actualGeometry; |
236 |
} else {
|
237 |
overlayGeometry = JTSFacade.union(actualGeometry, overlayGeometry); |
238 |
// overlayGeometry = actualGeometry.union(overlayGeometry);
|
239 |
}// if
|
240 |
|
241 |
}// visit
|
242 |
|
243 |
public String getProcessDescription() { |
244 |
return ""; |
245 |
} |
246 |
|
247 |
public void stop(FLayer layer) throws VisitorException { |
248 |
} |
249 |
|
250 |
public boolean start(FLayer layer) throws StartVisitorException { |
251 |
return true; |
252 |
} |
253 |
}//UnionOverlaysVisitor
|
254 |
|
255 |
|
256 |
public void visit(IGeometry g, final int index) throws VisitorException, ProcessVisitorException { |
257 |
if(g == null) |
258 |
return;
|
259 |
|
260 |
|
261 |
/*
|
262 |
if(g.getGeometryType() != XTypes.POLYGON &&
|
263 |
g.getGeometryType() != XTypes.MULTI)
|
264 |
return;
|
265 |
*/
|
266 |
Geometry firstJts = g.toJTSGeometry(); |
267 |
Geometry solution = null;
|
268 |
try {
|
269 |
UnionOverlaysVisitor unionVisitor = new UnionOverlaysVisitor();
|
270 |
unionVisitor.overlayLayerSelected = onlyOverlayLayerSelected; |
271 |
strategy.process(unionVisitor, g.getBounds2D()); |
272 |
|
273 |
// now we compute difference of firstJts and overlaysUnion
|
274 |
Geometry overlays = unionVisitor.getUnionOfOverlays(); |
275 |
if (overlays != null) { |
276 |
solution = EnhancedPrecisionOp.difference(firstJts, overlays); |
277 |
} else {
|
278 |
solution = firstJts; |
279 |
} |
280 |
|
281 |
/*
|
282 |
* TODO Que pasa si la diferencia entre dos lineas es un punto, o una linea???
|
283 |
* Saldr?an geometrias mezcladas
|
284 |
*
|
285 |
if (!(solution instanceof Polygon)) {
|
286 |
if (!(solution instanceof MultiPolygon)) {
|
287 |
// intersection of adjacent polygons is a linestring
|
288 |
// but we are not interested in it
|
289 |
return;
|
290 |
}
|
291 |
}
|
292 |
*/
|
293 |
if(!JTSFacade.checkNull(solution)){
|
294 |
featureProcessor.processFeature(createFeature(solution, index)); |
295 |
} |
296 |
} catch (ReadDriverException e) {
|
297 |
throw new ProcessVisitorException(overlayLayer.getName(),e, |
298 |
"Error buscando los overlays que intersectan con un feature");
|
299 |
} |
300 |
} |
301 |
|
302 |
public void stop(FLayer layer) throws VisitorException { |
303 |
featureProcessor.finish(); |
304 |
} |
305 |
|
306 |
public boolean start(FLayer layer) throws StartVisitorException { |
307 |
if (layer instanceof AlphanumericData && layer instanceof VectorialData) { |
308 |
try {
|
309 |
this.firstRs = ((AlphanumericData) layer).getRecordset();
|
310 |
numFieldsA = firstRs.getFieldCount(); |
311 |
this.featureProcessor.start();
|
312 |
} catch (ReadDriverException e) {
|
313 |
return false; |
314 |
} |
315 |
|
316 |
return true; |
317 |
} |
318 |
return false; |
319 |
} |
320 |
|
321 |
/**
|
322 |
*
|
323 |
* @param jtsGeometry
|
324 |
* @param firstLayerIndex
|
325 |
* @return
|
326 |
* @throws DriverException
|
327 |
*
|
328 |
* FIXME Revisar. Para el caso de la Union, hay que hacer dos pasadas:
|
329 |
* diferencia A-B y diferencia B-A. Pero los features tienen que tener el
|
330 |
* esquema de la intersecci?n. ?Como saber el lugar que ocupa cada atributo
|
331 |
* en el esquema ILayerDefinition, en el caso de que dos capas tuviesen
|
332 |
* atributos con el mismo nombre?) De momento, se buscar? en
|
333 |
* ILayerDefinition la posicion que ocupa un campo a partir de su nombre.
|
334 |
* Esto obliga a que 2 campos no tomen el mismo nombre
|
335 |
*
|
336 |
* POSIBLE SOLUCION: ANTEPONER EL NOMBRE DE LA LAYER AL NOMBRE DEL CAMPO
|
337 |
*/
|
338 |
private IFeature createFeature(Geometry jtsGeometry, int firstLayerIndex) |
339 |
throws ReadDriverException {
|
340 |
IFeature solution = null;
|
341 |
IGeometry diffGeometry = FConverter.jts_to_igeometry(jtsGeometry); |
342 |
FieldDescription[] fields = layerDefinition.getFieldsDesc();
|
343 |
Value[] featureAttr = new Value[fields.length]; |
344 |
for (int indexField = 0; indexField < numFieldsA; indexField++) { |
345 |
// for each field of firstRs
|
346 |
String fieldName = firstRs.getFieldName(indexField);
|
347 |
for (int j = 0; j < fields.length; j++) { |
348 |
if (fieldName.equalsIgnoreCase(fields[j].getFieldName())) {
|
349 |
featureAttr[j] = firstRs.getFieldValue(firstLayerIndex, |
350 |
indexField); |
351 |
break;
|
352 |
}// if
|
353 |
}// for
|
354 |
}// for
|
355 |
// now we put null values
|
356 |
for (int i = 0; i < featureAttr.length; i++) { |
357 |
if (featureAttr[i] == null) |
358 |
featureAttr[i] = ValueFactory.createNullValue(); |
359 |
} |
360 |
solution = FeatureFactory.createFeature(featureAttr, diffGeometry); |
361 |
return solution;
|
362 |
} |
363 |
|
364 |
public void setFeatureProcessor(FeatureProcessor featureProcessor) { |
365 |
this.featureProcessor = featureProcessor;
|
366 |
} |
367 |
|
368 |
public String getProcessDescription() { |
369 |
return "Computing differences between two layers"; |
370 |
} |
371 |
|
372 |
public ILayerDefinition getLayerDefinition() {
|
373 |
return layerDefinition;
|
374 |
} |
375 |
|
376 |
public void setLayerDefinition(ILayerDefinition layerDefinition) { |
377 |
this.layerDefinition = layerDefinition;
|
378 |
} |
379 |
|
380 |
} |