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