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