Statistics
| Revision:

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

History | View | Annotate | Download (23.1 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 1034 vcaballero
                                if (ct != null) {
257 1233 fjp
                                        bounds = ct.convert(bounds);
258 1034 vcaballero
                                }
259
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
260 8765 jjdelcerro
                                        ISymbol symbol = l.getSymbol(i);
261 1034 vcaballero
262
                                        if (bitSet.get(i)) {
263 8765 jjdelcerro
                                                symbol = symbol.getSymbolForSelection();
264 3940 caballero
                                        }
265 1034 vcaballero
266 8765 jjdelcerro
                                        if (symbol == null)
267
                                                continue;
268
269 597 fjp
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
270 239 fjp
271 5813 fjp
                                        if (bPoint
272
                                                        || ((bounds.getHeight() > viewPort.getDist1pixel()) || (bounds
273 8765 jjdelcerro
                                                                        .getWidth() > viewPort.getDist1pixel()))) {
274 3601 fjp
                                                geom = adapter.getShape(i);
275 3940 caballero
276 5813 fjp
                                                // PRUEBA DE VELOCIDAD
277
                                                // geom = ShapeFactory.createPolygon2D(new
278
                                                // GeneralPathX(bounds));
279 239 fjp
280 11034 caballero
281 9511 jmvivo
                                                //JMVIVO: OJO, No colnamos siempre porque
282
                                                // el FGeometry.drawInt (a diferencia del
283
                                                // FGeometry.draw) clona siempre la geometria
284
                                                // antes de pintarla (para transforma a enteros)
285 1034 vcaballero
                                                if (ct != null) {
286 5813 fjp
                                                        if (bMustClone)
287
                                                                geom = geom.cloneGeometry();
288 1034 vcaballero
                                                        geom.reProject(ct);
289
                                                }
290 5813 fjp
                                                if (lyr.isSpatialCacheEnabled()) {
291
                                                        if (cache.getMaxFeatures() >= cache.size()) {
292
                                                                // Ya reproyectado todo
293
                                                                cache.insert(bounds, geom);
294 5224 fjp
                                                        }
295
                                                }
296 5490 caballero
297 5813 fjp
                                                // FJP: CAMBIO: Sabemos que vamos a dibujar sobre una
298
                                                // imagen, con coordenadas enteras, as?
299
                                                // que lo tenemos en cuenta.
300 2859 fjp
                                                // ANTES: geom.draw(g, viewPort, symbol);
301 5813 fjp
                                                // AHORA:
302
                                                geom.drawInts(g, viewPort, symbol);
303
                                                // geom.draw(g, viewPort, symbol);
304
                                                /*
305
                                                 * if (lyr.isEditing()) { if (bitSet.get(i)) { Handler[]
306
                                                 * handlers = geom.getHandlers(IGeometry.SELECTHANDLER);
307
                                                 * FGraphicUtilities.DrawHandlers(g,
308
                                                 * viewPort.getAffineTransform(), handlers); } }
309
                                                 */
310 239 fjp
311 1034 vcaballero
                                        } else {
312 5813 fjp
                                                Point2D.Double pOrig = new Point2D.Double(bounds
313
                                                                .getMinX(), bounds.getMinY());
314 2469 fjp
                                                Point2D pDest, pDest2;
315 1034 vcaballero
316 5813 fjp
                                                pDest = viewPort.getAffineTransform().transform(pOrig,
317
                                                                null);
318
                                                pDest2 = g.getTransform().transform(pDest, null);
319 1034 vcaballero
320 2469 fjp
                                                int pixX = (int) pDest2.getX();
321
                                                int pixY = (int) pDest2.getY();
322 5813 fjp
                                                if (symbol == null)
323 2412 caballero
                                                        continue;
324 1034 vcaballero
                                                if ((pixX > 0) && (pixX < image.getWidth())) {
325
                                                        if ((pixY > 0) && (pixY < image.getHeight())) {
326 7502 mija
                                                                if (MAC_OS_X) {
327 9287 fjp
                                                                        auxBI.setRGB(pixX, pixY, symbol
328
                                                                                        .getOnePointRgb());
329 7502 mija
                                                                } else {
330 9287 fjp
                                                                        image.setRGB(pixX, pixY, symbol
331
                                                                                        .getOnePointRgb());
332 7502 mija
                                                                }
333 1034 vcaballero
                                                        }
334
                                                }
335
                                        }
336 214 fernando
                                }
337
                        }
338 8765 jjdelcerro
339 7502 mija
                        if (MAC_OS_X) {
340 9287 fjp
                                g.drawImage(auxBI, 0, 0, null);
341 8765 jjdelcerro
                        }
342 1034 vcaballero
343 3025 fjp
                        // logger.debug("getCapa().getRecordset().stop()");
344 11006 jmvivo
                        //CHEMA
345
                        // Cerramos la fuente alfanumerica cuando se ha hecho
346 11034 caballero
                        // una operacion de 'join'
347 11006 jmvivo
                        //((FLyrVect) getCapa()).getSource().getRecordset().stop();
348
                        if (lyr.isJoined()){
349
                                lyr.getRecordset().stop();
350
                        }
351
                        //CHEMA
352 214 fernando
353 1034 vcaballero
                        long t2 = System.currentTimeMillis();
354 3025 fjp
                        // logger.debug("adapter.stop()");
355 1034 vcaballero
                        adapter.stop();
356
357 3025 fjp
                        // System.out.println(t2 - t1);
358 1034 vcaballero
                } catch (DriverIOException e) {
359
                        throw new DriverException(e);
360 1828 fernando
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
361 1034 vcaballero
                        throw new DriverException(e);
362
                } catch (DriverException e) {
363
                        throw new DriverException(e);
364
                } catch (IOException e) {
365
                        throw new DriverException(e);
366 3963 caballero
                } catch (DriverLoadException e) {
367
                        // TODO Auto-generated catch block
368
                        e.printStackTrace();
369 1034 vcaballero
                }
370 214 fernando
        }
371 1034 vcaballero
372
        /**
373
         * M?todo utilizado para dibujar sobre el graphics que se pasa como
374
         * par?metro, pensado para utilizarse para imprimir.
375 8765 jjdelcerro
         *
376 5813 fjp
         * @param g
377
         *            Graphics2D
378
         * @param viewPort
379
         *            ViewPort.
380 1034 vcaballero
         * @param cancel
381 8765 jjdelcerro
         *
382 1034 vcaballero
         * @throws DriverException
383
         */
384 9287 fjp
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
385
                        PrintRequestAttributeSet printProperties) throws DriverException {
386 5813 fjp
                // super.draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del
387
                // padre, que es el que va sin acelaraci?n!!
388
                try {
389
                        ReadableVectorial adapter = ((SingleLayer) getCapa()).getSource();
390
                        if (adapter.getShapeCount() <= 0) {
391 9287 fjp
                                // logger.info("Layer:" + getCapa().getName() + " sin
392
                                // registros");
393 5813 fjp
                                return;
394
                        }
395
                        FLyrVect lyr = (FLyrVect) getCapa();
396 9066 caballero
                        lyr.beforePrinting(printProperties);
397
398 5813 fjp
                        Selectable selectable = lyr.getRecordset();
399
                        ICoordTrans ct = getCapa().getCoordTrans();
400
                        BitSet bitSet = selectable.getSelection();
401
                        BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver();
402 9370 jorpiell
                        VectorialDriver driver = (VectorialDriver) adapter
403 5813 fjp
                                        .getDriver();
404
                        // logger.debug("adapter.start() -> Layer:" + getCapa().getName());
405
                        adapter.start();
406
                        IGeometry geom;
407
                        if (adapter.getShapeCount() > 0) {
408
                                geom = adapter.getShape(0);
409
                        }
410
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa())
411
                                        .getLegend();
412 3277 fjp
413 5813 fjp
                        Rectangle2D extent = viewPort.getAdjustedExtent();
414
                        // AffineTransform at = viewPort.getAffineTransform();
415 3277 fjp
416 5813 fjp
                        int sc;
417 3277 fjp
418 5813 fjp
                        Rectangle2D bounds;
419 3277 fjp
420 5813 fjp
                        sc = adapter.getShapeCount();
421 3277 fjp
422 5813 fjp
                        long t1 = System.currentTimeMillis();
423
                        // logger.debug("getCapa().getRecordset().start()");
424 11034 caballero
425 11006 jmvivo
                        //CHEMA
426
                        // En teorioa el al hacer el adapter.start()
427
                        // deja abierta las fuentes de datos
428
                        // geometrica y alfanumerica (cuando no son la misma
429 11034 caballero
                        // excepto cuando se ha hecho una operacion de 'join' de la tabla
430 11006 jmvivo
                        // (join de las tablas de gvsig(gdbms) no join de sql), entoces
431
                        // el origen alfanumerico no es el mismo en el source
432
                        // que en la capa.
433
                        //
434
                        //((FLyrVect) getCapa()).getRecordset().start();
435
                        if (lyr.isJoined()){
436
                                lyr.getRecordset().start();
437
                        }
438
                        //CHEMA
439 11034 caballero
440
441 5813 fjp
                        // TODO: A revisar si es o no conveniente este sistema
442
                        // de comunicaci?n con los drivers.
443
                        DriverAttributes attr = adapter.getDriverAttributes();
444
                        boolean bMustClone = false;
445
                        if (attr != null) {
446
                                if (attr.isLoadedInMemory()) {
447
                                        bMustClone = attr.isLoadedInMemory();
448
                                }
449
                        }
450 3277 fjp
451 9287 fjp
                        List lstIndexes = null;
452
453
                        boolean bUseSpatialIndex = false;
454
                        if (lyr.getISpatialIndex() != null) {
455
                                if (isSpatialIndexNecessary(extent)) {
456
                                        lstIndexes = lyr.getISpatialIndex().query(extent);
457
                                        if (ct != null) {
458
                                                Rectangle2D newExtent = ct.getInverted()
459
                                                                .convert(extent);
460
                                                lstIndexes = lyr.getISpatialIndex().query(newExtent);
461
                                        } else {
462
                                                lstIndexes = lyr.getISpatialIndex().query(extent);
463
                                        }
464
                                        sc = lstIndexes.size();
465
                                        bUseSpatialIndex = true;
466
                                }// if
467
                        }// if
468
469
                        int i;
470
                        for (int aux = 0; aux < sc; aux++) {
471
                                if (bUseSpatialIndex) {
472
                                        Integer idRec = (Integer) lstIndexes.get(aux);
473
                                        i = idRec.intValue();
474
                                } else {
475
                                        i = aux;
476
                                }
477
478 5813 fjp
                                bounds = shapeBounds.getShapeBounds(i);
479 3940 caballero
480 5813 fjp
                                if (ct != null) {
481
                                        bounds = ct.convert(bounds);
482
                                }
483 3277 fjp
484 5813 fjp
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
485 8765 jjdelcerro
                                        ISymbol symbol = l.getSymbol(i);
486 9092 caballero
                                        if (symbol == null)
487
                                                continue;
488 5813 fjp
                                        if (bitSet.get(i)) {
489 8765 jjdelcerro
                                                symbol = symbol.getSymbolForSelection();
490 5813 fjp
                                        }
491 3277 fjp
492 9514 jmvivo
                                        //boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
493 3277 fjp
494 5813 fjp
                                        geom = driver.getShape(i);
495 3277 fjp
496 5813 fjp
                                        // PRUEBA DE VELOCIDAD
497
                                        // geom = ShapeFactory.createPolygon2D(new
498
                                        // GeneralPathX(bounds));
499 11034 caballero
500 9511 jmvivo
                                        // JMVIVO: Clonamos siempre que sea necesario y no
501
                                        // solo si hay que reproyectar. Porque el FGeometry.draw
502
                                        // va a aplicar la transformacion sobre la geometria original
503
                                        if (bMustClone)
504
                                                geom = geom.cloneGeometry();
505 3940 caballero
506 5813 fjp
                                        if (ct != null) {
507
                                                geom.reProject(ct);
508
                                        }
509 9013 caballero
510 5813 fjp
                                        geom.draw(g, viewPort, symbol);
511 9092 caballero
512 5813 fjp
                                }
513
                        }
514 3277 fjp
515 5813 fjp
                        // logger.debug("getCapa().getRecordset().stop()");
516 11006 jmvivo
                        //CHEMA
517
                        // Cerramos la fuente alfanumerica cuando se ha hecho
518 11034 caballero
                        // una operacion de 'join'
519 11006 jmvivo
                        //((FLyrVect) getCapa()).getSource().getRecordset().stop();
520
                        if (lyr.isJoined()){
521
                                lyr.getRecordset().stop();
522
                        }
523
                        //CHEMA
524 3277 fjp
525 5813 fjp
                        long t2 = System.currentTimeMillis();
526
                        // logger.debug("adapter.stop()");
527
                        adapter.stop();
528 9066 caballero
                        lyr.afterPrinting();
529 5813 fjp
                        // System.out.println(t2 - t1);
530
                } catch (DriverIOException e) {
531
                        throw new DriverException(e);
532
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
533
                        throw new DriverException(e);
534
                } catch (DriverException e) {
535
                        throw new DriverException(e);
536
                } catch (IOException e) {
537
                        throw new DriverException(e);
538
                }
539 3277 fjp
540 652 fernando
        }
541 5490 caballero
542 5813 fjp
        public FBitSet queryByShape(IGeometry g, int relationship)
543
                        throws DriverException, VisitException {
544 4419 azabala
                return queryByShape(g, relationship, null);
545
        }
546 3940 caballero
547 5813 fjp
        /*
548
         * (non-Javadoc)
549 8765 jjdelcerro
         *
550 5813 fjp
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.core.IGeometry,
551
         *      int)
552
         */
553
        public FBitSet queryByShape(IGeometry g, int relationship,
554
                        CancellableMonitorable cancel) throws DriverException,
555
                        VisitException {
556
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
557
                FLyrVect lyr = (FLyrVect) capa;
558
                // if (lyr.getSpatialIndex() == null) AZABALA
559
                if (lyr.getISpatialIndex() == null)
560
                        return super.queryByShape(g, relationship, null);
561 3940 caballero
562 5813 fjp
                long t1 = System.currentTimeMillis();
563
                ReadableVectorial va = lyr.getSource();
564
                ICoordTrans ct = lyr.getCoordTrans();
565
                Rectangle2D bounds = g.getBounds2D();
566
                List lstRecs = lyr.getISpatialIndex().query(bounds);
567
                Integer idRec;
568
                FBitSet bitset = new FBitSet();
569
                Geometry jtsShape = g.toJTSGeometry();
570
                IntersectionMatrix m;
571
                int index;
572
                try {
573
                        va.start();
574 2979 fjp
575 5813 fjp
                        for (int i = 0; i < lstRecs.size(); i++) {
576
                                if (cancel != null) {
577
                                        cancel.reportStep();
578
                                        if (cancel.isCanceled()) {
579
                                                break;
580
                                        }
581
                                }
582
                                idRec = (Integer) lstRecs.get(i);
583
                                index = idRec.intValue();
584
                                IGeometry geom = va.getShape(index);
585 9287 fjp
                                if (geom == null)
586 5917 azabala
                                        continue;
587 5813 fjp
                                if (ct != null) {
588
                                        geom.reProject(ct);
589
                                }
590
                                Geometry jtsGeom = geom.toJTSGeometry();
591
                                switch (relationship) {
592
                                case CONTAINS:
593
                                        m = jtsShape.relate(jtsGeom);
594
                                        if (m.isContains()) {
595
                                                bitset.set(index, true);
596
                                        }
597
                                        break;
598 2979 fjp
599 5813 fjp
                                case CROSSES:
600
                                        m = jtsShape.relate(jtsGeom);
601
                                        if (m.isCrosses(jtsGeom.getDimension(), jtsShape
602
                                                        .getDimension())) {
603
                                                bitset.set(index, true);
604
                                        }
605
                                        break;
606 2979 fjp
607 5813 fjp
                                case DISJOINT:
608
                                        // TODO: CREO QUE EL DISJOINT NO SE PUEDE METER AQUI
609
                                        m = jtsShape.relate(jtsGeom);
610
                                        if (m.isDisjoint()) {
611
                                                bitset.set(index, true);
612
                                        }
613
                                        break;
614 2979 fjp
615 5813 fjp
                                case EQUALS:
616
                                        m = jtsShape.relate(jtsGeom);
617
                                        if (m.isEquals(jtsGeom.getDimension(), jtsShape
618
                                                        .getDimension())) {
619
                                                bitset.set(index, true);
620
                                        }
621
                                        break;
622 2979 fjp
623 5813 fjp
                                case INTERSECTS:
624
                                        m = jtsShape.relate(jtsGeom);
625
                                        if (m.isIntersects()) {
626
                                                bitset.set(index, true);
627
                                        }
628
                                        break;
629 2979 fjp
630 5813 fjp
                                case OVERLAPS:
631
                                        m = jtsShape.relate(jtsGeom);
632
                                        if (m.isOverlaps(jtsGeom.getDimension(), jtsShape
633
                                                        .getDimension())) {
634
                                                bitset.set(index, true);
635
                                        }
636 2979 fjp
637 5813 fjp
                                        break;
638 2979 fjp
639 5813 fjp
                                case TOUCHES:
640
                                        m = jtsShape.relate(jtsGeom);
641
                                        if (m.isTouches(jtsGeom.getDimension(), jtsShape
642
                                                        .getDimension())) {
643
                                                bitset.set(index, true);
644
                                        }
645 2979 fjp
646 5813 fjp
                                        break;
647 2979 fjp
648 5813 fjp
                                case WITHIN:
649
                                        m = jtsShape.relate(jtsGeom);
650
                                        if (m.isWithin()) {
651
                                                bitset.set(index, true);
652
                                        }
653 2979 fjp
654 5813 fjp
                                        break;
655
                                }
656
                        }
657
                        va.stop();
658
                } catch (DriverIOException e) {
659
                        // TODO Auto-generated catch block
660
                        e.printStackTrace();
661
                }
662
                long t2 = System.currentTimeMillis();
663 9287 fjp
                // logger.info("queryByShape optimizado sobre la capa " + lyr.getName()
664
                // + ". " + (t2 - t1) + " mseg.");
665 5813 fjp
                return bitset;
666
        }
667 2979 fjp
668 5813 fjp
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel)
669
                        throws DriverException {
670
                // Si hay un ?ndice espacial, lo usamos para hacer el query.
671
                FLyrVect lyr = (FLyrVect) capa;
672
                if (lyr.getISpatialIndex() == null)
673
                        return super.queryByRect(rect, cancel);
674 2979 fjp
675 5813 fjp
                ReadableVectorial va = lyr.getSource();
676
                ICoordTrans ct = lyr.getCoordTrans();
677
                Rectangle2D bounds = rect;
678
                List lstRecs = lyr.getISpatialIndex().query(bounds);
679
                Integer idRec;
680
                FBitSet bitset = new FBitSet();
681
                int index;
682
                try {
683
                        va.start();
684
                        DriverAttributes attr = va.getDriverAttributes();
685
                        boolean bMustClone = false;
686
                        if (attr != null) {
687
                                if (attr.isLoadedInMemory()) {
688
                                        bMustClone = attr.isLoadedInMemory();
689
                                }
690
                        }
691 2979 fjp
692 5813 fjp
                        for (int i = 0; i < lstRecs.size(); i++) {
693
                                if (cancel != null) {
694
                                        cancel.reportStep();
695
                                        if (cancel.isCanceled()) {
696
                                                va.stop();
697
                                                return bitset;
698
                                        }
699
                                }
700
                                idRec = (Integer) lstRecs.get(i);
701
                                index = idRec.intValue();
702
                                IGeometry geom = va.getShape(index);
703 9287 fjp
                                if (geom == null)// azabala
704 5917 azabala
                                        continue;
705 5813 fjp
                                if (ct != null) {
706
                                        if (bMustClone)
707
                                                geom = geom.cloneGeometry();
708
                                        geom.reProject(ct);
709
                                }
710
                                if (geom.intersects(rect))
711
                                        bitset.set(index, true);
712
                        }
713
                        va.stop();
714
                } catch (DriverIOException e) {
715
                        // TODO Auto-generated catch block
716
                        e.printStackTrace();
717
                }
718
                return bitset;
719 3940 caballero
720 5813 fjp
        }
721 5490 caballero
722 5813 fjp
        public void process(FeatureVisitor visitor, Rectangle2D rectangle)
723
                        throws DriverException, VisitException {
724
                process(visitor, rectangle, null);
725
        }
726 5490 caballero
727 5813 fjp
        /**
728
         * Processes (by calling visitor.process() method) only those features of
729
         * the vectorial layer associated which intersects given rectangle2d.
730 8765 jjdelcerro
         *
731 5813 fjp
         */
732 5490 caballero
733 5813 fjp
        public void process(FeatureVisitor visitor, Rectangle2D rectangle,
734
                        CancellableMonitorable cancel) throws DriverException,
735
                        VisitException {
736
                if (visitor.start(capa)) {
737
                        FLyrVect lyr = (FLyrVect) capa;
738
                        // if we dont have spatial index or...
739
                        if (lyr.getISpatialIndex() == null) {
740
                                super.process(visitor, rectangle, cancel);
741
                                return;
742
                        }
743
                        // if spatial index is not worthy
744
                        if (!isSpatialIndexNecessary(rectangle)) {
745
                                super.process(visitor, rectangle, cancel);
746
                                return;
747
                        }
748
749
                        ReadableVectorial va = lyr.getSource();
750
                        ICoordTrans ct = lyr.getCoordTrans();
751
                        Rectangle2D bounds = rectangle;
752
                        List lstRecs = lyr.getISpatialIndex().query(bounds);
753 5703 azabala
                        Integer idRec;
754 5813 fjp
                        int index;
755
                        try {
756
                                va.start();
757
                                DriverAttributes attr = va.getDriverAttributes();
758
                                boolean bMustClone = false;
759
                                if (attr != null) {
760
                                        if (attr.isLoadedInMemory()) {
761
                                                bMustClone = attr.isLoadedInMemory();
762
                                        }
763
                                }
764
765
                                for (int i = 0; i < lstRecs.size(); i++) {
766
                                        if (cancel != null) {
767
                                                cancel.reportStep();
768
                                        }
769
                                        if (verifyCancelation(cancel, va, visitor))
770
                                                return;
771
                                        idRec = (Integer) lstRecs.get(i);
772
                                        index = idRec.intValue();
773
                                        IGeometry geom = va.getShape(index);
774 9287 fjp
                                        if (geom == null)// azabala
775 5917 azabala
                                                continue;
776 5813 fjp
                                        if (ct != null) {
777
                                                if (bMustClone)
778
                                                        geom = geom.cloneGeometry();
779
                                                geom.reProject(ct);
780
                                        }
781
                                        if (geom.intersects(rectangle))
782
                                                visitor.visit(geom, index);
783
                                }// for
784
                                va.stop();
785
                        } catch (DriverIOException e) {
786
                                // TODO Auto-generated catch block
787
                                e.printStackTrace();
788
                        }
789
                }// if visitor.start
790 4200 azabala
        }
791
792 5813 fjp
        /*
793
         * (non-Javadoc)
794 8765 jjdelcerro
         *
795 5813 fjp
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(java.awt.geom.Point2D,
796
         *      double)
797
         */
798
        public FBitSet queryByPoint(Point2D p, double tolerance,
799
                        CancellableMonitorable cancel) throws DriverException {
800
                // TODO: OJO!!!!. Est? implementado como un rectangulo.
801
                // Lo correcto deber?a ser calculando las distancias reales
802
                // es decir, con un c?rculo.
803
                Rectangle2D recPoint = new Rectangle2D.Double(p.getX()
804
                                - (tolerance / 2), p.getY() - (tolerance / 2), tolerance,
805
                                tolerance);
806
                return queryByRect(recPoint, cancel);
807
        }
808 214 fernando
}