Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DefaultStrategy.java @ 1484

History | View | Annotate | Download (9.24 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 com.iver.cit.gvsig.fmap.DriverException;
44
import com.iver.cit.gvsig.fmap.ViewPort;
45
import com.iver.cit.gvsig.fmap.core.IGeometry;
46
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
47
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
48
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
49
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
50
import com.iver.cit.gvsig.fmap.layers.FLayer;
51
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
52
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
53
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
54
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
55
import com.iver.cit.gvsig.fmap.operations.Cancellable;
56
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
57
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
58

    
59
import org.apache.log4j.Logger;
60

    
61
import org.cresques.cts.ICoordTrans;
62

    
63
import java.awt.Graphics2D;
64
import java.awt.geom.AffineTransform;
65
import java.awt.geom.Point2D;
66
import java.awt.geom.Rectangle2D;
67
import java.awt.image.BufferedImage;
68

    
69
import java.util.BitSet;
70

    
71

    
72
/**
73
 * Implementa la Strategy por defecto. Los m?todos que tendr?n en com?n la
74
 * mayor parte de las estrategias
75
 */
76
public class DefaultStrategy implements Strategy {
77
        private static Logger logger = Logger.getLogger(DefaultStrategy.class.getName());
78
        private FLayer capa = null;
79

    
80
        /**
81
         * Crea un nuevo DefaultStrategy.
82
         *
83
         * @param capa DOCUMENT ME!
84
         */
85
        public DefaultStrategy(FLayer capa) {
86
                this.capa = capa;
87

    
88
                SingleLayer foo = (SingleLayer) capa;
89
                ClassifiableVectorial vectorial = (ClassifiableVectorial) capa;
90
        }
91

    
92
        /**
93
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
94
         */
95
        public BitSet queryByRect(Rectangle2D rect) throws DriverException {
96
                QueryByRectVisitor visitor = new QueryByRectVisitor();
97

    
98
                visitor.setRect(rect);
99

    
100
                try {
101
                        process(visitor);
102
                } catch (VisitException e) {
103
                        throw new RuntimeException(
104
                                "QueryByRectVisitor lanza una VisitException?");
105
                }
106

    
107
                return visitor.getBitSet();
108
        }
109

    
110
        /**
111
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
112
         *                 int)
113
         */
114
        public BitSet queryByShape(IGeometry g, int relationship)
115
                throws DriverException, VisitException {
116
                QueryByShapeVisitor visitor = new QueryByShapeVisitor();
117
                visitor.setRelationShip(relationship);
118
                visitor.setShape(g);
119
                process(visitor);
120

    
121
                return visitor.getBitSet();
122
        }
123

    
124
        /**
125
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
126
         */
127
        public Rectangle2D getSelectionBounds() {
128
                return null;
129
        }
130

    
131
        /**
132
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#createIndex()
133
         */
134
        public void createIndex() {
135
        }
136

    
137
        /**
138
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
139
         *                 java.awt.Graphics2D, FStyle2D)
140
         */
141
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
142
                Cancellable cancel) throws DriverException {
143
                try {
144
                        VectorialAdapter adapter = ((SingleLayer) capa).getSource();
145
                        ICoordTrans ct = getCapa().getCoordTrans();
146
                        logger.debug("adapter.start()");
147
                        adapter.start();
148

    
149
                        logger.debug("getCapa().getRecordset().start()");
150
                        ((AlphanumericData) getCapa()).getRecordset().start();
151

    
152
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
153
                        int sc;
154
                        long t1 = System.currentTimeMillis();
155
                        Rectangle2D extent = viewPort.getAdjustedExtent();
156
                        AffineTransform at = viewPort.getAffineTransform();
157

    
158
                        sc = adapter.getShapeCount();
159
                        // TODO: A revisar si es o no conveniente este sistema
160
                        // de comunicaci?n con los drivers.
161
                        DriverAttributes attr = adapter.getDriverAttributes();
162
                        boolean bMustClone = false;
163
                        if (attr != null)
164
                        {
165
                            if (attr.isLoadedInMemory())
166
                            {
167
                                bMustClone = attr.isLoadedInMemory();                            
168
                            }
169
                        }
170

    
171
                        for (int i = 0; i < sc; i++) {
172
                                if (cancel.isCanceled()) {
173
                                        break;
174
                                }
175

    
176
                                IGeometry geom = adapter.getShape(i);
177

    
178
                                if (geom == null) {
179
                                        continue;
180
                                }
181

    
182
                                if (ct != null) {
183
                                    if (bMustClone)
184
                                        geom = geom.cloneGeometry();                                    
185
                                        geom.reProject(ct);
186
                                }
187

    
188
                                VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
189

    
190
                                if (geom.fastIntersects(extent.getMinX(), extent.getMinY(), 
191
                                        extent.getWidth(), extent.getHeight())) {
192
                                        FSymbol symbol = l.getSymbol(i);
193
                                        geom.draw(g, viewPort, symbol);
194
                                }
195
                        }
196

    
197
                        logger.debug("getCapa().getRecordset().stop()");
198
                        ((AlphanumericData) getCapa()).getRecordset().stop();
199

    
200
                        long t2 = System.currentTimeMillis();
201
                        logger.debug("adapter.stop()");
202
                        adapter.stop();
203
                        // TODO: A revisar si es o no conveniente este sistema
204
                        // de comunicaci?n con los drivers.
205
                        // DriverAttributes attr = adapter.getDriverAttributes();
206
                        /* if (attr != null)
207
                        {
208
                            if (attr.isLoadedInMemory())
209
                            {
210
                                // Quitamos lo de la reproyecci?n al vuelo para que 
211
                                // solo se haga una vez.
212
                                getCapa().setCoordTrans(null);
213
                            }
214
                        } */
215

    
216
                        System.out.println(t2 - t1);
217
                } catch (DriverIOException e) {
218
                        throw new DriverException(e);
219
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
220
                    throw new DriverException(e);
221
                } catch (DriverException e) {
222
                    throw new DriverException(e);
223
        }
224
        }
225

    
226
        /**
227
         * Devuelve la capa.
228
         *
229
         * @return Returns the capa.
230
         */
231
        public FLayer getCapa() {
232
                return capa;
233
        }
234

    
235
        /**
236
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#getFullExtent()
237
         */
238
        public Rectangle2D getFullExtent() throws DriverException {
239
                return null;
240
        }
241

    
242
        /**
243
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
244
         *                 java.util.BitSet)
245
         */
246
        public void process(FeatureVisitor visitor, BitSet subset)
247
                throws DriverException, VisitException {
248
                try {
249
                        logger.debug("visitor.start()");
250

    
251
                        if (visitor.start(capa)) {
252
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
253

    
254
                                va.start();
255
                                for (int i = 0; i < va.getShapeCount(); i++) {
256
                                        if (subset.get(i)) {
257
                                                visitor.visit(va.getShape(i), i);
258
                                        }
259
                                }
260
                                va.stop();
261

    
262
                                logger.debug("visitor.stop()");
263
                                visitor.stop(capa);
264
                        }
265
                } catch (DriverIOException e) {
266
                        throw new DriverException(e);
267
                }
268
        }
269

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

    
285
                        if (visitor.start(capa)) {
286
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
287
                                ICoordTrans ct = getCapa().getCoordTrans();
288
                                va.start();
289
                                for (int i = 0; i < va.getShapeCount(); i++) {
290
                                    IGeometry geom = va.getShape(i);
291
                                    if (ct != null) {
292
                                                geom.reProject(ct);
293
                                        }
294

    
295
                                        visitor.visit(geom, i);
296
                                }
297
                                va.stop();
298
                                logger.debug("visitor.stop()");
299
                                visitor.stop(capa);
300
                        }
301
                } catch (DriverIOException e) {
302
                        throw new DriverException(e);
303
                }
304
        }
305

    
306
        /**
307
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
308
         *                 double)
309
         */
310
        public BitSet queryByPoint(Point2D p, double tolerance)
311
                throws DriverException {
312
                QueryByPointVisitor visitor = new QueryByPointVisitor();
313
                visitor.setLayer(capa);
314
                visitor.setTolerance(tolerance);
315
                visitor.setQueriedPoint(p);
316

    
317
                try {
318
                        process(visitor);
319
                } catch (VisitException e) {
320
                        throw new RuntimeException(
321
                                "QueryByPointVisitor lanza una VisitException?");
322
                }
323

    
324
                return visitor.getBitSet();
325
        }
326

    
327
        /* (non-Javadoc)
328
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
329
         */
330
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
331
                throws DriverException {
332
                draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
333
        }
334
}