Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / ShpStrategy.java @ 9370

History | View | Annotate | Download (21.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.operations.strategies;
42

    
43
import java.awt.Graphics2D;
44
import java.awt.geom.Point2D;
45
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47
import java.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.VectorialDriver;
67
import com.iver.cit.gvsig.fmap.layers.FBitSet;
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
70
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
71
import com.iver.cit.gvsig.fmap.layers.SpatialCache;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
75
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
76
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
77
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
78
import com.iver.utiles.swing.threads.Cancellable;
79
import com.iver.utiles.swing.threads.CancellableMonitorable;
80
import com.vividsolutions.jts.geom.Geometry;
81
import com.vividsolutions.jts.geom.IntersectionMatrix;
82

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

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

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

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

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

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

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

    
154
                        Rectangle2D extent = viewPort.getAdjustedExtent();
155
                        // AffineTransform at = viewPort.getAffineTransform();
156

    
157
                        int sc;
158

    
159
                        Rectangle2D bounds;
160

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

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

    
175
                        List lstIndexes = null;
176

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

    
209
                        // SpatialCache cache = lyr.createSpatialCache();
210
                        lyr.getSpatialCache().clearAll();
211
                        SpatialCache cache = lyr.getSpatialCache();
212
                        int i;
213

    
214
                        // En OS X con renderer Quartz (JRE<6), mezclar setRGB con dibujado
215
                        // geometrico en mismo BufferedImage
216
                        // provoca ralentizaci?n brutal. Lo evitamos separando los setRGB en
217
                        // otro BufferedImage y juntandolos luego.
218
                        boolean MAC_OS_X = System.getProperty("os.name").toLowerCase()
219
                                        .startsWith("mac os x");
220
                        BufferedImage auxBI = null;
221
                        if (MAC_OS_X) {
222
                                auxBI = new BufferedImage(image.getWidth(), image.getHeight(),
223
                                                image.getType());
224
                        }
225

    
226
                        for (int aux = 0; aux < sc; aux++) {
227
                                // Salimos si alguien cancela
228
                                if (cancel != null) {
229
                                        // azabala (por si acaso, al arreglar bug de process)
230
                                        if (cancel.isCanceled()) {
231
                                                break;
232
                                        }
233
                                }
234
                                if (bUseSpatialIndex) {
235
                                        Integer idRec = (Integer) lstIndexes.get(aux);
236
                                        i = idRec.intValue();
237
                                } else {
238
                                        i = aux;
239
                                }
240
                                bounds = shapeBounds.getShapeBounds(i);
241
                                if (ct != null) {
242
                                        bounds = ct.convert(bounds);
243
                                }
244
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
245
                                        ISymbol symbol = l.getSymbol(i);
246

    
247
                                        if (bitSet.get(i)) {
248
                                                symbol = symbol.getSymbolForSelection();
249
                                        }
250

    
251
                                        if (symbol == null)
252
                                                continue;
253

    
254
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
255

    
256
                                        if (bPoint
257
                                                        || ((bounds.getHeight() > viewPort.getDist1pixel()) || (bounds
258
                                                                        .getWidth() > viewPort.getDist1pixel()))) {
259
                                                geom = adapter.getShape(i);
260

    
261
                                                // PRUEBA DE VELOCIDAD
262
                                                // geom = ShapeFactory.createPolygon2D(new
263
                                                // GeneralPathX(bounds));
264

    
265
                                                if (ct != null) {
266
                                                        if (bMustClone)
267
                                                                geom = geom.cloneGeometry();
268
                                                        geom.reProject(ct);
269
                                                }
270
                                                if (lyr.isSpatialCacheEnabled()) {
271
                                                        if (cache.getMaxFeatures() >= cache.size()) {
272
                                                                // Ya reproyectado todo
273
                                                                cache.insert(bounds, geom);
274
                                                        }
275
                                                }
276

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

    
291
                                        } else {
292
                                                Point2D.Double pOrig = new Point2D.Double(bounds
293
                                                                .getMinX(), bounds.getMinY());
294
                                                Point2D pDest, pDest2;
295

    
296
                                                pDest = viewPort.getAffineTransform().transform(pOrig,
297
                                                                null);
298
                                                pDest2 = g.getTransform().transform(pDest, null);
299

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

    
319
                        if (MAC_OS_X) {
320
                                g.drawImage(auxBI, 0, 0, null);
321
                        }
322

    
323
                        // logger.debug("getCapa().getRecordset().stop()");
324
                        ((FLyrVect) getCapa()).getSource().getRecordset().stop();
325

    
326
                        long t2 = System.currentTimeMillis();
327
                        // logger.debug("adapter.stop()");
328
                        adapter.stop();
329

    
330
                        // System.out.println(t2 - t1);
331
                } catch (DriverIOException e) {
332
                        throw new DriverException(e);
333
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
334
                        throw new DriverException(e);
335
                } catch (DriverException e) {
336
                        throw new DriverException(e);
337
                } catch (IOException e) {
338
                        throw new DriverException(e);
339
                } catch (DriverLoadException e) {
340
                        // TODO Auto-generated catch block
341
                        e.printStackTrace();
342
                }
343
        }
344

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

    
371
                        Selectable selectable = lyr.getRecordset();
372
                        ICoordTrans ct = getCapa().getCoordTrans();
373
                        BitSet bitSet = selectable.getSelection();
374
                        BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver();
375
                        VectorialDriver driver = (VectorialDriver) adapter
376
                                        .getDriver();
377
                        // logger.debug("adapter.start() -> Layer:" + getCapa().getName());
378
                        adapter.start();
379
                        IGeometry geom;
380
                        if (adapter.getShapeCount() > 0) {
381
                                geom = adapter.getShape(0);
382
                        }
383
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa())
384
                                        .getLegend();
385

    
386
                        Rectangle2D extent = viewPort.getAdjustedExtent();
387
                        // AffineTransform at = viewPort.getAffineTransform();
388

    
389
                        int sc;
390

    
391
                        Rectangle2D bounds;
392

    
393
                        sc = adapter.getShapeCount();
394

    
395
                        long t1 = System.currentTimeMillis();
396
                        // logger.debug("getCapa().getRecordset().start()");
397
                        ((AlphanumericData) getCapa()).getRecordset().start();
398

    
399
                        // TODO: A revisar si es o no conveniente este sistema
400
                        // de comunicaci?n con los drivers.
401
                        DriverAttributes attr = adapter.getDriverAttributes();
402
                        boolean bMustClone = false;
403
                        if (attr != null) {
404
                                if (attr.isLoadedInMemory()) {
405
                                        bMustClone = attr.isLoadedInMemory();
406
                                }
407
                        }
408

    
409
                        List lstIndexes = null;
410

    
411
                        boolean bUseSpatialIndex = false;
412
                        if (lyr.getISpatialIndex() != null) {
413
                                if (isSpatialIndexNecessary(extent)) {
414
                                        lstIndexes = lyr.getISpatialIndex().query(extent);
415
                                        if (ct != null) {
416
                                                Rectangle2D newExtent = ct.getInverted()
417
                                                                .convert(extent);
418
                                                lstIndexes = lyr.getISpatialIndex().query(newExtent);
419
                                        } else {
420
                                                lstIndexes = lyr.getISpatialIndex().query(extent);
421
                                        }
422
                                        sc = lstIndexes.size();
423
                                        bUseSpatialIndex = true;
424
                                }// if
425
                        }// if
426

    
427
                        int i;
428
                        for (int aux = 0; aux < sc; aux++) {
429
                                if (bUseSpatialIndex) {
430
                                        Integer idRec = (Integer) lstIndexes.get(aux);
431
                                        i = idRec.intValue();
432
                                } else {
433
                                        i = aux;
434
                                }
435

    
436
                                bounds = shapeBounds.getShapeBounds(i);
437

    
438
                                if (ct != null) {
439
                                        bounds = ct.convert(bounds);
440
                                }
441

    
442
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
443
                                        ISymbol symbol = l.getSymbol(i);
444
                                        if (symbol == null)
445
                                                continue;
446
                                        if (bitSet.get(i)) {
447
                                                symbol = symbol.getSymbolForSelection();
448
                                        }
449

    
450
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
451

    
452
                                        geom = driver.getShape(i);
453

    
454
                                        // PRUEBA DE VELOCIDAD
455
                                        // geom = ShapeFactory.createPolygon2D(new
456
                                        // GeneralPathX(bounds));
457

    
458
                                        if (ct != null) {
459
                                                if (bMustClone)
460
                                                        geom = geom.cloneGeometry();
461
                                                geom.reProject(ct);
462
                                        }
463

    
464
                                        geom.draw(g, viewPort, symbol);
465

    
466
                                }
467
                        }
468

    
469
                        // logger.debug("getCapa().getRecordset().stop()");
470
                        ((AlphanumericData) getCapa()).getRecordset().stop();
471

    
472
                        long t2 = System.currentTimeMillis();
473
                        // logger.debug("adapter.stop()");
474
                        adapter.stop();
475
                        lyr.afterPrinting();
476
                        // System.out.println(t2 - t1);
477
                } catch (DriverIOException e) {
478
                        throw new DriverException(e);
479
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
480
                        throw new DriverException(e);
481
                } catch (DriverException e) {
482
                        throw new DriverException(e);
483
                } catch (IOException e) {
484
                        throw new DriverException(e);
485
                }
486

    
487
        }
488

    
489
        public FBitSet queryByShape(IGeometry g, int relationship)
490
                        throws DriverException, VisitException {
491
                return queryByShape(g, relationship, null);
492
        }
493

    
494
        /*
495
         * (non-Javadoc)
496
         *
497
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.core.IGeometry,
498
         *      int)
499
         */
500
        public FBitSet queryByShape(IGeometry g, int relationship,
501
                        CancellableMonitorable cancel) throws DriverException,
502
                        VisitException {
503
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
504
                FLyrVect lyr = (FLyrVect) capa;
505
                // if (lyr.getSpatialIndex() == null) AZABALA
506
                if (lyr.getISpatialIndex() == null)
507
                        return super.queryByShape(g, relationship, null);
508

    
509
                long t1 = System.currentTimeMillis();
510
                ReadableVectorial va = lyr.getSource();
511
                ICoordTrans ct = lyr.getCoordTrans();
512
                Rectangle2D bounds = g.getBounds2D();
513
                List lstRecs = lyr.getISpatialIndex().query(bounds);
514
                Integer idRec;
515
                FBitSet bitset = new FBitSet();
516
                Geometry jtsShape = g.toJTSGeometry();
517
                IntersectionMatrix m;
518
                int index;
519
                try {
520
                        va.start();
521

    
522
                        for (int i = 0; i < lstRecs.size(); i++) {
523
                                if (cancel != null) {
524
                                        cancel.reportStep();
525
                                        if (cancel.isCanceled()) {
526
                                                break;
527
                                        }
528
                                }
529
                                idRec = (Integer) lstRecs.get(i);
530
                                index = idRec.intValue();
531
                                IGeometry geom = va.getShape(index);
532
                                if (geom == null)
533
                                        continue;
534
                                if (ct != null) {
535
                                        geom.reProject(ct);
536
                                }
537
                                Geometry jtsGeom = geom.toJTSGeometry();
538
                                switch (relationship) {
539
                                case CONTAINS:
540
                                        m = jtsShape.relate(jtsGeom);
541
                                        if (m.isContains()) {
542
                                                bitset.set(index, true);
543
                                        }
544
                                        break;
545

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

    
554
                                case DISJOINT:
555
                                        // TODO: CREO QUE EL DISJOINT NO SE PUEDE METER AQUI
556
                                        m = jtsShape.relate(jtsGeom);
557
                                        if (m.isDisjoint()) {
558
                                                bitset.set(index, true);
559
                                        }
560
                                        break;
561

    
562
                                case EQUALS:
563
                                        m = jtsShape.relate(jtsGeom);
564
                                        if (m.isEquals(jtsGeom.getDimension(), jtsShape
565
                                                        .getDimension())) {
566
                                                bitset.set(index, true);
567
                                        }
568
                                        break;
569

    
570
                                case INTERSECTS:
571
                                        m = jtsShape.relate(jtsGeom);
572
                                        if (m.isIntersects()) {
573
                                                bitset.set(index, true);
574
                                        }
575
                                        break;
576

    
577
                                case OVERLAPS:
578
                                        m = jtsShape.relate(jtsGeom);
579
                                        if (m.isOverlaps(jtsGeom.getDimension(), jtsShape
580
                                                        .getDimension())) {
581
                                                bitset.set(index, true);
582
                                        }
583

    
584
                                        break;
585

    
586
                                case TOUCHES:
587
                                        m = jtsShape.relate(jtsGeom);
588
                                        if (m.isTouches(jtsGeom.getDimension(), jtsShape
589
                                                        .getDimension())) {
590
                                                bitset.set(index, true);
591
                                        }
592

    
593
                                        break;
594

    
595
                                case WITHIN:
596
                                        m = jtsShape.relate(jtsGeom);
597
                                        if (m.isWithin()) {
598
                                                bitset.set(index, true);
599
                                        }
600

    
601
                                        break;
602
                                }
603
                        }
604
                        va.stop();
605
                } catch (DriverIOException e) {
606
                        // TODO Auto-generated catch block
607
                        e.printStackTrace();
608
                }
609
                long t2 = System.currentTimeMillis();
610
                // logger.info("queryByShape optimizado sobre la capa " + lyr.getName()
611
                // + ". " + (t2 - t1) + " mseg.");
612
                return bitset;
613
        }
614

    
615
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel)
616
                        throws DriverException {
617
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
618
                FLyrVect lyr = (FLyrVect) capa;
619
                if (lyr.getISpatialIndex() == null)
620
                        return super.queryByRect(rect, cancel);
621

    
622
                ReadableVectorial va = lyr.getSource();
623
                ICoordTrans ct = lyr.getCoordTrans();
624
                Rectangle2D bounds = rect;
625
                List lstRecs = lyr.getISpatialIndex().query(bounds);
626
                Integer idRec;
627
                FBitSet bitset = new FBitSet();
628
                int index;
629
                try {
630
                        va.start();
631
                        DriverAttributes attr = va.getDriverAttributes();
632
                        boolean bMustClone = false;
633
                        if (attr != null) {
634
                                if (attr.isLoadedInMemory()) {
635
                                        bMustClone = attr.isLoadedInMemory();
636
                                }
637
                        }
638

    
639
                        for (int i = 0; i < lstRecs.size(); i++) {
640
                                if (cancel != null) {
641
                                        cancel.reportStep();
642
                                        if (cancel.isCanceled()) {
643
                                                va.stop();
644
                                                return bitset;
645
                                        }
646
                                }
647
                                idRec = (Integer) lstRecs.get(i);
648
                                index = idRec.intValue();
649
                                IGeometry geom = va.getShape(index);
650
                                if (geom == null)// azabala
651
                                        continue;
652
                                if (ct != null) {
653
                                        if (bMustClone)
654
                                                geom = geom.cloneGeometry();
655
                                        geom.reProject(ct);
656
                                }
657
                                if (geom.intersects(rect))
658
                                        bitset.set(index, true);
659
                        }
660
                        va.stop();
661
                } catch (DriverIOException e) {
662
                        // TODO Auto-generated catch block
663
                        e.printStackTrace();
664
                }
665
                return bitset;
666

    
667
        }
668

    
669
        public void process(FeatureVisitor visitor, Rectangle2D rectangle)
670
                        throws DriverException, VisitException {
671
                process(visitor, rectangle, null);
672
        }
673

    
674
        /**
675
         * Processes (by calling visitor.process() method) only those features of
676
         * the vectorial layer associated which intersects given rectangle2d.
677
         *
678
         */
679

    
680
        public void process(FeatureVisitor visitor, Rectangle2D rectangle,
681
                        CancellableMonitorable cancel) throws DriverException,
682
                        VisitException {
683
                if (visitor.start(capa)) {
684
                        FLyrVect lyr = (FLyrVect) capa;
685
                        // if we dont have spatial index or...
686
                        if (lyr.getISpatialIndex() == null) {
687
                                super.process(visitor, rectangle, cancel);
688
                                return;
689
                        }
690
                        // if spatial index is not worthy
691
                        if (!isSpatialIndexNecessary(rectangle)) {
692
                                super.process(visitor, rectangle, cancel);
693
                                return;
694
                        }
695

    
696
                        ReadableVectorial va = lyr.getSource();
697
                        ICoordTrans ct = lyr.getCoordTrans();
698
                        Rectangle2D bounds = rectangle;
699
                        List lstRecs = lyr.getISpatialIndex().query(bounds);
700
                        Integer idRec;
701
                        int index;
702
                        try {
703
                                va.start();
704
                                DriverAttributes attr = va.getDriverAttributes();
705
                                boolean bMustClone = false;
706
                                if (attr != null) {
707
                                        if (attr.isLoadedInMemory()) {
708
                                                bMustClone = attr.isLoadedInMemory();
709
                                        }
710
                                }
711

    
712
                                for (int i = 0; i < lstRecs.size(); i++) {
713
                                        if (cancel != null) {
714
                                                cancel.reportStep();
715
                                        }
716
                                        if (verifyCancelation(cancel, va, visitor))
717
                                                return;
718
                                        idRec = (Integer) lstRecs.get(i);
719
                                        index = idRec.intValue();
720
                                        IGeometry geom = va.getShape(index);
721
                                        if (geom == null)// azabala
722
                                                continue;
723
                                        if (ct != null) {
724
                                                if (bMustClone)
725
                                                        geom = geom.cloneGeometry();
726
                                                geom.reProject(ct);
727
                                        }
728
                                        if (geom.intersects(rectangle))
729
                                                visitor.visit(geom, index);
730
                                }// for
731
                                va.stop();
732
                        } catch (DriverIOException e) {
733
                                // TODO Auto-generated catch block
734
                                e.printStackTrace();
735
                        }
736
                }// if visitor.start
737
        }
738

    
739
        /*
740
         * (non-Javadoc)
741
         *
742
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(java.awt.geom.Point2D,
743
         *      double)
744
         */
745
        public FBitSet queryByPoint(Point2D p, double tolerance,
746
                        CancellableMonitorable cancel) throws DriverException {
747
                // TODO: OJO!!!!. Est? implementado como un rectangulo.
748
                // Lo correcto deber?a ser calculando las distancias reales
749
                // es decir, con un c?rculo.
750
                Rectangle2D recPoint = new Rectangle2D.Double(p.getX()
751
                                - (tolerance / 2), p.getY() - (tolerance / 2), tolerance,
752
                                tolerance);
753
                return queryByRect(recPoint, cancel);
754
        }
755
}