Statistics
| Revision:

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

History | View | Annotate | Download (23.2 KB)

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