root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / ShpStrategy.java @ 9641
History | View | Annotate | Download (21.9 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.operations.strategies; |
42 |
|
43 |
import java.awt.Graphics2D; |
44 |
import java.awt.geom.Point2D; |
45 |
import java.awt.geom.Rectangle2D; |
46 |
import java.awt.image.BufferedImage; |
47 |
import java.io.IOException; |
48 |
import java.util.BitSet; |
49 |
import java.util.List; |
50 |
|
51 |
import javax.print.attribute.PrintRequestAttributeSet; |
52 |
|
53 |
import org.apache.log4j.Logger; |
54 |
import org.cresques.cts.ICoordTrans; |
55 |
import org.geotools.resources.geometry.XRectangle2D; |
56 |
|
57 |
import com.hardcode.driverManager.DriverLoadException; |
58 |
import com.iver.cit.gvsig.fmap.DriverException; |
59 |
import com.iver.cit.gvsig.fmap.ViewPort; |
60 |
import com.iver.cit.gvsig.fmap.core.FShape; |
61 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
62 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
63 |
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes; |
64 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
65 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
66 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
67 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
68 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
69 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
70 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
71 |
import com.iver.cit.gvsig.fmap.layers.SpatialCache; |
72 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
73 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial; |
74 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable; |
75 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer; |
76 |
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo; |
77 |
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
78 |
import com.iver.utiles.swing.threads.Cancellable; |
79 |
import com.iver.utiles.swing.threads.CancellableMonitorable; |
80 |
import com.vividsolutions.jts.geom.Geometry; |
81 |
import com.vividsolutions.jts.geom.IntersectionMatrix; |
82 |
|
83 |
/**
|
84 |
* Esta clase definir? las operaciones de la interfaz FLyrVect de la manera m?s
|
85 |
* ?ptima para los ficheros shp.
|
86 |
*/
|
87 |
public class ShpStrategy extends DefaultStrategy { |
88 |
private static Logger logger = Logger |
89 |
.getLogger(ShpStrategy.class.getName()); |
90 |
|
91 |
/**
|
92 |
* Crea una ShpStrategy.
|
93 |
*
|
94 |
* @param capa
|
95 |
*/
|
96 |
public ShpStrategy(FLayer capa) {
|
97 |
super(capa);
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
|
102 |
* java.awt.Graphics2D, ISymbol)
|
103 |
*/
|
104 |
public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, |
105 |
Cancellable cancel) throws DriverException {
|
106 |
try {
|
107 |
ReadableVectorial adapter = ((SingleLayer) getCapa()).getSource(); |
108 |
if (adapter.getShapeCount() <= 0) { |
109 |
// logger.info("Layer:" + getCapa().getName() + " sin registros");
|
110 |
return;
|
111 |
} |
112 |
|
113 |
// Selectable selection = (Selectable) getCapa();
|
114 |
FLyrVect lyr = (FLyrVect) getCapa(); |
115 |
Selectable selectable = lyr.getRecordset(); |
116 |
ICoordTrans ct = getCapa().getCoordTrans(); |
117 |
FBitSet bitSet = selectable.getSelection(); |
118 |
BoundedShapes shapeBounds; |
119 |
if (adapter instanceof BoundedShapes) |
120 |
shapeBounds = (BoundedShapes) adapter; |
121 |
else
|
122 |
shapeBounds = (BoundedShapes) adapter.getDriver(); |
123 |
// VectorialFileDriver driver = (VectorialFileDriver)
|
124 |
// adapter.getDriver();
|
125 |
// logger.debug("adapter.start() -> Layer:" + getCapa().getName());
|
126 |
adapter.start(); |
127 |
IGeometry geom; |
128 |
// if (adapter.getShapeCount() > 0) {
|
129 |
// geom = adapter.getShape(0);
|
130 |
// }
|
131 |
VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa()) |
132 |
.getLegend(); |
133 |
|
134 |
if (l instanceof ClassifiedLegendInfo) { |
135 |
ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) l; |
136 |
ISymbol[] symbs = clsfLegend.getSymbols();
|
137 |
// double rSym = 0;
|
138 |
// double maxRSym = -1;
|
139 |
|
140 |
for (int i = 0; i < symbs.length; i++) { |
141 |
// TODO: REVISAR LOS SIMBOLOS Y SUS TAMA?OS
|
142 |
|
143 |
/*
|
144 |
* Style2D pointSymbol = symbs[i].getPointStyle2D(); if
|
145 |
* (pointSymbol instanceof MarkStyle2D) { MarkStyle2D mrk2D =
|
146 |
* (MarkStyle2D) pointSymbol; rSym =
|
147 |
* viewPort.toMapDistance(mrk2D.getSize()); if (maxRSym <
|
148 |
* rSym) maxRSym = rSym; }
|
149 |
*/
|
150 |
} |
151 |
} |
152 |
|
153 |
Rectangle2D extent = viewPort.getAdjustedExtent();
|
154 |
// AffineTransform at = viewPort.getAffineTransform();
|
155 |
|
156 |
int sc;
|
157 |
|
158 |
Rectangle2D bounds;
|
159 |
|
160 |
long t1 = System.currentTimeMillis(); |
161 |
// logger.debug("getCapa().getRecordset().start()");
|
162 |
((FLyrVect) getCapa()).getRecordset().start(); |
163 |
|
164 |
// TODO: A revisar si es o no conveniente este sistema
|
165 |
// de comunicaci?n con los drivers.
|
166 |
DriverAttributes attr = adapter.getDriverAttributes(); |
167 |
boolean bMustClone = false; |
168 |
if (attr != null) { |
169 |
if (attr.isLoadedInMemory()) {
|
170 |
bMustClone = attr.isLoadedInMemory(); |
171 |
} |
172 |
} |
173 |
|
174 |
List lstIndexes = null; |
175 |
|
176 |
// If area of needed extent is less than fullExtent / 4,
|
177 |
// it will be worthy to use SpatialIndex.
|
178 |
// Otherwhise, we will not use it.
|
179 |
boolean bUseSpatialIndex = false; |
180 |
sc = adapter.getShapeCount(); |
181 |
// if (lyr.getSpatialIndex() != null) AZABALA
|
182 |
// long t11 = System.currentTimeMillis();
|
183 |
if (lyr.getISpatialIndex() != null) { |
184 |
if (isSpatialIndexNecessary(extent)) {
|
185 |
lstIndexes = lyr.getISpatialIndex().query(extent); |
186 |
// If the layer is reprojected, spatial index was created
|
187 |
// in its own projection, so we must to apply an inverse
|
188 |
// transform
|
189 |
if (ct != null) { |
190 |
Rectangle2D newExtent = ct.getInverted()
|
191 |
.convert(extent); |
192 |
// Rectangle2D newExtent = ct.convert(extent);
|
193 |
// lstIndexes = lyr.getISpatialIndex().query(extent);
|
194 |
lstIndexes = lyr.getISpatialIndex().query(newExtent); |
195 |
} else {
|
196 |
lstIndexes = lyr.getISpatialIndex().query(extent); |
197 |
} |
198 |
sc = lstIndexes.size(); |
199 |
bUseSpatialIndex = true;
|
200 |
}// if
|
201 |
}// if
|
202 |
/*
|
203 |
* long t12 = System.currentTimeMillis(); System.out.println("Tiempo
|
204 |
* en mirar el ?ndice espacial y recuperar los ?ndices:" +
|
205 |
* (t12-t11)); System.out.println("Numero de ?ndices:" + sc);
|
206 |
*/
|
207 |
|
208 |
// SpatialCache cache = lyr.createSpatialCache();
|
209 |
lyr.getSpatialCache().clearAll(); |
210 |
SpatialCache cache = lyr.getSpatialCache(); |
211 |
int i;
|
212 |
|
213 |
//En OS X con renderer Quartz (JRE<6), mezclar setRGB con dibujado geometrico en mismo BufferedImage
|
214 |
//provoca ralentizaci?n brutal. Lo evitamos separando los setRGB en otro BufferedImage y juntandolos luego.
|
215 |
boolean MAC_OS_X = System.getProperty("os.name").toLowerCase().startsWith("mac os x"); |
216 |
BufferedImage auxBI = null; |
217 |
if (MAC_OS_X) {
|
218 |
auxBI = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); |
219 |
} |
220 |
|
221 |
|
222 |
for (int aux = 0; aux < sc; aux++) { |
223 |
// Salimos si alguien cancela
|
224 |
if (cancel != null) { |
225 |
// azabala (por si acaso, al arreglar bug de process)
|
226 |
if (cancel.isCanceled()) {
|
227 |
break;
|
228 |
} |
229 |
} |
230 |
if (bUseSpatialIndex) {
|
231 |
Integer idRec = (Integer) lstIndexes.get(aux); |
232 |
i = idRec.intValue(); |
233 |
} else {
|
234 |
i = aux; |
235 |
} |
236 |
bounds = shapeBounds.getShapeBounds(i); |
237 |
if (ct != null) { |
238 |
bounds = ct.convert(bounds); |
239 |
} |
240 |
if (XRectangle2D.intersectInclusive(extent, bounds)) {
|
241 |
ISymbol symbol = l.getSymbol(i); |
242 |
|
243 |
if (bitSet.get(i)) {
|
244 |
symbol = symbol.getSymbolForSelection(); |
245 |
} |
246 |
|
247 |
if (symbol == null) |
248 |
continue;
|
249 |
|
250 |
boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
|
251 |
|
252 |
if (bPoint
|
253 |
|| ((bounds.getHeight() > viewPort.getDist1pixel()) || (bounds |
254 |
.getWidth() > viewPort.getDist1pixel()))) { |
255 |
geom = adapter.getShape(i); |
256 |
|
257 |
// PRUEBA DE VELOCIDAD
|
258 |
// geom = ShapeFactory.createPolygon2D(new
|
259 |
// GeneralPathX(bounds));
|
260 |
|
261 |
|
262 |
//JMVIVO: OJO, No colnamos siempre porque
|
263 |
// el FGeometry.drawInt (a diferencia del
|
264 |
// FGeometry.draw) clona siempre la geometria
|
265 |
// antes de pintarla (para transforma a enteros)
|
266 |
if (ct != null) { |
267 |
if (bMustClone)
|
268 |
geom = geom.cloneGeometry(); |
269 |
geom.reProject(ct); |
270 |
} |
271 |
if (lyr.isSpatialCacheEnabled()) {
|
272 |
if (cache.getMaxFeatures() >= cache.size()) {
|
273 |
// Ya reproyectado todo
|
274 |
cache.insert(bounds, geom); |
275 |
} |
276 |
} |
277 |
|
278 |
// FJP: CAMBIO: Sabemos que vamos a dibujar sobre una
|
279 |
// imagen, con coordenadas enteras, as?
|
280 |
// que lo tenemos en cuenta.
|
281 |
// ANTES: geom.draw(g, viewPort, symbol);
|
282 |
// AHORA:
|
283 |
geom.drawInts(g, viewPort, symbol); |
284 |
// geom.draw(g, viewPort, symbol);
|
285 |
/*
|
286 |
* if (lyr.isEditing()) { if (bitSet.get(i)) { Handler[]
|
287 |
* handlers = geom.getHandlers(IGeometry.SELECTHANDLER);
|
288 |
* FGraphicUtilities.DrawHandlers(g,
|
289 |
* viewPort.getAffineTransform(), handlers); } }
|
290 |
*/
|
291 |
|
292 |
} else {
|
293 |
Point2D.Double pOrig = new Point2D.Double(bounds |
294 |
.getMinX(), bounds.getMinY()); |
295 |
Point2D pDest, pDest2;
|
296 |
|
297 |
pDest = viewPort.getAffineTransform().transform(pOrig, |
298 |
null);
|
299 |
pDest2 = g.getTransform().transform(pDest, null);
|
300 |
|
301 |
int pixX = (int) pDest2.getX(); |
302 |
int pixY = (int) pDest2.getY(); |
303 |
if (symbol == null) |
304 |
continue;
|
305 |
if ((pixX > 0) && (pixX < image.getWidth())) { |
306 |
if ((pixY > 0) && (pixY < image.getHeight())) { |
307 |
if (MAC_OS_X) {
|
308 |
auxBI.setRGB(pixX, pixY, symbol.getOnePointRgb()); |
309 |
} else {
|
310 |
image.setRGB(pixX, pixY, symbol.getOnePointRgb()); |
311 |
} |
312 |
} |
313 |
} |
314 |
} |
315 |
} |
316 |
} |
317 |
|
318 |
if (MAC_OS_X) {
|
319 |
g.drawImage(auxBI,0,0,null); |
320 |
} |
321 |
|
322 |
// logger.debug("getCapa().getRecordset().stop()");
|
323 |
((FLyrVect) getCapa()).getSource().getRecordset().stop(); |
324 |
|
325 |
long t2 = System.currentTimeMillis(); |
326 |
// logger.debug("adapter.stop()");
|
327 |
adapter.stop(); |
328 |
|
329 |
// System.out.println(t2 - t1);
|
330 |
} catch (DriverIOException e) {
|
331 |
throw new DriverException(e); |
332 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
333 |
throw new DriverException(e); |
334 |
} catch (DriverException e) {
|
335 |
throw new DriverException(e); |
336 |
} catch (IOException e) { |
337 |
throw new DriverException(e); |
338 |
} catch (DriverLoadException e) {
|
339 |
// TODO Auto-generated catch block
|
340 |
e.printStackTrace(); |
341 |
} |
342 |
} |
343 |
|
344 |
/**
|
345 |
* M?todo utilizado para dibujar sobre el graphics que se pasa como
|
346 |
* par?metro, pensado para utilizarse para imprimir.
|
347 |
*
|
348 |
* @param g
|
349 |
* Graphics2D
|
350 |
* @param viewPort
|
351 |
* ViewPort.
|
352 |
* @param cancel
|
353 |
*
|
354 |
* @throws DriverException
|
355 |
*/
|
356 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet printProperties) |
357 |
throws DriverException {
|
358 |
// super.draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del
|
359 |
// padre, que es el que va sin acelaraci?n!!
|
360 |
try {
|
361 |
ReadableVectorial adapter = ((SingleLayer) getCapa()).getSource(); |
362 |
if (adapter.getShapeCount() <= 0) { |
363 |
// logger.info("Layer:" + getCapa().getName() + " sin registros");
|
364 |
return;
|
365 |
} |
366 |
FLyrVect lyr = (FLyrVect) getCapa(); |
367 |
lyr.beforePrinting(printProperties); |
368 |
|
369 |
Selectable selectable = lyr.getRecordset(); |
370 |
ICoordTrans ct = getCapa().getCoordTrans(); |
371 |
BitSet bitSet = selectable.getSelection();
|
372 |
BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver(); |
373 |
VectorialDriver driver = (VectorialDriver) adapter |
374 |
.getDriver(); |
375 |
// logger.debug("adapter.start() -> Layer:" + getCapa().getName());
|
376 |
adapter.start(); |
377 |
IGeometry geom; |
378 |
if (adapter.getShapeCount() > 0) { |
379 |
geom = adapter.getShape(0);
|
380 |
} |
381 |
VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa()) |
382 |
.getLegend(); |
383 |
|
384 |
Rectangle2D extent = viewPort.getAdjustedExtent();
|
385 |
// AffineTransform at = viewPort.getAffineTransform();
|
386 |
|
387 |
int sc;
|
388 |
|
389 |
Rectangle2D bounds;
|
390 |
|
391 |
sc = adapter.getShapeCount(); |
392 |
|
393 |
//long t1 = System.currentTimeMillis();
|
394 |
// logger.debug("getCapa().getRecordset().start()");
|
395 |
((AlphanumericData) getCapa()).getRecordset().start(); |
396 |
|
397 |
// TODO: A revisar si es o no conveniente este sistema
|
398 |
// de comunicaci?n con los drivers.
|
399 |
DriverAttributes attr = adapter.getDriverAttributes(); |
400 |
boolean bMustClone = false; |
401 |
if (attr != null) { |
402 |
if (attr.isLoadedInMemory()) {
|
403 |
bMustClone = attr.isLoadedInMemory(); |
404 |
} |
405 |
} |
406 |
|
407 |
List lstIndexes = null; |
408 |
|
409 |
boolean bUseSpatialIndex = false; |
410 |
if (lyr.getISpatialIndex() != null) { |
411 |
if (isSpatialIndexNecessary(extent)) {
|
412 |
lstIndexes = lyr.getISpatialIndex().query(extent); |
413 |
if (ct != null) { |
414 |
Rectangle2D newExtent = ct.getInverted()
|
415 |
.convert(extent); |
416 |
lstIndexes = lyr.getISpatialIndex().query(newExtent); |
417 |
} else {
|
418 |
lstIndexes = lyr.getISpatialIndex().query(extent); |
419 |
} |
420 |
sc = lstIndexes.size(); |
421 |
bUseSpatialIndex = true;
|
422 |
}// if
|
423 |
}// if
|
424 |
|
425 |
int i;
|
426 |
for (int aux = 0; aux < sc; aux++) { |
427 |
if (bUseSpatialIndex) {
|
428 |
Integer idRec = (Integer) lstIndexes.get(aux); |
429 |
i = idRec.intValue(); |
430 |
} else {
|
431 |
i = aux; |
432 |
} |
433 |
|
434 |
bounds = shapeBounds.getShapeBounds(i); |
435 |
|
436 |
if (ct != null) { |
437 |
bounds = ct.convert(bounds); |
438 |
} |
439 |
|
440 |
if (XRectangle2D.intersectInclusive(extent, bounds)) {
|
441 |
ISymbol symbol = l.getSymbol(i); |
442 |
if (symbol == null) |
443 |
continue;
|
444 |
if (bitSet.get(i)) {
|
445 |
symbol = symbol.getSymbolForSelection(); |
446 |
} |
447 |
|
448 |
//boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
|
449 |
|
450 |
geom = driver.getShape(i); |
451 |
|
452 |
// PRUEBA DE VELOCIDAD
|
453 |
// geom = ShapeFactory.createPolygon2D(new
|
454 |
// GeneralPathX(bounds));
|
455 |
|
456 |
// JMVIVO: Clonamos siempre que sea necesario y no
|
457 |
// solo si hay que reproyectar. Porque el FGeometry.draw
|
458 |
// va a aplicar la transformacion sobre la geometria original
|
459 |
if (bMustClone)
|
460 |
geom = geom.cloneGeometry(); |
461 |
|
462 |
if (ct != null) { |
463 |
geom.reProject(ct); |
464 |
} |
465 |
|
466 |
geom.draw(g, viewPort, symbol); |
467 |
|
468 |
} |
469 |
} |
470 |
|
471 |
// logger.debug("getCapa().getRecordset().stop()");
|
472 |
((AlphanumericData) getCapa()).getRecordset().stop(); |
473 |
|
474 |
//long t2 = System.currentTimeMillis();
|
475 |
// logger.debug("adapter.stop()");
|
476 |
adapter.stop(); |
477 |
lyr.afterPrinting(); |
478 |
// System.out.println(t2 - t1);
|
479 |
} catch (DriverIOException e) {
|
480 |
throw new DriverException(e); |
481 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
482 |
throw new DriverException(e); |
483 |
} catch (DriverException e) {
|
484 |
throw new DriverException(e); |
485 |
} catch (IOException e) { |
486 |
throw new DriverException(e); |
487 |
} |
488 |
|
489 |
|
490 |
} |
491 |
|
492 |
public FBitSet queryByShape(IGeometry g, int relationship) |
493 |
throws DriverException, VisitException {
|
494 |
return queryByShape(g, relationship, null); |
495 |
} |
496 |
|
497 |
/*
|
498 |
* (non-Javadoc)
|
499 |
*
|
500 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.core.IGeometry,
|
501 |
* int)
|
502 |
*/
|
503 |
public FBitSet queryByShape(IGeometry g, int relationship, |
504 |
CancellableMonitorable cancel) throws DriverException,
|
505 |
VisitException { |
506 |
// Si hay un ?ndice espacial, lo usamos para hacer el query.
|
507 |
FLyrVect lyr = (FLyrVect) capa; |
508 |
// if (lyr.getSpatialIndex() == null) AZABALA
|
509 |
if (lyr.getISpatialIndex() == null) |
510 |
return super.queryByShape(g, relationship, null); |
511 |
|
512 |
long t1 = System.currentTimeMillis(); |
513 |
ReadableVectorial va = lyr.getSource(); |
514 |
ICoordTrans ct = lyr.getCoordTrans(); |
515 |
Rectangle2D bounds = g.getBounds2D();
|
516 |
List lstRecs = lyr.getISpatialIndex().query(bounds);
|
517 |
Integer idRec;
|
518 |
FBitSet bitset = new FBitSet();
|
519 |
Geometry jtsShape = g.toJTSGeometry(); |
520 |
IntersectionMatrix m; |
521 |
int index;
|
522 |
try {
|
523 |
va.start(); |
524 |
|
525 |
for (int i = 0; i < lstRecs.size(); i++) { |
526 |
if (cancel != null) { |
527 |
cancel.reportStep(); |
528 |
if (cancel.isCanceled()) {
|
529 |
break;
|
530 |
} |
531 |
} |
532 |
idRec = (Integer) lstRecs.get(i);
|
533 |
index = idRec.intValue(); |
534 |
IGeometry geom = va.getShape(index); |
535 |
if(geom == null) |
536 |
continue;
|
537 |
if (ct != null) { |
538 |
geom.reProject(ct); |
539 |
} |
540 |
Geometry jtsGeom = geom.toJTSGeometry(); |
541 |
switch (relationship) {
|
542 |
case CONTAINS:
|
543 |
m = jtsShape.relate(jtsGeom); |
544 |
if (m.isContains()) {
|
545 |
bitset.set(index, true);
|
546 |
} |
547 |
break;
|
548 |
|
549 |
case CROSSES:
|
550 |
m = jtsShape.relate(jtsGeom); |
551 |
if (m.isCrosses(jtsGeom.getDimension(), jtsShape
|
552 |
.getDimension())) { |
553 |
bitset.set(index, true);
|
554 |
} |
555 |
break;
|
556 |
|
557 |
case DISJOINT:
|
558 |
// TODO: CREO QUE EL DISJOINT NO SE PUEDE METER AQUI
|
559 |
m = jtsShape.relate(jtsGeom); |
560 |
if (m.isDisjoint()) {
|
561 |
bitset.set(index, true);
|
562 |
} |
563 |
break;
|
564 |
|
565 |
case EQUALS:
|
566 |
m = jtsShape.relate(jtsGeom); |
567 |
if (m.isEquals(jtsGeom.getDimension(), jtsShape
|
568 |
.getDimension())) { |
569 |
bitset.set(index, true);
|
570 |
} |
571 |
break;
|
572 |
|
573 |
case INTERSECTS:
|
574 |
m = jtsShape.relate(jtsGeom); |
575 |
if (m.isIntersects()) {
|
576 |
bitset.set(index, true);
|
577 |
} |
578 |
break;
|
579 |
|
580 |
case OVERLAPS:
|
581 |
m = jtsShape.relate(jtsGeom); |
582 |
if (m.isOverlaps(jtsGeom.getDimension(), jtsShape
|
583 |
.getDimension())) { |
584 |
bitset.set(index, true);
|
585 |
} |
586 |
|
587 |
break;
|
588 |
|
589 |
case TOUCHES:
|
590 |
m = jtsShape.relate(jtsGeom); |
591 |
if (m.isTouches(jtsGeom.getDimension(), jtsShape
|
592 |
.getDimension())) { |
593 |
bitset.set(index, true);
|
594 |
} |
595 |
|
596 |
break;
|
597 |
|
598 |
case WITHIN:
|
599 |
m = jtsShape.relate(jtsGeom); |
600 |
if (m.isWithin()) {
|
601 |
bitset.set(index, true);
|
602 |
} |
603 |
|
604 |
break;
|
605 |
} |
606 |
} |
607 |
va.stop(); |
608 |
} catch (DriverIOException e) {
|
609 |
// TODO Auto-generated catch block
|
610 |
e.printStackTrace(); |
611 |
} |
612 |
long t2 = System.currentTimeMillis(); |
613 |
// logger.info("queryByShape optimizado sobre la capa " + lyr.getName()
|
614 |
// + ". " + (t2 - t1) + " mseg.");
|
615 |
return bitset;
|
616 |
} |
617 |
|
618 |
public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) |
619 |
throws DriverException {
|
620 |
// Si hay un ?ndice espacial, lo usamos para hacer el query.
|
621 |
FLyrVect lyr = (FLyrVect) capa; |
622 |
if (lyr.getISpatialIndex() == null) |
623 |
return super.queryByRect(rect, cancel); |
624 |
|
625 |
ReadableVectorial va = lyr.getSource(); |
626 |
ICoordTrans ct = lyr.getCoordTrans(); |
627 |
Rectangle2D bounds = rect;
|
628 |
List lstRecs = lyr.getISpatialIndex().query(bounds);
|
629 |
Integer idRec;
|
630 |
FBitSet bitset = new FBitSet();
|
631 |
int index;
|
632 |
try {
|
633 |
va.start(); |
634 |
DriverAttributes attr = va.getDriverAttributes(); |
635 |
boolean bMustClone = false; |
636 |
if (attr != null) { |
637 |
if (attr.isLoadedInMemory()) {
|
638 |
bMustClone = attr.isLoadedInMemory(); |
639 |
} |
640 |
} |
641 |
|
642 |
for (int i = 0; i < lstRecs.size(); i++) { |
643 |
if (cancel != null) { |
644 |
cancel.reportStep(); |
645 |
if (cancel.isCanceled()) {
|
646 |
va.stop(); |
647 |
return bitset;
|
648 |
} |
649 |
} |
650 |
idRec = (Integer) lstRecs.get(i);
|
651 |
index = idRec.intValue(); |
652 |
IGeometry geom = va.getShape(index); |
653 |
if(geom == null)//azabala |
654 |
continue;
|
655 |
if (ct != null) { |
656 |
if (bMustClone)
|
657 |
geom = geom.cloneGeometry(); |
658 |
geom.reProject(ct); |
659 |
} |
660 |
if (geom.intersects(rect))
|
661 |
bitset.set(index, true);
|
662 |
} |
663 |
va.stop(); |
664 |
} catch (DriverIOException e) {
|
665 |
// TODO Auto-generated catch block
|
666 |
e.printStackTrace(); |
667 |
} |
668 |
return bitset;
|
669 |
|
670 |
} |
671 |
|
672 |
public void process(FeatureVisitor visitor, Rectangle2D rectangle) |
673 |
throws DriverException, VisitException {
|
674 |
process(visitor, rectangle, null);
|
675 |
} |
676 |
|
677 |
/**
|
678 |
* Processes (by calling visitor.process() method) only those features of
|
679 |
* the vectorial layer associated which intersects given rectangle2d.
|
680 |
*
|
681 |
*/
|
682 |
|
683 |
public void process(FeatureVisitor visitor, Rectangle2D rectangle, |
684 |
CancellableMonitorable cancel) throws DriverException,
|
685 |
VisitException { |
686 |
if (visitor.start(capa)) {
|
687 |
FLyrVect lyr = (FLyrVect) capa; |
688 |
// if we dont have spatial index or...
|
689 |
if (lyr.getISpatialIndex() == null) { |
690 |
super.process(visitor, rectangle, cancel);
|
691 |
return;
|
692 |
} |
693 |
// if spatial index is not worthy
|
694 |
if (!isSpatialIndexNecessary(rectangle)) {
|
695 |
super.process(visitor, rectangle, cancel);
|
696 |
return;
|
697 |
} |
698 |
|
699 |
ReadableVectorial va = lyr.getSource(); |
700 |
ICoordTrans ct = lyr.getCoordTrans(); |
701 |
Rectangle2D bounds = rectangle;
|
702 |
List lstRecs = lyr.getISpatialIndex().query(bounds);
|
703 |
Integer idRec;
|
704 |
int index;
|
705 |
try {
|
706 |
va.start(); |
707 |
DriverAttributes attr = va.getDriverAttributes(); |
708 |
boolean bMustClone = false; |
709 |
if (attr != null) { |
710 |
if (attr.isLoadedInMemory()) {
|
711 |
bMustClone = attr.isLoadedInMemory(); |
712 |
} |
713 |
} |
714 |
|
715 |
for (int i = 0; i < lstRecs.size(); i++) { |
716 |
if (cancel != null) { |
717 |
cancel.reportStep(); |
718 |
} |
719 |
if (verifyCancelation(cancel, va, visitor))
|
720 |
return;
|
721 |
idRec = (Integer) lstRecs.get(i);
|
722 |
index = idRec.intValue(); |
723 |
IGeometry geom = va.getShape(index); |
724 |
if(geom == null)//azabala |
725 |
continue;
|
726 |
if (ct != null) { |
727 |
if (bMustClone)
|
728 |
geom = geom.cloneGeometry(); |
729 |
geom.reProject(ct); |
730 |
} |
731 |
if (geom.intersects(rectangle))
|
732 |
visitor.visit(geom, index); |
733 |
}// for
|
734 |
va.stop(); |
735 |
} catch (DriverIOException e) {
|
736 |
// TODO Auto-generated catch block
|
737 |
e.printStackTrace(); |
738 |
} |
739 |
}// if visitor.start
|
740 |
} |
741 |
|
742 |
/*
|
743 |
* (non-Javadoc)
|
744 |
*
|
745 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(java.awt.geom.Point2D,
|
746 |
* double)
|
747 |
*/
|
748 |
public FBitSet queryByPoint(Point2D p, double tolerance, |
749 |
CancellableMonitorable cancel) throws DriverException {
|
750 |
// TODO: OJO!!!!. Est? implementado como un rectangulo.
|
751 |
// Lo correcto deber?a ser calculando las distancias reales
|
752 |
// es decir, con un c?rculo.
|
753 |
Rectangle2D recPoint = new Rectangle2D.Double(p.getX() |
754 |
- (tolerance / 2), p.getY() - (tolerance / 2), tolerance, |
755 |
tolerance); |
756 |
return queryByRect(recPoint, cancel);
|
757 |
} |
758 |
} |