root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / intersection / fmap / IntersectVisitor.java @ 5412
History | View | Annotate | Download (9.13 KB)
1 |
/*
|
---|---|
2 |
* Created on 21-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: IntersectVisitor.java 5412 2006-05-24 21:15:07Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.1 2006-05-24 21:10:40 azabala
|
49 |
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
|
50 |
*
|
51 |
* Revision 1.6 2006/05/01 19:12:17 azabala
|
52 |
* optimizaciones haciendo uso de strategy.process(rectangle) (el bitset se estaba recorriendo secuencial, no aleatorio)
|
53 |
*
|
54 |
* Revision 1.5 2006/03/26 20:03:06 azabala
|
55 |
* *** empty log message ***
|
56 |
*
|
57 |
* Revision 1.4 2006/03/23 21:04:36 azabala
|
58 |
* *** empty log message ***
|
59 |
*
|
60 |
* Revision 1.3 2006/03/21 19:29:18 azabala
|
61 |
* *** empty log message ***
|
62 |
*
|
63 |
* Revision 1.2 2006/03/07 21:01:33 azabala
|
64 |
* *** empty log message ***
|
65 |
*
|
66 |
* Revision 1.1 2006/03/06 19:48:39 azabala
|
67 |
* *** empty log message ***
|
68 |
*
|
69 |
* Revision 1.2 2006/03/05 19:58:47 azabala
|
70 |
* *** empty log message ***
|
71 |
*
|
72 |
* Revision 1.1 2006/02/26 20:54:38 azabala
|
73 |
* *** empty log message ***
|
74 |
*
|
75 |
*
|
76 |
*/
|
77 |
package com.iver.cit.gvsig.geoprocess.intersection.fmap; |
78 |
|
79 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
80 |
import com.hardcode.gdbms.engine.values.Value; |
81 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
82 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
83 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
84 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
85 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
86 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
87 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
88 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
89 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
90 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
91 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
92 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData; |
93 |
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor; |
94 |
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy; |
95 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
96 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureFactory; |
97 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
98 |
import com.vividsolutions.jts.geom.Geometry; |
99 |
import com.vividsolutions.jts.geom.MultiPolygon; |
100 |
import com.vividsolutions.jts.geom.Polygon; |
101 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
102 |
|
103 |
/**
|
104 |
* Computes intersections of features of a layer with features of an overlay
|
105 |
* layer.
|
106 |
*
|
107 |
* @author azabala
|
108 |
*
|
109 |
* FIXME Crear una clase abstracta: OverlayVisitor, pues todos los visitors de
|
110 |
* un overlay son muy parecidos.
|
111 |
*
|
112 |
*/
|
113 |
public class IntersectVisitor implements FeatureVisitor { |
114 |
|
115 |
/**
|
116 |
* Allows to get attributes of first layer features which is being
|
117 |
* intersected
|
118 |
*/
|
119 |
SelectableDataSource firstRs; |
120 |
|
121 |
/**
|
122 |
* Number of fields of first layer recordset
|
123 |
*/
|
124 |
int numFieldsA;
|
125 |
|
126 |
/**
|
127 |
* looks for overlay features of the processed feauture by spatial criteria
|
128 |
* (queryByEnvelope)
|
129 |
*/
|
130 |
FLyrVect overlayLayer; |
131 |
|
132 |
/**
|
133 |
* Strategy to process overlay layer
|
134 |
*/
|
135 |
Strategy strategy; |
136 |
|
137 |
/**
|
138 |
* flag to decide if process onlye features selected in overlay layer.
|
139 |
*/
|
140 |
boolean onlyOverlaySelection;
|
141 |
|
142 |
/**
|
143 |
* Gets attributes of second layer features which are being intersected.
|
144 |
*/
|
145 |
SelectableDataSource secondRs; |
146 |
|
147 |
/**
|
148 |
* Number of fields of second layer recordset
|
149 |
*/
|
150 |
int numFieldsB;
|
151 |
|
152 |
/**
|
153 |
* Schema of the result layer
|
154 |
*/
|
155 |
ILayerDefinition layerDefinition; |
156 |
|
157 |
/**
|
158 |
* It processes features resulting of intersetions. It could saves them in
|
159 |
* persistent datastore, caching them, reprocess them, reprojects them, etc.
|
160 |
*/
|
161 |
FeatureProcessor featureProcessor; |
162 |
|
163 |
/**
|
164 |
* Constructor
|
165 |
* @param overlayLayer
|
166 |
* @param processor
|
167 |
* @param strategy
|
168 |
* @param onlyOverlaySelection
|
169 |
* @throws DriverException
|
170 |
* @throws com.iver.cit.gvsig.fmap.DriverException
|
171 |
*/
|
172 |
public IntersectVisitor(FLyrVect overlayLayer, FeatureProcessor processor,
|
173 |
Strategy strategy, boolean onlyOverlaySelection)
|
174 |
throws DriverException, com.iver.cit.gvsig.fmap.DriverException {
|
175 |
this.overlayLayer = overlayLayer;
|
176 |
this.featureProcessor = processor;
|
177 |
this.strategy = strategy;
|
178 |
this.onlyOverlaySelection = onlyOverlaySelection;
|
179 |
secondRs = overlayLayer.getRecordset(); |
180 |
numFieldsB = secondRs.getFieldCount(); |
181 |
} |
182 |
|
183 |
public void visit(IGeometry g, final int index) throws VisitException { |
184 |
|
185 |
final Geometry firstJts = g.toJTSGeometry();
|
186 |
final boolean onlyOverlay = onlyOverlaySelection; |
187 |
try {
|
188 |
strategy.process(new FeatureVisitor(){
|
189 |
public void visit(IGeometry g, int indexOverlay) throws VisitException { |
190 |
if(onlyOverlay){
|
191 |
try {
|
192 |
if(!overlayLayer.getRecordset().getSelection().get(index))
|
193 |
return;
|
194 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
195 |
throw new VisitException( |
196 |
"Error en interseccion: verificando si un posible overlay esta seleccionado", e);
|
197 |
}//geometry g is not selected
|
198 |
} |
199 |
Geometry overlayJts = g.toJTSGeometry(); |
200 |
if (firstJts.intersects(overlayJts)) {
|
201 |
Geometry newGeoJts = EnhancedPrecisionOp.intersection(firstJts, overlayJts); |
202 |
if (!(newGeoJts instanceof Polygon) |
203 |
&& !(newGeoJts instanceof MultiPolygon)) {
|
204 |
// intersection of adjacent polygons is a linestring
|
205 |
// but we are not interested in it
|
206 |
return;
|
207 |
} |
208 |
IFeature intersectionFeature; |
209 |
try {
|
210 |
intersectionFeature = createFeature(newGeoJts, |
211 |
index, indexOverlay); |
212 |
} catch (DriverException e) {
|
213 |
throw new VisitException( |
214 |
"Error al crear el feature resultante de la interseccion");
|
215 |
} |
216 |
featureProcessor.processFeature(intersectionFeature); |
217 |
}// if intersects
|
218 |
} |
219 |
|
220 |
public String getProcessDescription() { |
221 |
return "Computing intersections of a polygon with its adjacents"; |
222 |
} |
223 |
public void stop(FLayer layer) { |
224 |
} |
225 |
public boolean start(FLayer layer) { |
226 |
return true; |
227 |
}},g.getBounds2D()); |
228 |
|
229 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
230 |
throw new VisitException( |
231 |
"Error buscando los overlays que intersectan con un feature");
|
232 |
} |
233 |
} |
234 |
|
235 |
public void stop(FLayer layer) { |
236 |
featureProcessor.finish(); |
237 |
} |
238 |
|
239 |
public boolean start(FLayer layer) { |
240 |
if (layer instanceof AlphanumericData && layer instanceof VectorialData) { |
241 |
try {
|
242 |
this.firstRs = ((AlphanumericData) layer).getRecordset();
|
243 |
numFieldsA = firstRs.getFieldCount(); |
244 |
this.featureProcessor.start();
|
245 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
246 |
return false; |
247 |
} catch (DriverException e) {
|
248 |
return false; |
249 |
} catch (EditionException e) {
|
250 |
return false; |
251 |
} |
252 |
return true; |
253 |
} |
254 |
return false; |
255 |
} |
256 |
|
257 |
|
258 |
/**
|
259 |
* TODO Meter este metodo en FeatureFactory, porque lo estoy copypasteando
|
260 |
* en todos los Visitor
|
261 |
*
|
262 |
* FIXME El esquema es resultado de la mezcla de los esquemas de dos capas
|
263 |
* ?Como saber qu? campo de ILayerDefinition se toma de firstRs y cual de secondRs?
|
264 |
* Mirando DefinitionUtils, vemos que primero se recorre firstLayer,
|
265 |
* y luego secondLayer. REVISAR
|
266 |
*/
|
267 |
private IFeature createFeature(Geometry jtsGeometry, int firstLayerIndex, |
268 |
int overlayLayerIndex) throws DriverException { |
269 |
IFeature solution = null;
|
270 |
IGeometry intersectGeometry = FConverter.jts_to_igeometry(jtsGeometry); |
271 |
|
272 |
|
273 |
Value[] featureAttr = new Value[numFieldsA + numFieldsB]; |
274 |
for (int indexField = 0; indexField < numFieldsA; indexField++) { |
275 |
featureAttr[indexField] = firstRs.getFieldValue(firstLayerIndex, |
276 |
indexField); |
277 |
} |
278 |
for (int indexFieldB = 0; indexFieldB < numFieldsB; indexFieldB++) { |
279 |
featureAttr[numFieldsA + indexFieldB] = secondRs.getFieldValue( |
280 |
overlayLayerIndex, indexFieldB); |
281 |
} |
282 |
solution = FeatureFactory.createFeature(featureAttr, intersectGeometry); |
283 |
return solution;
|
284 |
|
285 |
} |
286 |
|
287 |
public void setFeatureProcessor(FeatureProcessor featureProcessor) { |
288 |
this.featureProcessor = featureProcessor;
|
289 |
} |
290 |
|
291 |
public String getProcessDescription() { |
292 |
return "Computing intersections between two layers"; |
293 |
} |
294 |
|
295 |
public ILayerDefinition getLayerDefinition() {
|
296 |
return layerDefinition;
|
297 |
} |
298 |
|
299 |
public void setLayerDefinition(ILayerDefinition layerDefinition) { |
300 |
this.layerDefinition = layerDefinition;
|
301 |
} |
302 |
|
303 |
} |