Statistics
| Revision:

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

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

    
58
import org.apache.log4j.Logger;
59

    
60
import org.cresques.cts.ICoordTrans;
61

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

    
68
import java.util.BitSet;
69

    
70

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

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

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

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

    
97
                visitor.setRect(rect);
98

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

    
106
                return visitor.getBitSet();
107
        }
108

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

    
120
                return visitor.getBitSet();
121
        }
122

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

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

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

    
148
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
149
                        int sc;
150
                        long t1 = System.currentTimeMillis();
151
                        Rectangle2D extent = viewPort.getAdjustedExtent();
152
                        AffineTransform at = viewPort.getAffineTransform();
153

    
154
                        sc = adapter.getShapeCount();
155

    
156
                        for (int i = 0; i < sc; i++) {
157
                                if (cancel.isCanceled()) {
158
                                        break;
159
                                }
160

    
161
                                IGeometry geom = adapter.getShape(i);
162

    
163
                                if (geom == null) {
164
                                        continue;
165
                                }
166

    
167
                                if (ct != null) {
168
                                        geom.reProject(ct);
169
                                }
170

    
171
                                VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
172

    
173
                                if (geom.intersects(extent)) {
174
                                        FSymbol symbol = l.getSymbol(i);
175
                                        geom.draw(g, viewPort, symbol);
176
                                }
177
                        }
178

    
179
                        long t2 = System.currentTimeMillis();
180
                        logger.debug("adapter.stop()");
181
                        adapter.stop();
182
                        // TODO: A revisar si es o no conveniente este sistema
183
                        // de comunicaci?n con los drivers.
184
                        DriverAttributes attr = adapter.getDriverAttributes();
185
                        if (attr != null)
186
                        {
187
                            if (attr.isLoadedInMemory())
188
                            {
189
                                // Quitamos lo de la reproyecci?n al vuelo para que 
190
                                // solo se haga una vez.
191
                                getCapa().setCoordTrans(null);
192
                            }
193
                        }
194

    
195
                        System.out.println(t2 - t1);
196
                } catch (DriverIOException e) {
197
                        throw new DriverException(e);
198
                }
199
        }
200

    
201
        /**
202
         * Devuelve la capa.
203
         *
204
         * @return Returns the capa.
205
         */
206
        public FLayer getCapa() {
207
                return capa;
208
        }
209

    
210
        /**
211
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#getFullExtent()
212
         */
213
        public Rectangle2D getFullExtent() throws DriverException {
214
                return null;
215
        }
216

    
217
        /**
218
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
219
         *                 java.util.BitSet)
220
         */
221
        public void process(FeatureVisitor visitor, BitSet subset)
222
                throws DriverException, VisitException {
223
                try {
224
                        logger.debug("visitor.start()");
225

    
226
                        if (visitor.start(capa)) {
227
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
228

    
229
                                va.start();
230
                                for (int i = 0; i < va.getShapeCount(); i++) {
231
                                        if (subset.get(i)) {
232
                                                visitor.visit(va.getShape(i), i);
233
                                        }
234
                                }
235
                                va.stop();
236

    
237
                                logger.debug("visitor.stop()");
238
                                visitor.stop(capa);
239
                        }
240
                } catch (DriverIOException e) {
241
                        throw new DriverException(e);
242
                }
243
        }
244

    
245
        /**
246
         * DOCUMENT ME!
247
         *
248
         * @param visitor DOCUMENT ME!
249
         *
250
         * @throws DriverException DOCUMENT ME!
251
         * @throws VisitException
252
         *
253
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
254
         */
255
        public void process(FeatureVisitor visitor)
256
                throws DriverException, VisitException {
257
                try {
258
                        logger.debug("visitor.start()");
259

    
260
                        if (visitor.start(capa)) {
261
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
262

    
263
                                va.start();
264
                                for (int i = 0; i < va.getShapeCount(); i++) {
265
                                        visitor.visit(va.getShape(i), i);
266
                                }
267
                                va.stop();
268
                                logger.debug("visitor.stop()");
269
                                visitor.stop(capa);
270
                        }
271
                } catch (DriverIOException e) {
272
                        throw new DriverException(e);
273
                }
274
        }
275

    
276
        /**
277
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
278
         *                 double)
279
         */
280
        public BitSet queryByPoint(Point2D p, double tolerance)
281
                throws DriverException {
282
                QueryByPointVisitor visitor = new QueryByPointVisitor();
283
                visitor.setLayer(capa);
284
                visitor.setTolerance(tolerance);
285
                visitor.setQueriedPoint(p);
286

    
287
                try {
288
                        process(visitor);
289
                } catch (VisitException e) {
290
                        throw new RuntimeException(
291
                                "QueryByPointVisitor lanza una VisitException?");
292
                }
293

    
294
                return visitor.getBitSet();
295
        }
296

    
297
        /* (non-Javadoc)
298
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
299
         */
300
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
301
                throws DriverException {
302
                draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
303
        }
304
}