root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / difference / fmap / DifferenceVisitor.java @ 5628
History | View | Annotate | Download (10 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 5628 2006-06-02 18:21:28Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.2 2006-06-02 18:21:28 azabala
|
49 |
* *** empty log message ***
|
50 |
*
|
51 |
* Revision 1.1 2006/05/24 21:11:38 azabala
|
52 |
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
|
53 |
*
|
54 |
* Revision 1.4 2006/05/01 19:15:18 azabala
|
55 |
* *** empty log message ***
|
56 |
*
|
57 |
* Revision 1.3 2006/03/26 20:02:25 azabala
|
58 |
* *** empty log message ***
|
59 |
*
|
60 |
* Revision 1.2 2006/03/07 21:01:33 azabala
|
61 |
* *** empty log message ***
|
62 |
*
|
63 |
* Revision 1.1 2006/03/06 19:48:39 azabala
|
64 |
* *** empty log message ***
|
65 |
*
|
66 |
* Revision 1.2 2006/03/05 19:58:10 azabala
|
67 |
* *** empty log message ***
|
68 |
*
|
69 |
* Revision 1.1 2006/02/26 20:53:28 azabala
|
70 |
* *** empty log message ***
|
71 |
*
|
72 |
*
|
73 |
*/
|
74 |
package com.iver.cit.gvsig.geoprocess.difference.fmap; |
75 |
|
76 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
77 |
import com.hardcode.gdbms.engine.values.Value; |
78 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
79 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
80 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
81 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
82 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
83 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
84 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
85 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
86 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
87 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
88 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
89 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData; |
90 |
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
91 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
92 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
93 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory; |
94 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
95 |
import com.vividsolutions.jts.geom.Geometry; |
96 |
import com.vividsolutions.jts.geom.MultiPolygon; |
97 |
import com.vividsolutions.jts.geom.Polygon; |
98 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
99 |
|
100 |
public class DifferenceVisitor implements FeatureVisitor { |
101 |
|
102 |
/**
|
103 |
* Allows to get attributes of first layer features which is being
|
104 |
* differenced
|
105 |
*/
|
106 |
SelectableDataSource firstRs; |
107 |
|
108 |
/**
|
109 |
* Number of fields of first layer recordset
|
110 |
*/
|
111 |
int numFieldsA;
|
112 |
|
113 |
/**
|
114 |
* looks for overlay features of the processed feauture by spatial criteria
|
115 |
* (queryByEnvelope)
|
116 |
*/
|
117 |
FLyrVect overlayLayer; |
118 |
|
119 |
/**
|
120 |
* Strategy to process overlaylayer, allowing cancelations
|
121 |
*/
|
122 |
Strategy strategy; |
123 |
|
124 |
/**
|
125 |
* this flag marks if visitor only must process overlay layer selection
|
126 |
*/
|
127 |
boolean onlyOverlayLayerSelected;
|
128 |
|
129 |
/**
|
130 |
* It processes features resulting of intersetions. It could saves them in
|
131 |
* persistent datastore, caching them, reprocess them, reprojects them, etc.
|
132 |
*/
|
133 |
FeatureProcessor featureProcessor; |
134 |
|
135 |
/**
|
136 |
* Schema of result layer
|
137 |
*/
|
138 |
ILayerDefinition layerDefinition; |
139 |
|
140 |
/**
|
141 |
* Constructor
|
142 |
*
|
143 |
* @param overlayLayer
|
144 |
* @param processor
|
145 |
* @throws DriverException
|
146 |
*/
|
147 |
public DifferenceVisitor(FLyrVect overlayLayer, FeatureProcessor processor,
|
148 |
Strategy strategy, boolean onlySelection) throws DriverException { |
149 |
this.overlayLayer = overlayLayer;
|
150 |
this.featureProcessor = processor;
|
151 |
this.strategy = strategy;
|
152 |
this.onlyOverlayLayerSelected = onlySelection;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Inner class to process with a given strategy all geometries of overlay layer
|
157 |
* that overlays with a given geometry of input layer.
|
158 |
* @author azabala
|
159 |
*
|
160 |
*/
|
161 |
class UnionOverlaysVisitor implements FeatureVisitor { |
162 |
/**
|
163 |
* Result of the strategy process (union of overlays of a IGeometry)
|
164 |
*/
|
165 |
Geometry overlayGeometry; |
166 |
|
167 |
/**
|
168 |
* Flag to process or not selections of overlay layer
|
169 |
*/
|
170 |
boolean overlayLayerSelected = true; |
171 |
|
172 |
Geometry getUnionOfOverlays() { |
173 |
return overlayGeometry;
|
174 |
} |
175 |
|
176 |
public void visit(IGeometry g, int index) throws VisitException { |
177 |
if(g == null) |
178 |
return;
|
179 |
if (overlayLayerSelected) {
|
180 |
try {
|
181 |
if (!overlayLayer.getRecordset().getSelection().get(index))
|
182 |
return;
|
183 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
184 |
throw new VisitException( |
185 |
"Error en diferencia: verificando si un posible overlay esta seleccionado",
|
186 |
e); |
187 |
}// geometry g is not selected
|
188 |
} |
189 |
Geometry actualGeometry = g.toJTSGeometry(); |
190 |
if (overlayGeometry == null) { |
191 |
overlayGeometry = actualGeometry; |
192 |
} else {
|
193 |
/*
|
194 |
* Geometry[] geoms = new Geometry[2];
|
195 |
* geoms[0] = actualGeometry;
|
196 |
* geoms[1] = overlayGeometry;
|
197 |
* GeometryCollection gc = geomFact.
|
198 |
* createGeometryCollection(geoms);
|
199 |
* geometry = gc.buffer(0);
|
200 |
*/
|
201 |
overlayGeometry = actualGeometry.union(overlayGeometry); |
202 |
}// if
|
203 |
|
204 |
}// visit
|
205 |
|
206 |
public String getProcessDescription() { |
207 |
return ""; |
208 |
} |
209 |
|
210 |
public void stop(FLayer layer) { |
211 |
} |
212 |
|
213 |
public boolean start(FLayer layer) { |
214 |
return true; |
215 |
} |
216 |
}//UnionOverlaysVisitor
|
217 |
|
218 |
|
219 |
public void visit(IGeometry g, final int index) throws VisitException { |
220 |
if(g == null) |
221 |
return;
|
222 |
Geometry firstJts = g.toJTSGeometry(); |
223 |
Geometry solution = null;
|
224 |
try {
|
225 |
UnionOverlaysVisitor unionVisitor = new UnionOverlaysVisitor();
|
226 |
unionVisitor.overlayLayerSelected = onlyOverlayLayerSelected; |
227 |
strategy.process(unionVisitor, g.getBounds2D()); |
228 |
// now we compute difference of firstJts and overlaysUnion
|
229 |
Geometry overlays = unionVisitor.getUnionOfOverlays(); |
230 |
if (overlays != null) { |
231 |
solution = EnhancedPrecisionOp.difference(firstJts, overlays); |
232 |
} else {
|
233 |
solution = firstJts; |
234 |
} |
235 |
if (!(solution instanceof Polygon)) { |
236 |
if (!(solution instanceof MultiPolygon)) { |
237 |
// intersection of adjacent polygons is a linestring
|
238 |
// but we are not interested in it
|
239 |
return;
|
240 |
} |
241 |
} |
242 |
featureProcessor.processFeature(createFeature(solution, index)); |
243 |
|
244 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
245 |
throw new VisitException( |
246 |
"Error buscando los overlays que intersectan con un feature");
|
247 |
} catch (DriverException e) {
|
248 |
throw new VisitException( |
249 |
"Error al guardar feature en el geoproceso diferencia");
|
250 |
} |
251 |
} |
252 |
|
253 |
public void stop(FLayer layer) { |
254 |
featureProcessor.finish(); |
255 |
} |
256 |
|
257 |
public boolean start(FLayer layer) { |
258 |
if (layer instanceof AlphanumericData && layer instanceof VectorialData) { |
259 |
try {
|
260 |
this.firstRs = ((AlphanumericData) layer).getRecordset();
|
261 |
numFieldsA = firstRs.getFieldCount(); |
262 |
this.featureProcessor.start();
|
263 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
264 |
return false; |
265 |
} catch (DriverException e) {
|
266 |
return false; |
267 |
} catch (EditionException e) {
|
268 |
return false; |
269 |
} |
270 |
|
271 |
return true; |
272 |
} |
273 |
return false; |
274 |
} |
275 |
|
276 |
/**
|
277 |
*
|
278 |
* @param jtsGeometry
|
279 |
* @param firstLayerIndex
|
280 |
* @return
|
281 |
* @throws DriverException
|
282 |
*
|
283 |
* FIXME Revisar. Para el caso de la Union, hay que hacer dos pasadas:
|
284 |
* diferencia A-B y diferencia B-A. Pero los features tienen que tener el
|
285 |
* esquema de la intersecci?n. ?Como saber el lugar que ocupa cada atributo
|
286 |
* en el esquema ILayerDefinition, en el caso de que dos capas tuviesen
|
287 |
* atributos con el mismo nombre?) De momento, se buscar? en
|
288 |
* ILayerDefinition la posicion que ocupa un campo a partir de su nombre.
|
289 |
* Esto obliga a que 2 campos no tomen el mismo nombre
|
290 |
*/
|
291 |
private IFeature createFeature(Geometry jtsGeometry, int firstLayerIndex) |
292 |
throws DriverException {
|
293 |
IFeature solution = null;
|
294 |
IGeometry diffGeometry = FConverter.jts_to_igeometry(jtsGeometry); |
295 |
FieldDescription[] fields = layerDefinition.getFieldsDesc();
|
296 |
Value[] featureAttr = new Value[fields.length]; |
297 |
for (int indexField = 0; indexField < numFieldsA; indexField++) { |
298 |
// for each field of firstRs
|
299 |
String fieldName = firstRs.getFieldName(indexField);
|
300 |
for (int j = 0; j < fields.length; j++) { |
301 |
if (fieldName.equalsIgnoreCase(fields[j].getFieldName())) {
|
302 |
featureAttr[j] = firstRs.getFieldValue(firstLayerIndex, |
303 |
indexField); |
304 |
break;
|
305 |
}// if
|
306 |
}// for
|
307 |
}// for
|
308 |
// now we put null values
|
309 |
for (int i = 0; i < featureAttr.length; i++) { |
310 |
if (featureAttr[i] == null) |
311 |
featureAttr[i] = ValueFactory.createNullValue(); |
312 |
} |
313 |
solution = FeatureFactory.createFeature(featureAttr, diffGeometry); |
314 |
return solution;
|
315 |
} |
316 |
|
317 |
/*
|
318 |
* Value[] featureAttr = new Value[numFieldsA]; for (int indexField = 0;
|
319 |
* indexField < numFieldsA; indexField++) { featureAttr[indexField] =
|
320 |
* firstRs.getFieldValue(firstLayerIndex, indexField); } solution =
|
321 |
* FeatureFactory.createFeature(featureAttr, diffGeometry); return solution;
|
322 |
*/
|
323 |
|
324 |
public void setFeatureProcessor(FeatureProcessor featureProcessor) { |
325 |
this.featureProcessor = featureProcessor;
|
326 |
} |
327 |
|
328 |
public String getProcessDescription() { |
329 |
return "Computing differences between two layers"; |
330 |
} |
331 |
|
332 |
public ILayerDefinition getLayerDefinition() {
|
333 |
return layerDefinition;
|
334 |
} |
335 |
|
336 |
public void setLayerDefinition(ILayerDefinition layerDefinition) { |
337 |
this.layerDefinition = layerDefinition;
|
338 |
} |
339 |
|
340 |
} |