Statistics
| Revision:

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

History | View | Annotate | Download (9.9 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.FBitSet;
60
import com.iver.cit.gvsig.fmap.layers.FLayer;
61
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
62
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
63
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
64
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
66
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
67
import com.iver.cit.gvsig.fmap.operations.Cancellable;
68
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
69
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
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
    public static final int EQUALS = 0;
78
    public static final int DISJOINT = 1;
79
    public static final int INTERSECTS = 2;
80
    public static final int TOUCHES = 3;
81
    public static final int CROSSES = 4;
82
    public static final int WITHIN = 5;
83
    public static final int CONTAINS = 6;
84
    public static final int OVERLAPS = 7;
85
    
86
        private static Logger logger = Logger.getLogger(DefaultStrategy.class.getName());
87
        FLayer capa = null;
88

    
89
        /**
90
         * Crea un nuevo DefaultStrategy.
91
         *
92
         * @param capa DOCUMENT ME!
93
         */
94
        public DefaultStrategy(FLayer capa) {
95
                this.capa = capa;
96

    
97
                SingleLayer foo = (SingleLayer) capa;
98
                ClassifiableVectorial vectorial = (ClassifiableVectorial) capa;
99
        }
100

    
101
        /**
102
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
103
         */
104
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
105
                QueryByRectVisitor visitor = new QueryByRectVisitor();
106

    
107
                visitor.setRect(rect);
108

    
109
                try {
110
                        process(visitor);
111
                } catch (VisitException e) {
112
                        throw new RuntimeException(
113
                                "QueryByRectVisitor lanza una VisitException?");
114
                }
115

    
116
                return visitor.getBitSet();
117
        }
118

    
119
        /**
120
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
121
         *                 int)
122
         */
123
        public FBitSet queryByShape(IGeometry g, int relationship)
124
                throws DriverException, VisitException {
125
                QueryByShapeVisitor visitor = new QueryByShapeVisitor();
126
                visitor.setRelationShip(relationship);
127
                visitor.setShape(g);
128
                process(visitor);
129

    
130
                return visitor.getBitSet();
131
        }
132

    
133
        /**
134
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
135
         */
136
        public Rectangle2D getSelectionBounds() {
137
                return null;
138
        }
139

    
140
        /**
141
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#createIndex()
142
         */
143
        public void createIndex() {
144
        }
145

    
146
        /**
147
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
148
         *                 java.awt.Graphics2D, FStyle2D)
149
         */
150
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
151
                Cancellable cancel) throws DriverException {
152
                try {
153
                        VectorialAdapter adapter = ((SingleLayer) capa).getSource();
154
                        ICoordTrans ct = getCapa().getCoordTrans();
155
                        logger.debug("adapter.start()");
156
                        adapter.start();
157

    
158
                        logger.debug("getCapa().getRecordset().start()");
159
                        SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset(); 
160
                        if (rsSel != null)
161
                            rsSel.start();
162

    
163
                        int sc;
164
                        Rectangle2D extent = viewPort.getAdjustedExtent();
165
                        AffineTransform at = viewPort.getAffineTransform();
166

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

    
185
                                
186
                                IGeometry geom = adapter.getShape(i);
187

    
188
                                if (geom == null) {
189
                                        continue;
190
                                }
191

    
192
                                if (ct != null) {
193
                                    if (bMustClone)
194
                                        geom = geom.cloneGeometry();                                    
195
                                        geom.reProject(ct);
196
                                }
197
                                
198
                                // if (geom.intersects(extent)) {
199
                                if (geom.fastIntersects(extent.getMinX(), extent.getMinY(), 
200
                                         extent.getWidth(), extent.getHeight())) {
201
                                        FSymbol symbol = l.getSymbol(i);
202
                                        
203
                    if (symbol ==null)
204
                        continue;
205
                    if (getCapa() instanceof Selectable){
206
                    Selectable selection = (Selectable) getCapa();
207
                                BitSet bitSet = selection.getSelection();
208
                    if (bitSet.get(i)) {
209
                                                symbol = FSymbol.getSymbolForSelection(symbol);
210
                                        }
211
                    }
212
                                        geom.draw(g, viewPort, symbol);
213
                                }
214
                                /* else
215
                                {
216
                                    System.out.println("no pinto id=" + i);
217
                                } */
218
                        }
219

    
220
                        logger.debug("getCapa().getRecordset().stop()");
221
                        if (rsSel != null)
222
                            rsSel.stop();
223

    
224

    
225
                        logger.debug("adapter.stop()");
226
                        adapter.stop();
227
                        // TODO: A revisar si es o no conveniente este sistema
228
                        // de comunicaci?n con los drivers.
229
                        // DriverAttributes attr = adapter.getDriverAttributes();
230
                        /* if (attr != null)
231
                        {
232
                            if (attr.isLoadedInMemory())
233
                            {
234
                                // Quitamos lo de la reproyecci?n al vuelo para que 
235
                                // solo se haga una vez.
236
                                getCapa().setCoordTrans(null);
237
                            }
238
                        } */
239

    
240
                } catch (DriverIOException e) {
241
                        throw new DriverException(e);
242
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
243
                    throw new DriverException(e);
244
                } catch (DriverException e) {
245
                    throw new DriverException(e);
246
        }
247
        }
248

    
249
        /**
250
         * Devuelve la capa.
251
         *
252
         * @return Returns the capa.
253
         */
254
        public FLayer getCapa() {
255
                return capa;
256
        }
257

    
258
        /**
259
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
260
         *                 java.util.BitSet)
261
         */
262
        public void process(FeatureVisitor visitor, BitSet subset)
263
                throws DriverException, VisitException {
264
                try {
265
                        logger.debug("visitor.start()");
266

    
267
                        if (visitor.start(capa)) {
268
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
269

    
270
                                va.start();
271
                                for (int i = 0; i < va.getShapeCount(); i++) {
272
                                        if (subset.get(i)) {
273
                                                visitor.visit(va.getShape(i), i);
274
                                        }
275
                                }
276
                                va.stop();
277

    
278
                                logger.debug("visitor.stop()");
279
                                visitor.stop(capa);
280
                        }
281
                } catch (DriverIOException e) {
282
                        throw new DriverException(e);
283
                }
284
        }
285

    
286
        /**
287
         * DOCUMENT ME!
288
         *
289
         * @param visitor DOCUMENT ME!
290
         *
291
         * @throws DriverException DOCUMENT ME!
292
         * @throws VisitException
293
         *
294
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
295
         */
296
        public void process(FeatureVisitor visitor)
297
                throws DriverException, VisitException {
298
                try {
299
                        logger.debug("visitor.start()");
300

    
301
                        if (visitor.start(capa)) {
302
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
303
                                ICoordTrans ct = getCapa().getCoordTrans();
304
                                va.start();
305
                                for (int i = 0; i < va.getShapeCount(); i++) {
306
                                    IGeometry geom = va.getShape(i);
307
                                    if (ct != null) {
308
                                                geom.reProject(ct);
309
                                        }
310

    
311
                                        visitor.visit(geom, i);
312
                                }
313
                                va.stop();
314
                                logger.debug("visitor.stop()");
315
                                visitor.stop(capa);
316
                        }
317
                } catch (DriverIOException e) {
318
                        throw new DriverException(e);
319
                }
320
        }
321

    
322
        /**
323
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
324
         *                 double)
325
         */
326
        public FBitSet queryByPoint(Point2D p, double tolerance)
327
                throws DriverException {
328
        // TODO: OJO!!!!. Est? implementado como un rectangulo.
329
        // Lo correcto deber?a ser calculando las distancias reales
330
        // es decir, con un c?rculo.
331
        Rectangle2D recPoint = new Rectangle2D.Double(p.getX() - (tolerance / 2),
332
                p.getY() - (tolerance / 2), tolerance, tolerance);
333
                return queryByRect(recPoint);
334
        }
335

    
336
        /* (non-Javadoc)
337
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
338
         */
339
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
340
                throws DriverException {
341
                draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
342
        }
343
}