Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DefaultStrategy.java @ 1704

History | View | Annotate | Download (9.23 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.SelectableDataSource;
52
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
53
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
54
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
55
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
56
import com.iver.cit.gvsig.fmap.operations.Cancellable;
57
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
58
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
59

    
60
import org.apache.log4j.Logger;
61

    
62
import org.cresques.cts.ICoordTrans;
63

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

    
70
import java.util.BitSet;
71

    
72

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

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

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

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

    
99
                visitor.setRect(rect);
100

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

    
108
                return visitor.getBitSet();
109
        }
110

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

    
122
                return visitor.getBitSet();
123
        }
124

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

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

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

    
150
                        logger.debug("getCapa().getRecordset().start()");
151
                        SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset(); 
152
                        if (rsSel != null)
153
                            rsSel.start();
154

    
155
                        int sc;
156
                        long t1 = System.currentTimeMillis();
157
                        Rectangle2D extent = viewPort.getAdjustedExtent();
158
                        AffineTransform at = viewPort.getAffineTransform();
159

    
160
                        sc = adapter.getShapeCount();
161
                        // TODO: A revisar si es o no conveniente este sistema
162
                        // de comunicaci?n con los drivers.
163
                        DriverAttributes attr = adapter.getDriverAttributes();
164
                        boolean bMustClone = false;
165
                        if (attr != null)
166
                        {
167
                            if (attr.isLoadedInMemory())
168
                            {
169
                                bMustClone = attr.isLoadedInMemory();                            
170
                            }
171
                        }
172
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
173
                        for (int i = 0; i < sc; i++) {
174
                                if (cancel.isCanceled()) {
175
                                        break;
176
                                }
177

    
178
                                
179
                                IGeometry geom = adapter.getShape(i);
180

    
181
                                if (geom == null) {
182
                                        continue;
183
                                }
184

    
185
                                if (ct != null) {
186
                                    if (bMustClone)
187
                                        geom = geom.cloneGeometry();                                    
188
                                        geom.reProject(ct);
189
                                }
190
                                
191
                                // if (geom.intersects(extent)) {
192
                                if (geom.fastIntersects(extent.getMinX(), extent.getMinY(), 
193
                                         extent.getWidth(), extent.getHeight())) {
194
                                        FSymbol symbol = l.getSymbol(i);
195
                                        
196
                                        geom.draw(g, viewPort, symbol);
197
                                }
198
                                /* else
199
                                {
200
                                    System.out.println("no pinto id=" + i);
201
                                } */
202
                        }
203

    
204
                        logger.debug("getCapa().getRecordset().stop()");
205
                        if (rsSel != null)
206
                            rsSel.stop();
207

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

    
224
                        System.out.println(t2 - t1);
225
                } catch (DriverIOException e) {
226
                        throw new DriverException(e);
227
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
228
                    throw new DriverException(e);
229
                } catch (DriverException e) {
230
                    throw new DriverException(e);
231
        }
232
        }
233

    
234
        /**
235
         * Devuelve la capa.
236
         *
237
         * @return Returns the capa.
238
         */
239
        public FLayer getCapa() {
240
                return capa;
241
        }
242

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

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

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

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

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

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

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

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

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

    
325
                return visitor.getBitSet();
326
        }
327

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