Statistics
| Revision:

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

History | View | Annotate | Download (9.18 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 org.apache.log4j.Logger;
51
import org.cresques.cts.ICoordTrans;
52

    
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.ViewPort;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
57
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
58
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
61
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
62
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
63
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
64
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
65
import com.iver.cit.gvsig.fmap.operations.Cancellable;
66
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
67
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
68

    
69

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

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

    
86
                SingleLayer foo = (SingleLayer) capa;
87
                ClassifiableVectorial vectorial = (ClassifiableVectorial) capa;
88
        }
89

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

    
96
                visitor.setRect(rect);
97

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

    
105
                return visitor.getBitSet();
106
        }
107

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

    
119
                return visitor.getBitSet();
120
        }
121

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

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

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

    
147
                        logger.debug("getCapa().getRecordset().start()");
148
                        SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset(); 
149
                        if (rsSel != null)
150
                            rsSel.start();
151

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

    
157
                        sc = adapter.getShapeCount();
158
                        // TODO: A revisar si es o no conveniente este sistema
159
                        // de comunicaci?n con los drivers.
160
                        DriverAttributes attr = adapter.getDriverAttributes();
161
                        boolean bMustClone = false;
162
                        if (attr != null)
163
                        {
164
                            if (attr.isLoadedInMemory())
165
                            {
166
                                bMustClone = attr.isLoadedInMemory();                            
167
                            }
168
                        }
169
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
170
                        for (int i = 0; i < sc; i++) {
171
                                if (cancel.isCanceled()) {
172
                                        break;
173
                                }
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
                                // if (geom.intersects(extent)) {
189
                                if (geom.fastIntersects(extent.getMinX(), extent.getMinY(), 
190
                                         extent.getWidth(), extent.getHeight())) {
191
                                        FSymbol symbol = l.getSymbol(i);
192
                                        
193
                                        geom.draw(g, viewPort, symbol);
194
                                }
195
                                /* else
196
                                {
197
                                    System.out.println("no pinto id=" + i);
198
                                } */
199
                        }
200

    
201
                        logger.debug("getCapa().getRecordset().stop()");
202
                        if (rsSel != null)
203
                            rsSel.stop();
204

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

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

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

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

    
249
                        if (visitor.start(capa)) {
250
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
251

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

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

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

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

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

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

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

    
322
                return visitor.getBitSet();
323
        }
324

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