Statistics
| Revision:

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

History | View | Annotate | Download (13.2 KB)

1 1260 fernando
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2 1100 fjp
 *
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 1828 fernando
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 9013 caballero
import javax.print.attribute.PrintRequestAttributeSet;
51
52 1828 fernando
import org.apache.log4j.Logger;
53
import org.cresques.cts.ICoordTrans;
54
55 652 fernando
import com.iver.cit.gvsig.fmap.DriverException;
56 214 fernando
import com.iver.cit.gvsig.fmap.ViewPort;
57 305 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
58 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
59 1233 fjp
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
60 214 fernando
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61 2183 fernando
import com.iver.cit.gvsig.fmap.layers.FBitSet;
62 562 fernando
import com.iver.cit.gvsig.fmap.layers.FLayer;
63 5490 caballero
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64 3940 caballero
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
65 1691 fjp
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
66 1445 fjp
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
67 562 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
68 2613 caballero
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
69 562 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
70 321 fernando
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
71 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
72
import com.iver.utiles.swing.threads.CancellableMonitorable;
73 214 fernando
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 2979 fjp
    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 3940 caballero
89 528 vcaballero
        private static Logger logger = Logger.getLogger(DefaultStrategy.class.getName());
90 1704 fjp
        FLayer capa = null;
91 8765 jjdelcerro
92 214 fernando
        /**
93
         * Crea un nuevo DefaultStrategy.
94
         *
95
         * @param capa DOCUMENT ME!
96
         */
97 562 fernando
        public DefaultStrategy(FLayer capa) {
98 214 fernando
                this.capa = capa;
99 1034 vcaballero
100 562 fernando
                SingleLayer foo = (SingleLayer) capa;
101 1034 vcaballero
                ClassifiableVectorial vectorial = (ClassifiableVectorial) capa;
102 214 fernando
        }
103
104
        /**
105
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
106
         */
107 4347 azabala
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) throws DriverException {
108 562 fernando
                QueryByRectVisitor visitor = new QueryByRectVisitor();
109 980 fjp
110 235 vcaballero
                visitor.setRect(rect);
111 1034 vcaballero
112
                try {
113 4311 azabala
                        process(visitor, cancel);
114 929 fernando
                } catch (VisitException e) {
115 1034 vcaballero
                        throw new RuntimeException(
116
                                "QueryByRectVisitor lanza una VisitException?");
117 929 fernando
                }
118 562 fernando
119 235 vcaballero
                return visitor.getBitSet();
120 214 fernando
        }
121
122
        /**
123 305 fjp
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
124 214 fernando
         *                 int)
125
         */
126 4347 azabala
        public FBitSet queryByShape(IGeometry g, int relationship, CancellableMonitorable cancel)
127 929 fernando
                throws DriverException, VisitException {
128 7011 fjp
                QueryByGeometryVisitor visitor = new QueryByGeometryVisitor(g, relationship);
129 235 vcaballero
                process(visitor);
130 562 fernando
131 235 vcaballero
                return visitor.getBitSet();
132 214 fernando
        }
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 228 fernando
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
149 8765 jjdelcerro
         *                 java.awt.Graphics2D, ISymbol)
150 214 fernando
         */
151 562 fernando
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
152 1034 vcaballero
                Cancellable cancel) throws DriverException {
153
                try {
154 3940 caballero
                        ReadableVectorial adapter = ((SingleLayer) capa).getSource();
155 1034 vcaballero
                        ICoordTrans ct = getCapa().getCoordTrans();
156 8765 jjdelcerro
//                        logger.info("adapter.start()");
157 1034 vcaballero
                        adapter.start();
158 214 fernando
159 8765 jjdelcerro
//                        logger.info("getCapa().getRecordset().start()");
160 3940 caballero
                        SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset();
161 1691 fjp
                        if (rsSel != null)
162
                            rsSel.start();
163 1445 fjp
164 1034 vcaballero
                        int sc;
165
                        Rectangle2D extent = viewPort.getAdjustedExtent();
166
                        AffineTransform at = viewPort.getAffineTransform();
167 214 fernando
168 1034 vcaballero
                        sc = adapter.getShapeCount();
169 1484 fjp
                        // 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 3940 caballero
                                bMustClone = attr.isLoadedInMemory();
178 1484 fjp
                            }
179
                        }
180 1704 fjp
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
181 3302 fjp
            FBitSet bitSet = null;
182
            if (getCapa() instanceof Selectable){
183
                Selectable selection = (Selectable) getCapa();
184
                bitSet = selection.getSelection();
185 3940 caballero
            }
186 1034 vcaballero
                        for (int i = 0; i < sc; i++) {
187
                                if (cancel.isCanceled()) {
188
                                        break;
189
                                }
190 562 fernando
191 3940 caballero
192 1034 vcaballero
                                IGeometry geom = adapter.getShape(i);
193 214 fernando
194 1034 vcaballero
                                if (geom == null) {
195
                                        continue;
196
                                }
197 562 fernando
198 1034 vcaballero
                                if (ct != null) {
199 1484 fjp
                                    if (bMustClone)
200 3940 caballero
                                        geom = geom.cloneGeometry();
201 1034 vcaballero
                                        geom.reProject(ct);
202
                                }
203 3940 caballero
204 1691 fjp
                                // if (geom.intersects(extent)) {
205 3940 caballero
                                if (geom.fastIntersects(extent.getMinX(), extent.getMinY(),
206 1691 fjp
                                         extent.getWidth(), extent.getHeight())) {
207 8765 jjdelcerro
                                        ISymbol symbol = l.getSymbol(i);
208 3940 caballero
209 2454 fjp
                    if (symbol ==null)
210
                        continue;
211 3302 fjp
                    if (bitSet != null)
212
                        if (bitSet.get(i)) {
213 8765 jjdelcerro
                                                    symbol = symbol.getSymbolForSelection();
214 3302 fjp
                                            }
215 8765 jjdelcerro
                    if (symbol != null)
216
                            geom.draw(g, viewPort, symbol);
217
218 1034 vcaballero
                                }
219 1691 fjp
                                /* else
220
                                {
221
                                    System.out.println("no pinto id=" + i);
222
                                } */
223 214 fernando
                        }
224
225 8765 jjdelcerro
//                        logger.info("getCapa().getRecordset().stop()");
226 1691 fjp
                        if (rsSel != null)
227
                            rsSel.stop();
228 1445 fjp
229 2183 fernando
230 8765 jjdelcerro
//                        logger.debug("adapter.stop()");
231 1034 vcaballero
                        adapter.stop();
232 1233 fjp
                        // TODO: A revisar si es o no conveniente este sistema
233
                        // de comunicaci?n con los drivers.
234 1484 fjp
                        // DriverAttributes attr = adapter.getDriverAttributes();
235
                        /* if (attr != null)
236 1233 fjp
                        {
237
                            if (attr.isLoadedInMemory())
238
                            {
239 3940 caballero
                                // Quitamos lo de la reproyecci?n al vuelo para que
240 1233 fjp
                                // solo se haga una vez.
241
                                getCapa().setCoordTrans(null);
242
                            }
243 1484 fjp
                        } */
244 214 fernando
245 1034 vcaballero
                } catch (DriverIOException e) {
246 652 fernando
                        throw new DriverException(e);
247 1828 fernando
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
248 1445 fjp
                    throw new DriverException(e);
249
                } catch (DriverException e) {
250
                    throw new DriverException(e);
251
        }
252 214 fernando
        }
253
254 5223 fjp
255 214 fernando
        /**
256 1034 vcaballero
         * Devuelve la capa.
257 214 fernando
         *
258
         * @return Returns the capa.
259
         */
260 562 fernando
        public FLayer getCapa() {
261 214 fernando
                return capa;
262
        }
263 216 fernando
264
        /**
265 562 fernando
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
266 1055 vcaballero
         *                 java.util.BitSet)
267 228 fernando
         */
268 562 fernando
        public void process(FeatureVisitor visitor, BitSet subset)
269 929 fernando
                throws DriverException, VisitException {
270 4311 azabala
                process(visitor, subset, null);
271
        }
272
273
        /**
274
         * DOCUMENT ME!
275
         *
276
         * @param visitor DOCUMENT ME!
277
         *
278
         * @throws DriverException DOCUMENT ME!
279
         * @throws VisitException
280
         *
281
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
282
         */
283
        public void process(FeatureVisitor visitor)
284
                throws DriverException, VisitException {
285 4347 azabala
                process(visitor, (CancellableMonitorable)null);
286 4311 azabala
        }
287
288
        /**
289
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
290
         *                 double)
291
         */
292 4347 azabala
        public FBitSet queryByPoint(Point2D p, double tolerance, CancellableMonitorable cancel)
293 4311 azabala
                throws DriverException {
294
        // TODO: OJO!!!!. Est? implementado como un rectangulo.
295
        // Lo correcto deber?a ser calculando las distancias reales
296
        // es decir, con un c?rculo.
297
        Rectangle2D recPoint = new Rectangle2D.Double(p.getX() - (tolerance / 2),
298
                p.getY() - (tolerance / 2), tolerance, tolerance);
299
                return queryByRect(recPoint);
300
        }
301
302
        /* (non-Javadoc)
303
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
304
         */
305 9013 caballero
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, PrintRequestAttributeSet properties)
306 4311 azabala
                throws DriverException {
307 9066 caballero
                if (capa instanceof FLyrVect) {
308
                        ((FLyrVect)capa).beforePrinting(properties);
309
                        draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
310
                        ((FLyrVect)capa).afterPrinting();
311
                }else{
312
                        draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
313
                }
314 4311 azabala
        }
315
316
        public void process(FeatureVisitor visitor, Rectangle2D rectangle) throws DriverException, VisitException {
317
                process(visitor, rectangle, null);
318
        }
319 8765 jjdelcerro
320 4311 azabala
        /**
321
         * Verifies cancelation events, and return a boolean flag
322 8765 jjdelcerro
         * if processes must be stopped for this cancelations events.
323
         *
324 4311 azabala
         * @param cancel
325
         * @param va
326
         * @param visitor
327
         * @return
328 8765 jjdelcerro
         * @throws DriverIOException
329 4311 azabala
         */
330
        protected boolean verifyCancelation(Cancellable cancel, ReadableVectorial va, FeatureVisitor visitor) throws DriverIOException{
331
                if(cancel != null){
332
                        if(cancel.isCanceled()){
333
                                va.stop();
334
                                visitor.stop(capa);
335 8765 jjdelcerro
//                                logger.info("visitor canceled");
336 4311 azabala
                                return true;
337
                        }
338
                }
339
                return false;
340
        }
341
342 4347 azabala
        public void process(FeatureVisitor visitor, BitSet subset, CancellableMonitorable cancel) throws DriverException, VisitException {
343 1034 vcaballero
                try {
344 8765 jjdelcerro
//                        logger.info("visitor.start()");
345 562 fernando
346 1034 vcaballero
                        if (visitor.start(capa)) {
347 3940 caballero
                                ReadableVectorial va = ((SingleLayer) capa).getSource();
348 1253 fernando
                                va.start();
349 1034 vcaballero
                                for (int i = 0; i < va.getShapeCount(); i++) {
350 8765 jjdelcerro
351 4311 azabala
                                        if(verifyCancelation(cancel, va, visitor))
352
                                                return;
353 1034 vcaballero
                                        if (subset.get(i)) {
354 4502 azabala
                                                if(cancel != null){
355
                                                        cancel.reportStep();
356
                                                }
357 1034 vcaballero
                                                visitor.visit(va.getShape(i), i);
358
                                        }
359 562 fernando
                                }
360 1253 fernando
                                va.stop();
361 562 fernando
362 8765 jjdelcerro
//                                logger.info("visitor.stop()");
363 1034 vcaballero
                                visitor.stop(capa);
364
                        }
365
                } catch (DriverIOException e) {
366
                        throw new DriverException(e);
367 229 vcaballero
                }
368 228 fernando
        }
369 562 fernando
370 4347 azabala
        public void process(FeatureVisitor visitor, CancellableMonitorable cancel) throws DriverException, VisitException {
371 1034 vcaballero
                try {
372 8765 jjdelcerro
//                        logger.info("visitor.start()");
373 562 fernando
374 1034 vcaballero
                        if (visitor.start(capa)) {
375 3940 caballero
                                ReadableVectorial va = ((SingleLayer) capa).getSource();
376 1293 fjp
                                ICoordTrans ct = getCapa().getCoordTrans();
377 1253 fernando
                                va.start();
378 1034 vcaballero
                                for (int i = 0; i < va.getShapeCount(); i++) {
379 4419 azabala
                                        if(cancel != null){
380
                                                cancel.reportStep();
381
                                        }
382 4311 azabala
                                        if(verifyCancelation(cancel, va, visitor))
383
                                                return;
384 1293 fjp
                                    IGeometry geom = va.getShape(i);
385
                                    if (ct != null) {
386
                                                geom.reProject(ct);
387
                                        }
388
389
                                        visitor.visit(geom, i);
390 1034 vcaballero
                                }
391 1253 fernando
                                va.stop();
392 8765 jjdelcerro
//                                logger.info("visitor.stop()");
393 1034 vcaballero
                                visitor.stop(capa);
394
                        }
395
                } catch (DriverIOException e) {
396 652 fernando
                        throw new DriverException(e);
397 1034 vcaballero
                }
398 232 vcaballero
        }
399 562 fernando
400 4347 azabala
        public void process(FeatureVisitor visitor, Rectangle2D rectangle, CancellableMonitorable cancel) throws DriverException, VisitException {
401 4200 azabala
                FilterRectVisitor filterVisitor = new FilterRectVisitor();
402
                filterVisitor.setRectangle(rectangle);
403
                filterVisitor.setWrappedVisitor(visitor);
404
                try {
405 4311 azabala
                        process(filterVisitor, cancel);
406 4200 azabala
                } catch (VisitException e) {
407
                        throw new RuntimeException(
408
                                "process(visitor,rectangle) lanza una VisitException?");
409 8765 jjdelcerro
                }
410 4200 azabala
        }
411 4311 azabala
412
        public FBitSet queryByPoint(Point2D p, double tolerance) throws DriverException {
413
                return queryByPoint(p, tolerance, null);
414
        }
415
416
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
417
                return queryByRect(rect, null);
418
        }
419
420
        public FBitSet queryByShape(IGeometry g, int relationship) throws DriverException, VisitException {
421
                return queryByShape(g, relationship, null);
422
        }
423
424 5490 caballero
        /**
425
         * Tells when in a spatial query the use of an spatial index is an advanced,
426
         * or it would be better to use a secuential search.
427
         * <br>
428
         * The criteria to decide is the area of the query region. If it is less than
429
         * 1/4 of full extent layer region, the spatial index will be an improve.
430
         * Else, a secuential scan would be better
431
         * @param rectangle
432
         * @return
433
         * @throws DriverException
434
         */
435
        protected boolean isSpatialIndexNecessary(Rectangle2D extent) throws DriverException {
436
                FLyrVect lyr = (FLyrVect) getCapa();
437
                double areaExtent = extent.getWidth() * extent.getHeight();
438
                double areaFullExtent = lyr.getFullExtent().getWidth() *
439
                                                        lyr.getFullExtent().getHeight();
440
                return areaExtent < (areaFullExtent / 4.0);
441 8765 jjdelcerro
442 5490 caballero
        }
443
444 214 fernando
}