Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1014 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DefaultStrategy.java @ 13593

History | View | Annotate | Download (15.9 KB)

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

    
43
import java.awt.Graphics2D;
44
import java.awt.geom.AffineTransform;
45
import java.awt.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47
import java.awt.image.BufferedImage;
48
import java.util.BitSet;
49

    
50
import javax.print.attribute.PrintRequestAttributeSet;
51

    
52
import org.apache.log4j.Logger;
53
import org.cresques.cts.ICoordTrans;
54

    
55
import com.iver.cit.gvsig.fmap.DriverException;
56
import com.iver.cit.gvsig.fmap.ViewPort;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ISymbol;
59
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.layers.FBitSet;
62
import com.iver.cit.gvsig.fmap.layers.FLayer;
63
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
65
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
66
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
67
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
68
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
69
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
70
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
71
import com.iver.utiles.swing.threads.Cancellable;
72
import com.iver.utiles.swing.threads.CancellableMonitorable;
73

    
74

    
75
/**
76
 * Implementa la Strategy por defecto. Los m?todos que tendr?n en com?n la
77
 * mayor parte de las estrategias
78
 */
79
public class DefaultStrategy implements Strategy {
80
    public static final int EQUALS = 0;
81
    public static final int DISJOINT = 1;
82
    public static final int INTERSECTS = 2;
83
    public static final int TOUCHES = 3;
84
    public static final int CROSSES = 4;
85
    public static final int WITHIN = 5;
86
    public static final int CONTAINS = 6;
87
    public static final int OVERLAPS = 7;
88

    
89
        private static Logger logger = Logger.getLogger(DefaultStrategy.class.getName());
90
        FLayer capa = null;
91

    
92
        /**
93
         * Crea un nuevo DefaultStrategy.
94
         *
95
         * @param capa DOCUMENT ME!
96
         */
97
        public DefaultStrategy(FLayer capa) {
98
                this.capa = capa;
99

    
100
                SingleLayer foo = (SingleLayer) capa;
101
                ClassifiableVectorial vectorial = (ClassifiableVectorial) capa;
102
        }
103

    
104
        /**
105
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
106
         */
107
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) throws DriverException {
108
                QueryByRectVisitor visitor = new QueryByRectVisitor();
109

    
110
                visitor.setRect(rect);
111

    
112
                try {
113
                        process(visitor, cancel);
114
                } catch (VisitException e) {
115
                        throw new RuntimeException(
116
                                "QueryByRectVisitor lanza una VisitException?");
117
                }
118

    
119
                return visitor.getBitSet();
120
        }
121

    
122
        /**
123
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
124
         *                 int)
125
         */
126
        public FBitSet queryByShape(IGeometry g, int relationship, CancellableMonitorable cancel)
127
                throws DriverException, VisitException {
128
                QueryByGeometryVisitor visitor = new QueryByGeometryVisitor(g, relationship);
129
                process(visitor);
130

    
131
                return visitor.getBitSet();
132
        }
133

    
134
        /**
135
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
136
         */
137
        public Rectangle2D getSelectionBounds() {
138
                return null;
139
        }
140

    
141
        /**
142
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#createIndex()
143
         */
144
        public void createIndex() {
145
        }
146

    
147
        /**
148
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
149
         *                 java.awt.Graphics2D, ISymbol)
150
         */
151
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
152
                Cancellable cancel) throws DriverException {
153
                try {
154
                        ReadableVectorial adapter = ((SingleLayer) capa).getSource();
155
                        ICoordTrans ct = getCapa().getCoordTrans();
156
//                        logger.info("adapter.start()");
157
                        adapter.start();
158

    
159
//                        logger.info("getCapa().getRecordset().start()");
160
                        SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset();
161
                        if (rsSel != null)
162
                            rsSel.start();
163

    
164
                        int sc;
165
                        Rectangle2D extent = viewPort.getAdjustedExtent();
166
                        AffineTransform at = viewPort.getAffineTransform();
167

    
168
                        sc = adapter.getShapeCount();
169
                        // TODO: A revisar si es o no conveniente este sistema
170
                        // de comunicaci?n con los drivers.
171
                        DriverAttributes attr = adapter.getDriverAttributes();
172
                        boolean bMustClone = false;
173
                        if (attr != null)
174
                        {
175
                            if (attr.isLoadedInMemory())
176
                            {
177
                                bMustClone = attr.isLoadedInMemory();
178
                            }
179
                        }
180
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
181
            FBitSet bitSet = rsSel.getSelection();
182
                        for (int i = 0; i < sc; i++) {
183
                                if (cancel.isCanceled()) {
184
                                        break;
185
                                }
186

    
187

    
188
                                IGeometry geom = adapter.getShape(i);
189

    
190
                                if (geom == null) {
191
                                        continue;
192
                                }
193

    
194
                                if (ct != null) {
195
                                    if (bMustClone)
196
                                        geom = geom.cloneGeometry();
197
                                        geom.reProject(ct);
198
                                }
199

    
200
                                // if (geom.intersects(extent)) {
201
                                if (geom.fastIntersects(extent.getMinX(), extent.getMinY(),
202
                                         extent.getWidth(), extent.getHeight())) {
203
                                        ISymbol symbol = l.getSymbol(i);
204

    
205
                    if (symbol ==null)
206
                        continue;
207
                    if (bitSet != null)
208
                        if (bitSet.get(i)) {
209
                                                    symbol = symbol.getSymbolForSelection();
210
                                            }
211
                    if (symbol != null)
212
                            geom.draw(g, viewPort, symbol);
213

    
214
                                }
215
                                /* else
216
                                {
217
                                    System.out.println("no pinto id=" + i);
218
                                } */
219
                        }
220

    
221
//                        logger.info("getCapa().getRecordset().stop()");
222
                        if (rsSel != null)
223
                            rsSel.stop();
224

    
225

    
226
//                        logger.debug("adapter.stop()");
227
                        adapter.stop();
228
                        // TODO: A revisar si es o no conveniente este sistema
229
                        // de comunicaci?n con los drivers.
230
                        // DriverAttributes attr = adapter.getDriverAttributes();
231
                        /* if (attr != null)
232
                        {
233
                            if (attr.isLoadedInMemory())
234
                            {
235
                                // Quitamos lo de la reproyecci?n al vuelo para que
236
                                // solo se haga una vez.
237
                                getCapa().setCoordTrans(null);
238
                            }
239
                        } */
240

    
241
                } catch (DriverIOException e) {
242
                        throw new DriverException(e);
243
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
244
                    throw new DriverException(e);
245
                } catch (DriverException e) {
246
                    throw new DriverException(e);
247
        }
248
        }
249

    
250

    
251
        /**
252
         * Devuelve la capa.
253
         *
254
         * @return Returns the capa.
255
         */
256
        public FLayer getCapa() {
257
                return capa;
258
        }
259

    
260
        /**
261
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
262
         *                 java.util.BitSet)
263
         */
264
        public void process(FeatureVisitor visitor, BitSet subset)
265
                throws DriverException, VisitException {
266
                process(visitor, subset, null);
267
        }
268

    
269
        /**
270
         * DOCUMENT ME!
271
         *
272
         * @param visitor DOCUMENT ME!
273
         *
274
         * @throws DriverException DOCUMENT ME!
275
         * @throws VisitException
276
         *
277
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
278
         */
279
        public void process(FeatureVisitor visitor)
280
                throws DriverException, VisitException {
281
                process(visitor, (CancellableMonitorable)null);
282
        }
283

    
284
        /**
285
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
286
         *                 double)
287
         */
288
        public FBitSet queryByPoint(Point2D p, double tolerance, CancellableMonitorable cancel)
289
                throws DriverException {
290
        // TODO: OJO!!!!. Est? implementado como un rectangulo.
291
        // Lo correcto deber?a ser calculando las distancias reales
292
        // es decir, con un c?rculo.
293
        Rectangle2D recPoint = new Rectangle2D.Double(p.getX() - (tolerance / 2),
294
                p.getY() - (tolerance / 2), tolerance, tolerance);
295
                return queryByRect(recPoint);
296
        }
297

    
298
        /* (non-Javadoc)
299
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
300
         */
301
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties)
302
                throws DriverException {
303
                if (capa instanceof FLyrVect) {
304
                        ((FLyrVect)capa).beforePrinting(properties);
305
                        draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
306
                        ((FLyrVect)capa).afterPrinting();
307
                }else{
308
                        draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
309
                }
310
        }
311

    
312
        public void process(FeatureVisitor visitor, Rectangle2D rectangle) throws DriverException, VisitException {
313
                process(visitor, rectangle, null);
314
        }
315

    
316
        /**
317
         * Verifies cancelation events, and return a boolean flag
318
         * if processes must be stopped for this cancelations events.
319
         *
320
         * @param cancel
321
         * @param va
322
         * @param visitor
323
         * @return
324
         * @throws DriverIOException
325
         */
326
        protected boolean verifyCancelation(Cancellable cancel, ReadableVectorial va, FeatureVisitor visitor) throws DriverIOException{
327
                if(cancel != null){
328
                        if(cancel.isCanceled()){
329
                                va.stop();
330
                                visitor.stop(capa);
331
//                                logger.info("visitor canceled");
332
                                return true;
333
                        }
334
                }
335
                return false;
336
        }
337

    
338
        public void process(FeatureVisitor visitor, BitSet subset, CancellableMonitorable cancel) throws DriverException, VisitException {
339
                try {
340
//                        logger.info("visitor.start()");
341

    
342
                        if (visitor.start(capa)) {
343
                                ReadableVectorial va = ((SingleLayer) capa).getSource();
344
                                va.start();
345
                                for (int i = 0; i < va.getShapeCount(); i++) {
346

    
347
                                        if(verifyCancelation(cancel, va, visitor))
348
                                                return;
349
                                        if (subset.get(i)) {
350
                                                if(cancel != null){
351
                                                        cancel.reportStep();
352
                                                }
353
                                                visitor.visit(va.getShape(i), i);
354
                                        }
355
                                }
356
                                va.stop();
357

    
358
//                                logger.info("visitor.stop()");
359
                                visitor.stop(capa);
360
                        }
361
                } catch (DriverIOException e) {
362
                        throw new DriverException(e);
363
                }
364
        }
365

    
366
        public void process(FeatureVisitor visitor, CancellableMonitorable cancel) throws DriverException, VisitException {
367
                try {
368
//                        logger.info("visitor.start()");
369

    
370
                        if (visitor.start(capa)) {
371
                                ReadableVectorial va = ((SingleLayer) capa).getSource();
372
                                ICoordTrans ct = getCapa().getCoordTrans();
373
                                va.start();
374
                                for (int i = 0; i < va.getShapeCount(); i++) {
375
                                        if(cancel != null){
376
                                                cancel.reportStep();
377
                                        }
378
                                        if(verifyCancelation(cancel, va, visitor))
379
                                                return;
380
                                    IGeometry geom = va.getShape(i);
381
                                    if (ct != null) {
382
                                                geom.reProject(ct);
383
                                        }
384

    
385
                                        visitor.visit(geom, i);
386
                                }
387
                                va.stop();
388
//                                logger.info("visitor.stop()");
389
                                visitor.stop(capa);
390
                        }
391
                } catch (DriverIOException e) {
392
                        throw new DriverException(e);
393
                }
394
        }
395

    
396
        public void process(FeatureVisitor visitor, Rectangle2D rectangle, CancellableMonitorable cancel) throws DriverException, VisitException {
397
                FilterRectVisitor filterVisitor = new FilterRectVisitor();
398
                filterVisitor.setRectangle(rectangle);
399
                filterVisitor.setWrappedVisitor(visitor);
400
                try {
401
                        process(filterVisitor, cancel);
402
                } catch (VisitException e) {
403
                        throw new RuntimeException(
404
                                "process(visitor,rectangle) lanza una VisitException?");
405
                }
406
        }
407

    
408
        public FBitSet queryByPoint(Point2D p, double tolerance) throws DriverException {
409
                return queryByPoint(p, tolerance, null);
410
        }
411

    
412
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
413
                return queryByRect(rect, null);
414
        }
415

    
416
        public FBitSet queryByShape(IGeometry g, int relationship) throws DriverException, VisitException {
417
                return queryByShape(g, relationship, null);
418
        }
419

    
420
        /**
421
         * Tells when in a spatial query the use of an spatial index is an advanced,
422
         * or it would be better to use a secuential search.
423
         * <br>
424
         * The criteria to decide is the area of the query region. If it is less than
425
         * 1/4 of full extent layer region, the spatial index will be an improve.
426
         * Else, a secuential scan would be better
427
         * @param rectangle
428
         * @return
429
         * @throws DriverException
430
         */
431
        protected boolean isSpatialIndexNecessary(Rectangle2D extent) throws DriverException {
432
                FLyrVect lyr = (FLyrVect) getCapa();
433
                double areaExtent = extent.getWidth() * extent.getHeight();
434
                double areaFullExtent = lyr.getFullExtent().getWidth() *
435
                                                        lyr.getFullExtent().getHeight();
436
                return areaExtent < (areaFullExtent / 4.0);
437

    
438
        }
439

    
440
        /**
441
         * Similar to process, but calls visitWithSymbol in FeatureVisitor.
442
         * @param visitor
443
         * @param rectangle
444
         * @param cancel
445
         * @throws DriverException
446
         * @throws VisitException
447
         */
448
        public void processWithSymbols(ExtendsFeatureVisitor visitor, Rectangle2D extent, CancellableMonitorable cancel) throws DriverException, VisitException {
449
                
450
                if (visitor==null)
451
                        return;
452
                
453
                /*IGeometry geom = null;
454
                int index = -1;
455
                ISymbol symbol = null;
456
                visitor.visitWithSymbol(geom, index, symbol); // TEST */
457
                
458
                try {
459
                        ReadableVectorial adapter = ((SingleLayer) capa).getSource();
460
                        ICoordTrans ct = getCapa().getCoordTrans();
461
                        //logger.debug("adapter.start()");
462
                        adapter.start();
463

    
464
                        //logger.debug("getCapa().getRecordset().start()");
465
                        SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset();
466
                        if (rsSel != null)
467
                            rsSel.start();
468

    
469
                        int sc = adapter.getShapeCount();
470
                        
471
                        // TODO: A revisar si es o no conveniente este sistema
472
                        // de comunicaci?n con los drivers.
473
                        DriverAttributes attr = adapter.getDriverAttributes();
474
                        boolean bMustClone = false;
475
                        if (attr != null)
476
                        {
477
                            if (attr.isLoadedInMemory())
478
                            {
479
                                bMustClone = attr.isLoadedInMemory();
480
                            }
481
                        }
482
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
483
            FBitSet bitSet = null;
484
            if (getCapa() instanceof Selectable){
485
                Selectable selection = (Selectable) getCapa();
486
                bitSet = selection.getSelection();
487
            }
488
                        for (int i = 0; i < sc; i++) {
489
                                if (cancel!=null && cancel.isCanceled()) {
490
                                        break;
491
                                }
492

    
493
                                IGeometry geom = adapter.getShape(i);
494

    
495
                                if (geom == null) {
496
                                        continue;
497
                                }
498

    
499
                                if (ct != null) {
500
                                    if (bMustClone)
501
                                        geom = geom.cloneGeometry();
502
                                        geom.reProject(ct);
503
                                }
504

    
505
                                if (extent == null || 
506
                                                geom.fastIntersects(extent.getMinX(), extent.getMinY(),
507
                                             extent.getWidth(), extent.getHeight())) {
508
                                        ISymbol symbol = l.getSymbol(i);
509

    
510
                    if (symbol ==null)
511
                        continue;
512
                    if (bitSet != null)
513
                        if (bitSet.get(i)) {
514
                                                    symbol = symbol.getSymbolForSelection();
515
                                            }
516
                                        // send feature for process in visitor
517
                                        visitor.visitWithSymbol(geom, i, symbol);
518
                                        
519
                                }
520
                                /* else
521
                                {
522
                                    System.out.println("no pinto id=" + i);
523
                                } */
524
                        }
525

    
526
                        //logger.debug("getCapa().getRecordset().stop()");
527
                        if (rsSel != null)
528
                            rsSel.stop();
529

    
530
                        //logger.debug("adapter.stop()");
531
                        adapter.stop();
532

    
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
        }
540
        }
541

    
542

    
543
}