Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / ShpStrategy.java @ 6259

History | View | Annotate | Download (20.1 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 org.apache.log4j.Logger;
52
import org.cresques.cts.ICoordTrans;
53
import org.geotools.resources.geometry.XRectangle2D;
54

    
55
import com.hardcode.driverManager.DriverLoadException;
56
import com.iver.cit.gvsig.fmap.DriverException;
57
import com.iver.cit.gvsig.fmap.ViewPort;
58
import com.iver.cit.gvsig.fmap.core.FShape;
59
import com.iver.cit.gvsig.fmap.core.IGeometry;
60
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
61
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
62
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
63
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
64
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
65
import com.iver.cit.gvsig.fmap.layers.FBitSet;
66
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
68
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
69
import com.iver.cit.gvsig.fmap.layers.SpatialCache;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
74
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
75
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
76
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
77
import com.iver.utiles.swing.threads.Cancellable;
78
import com.iver.utiles.swing.threads.CancellableMonitorable;
79
import com.vividsolutions.jts.geom.Geometry;
80
import com.vividsolutions.jts.geom.IntersectionMatrix;
81

    
82
/**
83
 * Esta clase definir? las operaciones de la interfaz FLyrVect de la manera m?s
84
 * ?ptima para los ficheros shp.
85
 */
86
public class ShpStrategy extends DefaultStrategy {
87
        private static Logger logger = Logger
88
                        .getLogger(ShpStrategy.class.getName());
89

    
90
        /**
91
         * Crea una ShpStrategy.
92
         * 
93
         * @param capa
94
         */
95
        public ShpStrategy(FLayer capa) {
96
                super(capa);
97
        }
98

    
99
        /**
100
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
101
         *      java.awt.Graphics2D, FStyle2D)
102
         */
103
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
104
                        Cancellable cancel) throws DriverException {
105
                try {
106
                        ReadableVectorial adapter = ((SingleLayer) getCapa()).getSource();
107
                        if (adapter.getShapeCount() <= 0) {
108
                                logger.debug("Layer:" + getCapa().getName() + " sin registros");
109
                                return;
110
                        }
111

    
112
                        // Selectable selection = (Selectable) getCapa();
113
                        FLyrVect lyr = (FLyrVect) getCapa();
114
                        Selectable selectable = lyr.getRecordset();
115
                        ICoordTrans ct = getCapa().getCoordTrans();
116
                        FBitSet bitSet = selectable.getSelection();
117
                        BoundedShapes shapeBounds;
118
                        if (adapter instanceof BoundedShapes)
119
                                shapeBounds = (BoundedShapes) adapter;
120
                        else
121
                                shapeBounds = (BoundedShapes) adapter.getDriver();
122
                        // VectorialFileDriver driver = (VectorialFileDriver)
123
                        // adapter.getDriver();
124
                        // logger.debug("adapter.start() -> Layer:" + getCapa().getName());
125
                        adapter.start();
126
                        IGeometry geom;
127
                        if (adapter.getShapeCount() > 0) {
128
                                geom = adapter.getShape(0);
129
                        }
130
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa())
131
                                        .getLegend();
132

    
133
                        if (l instanceof ClassifiedLegendInfo) {
134
                                ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) l;
135
                                FSymbol[] symbs = clsfLegend.getSymbols();
136
                                // double rSym = 0;
137
                                // double maxRSym = -1;
138

    
139
                                for (int i = 0; i < symbs.length; i++) {
140
                                        // TODO: REVISAR LOS SIMBOLOS Y SUS TAMA?OS
141

    
142
                                        /*
143
                                         * Style2D pointSymbol = symbs[i].getPointStyle2D(); if
144
                                         * (pointSymbol instanceof MarkStyle2D) { MarkStyle2D mrk2D =
145
                                         * (MarkStyle2D) pointSymbol; rSym =
146
                                         * viewPort.toMapDistance(mrk2D.getSize()); if (maxRSym <
147
                                         * rSym) maxRSym = rSym; }
148
                                         */
149
                                }
150
                        }
151

    
152
                        Rectangle2D extent = viewPort.getAdjustedExtent();
153
                        // AffineTransform at = viewPort.getAffineTransform();
154

    
155
                        int sc;
156

    
157
                        Rectangle2D bounds;
158

    
159
                        long t1 = System.currentTimeMillis();
160
                        // logger.debug("getCapa().getRecordset().start()");
161
                        ((FLyrVect) getCapa()).getRecordset().start();
162

    
163
                        // TODO: A revisar si es o no conveniente este sistema
164
                        // de comunicaci?n con los drivers.
165
                        DriverAttributes attr = adapter.getDriverAttributes();
166
                        boolean bMustClone = false;
167
                        if (attr != null) {
168
                                if (attr.isLoadedInMemory()) {
169
                                        bMustClone = attr.isLoadedInMemory();
170
                                }
171
                        }
172

    
173
                        List lstIndexes = null;
174

    
175
                        // If area of needed extent is less than fullExtent / 4,
176
                        // it will be worthy to use SpatialIndex.
177
                        // Otherwhise, we will not use it.
178
                        boolean bUseSpatialIndex = false;
179
                        sc = adapter.getShapeCount();
180
                        // if (lyr.getSpatialIndex() != null) AZABALA
181
                        // long t11 = System.currentTimeMillis();
182
                        if (lyr.getISpatialIndex() != null) {
183
                                if (isSpatialIndexNecessary(extent)) {
184
                                        lstIndexes = lyr.getISpatialIndex().query(extent);
185
                                        // If the layer is reprojected, spatial index was created
186
                                        // in its own projection, so we must to apply an inverse
187
                                        // transform
188
                                        if (ct != null) {
189
                                                Rectangle2D newExtent = ct.getInverted()
190
                                                                .convert(extent);
191
                                                // Rectangle2D newExtent = ct.convert(extent);
192
                                                // lstIndexes = lyr.getISpatialIndex().query(extent);
193
                                                lstIndexes = lyr.getISpatialIndex().query(newExtent);
194
                                        } else {
195
                                                lstIndexes = lyr.getISpatialIndex().query(extent);
196
                                        }
197
                                        sc = lstIndexes.size();
198
                                        bUseSpatialIndex = true;
199
                                }// if
200
                        }// if
201
                        /*
202
                         * long t12 = System.currentTimeMillis(); System.out.println("Tiempo
203
                         * en mirar el ?ndice espacial y recuperar los ?ndices:" +
204
                         * (t12-t11)); System.out.println("Numero de ?ndices:" + sc);
205
                         */
206

    
207
                        // SpatialCache cache = lyr.createSpatialCache();
208
                        lyr.getSpatialCache().clearAll();
209
                        SpatialCache cache = lyr.getSpatialCache();
210
                        int i;
211
                        for (int aux = 0; aux < sc; aux++) {
212
                                // Salimos si alguien cancela
213
                                if (cancel != null) {
214
                                        // azabala (por si acaso, al arreglar bug de process)
215
                                        if (cancel.isCanceled()) {
216
                                                break;
217
                                        }
218
                                }
219
                                if (bUseSpatialIndex) {
220
                                        Integer idRec = (Integer) lstIndexes.get(aux);
221
                                        i = idRec.intValue();
222
                                } else {
223
                                        i = aux;
224
                                }
225
                                bounds = shapeBounds.getShapeBounds(i);
226
                                if (ct != null) {
227
                                        bounds = ct.convert(bounds);
228
                                }
229
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
230
                                        FSymbol symbol = l.getSymbol(i);
231

    
232
                                        if (bitSet.get(i)) {
233
                                                symbol = FSymbol.getSymbolForSelection(symbol);
234
                                        }
235

    
236
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
237

    
238
                                        if (bPoint
239
                                                        || ((bounds.getHeight() > viewPort.getDist1pixel()) || (bounds
240
                                                                        .getWidth() > viewPort.getDist1pixel()))) { 
241
                                                geom = adapter.getShape(i);
242

    
243
                                                // PRUEBA DE VELOCIDAD
244
                                                // geom = ShapeFactory.createPolygon2D(new
245
                                                // GeneralPathX(bounds));
246

    
247
                                                if (ct != null) {
248
                                                        if (bMustClone)
249
                                                                geom = geom.cloneGeometry();
250
                                                        geom.reProject(ct);
251
                                                }
252
                                                if (lyr.isSpatialCacheEnabled()) {
253
                                                        if (cache.getMaxFeatures() >= cache.size()) {
254
                                                                // Ya reproyectado todo
255
                                                                cache.insert(bounds, geom);
256
                                                        }
257
                                                }
258

    
259
                                                // FJP: CAMBIO: Sabemos que vamos a dibujar sobre una
260
                                                // imagen, con coordenadas enteras, as?
261
                                                // que lo tenemos en cuenta.
262
                                                // ANTES: geom.draw(g, viewPort, symbol);
263
                                                // AHORA:
264
                                                geom.drawInts(g, viewPort, symbol);
265
                                                // geom.draw(g, viewPort, symbol);
266
                                                /*
267
                                                 * if (lyr.isEditing()) { if (bitSet.get(i)) { Handler[]
268
                                                 * handlers = geom.getHandlers(IGeometry.SELECTHANDLER);
269
                                                 * FGraphicUtilities.DrawHandlers(g,
270
                                                 * viewPort.getAffineTransform(), handlers); } }
271
                                                 */
272

    
273
                                        } else {
274
                                                Point2D.Double pOrig = new Point2D.Double(bounds
275
                                                                .getMinX(), bounds.getMinY());
276
                                                Point2D pDest, pDest2;
277

    
278
                                                pDest = viewPort.getAffineTransform().transform(pOrig,
279
                                                                null);
280
                                                pDest2 = g.getTransform().transform(pDest, null);
281

    
282
                                                int pixX = (int) pDest2.getX();
283
                                                int pixY = (int) pDest2.getY();
284
                                                if (symbol == null)
285
                                                        continue;
286
                                                if ((pixX > 0) && (pixX < image.getWidth())) {
287
                                                        if ((pixY > 0) && (pixY < image.getHeight())) {
288
                                                                image.setRGB(pixX, pixY, symbol.getRgb());
289
                                                        }
290
                                                }
291
                                        }
292
                                }
293
                        }
294

    
295
                        // logger.debug("getCapa().getRecordset().stop()");
296
                        ((FLyrVect) getCapa()).getSource().getRecordset().stop();
297

    
298
                        long t2 = System.currentTimeMillis();
299
                        // logger.debug("adapter.stop()");
300
                        adapter.stop();
301

    
302
                        // System.out.println(t2 - t1);
303
                } catch (DriverIOException e) {
304
                        throw new DriverException(e);
305
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
306
                        throw new DriverException(e);
307
                } catch (DriverException e) {
308
                        throw new DriverException(e);
309
                } catch (IOException e) {
310
                        throw new DriverException(e);
311
                } catch (DriverLoadException e) {
312
                        // TODO Auto-generated catch block
313
                        e.printStackTrace();
314
                }
315
        }
316

    
317
        /**
318
         * M?todo utilizado para dibujar sobre el graphics que se pasa como
319
         * par?metro, pensado para utilizarse para imprimir.
320
         * 
321
         * @param g
322
         *            Graphics2D
323
         * @param viewPort
324
         *            ViewPort.
325
         * @param cancel
326
         * 
327
         * @throws DriverException
328
         */
329
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
330
                        throws DriverException {
331
                // super.draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del
332
                // padre, que es el que va sin acelaraci?n!!
333
                try {
334
                        ReadableVectorial adapter = ((SingleLayer) getCapa()).getSource();
335
                        if (adapter.getShapeCount() <= 0) {
336
                                logger.debug("Layer:" + getCapa().getName() + " sin registros");
337
                                return;
338
                        }
339
                        FLyrVect lyr = (FLyrVect) getCapa();
340
                        Selectable selectable = lyr.getRecordset();
341
                        // Selectable selection = (Selectable) getCapa().;
342
                        ICoordTrans ct = getCapa().getCoordTrans();
343
                        BitSet bitSet = selectable.getSelection();
344
                        BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver();
345
                        VectorialFileDriver driver = (VectorialFileDriver) adapter
346
                                        .getDriver();
347
                        // logger.debug("adapter.start() -> Layer:" + getCapa().getName());
348
                        adapter.start();
349
                        IGeometry geom;
350
                        if (adapter.getShapeCount() > 0) {
351
                                geom = adapter.getShape(0);
352
                        }
353
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa())
354
                                        .getLegend();
355

    
356
                        Rectangle2D extent = viewPort.getAdjustedExtent();
357
                        // AffineTransform at = viewPort.getAffineTransform();
358

    
359
                        int sc;
360

    
361
                        Rectangle2D bounds;
362

    
363
                        sc = adapter.getShapeCount();
364

    
365
                        long t1 = System.currentTimeMillis();
366
                        // logger.debug("getCapa().getRecordset().start()");
367
                        ((AlphanumericData) getCapa()).getRecordset().start();
368

    
369
                        // TODO: A revisar si es o no conveniente este sistema
370
                        // de comunicaci?n con los drivers.
371
                        DriverAttributes attr = adapter.getDriverAttributes();
372
                        boolean bMustClone = false;
373
                        if (attr != null) {
374
                                if (attr.isLoadedInMemory()) {
375
                                        bMustClone = attr.isLoadedInMemory();
376
                                }
377
                        }
378

    
379
                        for (int i = 0; i < sc; i++) {
380
                                bounds = shapeBounds.getShapeBounds(i);
381

    
382
                                if (ct != null) {
383
                                        bounds = ct.convert(bounds);
384
                                }
385

    
386
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
387
                                        FSymbol symbol = l.getSymbol(i);
388

    
389
                                        if (bitSet.get(i)) {
390
                                                symbol = FSymbol.getSymbolForSelection(symbol);
391
                                        }
392

    
393
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
394

    
395
                                        geom = driver.getShape(i);
396

    
397
                                        // PRUEBA DE VELOCIDAD
398
                                        // geom = ShapeFactory.createPolygon2D(new
399
                                        // GeneralPathX(bounds));
400

    
401
                                        if (ct != null) {
402
                                                if (bMustClone)
403
                                                        geom = geom.cloneGeometry();
404
                                                geom.reProject(ct);
405
                                        }
406
                                        geom.draw(g, viewPort, symbol);
407
                                }
408
                        }
409

    
410
                        // logger.debug("getCapa().getRecordset().stop()");
411
                        ((AlphanumericData) getCapa()).getRecordset().stop();
412

    
413
                        long t2 = System.currentTimeMillis();
414
                        // logger.debug("adapter.stop()");
415
                        adapter.stop();
416

    
417
                        // System.out.println(t2 - t1);
418
                } catch (DriverIOException e) {
419
                        throw new DriverException(e);
420
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
421
                        throw new DriverException(e);
422
                } catch (DriverException e) {
423
                        throw new DriverException(e);
424
                } catch (IOException e) {
425
                        throw new DriverException(e);
426
                }
427

    
428
        }
429

    
430
        public FBitSet queryByShape(IGeometry g, int relationship)
431
                        throws DriverException, VisitException {
432
                return queryByShape(g, relationship, null);
433
        }
434

    
435
        /*
436
         * (non-Javadoc)
437
         * 
438
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.core.IGeometry,
439
         *      int)
440
         */
441
        public FBitSet queryByShape(IGeometry g, int relationship,
442
                        CancellableMonitorable cancel) throws DriverException,
443
                        VisitException {
444
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
445
                FLyrVect lyr = (FLyrVect) capa;
446
                // if (lyr.getSpatialIndex() == null) AZABALA
447
                if (lyr.getISpatialIndex() == null)
448
                        return super.queryByShape(g, relationship, null);
449

    
450
                long t1 = System.currentTimeMillis();
451
                ReadableVectorial va = lyr.getSource();
452
                ICoordTrans ct = lyr.getCoordTrans();
453
                Rectangle2D bounds = g.getBounds2D();
454
                List lstRecs = lyr.getISpatialIndex().query(bounds);
455
                Integer idRec;
456
                FBitSet bitset = new FBitSet();
457
                Geometry jtsShape = g.toJTSGeometry();
458
                IntersectionMatrix m;
459
                int index;
460
                try {
461
                        va.start();
462

    
463
                        for (int i = 0; i < lstRecs.size(); i++) {
464
                                if (cancel != null) {
465
                                        cancel.reportStep();
466
                                        if (cancel.isCanceled()) {
467
                                                break;
468
                                        }
469
                                }
470
                                idRec = (Integer) lstRecs.get(i);
471
                                index = idRec.intValue();
472
                                IGeometry geom = va.getShape(index);
473
                                if(geom == null)
474
                                        continue;
475
                                if (ct != null) {
476
                                        geom.reProject(ct);
477
                                }
478
                                Geometry jtsGeom = geom.toJTSGeometry();
479
                                switch (relationship) {
480
                                case CONTAINS:
481
                                        m = jtsShape.relate(jtsGeom);
482
                                        if (m.isContains()) {
483
                                                bitset.set(index, true);
484
                                        }
485
                                        break;
486

    
487
                                case CROSSES:
488
                                        m = jtsShape.relate(jtsGeom);
489
                                        if (m.isCrosses(jtsGeom.getDimension(), jtsShape
490
                                                        .getDimension())) {
491
                                                bitset.set(index, true);
492
                                        }
493
                                        break;
494

    
495
                                case DISJOINT:
496
                                        // TODO: CREO QUE EL DISJOINT NO SE PUEDE METER AQUI
497
                                        m = jtsShape.relate(jtsGeom);
498
                                        if (m.isDisjoint()) {
499
                                                bitset.set(index, true);
500
                                        }
501
                                        break;
502

    
503
                                case EQUALS:
504
                                        m = jtsShape.relate(jtsGeom);
505
                                        if (m.isEquals(jtsGeom.getDimension(), jtsShape
506
                                                        .getDimension())) {
507
                                                bitset.set(index, true);
508
                                        }
509
                                        break;
510

    
511
                                case INTERSECTS:
512
                                        m = jtsShape.relate(jtsGeom);
513
                                        if (m.isIntersects()) {
514
                                                bitset.set(index, true);
515
                                        }
516
                                        break;
517

    
518
                                case OVERLAPS:
519
                                        m = jtsShape.relate(jtsGeom);
520
                                        if (m.isOverlaps(jtsGeom.getDimension(), jtsShape
521
                                                        .getDimension())) {
522
                                                bitset.set(index, true);
523
                                        }
524

    
525
                                        break;
526

    
527
                                case TOUCHES:
528
                                        m = jtsShape.relate(jtsGeom);
529
                                        if (m.isTouches(jtsGeom.getDimension(), jtsShape
530
                                                        .getDimension())) {
531
                                                bitset.set(index, true);
532
                                        }
533

    
534
                                        break;
535

    
536
                                case WITHIN:
537
                                        m = jtsShape.relate(jtsGeom);
538
                                        if (m.isWithin()) {
539
                                                bitset.set(index, true);
540
                                        }
541

    
542
                                        break;
543
                                }
544
                        }
545
                        va.stop();
546
                } catch (DriverIOException e) {
547
                        // TODO Auto-generated catch block
548
                        e.printStackTrace();
549
                }
550
                long t2 = System.currentTimeMillis();
551
                logger.debug("queryByShape optimizado sobre la capa " + lyr.getName()
552
                                + ". " + (t2 - t1) + " mseg.");
553
                return bitset;
554
        }
555

    
556
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel)
557
                        throws DriverException {
558
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
559
                FLyrVect lyr = (FLyrVect) capa;
560
                if (lyr.getISpatialIndex() == null)
561
                        return super.queryByRect(rect, cancel);
562

    
563
                ReadableVectorial va = lyr.getSource();
564
                ICoordTrans ct = lyr.getCoordTrans();
565
                Rectangle2D bounds = rect;
566
                List lstRecs = lyr.getISpatialIndex().query(bounds);
567
                Integer idRec;
568
                FBitSet bitset = new FBitSet();
569
                int index;
570
                try {
571
                        va.start();
572
                        DriverAttributes attr = va.getDriverAttributes();
573
                        boolean bMustClone = false;
574
                        if (attr != null) {
575
                                if (attr.isLoadedInMemory()) {
576
                                        bMustClone = attr.isLoadedInMemory();
577
                                }
578
                        }
579

    
580
                        for (int i = 0; i < lstRecs.size(); i++) {
581
                                if (cancel != null) {
582
                                        cancel.reportStep();
583
                                        if (cancel.isCanceled()) {
584
                                                va.stop();
585
                                                return bitset;
586
                                        }
587
                                }
588
                                idRec = (Integer) lstRecs.get(i);
589
                                index = idRec.intValue();
590
                                IGeometry geom = va.getShape(index);
591
                                if(geom == null)//azabala
592
                                        continue;
593
                                if (ct != null) {
594
                                        if (bMustClone)
595
                                                geom = geom.cloneGeometry();
596
                                        geom.reProject(ct);
597
                                }
598
                                if (geom.intersects(rect))
599
                                        bitset.set(index, true);
600
                        }
601
                        va.stop();
602
                } catch (DriverIOException e) {
603
                        // TODO Auto-generated catch block
604
                        e.printStackTrace();
605
                }
606
                return bitset;
607

    
608
        }
609

    
610
        public void process(FeatureVisitor visitor, Rectangle2D rectangle)
611
                        throws DriverException, VisitException {
612
                process(visitor, rectangle, null);
613
        }
614

    
615
        /**
616
         * Processes (by calling visitor.process() method) only those features of
617
         * the vectorial layer associated which intersects given rectangle2d.
618
         * 
619
         */
620

    
621
        public void process(FeatureVisitor visitor, Rectangle2D rectangle,
622
                        CancellableMonitorable cancel) throws DriverException,
623
                        VisitException {
624
                if (visitor.start(capa)) {
625
                        FLyrVect lyr = (FLyrVect) capa;
626
                        // if we dont have spatial index or...
627
                        if (lyr.getISpatialIndex() == null) {
628
                                super.process(visitor, rectangle, cancel);
629
                                return;
630
                        }
631
                        // if spatial index is not worthy
632
                        if (!isSpatialIndexNecessary(rectangle)) {
633
                                super.process(visitor, rectangle, cancel);
634
                                return;
635
                        }
636

    
637
                        ReadableVectorial va = lyr.getSource();
638
                        ICoordTrans ct = lyr.getCoordTrans();
639
                        Rectangle2D bounds = rectangle;
640
                        List lstRecs = lyr.getISpatialIndex().query(bounds);
641
                        Integer idRec;
642
                        int index;
643
                        try {
644
                                va.start();
645
                                DriverAttributes attr = va.getDriverAttributes();
646
                                boolean bMustClone = false;
647
                                if (attr != null) {
648
                                        if (attr.isLoadedInMemory()) {
649
                                                bMustClone = attr.isLoadedInMemory();
650
                                        }
651
                                }
652

    
653
                                for (int i = 0; i < lstRecs.size(); i++) {
654
                                        if (cancel != null) {
655
                                                cancel.reportStep();
656
                                        }
657
                                        if (verifyCancelation(cancel, va, visitor))
658
                                                return;
659
                                        idRec = (Integer) lstRecs.get(i);
660
                                        index = idRec.intValue();
661
                                        IGeometry geom = va.getShape(index);
662
                                        if(geom == null)//azabala
663
                                                continue;
664
                                        if (ct != null) {
665
                                                if (bMustClone)
666
                                                        geom = geom.cloneGeometry();
667
                                                geom.reProject(ct);
668
                                        }
669
                                        if (geom.intersects(rectangle))
670
                                                visitor.visit(geom, index);
671
                                }// for
672
                                va.stop();
673
                        } catch (DriverIOException e) {
674
                                // TODO Auto-generated catch block
675
                                e.printStackTrace();
676
                        }
677
                }// if visitor.start
678
        }
679

    
680
        /*
681
         * (non-Javadoc)
682
         * 
683
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(java.awt.geom.Point2D,
684
         *      double)
685
         */
686
        public FBitSet queryByPoint(Point2D p, double tolerance,
687
                        CancellableMonitorable cancel) throws DriverException {
688
                // TODO: OJO!!!!. Est? implementado como un rectangulo.
689
                // Lo correcto deber?a ser calculando las distancias reales
690
                // es decir, con un c?rculo.
691
                Rectangle2D recPoint = new Rectangle2D.Double(p.getX()
692
                                - (tolerance / 2), p.getY() - (tolerance / 2), tolerance,
693
                                tolerance);
694
                return queryByRect(recPoint, cancel);
695
        }
696
}