Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_RELEASE / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / ShpStrategy.java @ 9167

History | View | Annotate | Download (20.8 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.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.VectorialFileDriver;
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
                                                if (ct != null) {
262
                                                        if (bMustClone)
263
                                                                geom = geom.cloneGeometry();
264
                                                        geom.reProject(ct);
265
                                                }
266
                                                if (lyr.isSpatialCacheEnabled()) {
267
                                                        if (cache.getMaxFeatures() >= cache.size()) {
268
                                                                // Ya reproyectado todo
269
                                                                cache.insert(bounds, geom);
270
                                                        }
271
                                                }
272

    
273
                                                // FJP: CAMBIO: Sabemos que vamos a dibujar sobre una
274
                                                // imagen, con coordenadas enteras, as?
275
                                                // que lo tenemos en cuenta.
276
                                                // ANTES: geom.draw(g, viewPort, symbol);
277
                                                // AHORA:
278
                                                geom.drawInts(g, viewPort, symbol);
279
                                                // geom.draw(g, viewPort, symbol);
280
                                                /*
281
                                                 * if (lyr.isEditing()) { if (bitSet.get(i)) { Handler[]
282
                                                 * handlers = geom.getHandlers(IGeometry.SELECTHANDLER);
283
                                                 * FGraphicUtilities.DrawHandlers(g,
284
                                                 * viewPort.getAffineTransform(), handlers); } }
285
                                                 */
286

    
287
                                        } else {
288
                                                Point2D.Double pOrig = new Point2D.Double(bounds
289
                                                                .getMinX(), bounds.getMinY());
290
                                                Point2D pDest, pDest2;
291

    
292
                                                pDest = viewPort.getAffineTransform().transform(pOrig,
293
                                                                null);
294
                                                pDest2 = g.getTransform().transform(pDest, null);
295

    
296
                                                int pixX = (int) pDest2.getX();
297
                                                int pixY = (int) pDest2.getY();
298
                                                if (symbol == null)
299
                                                        continue;
300
                                                if ((pixX > 0) && (pixX < image.getWidth())) {
301
                                                        if ((pixY > 0) && (pixY < image.getHeight())) {
302
                                                                if (MAC_OS_X) {
303
                                                                        auxBI.setRGB(pixX, pixY, symbol.getOnePointRgb());
304
                                                                } else {
305
                                                                        image.setRGB(pixX, pixY, symbol.getOnePointRgb());
306
                                                                }
307
                                                        }
308
                                                }
309
                                        }
310
                                }
311
                        }
312

    
313
                        if (MAC_OS_X) {
314
                                g.drawImage(auxBI,0,0,null);
315
                        }
316

    
317
                        // logger.debug("getCapa().getRecordset().stop()");
318
                        ((FLyrVect) getCapa()).getSource().getRecordset().stop();
319

    
320
                        long t2 = System.currentTimeMillis();
321
                        // logger.debug("adapter.stop()");
322
                        adapter.stop();
323

    
324
                        // System.out.println(t2 - t1);
325
                } catch (DriverIOException e) {
326
                        throw new DriverException(e);
327
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
328
                        throw new DriverException(e);
329
                } catch (DriverException e) {
330
                        throw new DriverException(e);
331
                } catch (IOException e) {
332
                        throw new DriverException(e);
333
                } catch (DriverLoadException e) {
334
                        // TODO Auto-generated catch block
335
                        e.printStackTrace();
336
                }
337
        }
338

    
339
        /**
340
         * M?todo utilizado para dibujar sobre el graphics que se pasa como
341
         * par?metro, pensado para utilizarse para imprimir.
342
         *
343
         * @param g
344
         *            Graphics2D
345
         * @param viewPort
346
         *            ViewPort.
347
         * @param cancel
348
         *
349
         * @throws DriverException
350
         */
351
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet printProperties)
352
                        throws DriverException {
353
                // super.draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del
354
                // padre, que es el que va sin acelaraci?n!!
355
                try {
356
                        ReadableVectorial adapter = ((SingleLayer) getCapa()).getSource();
357
                        if (adapter.getShapeCount() <= 0) {
358
//                                logger.info("Layer:" + getCapa().getName() + " sin registros");
359
                                return;
360
                        }
361
                        FLyrVect lyr = (FLyrVect) getCapa();
362
                        lyr.beforePrinting(printProperties);
363

    
364
                        Selectable selectable = lyr.getRecordset();
365
                        ICoordTrans ct = getCapa().getCoordTrans();
366
                        BitSet bitSet = selectable.getSelection();
367
                        BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver();
368
                        VectorialFileDriver driver = (VectorialFileDriver) adapter
369
                                        .getDriver();
370
                        // logger.debug("adapter.start() -> Layer:" + getCapa().getName());
371
                        adapter.start();
372
                        IGeometry geom;
373
                        if (adapter.getShapeCount() > 0) {
374
                                geom = adapter.getShape(0);
375
                        }
376
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa())
377
                                        .getLegend();
378

    
379
                        Rectangle2D extent = viewPort.getAdjustedExtent();
380
                        // AffineTransform at = viewPort.getAffineTransform();
381

    
382
                        int sc;
383

    
384
                        Rectangle2D bounds;
385

    
386
                        sc = adapter.getShapeCount();
387

    
388
                        long t1 = System.currentTimeMillis();
389
                        // logger.debug("getCapa().getRecordset().start()");
390
                        ((AlphanumericData) getCapa()).getRecordset().start();
391

    
392
                        // TODO: A revisar si es o no conveniente este sistema
393
                        // de comunicaci?n con los drivers.
394
                        DriverAttributes attr = adapter.getDriverAttributes();
395
                        boolean bMustClone = false;
396
                        if (attr != null) {
397
                                if (attr.isLoadedInMemory()) {
398
                                        bMustClone = attr.isLoadedInMemory();
399
                                }
400
                        }
401

    
402
                        for (int i = 0; i < sc; i++) {
403
                                bounds = shapeBounds.getShapeBounds(i);
404

    
405
                                if (ct != null) {
406
                                        bounds = ct.convert(bounds);
407
                                }
408

    
409
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
410
                                        ISymbol symbol = l.getSymbol(i);
411
                                        if (symbol == null)
412
                                                continue;
413
                                        if (bitSet.get(i)) {
414
                                                symbol = symbol.getSymbolForSelection();
415
                                        }
416

    
417
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
418

    
419
                                        geom = driver.getShape(i);
420

    
421
                                        // PRUEBA DE VELOCIDAD
422
                                        // geom = ShapeFactory.createPolygon2D(new
423
                                        // GeneralPathX(bounds));
424

    
425
                                        if (ct != null) {
426
                                                if (bMustClone)
427
                                                        geom = geom.cloneGeometry();
428
                                                geom.reProject(ct);
429
                                        }
430

    
431
                                        geom.draw(g, viewPort, symbol);
432

    
433
                                }
434
                        }
435

    
436
                        // logger.debug("getCapa().getRecordset().stop()");
437
                        ((AlphanumericData) getCapa()).getRecordset().stop();
438

    
439
                        long t2 = System.currentTimeMillis();
440
                        // logger.debug("adapter.stop()");
441
                        adapter.stop();
442
                        lyr.afterPrinting();
443
                        // System.out.println(t2 - t1);
444
                } catch (DriverIOException e) {
445
                        throw new DriverException(e);
446
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
447
                        throw new DriverException(e);
448
                } catch (DriverException e) {
449
                        throw new DriverException(e);
450
                } catch (IOException e) {
451
                        throw new DriverException(e);
452
                }
453

    
454

    
455
        }
456

    
457
        public FBitSet queryByShape(IGeometry g, int relationship)
458
                        throws DriverException, VisitException {
459
                return queryByShape(g, relationship, null);
460
        }
461

    
462
        /*
463
         * (non-Javadoc)
464
         *
465
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.core.IGeometry,
466
         *      int)
467
         */
468
        public FBitSet queryByShape(IGeometry g, int relationship,
469
                        CancellableMonitorable cancel) throws DriverException,
470
                        VisitException {
471
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
472
                FLyrVect lyr = (FLyrVect) capa;
473
                // if (lyr.getSpatialIndex() == null) AZABALA
474
                if (lyr.getISpatialIndex() == null)
475
                        return super.queryByShape(g, relationship, null);
476

    
477
                long t1 = System.currentTimeMillis();
478
                ReadableVectorial va = lyr.getSource();
479
                ICoordTrans ct = lyr.getCoordTrans();
480
                Rectangle2D bounds = g.getBounds2D();
481
                List lstRecs = lyr.getISpatialIndex().query(bounds);
482
                Integer idRec;
483
                FBitSet bitset = new FBitSet();
484
                Geometry jtsShape = g.toJTSGeometry();
485
                IntersectionMatrix m;
486
                int index;
487
                try {
488
                        va.start();
489

    
490
                        for (int i = 0; i < lstRecs.size(); i++) {
491
                                if (cancel != null) {
492
                                        cancel.reportStep();
493
                                        if (cancel.isCanceled()) {
494
                                                break;
495
                                        }
496
                                }
497
                                idRec = (Integer) lstRecs.get(i);
498
                                index = idRec.intValue();
499
                                IGeometry geom = va.getShape(index);
500
                                if(geom == null)
501
                                        continue;
502
                                if (ct != null) {
503
                                        geom.reProject(ct);
504
                                }
505
                                Geometry jtsGeom = geom.toJTSGeometry();
506
                                switch (relationship) {
507
                                case CONTAINS:
508
                                        m = jtsShape.relate(jtsGeom);
509
                                        if (m.isContains()) {
510
                                                bitset.set(index, true);
511
                                        }
512
                                        break;
513

    
514
                                case CROSSES:
515
                                        m = jtsShape.relate(jtsGeom);
516
                                        if (m.isCrosses(jtsGeom.getDimension(), jtsShape
517
                                                        .getDimension())) {
518
                                                bitset.set(index, true);
519
                                        }
520
                                        break;
521

    
522
                                case DISJOINT:
523
                                        // TODO: CREO QUE EL DISJOINT NO SE PUEDE METER AQUI
524
                                        m = jtsShape.relate(jtsGeom);
525
                                        if (m.isDisjoint()) {
526
                                                bitset.set(index, true);
527
                                        }
528
                                        break;
529

    
530
                                case EQUALS:
531
                                        m = jtsShape.relate(jtsGeom);
532
                                        if (m.isEquals(jtsGeom.getDimension(), jtsShape
533
                                                        .getDimension())) {
534
                                                bitset.set(index, true);
535
                                        }
536
                                        break;
537

    
538
                                case INTERSECTS:
539
                                        m = jtsShape.relate(jtsGeom);
540
                                        if (m.isIntersects()) {
541
                                                bitset.set(index, true);
542
                                        }
543
                                        break;
544

    
545
                                case OVERLAPS:
546
                                        m = jtsShape.relate(jtsGeom);
547
                                        if (m.isOverlaps(jtsGeom.getDimension(), jtsShape
548
                                                        .getDimension())) {
549
                                                bitset.set(index, true);
550
                                        }
551

    
552
                                        break;
553

    
554
                                case TOUCHES:
555
                                        m = jtsShape.relate(jtsGeom);
556
                                        if (m.isTouches(jtsGeom.getDimension(), jtsShape
557
                                                        .getDimension())) {
558
                                                bitset.set(index, true);
559
                                        }
560

    
561
                                        break;
562

    
563
                                case WITHIN:
564
                                        m = jtsShape.relate(jtsGeom);
565
                                        if (m.isWithin()) {
566
                                                bitset.set(index, true);
567
                                        }
568

    
569
                                        break;
570
                                }
571
                        }
572
                        va.stop();
573
                } catch (DriverIOException e) {
574
                        // TODO Auto-generated catch block
575
                        e.printStackTrace();
576
                }
577
                long t2 = System.currentTimeMillis();
578
//                logger.info("queryByShape optimizado sobre la capa " + lyr.getName()
579
//                                + ". " + (t2 - t1) + " mseg.");
580
                return bitset;
581
        }
582

    
583
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel)
584
                        throws DriverException {
585
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
586
                FLyrVect lyr = (FLyrVect) capa;
587
                if (lyr.getISpatialIndex() == null)
588
                        return super.queryByRect(rect, cancel);
589

    
590
                ReadableVectorial va = lyr.getSource();
591
                ICoordTrans ct = lyr.getCoordTrans();
592
                Rectangle2D bounds = rect;
593
                List lstRecs = lyr.getISpatialIndex().query(bounds);
594
                Integer idRec;
595
                FBitSet bitset = new FBitSet();
596
                int index;
597
                try {
598
                        va.start();
599
                        DriverAttributes attr = va.getDriverAttributes();
600
                        boolean bMustClone = false;
601
                        if (attr != null) {
602
                                if (attr.isLoadedInMemory()) {
603
                                        bMustClone = attr.isLoadedInMemory();
604
                                }
605
                        }
606

    
607
                        for (int i = 0; i < lstRecs.size(); i++) {
608
                                if (cancel != null) {
609
                                        cancel.reportStep();
610
                                        if (cancel.isCanceled()) {
611
                                                va.stop();
612
                                                return bitset;
613
                                        }
614
                                }
615
                                idRec = (Integer) lstRecs.get(i);
616
                                index = idRec.intValue();
617
                                IGeometry geom = va.getShape(index);
618
                                if(geom == null)//azabala
619
                                        continue;
620
                                if (ct != null) {
621
                                        if (bMustClone)
622
                                                geom = geom.cloneGeometry();
623
                                        geom.reProject(ct);
624
                                }
625
                                if (geom.intersects(rect))
626
                                        bitset.set(index, true);
627
                        }
628
                        va.stop();
629
                } catch (DriverIOException e) {
630
                        // TODO Auto-generated catch block
631
                        e.printStackTrace();
632
                }
633
                return bitset;
634

    
635
        }
636

    
637
        public void process(FeatureVisitor visitor, Rectangle2D rectangle)
638
                        throws DriverException, VisitException {
639
                process(visitor, rectangle, null);
640
        }
641

    
642
        /**
643
         * Processes (by calling visitor.process() method) only those features of
644
         * the vectorial layer associated which intersects given rectangle2d.
645
         *
646
         */
647

    
648
        public void process(FeatureVisitor visitor, Rectangle2D rectangle,
649
                        CancellableMonitorable cancel) throws DriverException,
650
                        VisitException {
651
                if (visitor.start(capa)) {
652
                        FLyrVect lyr = (FLyrVect) capa;
653
                        // if we dont have spatial index or...
654
                        if (lyr.getISpatialIndex() == null) {
655
                                super.process(visitor, rectangle, cancel);
656
                                return;
657
                        }
658
                        // if spatial index is not worthy
659
                        if (!isSpatialIndexNecessary(rectangle)) {
660
                                super.process(visitor, rectangle, cancel);
661
                                return;
662
                        }
663

    
664
                        ReadableVectorial va = lyr.getSource();
665
                        ICoordTrans ct = lyr.getCoordTrans();
666
                        Rectangle2D bounds = rectangle;
667
                        List lstRecs = lyr.getISpatialIndex().query(bounds);
668
                        Integer idRec;
669
                        int index;
670
                        try {
671
                                va.start();
672
                                DriverAttributes attr = va.getDriverAttributes();
673
                                boolean bMustClone = false;
674
                                if (attr != null) {
675
                                        if (attr.isLoadedInMemory()) {
676
                                                bMustClone = attr.isLoadedInMemory();
677
                                        }
678
                                }
679

    
680
                                for (int i = 0; i < lstRecs.size(); i++) {
681
                                        if (cancel != null) {
682
                                                cancel.reportStep();
683
                                        }
684
                                        if (verifyCancelation(cancel, va, visitor))
685
                                                return;
686
                                        idRec = (Integer) lstRecs.get(i);
687
                                        index = idRec.intValue();
688
                                        IGeometry geom = va.getShape(index);
689
                                        if(geom == null)//azabala
690
                                                continue;
691
                                        if (ct != null) {
692
                                                if (bMustClone)
693
                                                        geom = geom.cloneGeometry();
694
                                                geom.reProject(ct);
695
                                        }
696
                                        if (geom.intersects(rectangle))
697
                                                visitor.visit(geom, index);
698
                                }// for
699
                                va.stop();
700
                        } catch (DriverIOException e) {
701
                                // TODO Auto-generated catch block
702
                                e.printStackTrace();
703
                        }
704
                }// if visitor.start
705
        }
706

    
707
        /*
708
         * (non-Javadoc)
709
         *
710
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(java.awt.geom.Point2D,
711
         *      double)
712
         */
713
        public FBitSet queryByPoint(Point2D p, double tolerance,
714
                        CancellableMonitorable cancel) throws DriverException {
715
                // TODO: OJO!!!!. Est? implementado como un rectangulo.
716
                // Lo correcto deber?a ser calculando las distancias reales
717
                // es decir, con un c?rculo.
718
                Rectangle2D recPoint = new Rectangle2D.Double(p.getX()
719
                                - (tolerance / 2), p.getY() - (tolerance / 2), tolerance,
720
                                tolerance);
721
                return queryByRect(recPoint, cancel);
722
        }
723
}