Statistics
| Revision:

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

History | View | Annotate | Download (20.7 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
                        
212
                        boolean MAC_OS_X = System.getProperty("os.name").toLowerCase().startsWith("mac os x");
213
//                        if (MAC_OS_X) {
214
//                                System.err.println("Detectado OS X...");
215
//                        } 
216
                        BufferedImage auxBI = null; //se usar? para separar el dibujado normal de los setRGB; as? se evita ralentizado en OS X
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
                                        FSymbol symbol = l.getSymbol(i);
242

    
243
                                        if (bitSet.get(i)) {
244
                                                symbol = FSymbol.getSymbolForSelection(symbol);
245
                                        }
246

    
247
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
248

    
249
                                        if (bPoint
250
                                                        || ((bounds.getHeight() > viewPort.getDist1pixel()) || (bounds
251
                                                                        .getWidth() > viewPort.getDist1pixel()))) { 
252
                                                geom = adapter.getShape(i);
253

    
254
                                                // PRUEBA DE VELOCIDAD
255
                                                // geom = ShapeFactory.createPolygon2D(new
256
                                                // GeneralPathX(bounds));
257

    
258
                                                if (ct != null) {
259
                                                        if (bMustClone)
260
                                                                geom = geom.cloneGeometry();
261
                                                        geom.reProject(ct);
262
                                                }
263
                                                if (lyr.isSpatialCacheEnabled()) {
264
                                                        if (cache.getMaxFeatures() >= cache.size()) {
265
                                                                // Ya reproyectado todo
266
                                                                cache.insert(bounds, geom);
267
                                                        }
268
                                                }
269

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

    
284
                                        } else {
285
                                                Point2D.Double pOrig = new Point2D.Double(bounds
286
                                                                .getMinX(), bounds.getMinY());
287
                                                Point2D pDest, pDest2;
288

    
289
                                                pDest = viewPort.getAffineTransform().transform(pOrig,
290
                                                                null);
291
                                                pDest2 = g.getTransform().transform(pDest, null);
292

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

    
314
                        // logger.debug("getCapa().getRecordset().stop()");
315
                        ((FLyrVect) getCapa()).getSource().getRecordset().stop();
316

    
317
                        long t2 = System.currentTimeMillis();
318
                        // logger.debug("adapter.stop()");
319
                        adapter.stop();
320

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

    
336
        /**
337
         * M?todo utilizado para dibujar sobre el graphics que se pasa como
338
         * par?metro, pensado para utilizarse para imprimir.
339
         * 
340
         * @param g
341
         *            Graphics2D
342
         * @param viewPort
343
         *            ViewPort.
344
         * @param cancel
345
         * 
346
         * @throws DriverException
347
         */
348
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
349
                        throws DriverException {
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.debug("Layer:" + getCapa().getName() + " sin registros");
356
                                return;
357
                        }
358
                        FLyrVect lyr = (FLyrVect) getCapa();
359
                        Selectable selectable = lyr.getRecordset();
360
                        // Selectable selection = (Selectable) getCapa().;
361
                        ICoordTrans ct = getCapa().getCoordTrans();
362
                        BitSet bitSet = selectable.getSelection();
363
                        BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver();
364
                        VectorialFileDriver driver = (VectorialFileDriver) adapter
365
                                        .getDriver();
366
                        // logger.debug("adapter.start() -> Layer:" + getCapa().getName());
367
                        adapter.start();
368
                        IGeometry geom;
369
                        if (adapter.getShapeCount() > 0) {
370
                                geom = adapter.getShape(0);
371
                        }
372
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa())
373
                                        .getLegend();
374

    
375
                        Rectangle2D extent = viewPort.getAdjustedExtent();
376
                        // AffineTransform at = viewPort.getAffineTransform();
377

    
378
                        int sc;
379

    
380
                        Rectangle2D bounds;
381

    
382
                        sc = adapter.getShapeCount();
383

    
384
                        long t1 = System.currentTimeMillis();
385
                        // logger.debug("getCapa().getRecordset().start()");
386
                        ((AlphanumericData) getCapa()).getRecordset().start();
387

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

    
398
                        for (int i = 0; i < sc; i++) {
399
                                bounds = shapeBounds.getShapeBounds(i);
400

    
401
                                if (ct != null) {
402
                                        bounds = ct.convert(bounds);
403
                                }
404

    
405
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
406
                                        FSymbol symbol = l.getSymbol(i);
407

    
408
                                        if (bitSet.get(i)) {
409
                                                symbol = FSymbol.getSymbolForSelection(symbol);
410
                                        }
411

    
412
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
413

    
414
                                        geom = driver.getShape(i);
415

    
416
                                        // PRUEBA DE VELOCIDAD
417
                                        // geom = ShapeFactory.createPolygon2D(new
418
                                        // GeneralPathX(bounds));
419

    
420
                                        if (ct != null) {
421
                                                if (bMustClone)
422
                                                        geom = geom.cloneGeometry();
423
                                                geom.reProject(ct);
424
                                        }
425
                                        geom.draw(g, viewPort, symbol);
426
                                }
427
                        }
428

    
429
                        // logger.debug("getCapa().getRecordset().stop()");
430
                        ((AlphanumericData) getCapa()).getRecordset().stop();
431

    
432
                        long t2 = System.currentTimeMillis();
433
                        // logger.debug("adapter.stop()");
434
                        adapter.stop();
435

    
436
                        // System.out.println(t2 - t1);
437
                } catch (DriverIOException e) {
438
                        throw new DriverException(e);
439
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
440
                        throw new DriverException(e);
441
                } catch (DriverException e) {
442
                        throw new DriverException(e);
443
                } catch (IOException e) {
444
                        throw new DriverException(e);
445
                }
446

    
447
        }
448

    
449
        public FBitSet queryByShape(IGeometry g, int relationship)
450
                        throws DriverException, VisitException {
451
                return queryByShape(g, relationship, null);
452
        }
453

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

    
469
                long t1 = System.currentTimeMillis();
470
                ReadableVectorial va = lyr.getSource();
471
                ICoordTrans ct = lyr.getCoordTrans();
472
                Rectangle2D bounds = g.getBounds2D();
473
                List lstRecs = lyr.getISpatialIndex().query(bounds);
474
                Integer idRec;
475
                FBitSet bitset = new FBitSet();
476
                Geometry jtsShape = g.toJTSGeometry();
477
                IntersectionMatrix m;
478
                int index;
479
                try {
480
                        va.start();
481

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

    
506
                                case CROSSES:
507
                                        m = jtsShape.relate(jtsGeom);
508
                                        if (m.isCrosses(jtsGeom.getDimension(), jtsShape
509
                                                        .getDimension())) {
510
                                                bitset.set(index, true);
511
                                        }
512
                                        break;
513

    
514
                                case DISJOINT:
515
                                        // TODO: CREO QUE EL DISJOINT NO SE PUEDE METER AQUI
516
                                        m = jtsShape.relate(jtsGeom);
517
                                        if (m.isDisjoint()) {
518
                                                bitset.set(index, true);
519
                                        }
520
                                        break;
521

    
522
                                case EQUALS:
523
                                        m = jtsShape.relate(jtsGeom);
524
                                        if (m.isEquals(jtsGeom.getDimension(), jtsShape
525
                                                        .getDimension())) {
526
                                                bitset.set(index, true);
527
                                        }
528
                                        break;
529

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

    
537
                                case OVERLAPS:
538
                                        m = jtsShape.relate(jtsGeom);
539
                                        if (m.isOverlaps(jtsGeom.getDimension(), jtsShape
540
                                                        .getDimension())) {
541
                                                bitset.set(index, true);
542
                                        }
543

    
544
                                        break;
545

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

    
553
                                        break;
554

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

    
561
                                        break;
562
                                }
563
                        }
564
                        va.stop();
565
                } catch (DriverIOException e) {
566
                        // TODO Auto-generated catch block
567
                        e.printStackTrace();
568
                }
569
                long t2 = System.currentTimeMillis();
570
                logger.debug("queryByShape optimizado sobre la capa " + lyr.getName()
571
                                + ". " + (t2 - t1) + " mseg.");
572
                return bitset;
573
        }
574

    
575
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel)
576
                        throws DriverException {
577
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
578
                FLyrVect lyr = (FLyrVect) capa;
579
                if (lyr.getISpatialIndex() == null)
580
                        return super.queryByRect(rect, cancel);
581

    
582
                ReadableVectorial va = lyr.getSource();
583
                ICoordTrans ct = lyr.getCoordTrans();
584
                Rectangle2D bounds = rect;
585
                List lstRecs = lyr.getISpatialIndex().query(bounds);
586
                Integer idRec;
587
                FBitSet bitset = new FBitSet();
588
                int index;
589
                try {
590
                        va.start();
591
                        DriverAttributes attr = va.getDriverAttributes();
592
                        boolean bMustClone = false;
593
                        if (attr != null) {
594
                                if (attr.isLoadedInMemory()) {
595
                                        bMustClone = attr.isLoadedInMemory();
596
                                }
597
                        }
598

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

    
627
        }
628

    
629
        public void process(FeatureVisitor visitor, Rectangle2D rectangle)
630
                        throws DriverException, VisitException {
631
                process(visitor, rectangle, null);
632
        }
633

    
634
        /**
635
         * Processes (by calling visitor.process() method) only those features of
636
         * the vectorial layer associated which intersects given rectangle2d.
637
         * 
638
         */
639

    
640
        public void process(FeatureVisitor visitor, Rectangle2D rectangle,
641
                        CancellableMonitorable cancel) throws DriverException,
642
                        VisitException {
643
                if (visitor.start(capa)) {
644
                        FLyrVect lyr = (FLyrVect) capa;
645
                        // if we dont have spatial index or...
646
                        if (lyr.getISpatialIndex() == null) {
647
                                super.process(visitor, rectangle, cancel);
648
                                return;
649
                        }
650
                        // if spatial index is not worthy
651
                        if (!isSpatialIndexNecessary(rectangle)) {
652
                                super.process(visitor, rectangle, cancel);
653
                                return;
654
                        }
655

    
656
                        ReadableVectorial va = lyr.getSource();
657
                        ICoordTrans ct = lyr.getCoordTrans();
658
                        Rectangle2D bounds = rectangle;
659
                        List lstRecs = lyr.getISpatialIndex().query(bounds);
660
                        Integer idRec;
661
                        int index;
662
                        try {
663
                                va.start();
664
                                DriverAttributes attr = va.getDriverAttributes();
665
                                boolean bMustClone = false;
666
                                if (attr != null) {
667
                                        if (attr.isLoadedInMemory()) {
668
                                                bMustClone = attr.isLoadedInMemory();
669
                                        }
670
                                }
671

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

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