Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1011 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DBStrategy.java @ 12904

History | View | Annotate | Download (10.9 KB)

1 1691 fjp
/*
2
 * Created on 08-mar-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5 12799 jmvivo
 *
6 1691 fjp
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7 12799 jmvivo
 *
8 1691 fjp
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12 12799 jmvivo
 *
13 1691 fjp
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17 12799 jmvivo
 *
18 1691 fjp
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 12799 jmvivo
 *
22 1691 fjp
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33 12799 jmvivo
 *
34 1691 fjp
 *    or
35 12799 jmvivo
 *
36 1691 fjp
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40 12799 jmvivo
 *
41 1691 fjp
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.operations.strategies;
45
46
import java.awt.Graphics2D;
47 3095 fjp
import java.awt.geom.Rectangle2D;
48 1691 fjp
import java.awt.image.BufferedImage;
49
50
import org.cresques.cts.ICoordTrans;
51
52
import com.iver.cit.gvsig.fmap.DriverException;
53
import com.iver.cit.gvsig.fmap.ViewPort;
54 2183 fernando
import com.iver.cit.gvsig.fmap.core.IFeature;
55 1691 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
56 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
57 3095 fjp
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
58 4171 fjp
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
59 2183 fernando
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
60 11928 caballero
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
61 2183 fernando
import com.iver.cit.gvsig.fmap.layers.FBitSet;
62 1691 fjp
import com.iver.cit.gvsig.fmap.layers.FLayer;
63 3236 fjp
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64 4171 fjp
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
65 5236 fjp
import com.iver.cit.gvsig.fmap.layers.SpatialCache;
66 2183 fernando
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
67 1691 fjp
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
68 1704 fjp
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
69 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
70
import com.iver.utiles.swing.threads.CancellableMonitorable;
71 1691 fjp
72
/**
73
 * @author FJP
74
 *
75
 * TODO To change the template for this generated type comment go to
76
 * Window - Preferences - Java - Code Generation - Code and Comments
77
 */
78
public class DBStrategy extends DefaultStrategy {
79
80
        public DBStrategy(FLayer capa) {
81
                super(capa);
82
        }
83
84
85
    /* (non-Javadoc)
86
     * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable)
87
     */
88
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel) throws DriverException {
89 1704 fjp
        // Nos aprovechamos del SQL para lanzar la consulta
90
        // teniendo en cuenta el boundingbox que toca.
91 3236 fjp
        FLyrVect lyr = (FLyrVect) getCapa();
92 4171 fjp
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
93 12799 jmvivo
94
        boolean isJoined = lyr.isJoined();
95
96 11928 caballero
        IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver();
97 4171 fjp
        try {
98
                        dbAdapter.start();
99
                Selectable selectable=lyr.getRecordset();
100
                        ICoordTrans ct = lyr.getCoordTrans();
101
                        FBitSet bitSet = selectable.getSelection();
102 12799 jmvivo
103 4171 fjp
                String strEPSG = viewPort.getProjection().getAbrev().substring(5);
104
                // TODO: EXPLORAR LAS LEYENDAS PARA SABER LOS CAMPOS QUE VAMOS
105
                // A NECESITAR RECUPERAR, PARA INCLUIR EN LA CONSULTA SOLO
106
                // AQUELLOS QUE VAMOS A NECESITAR. CON ESO GANAREMOS VELOCIDAD.
107
                // Ejemplo:
108
                // En ArcSDE:
109
                // con Vias cogiendo un campo solo: 5 segundos
110
                // con todos los campos de Vias: 11 segundos.
111
                // => MODIFICAR EL getFeatureIterator para que admita los nombres
112
                // de los campos adem?s del rect?ngulo que pides.
113
                String[] usedFields = null;
114 4191 fjp
                VectorialLegend l = null;
115 4171 fjp
                if (lyr.getLegend() instanceof VectorialLegend)
116
                {
117 4191 fjp
                    l = (VectorialLegend) lyr.getLegend();
118 4171 fjp
                }
119 12799 jmvivo
120 4171 fjp
                Rectangle2D rectAux = viewPort.getAdjustedExtent();
121
                if (ct != null) {
122
                    ICoordTrans invertedCT = ct.getInverted();
123 12799 jmvivo
                    rectAux = invertedCT.convert(rectAux);
124 4171 fjp
                }
125 12799 jmvivo
126
127
                if (!isJoined){
128
                        usedFields = l.getUsedFields();
129
                }
130
131
132 4171 fjp
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, usedFields);
133
                if (geomIt == null)
134
                {
135
                    // dbAdapter.stop();
136
                    return;
137
                }
138 12799 jmvivo
139
140 4171 fjp
                DriverAttributes attr = dbAdapter.getDriverAttributes();
141
                boolean bMustClone = false;
142
                if (attr != null)
143
                {
144
                    if (attr.isLoadedInMemory())
145
                    {
146 12799 jmvivo
                        bMustClone = attr.isLoadedInMemory();
147 4171 fjp
                    }
148
                }
149 12799 jmvivo
150
151 4171 fjp
                int i;
152 8765 jjdelcerro
                ISymbol symbol;
153 5813 fjp
                SpatialCache cache = lyr.getSpatialCache();
154
                cache.clearAll();
155 4171 fjp
                while (geomIt.hasNext())
156
                {
157
                        if (cancel.isCanceled()) {
158
                            geomIt.closeIterator();
159
                                break;
160
                        }
161
                        IFeature feat = geomIt.next();
162 4191 fjp
                        if (feat == null)
163
                        {
164
                                continue;
165
                        }
166 4171 fjp
                    IGeometry geom = feat.getGeometry();
167 12799 jmvivo
168 5008 fjp
                    // System.out.println("PINTANDO FEATURE " + feat.getID());
169 12799 jmvivo
170 4171 fjp
                    if (ct != null) {
171
                        if (bMustClone)
172
                            geom = geom.cloneGeometry();
173
                        geom.reProject(ct);
174
                    }
175 12799 jmvivo
176 4181 fjp
                    i = dbAdapter.getRowIndexByFID(feat);
177 12799 jmvivo
178
                    if (isJoined){
179
                            symbol = l.getSymbol(i);
180
                    } else {
181
                            symbol = l.getSymbolByFeature(feat);
182
                    }
183
184 4171 fjp
                    if (symbol == null) continue;
185
                    if (bitSet.get(i)) {
186 8765 jjdelcerro
                        symbol = symbol.getSymbolForSelection();
187 12799 jmvivo
                    }
188
189 5813 fjp
                                if (lyr.isSpatialCacheEnabled()) {
190
                                        if (cache.getMaxFeatures() >= cache.size()) {
191
                                                // Ya reproyectado todo
192
                                                cache.insert(geom.getBounds2D(), geom);
193 5236 fjp
                                        }
194 5813 fjp
                                }
195 5236 fjp
196 4171 fjp
                    // symbol = l.getDefaultSymbol();
197 5236 fjp
                    geom.drawInts(g,viewPort, symbol);
198 12799 jmvivo
199
                }
200 4171 fjp
                dbAdapter.stop();
201
                } catch (DriverIOException e) {
202
                        throw new DriverException(e);
203
                }
204 12799 jmvivo
205 1691 fjp
    }
206 4200 azabala
    /**
207
     * Processes features of layer whose geometries would
208
     * intersect rectangle passed as param by calling visitor's
209
     * visit method.
210 12799 jmvivo
     *
211 4200 azabala
     * FIXME FeatureVisitor is designed to work with memory and
212 12799 jmvivo
     * file drivers (visit(IGeometry, index), and to work with
213 4200 azabala
     * geometries (thats the reason for not to create Values, instead
214
     * of pass an index)
215 12799 jmvivo
     *
216 4200 azabala
     * But DBDrivers recovers IGeometry and Values in dbAdapter.getFeatureIterator
217
     * method. We must add a visit(Feature) method to FeatureVisitor.
218 12799 jmvivo
     *
219
     *
220 4200 azabala
     */
221 12799 jmvivo
    public void process(FeatureVisitor visitor, Rectangle2D rect, CancellableMonitorable cancel)
222 4200 azabala
                                            throws DriverException, VisitException{
223
             FLyrVect lyr = (FLyrVect) getCapa();
224
         ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
225 11928 caballero
         IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver();
226 4200 azabala
         try {
227
                         dbAdapter.start();
228
                         ICoordTrans ct = lyr.getCoordTrans();
229 12799 jmvivo
                         String strEPSG = lyr.getProjection().getAbrev().substring(5);
230 4200 azabala
                 Rectangle2D rectAux = rect;
231
                 if (ct != null) {
232
                     ICoordTrans invertedCT = ct.getInverted();
233 12799 jmvivo
                     rectAux = invertedCT.convert(rectAux);
234 4200 azabala
                 }
235
                 IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
236
                 if (geomIt == null){
237
                     return;
238
                 }
239
                 DriverAttributes attr = dbAdapter.getDriverAttributes();
240
                 boolean bMustClone = false;
241
                 if (attr != null)
242
                 {
243
                     if (attr.isLoadedInMemory())
244
                     {
245 12799 jmvivo
                         bMustClone = attr.isLoadedInMemory();
246 4200 azabala
                     }//if
247
                 }//if
248
                 int i;
249
                 while (geomIt.hasNext())
250
                 {
251 4419 azabala
                         if(cancel != null){
252
                                        cancel.reportStep();
253
                                }
254 4200 azabala
                         IFeature feat = geomIt.next();
255
                     IGeometry geom = feat.getGeometry();
256
                     if (ct != null) {
257
                         if (bMustClone){
258
                             geom = geom.cloneGeometry();
259
                         }//if
260
                         geom.reProject(ct);
261
                     }//if
262
                     i = dbDriver.getRowIndexByFID(feat);
263
                     if (geom.intersects(rect)){
264
                             visitor.visit(geom, i);
265
                     }
266
                 }
267
                 dbAdapter.stop();
268
                 } catch (DriverIOException e) {
269
                         throw new DriverException(e);
270
                 }
271
    }
272 4115 fjp
273
        /* (non-Javadoc)
274
         * @see com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy#queryByRect(java.awt.geom.Rectangle2D)
275
         */
276 4347 azabala
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) throws DriverException {
277 4115 fjp
        // Nos aprovechamos del SQL para lanzar la consulta
278
        // teniendo en cuenta el boundingbox que toca.
279
        FLyrVect lyr = (FLyrVect) getCapa();
280 4171 fjp
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
281 11928 caballero
        IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver();
282 4171 fjp
        try {
283
                        dbAdapter.start();
284
                        ICoordTrans ct = lyr.getCoordTrans();
285 12799 jmvivo
                        String strEPSG = lyr.getProjection().getAbrev().substring(5);
286 4171 fjp
                Rectangle2D rectAux = rect;
287
                if (ct != null) {
288
                    ICoordTrans invertedCT = ct.getInverted();
289 12799 jmvivo
                    rectAux = invertedCT.convert(rectAux);
290 4171 fjp
                }
291 12799 jmvivo
292 4171 fjp
                FBitSet selection = new FBitSet();
293 12799 jmvivo
294 4171 fjp
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
295
                if (geomIt == null)
296
                {
297
                    return selection;
298
                }
299 12799 jmvivo
300
301 4171 fjp
                DriverAttributes attr = dbAdapter.getDriverAttributes();
302
                boolean bMustClone = false;
303
                if (attr != null)
304
                {
305
                    if (attr.isLoadedInMemory())
306
                    {
307 12799 jmvivo
                        bMustClone = attr.isLoadedInMemory();
308 4171 fjp
                    }
309
                }
310 12799 jmvivo
311
312 4171 fjp
                int i;
313 12799 jmvivo
314 4171 fjp
                while (geomIt.hasNext())
315
                {
316 4341 fjp
                        if(cancel != null)
317 12799 jmvivo
                        {
318 4419 azabala
                                cancel.reportStep();
319 4341 fjp
                                if(cancel.isCanceled())
320
                                {
321
                                        dbAdapter.stop();
322
                                        return selection;
323 12799 jmvivo
                                }
324 4311 azabala
                        }
325 4171 fjp
                        IFeature feat = geomIt.next();
326
                    IGeometry geom = feat.getGeometry();
327 12799 jmvivo
328 4171 fjp
                    if (ct != null) {
329
                        if (bMustClone)
330
                            geom = geom.cloneGeometry();
331
                        geom.reProject(ct);
332
                    }
333 12799 jmvivo
334 4171 fjp
                    i = dbDriver.getRowIndexByFID(feat);
335
                    if (geom.intersects(rect))
336
                            selection.set(i, true);
337
                }
338
                dbAdapter.stop();
339 12799 jmvivo
340 4171 fjp
                return selection;
341
                } catch (DriverIOException e) {
342
                        throw new DriverException(e);
343
                }
344 4115 fjp
345
        }
346 1691 fjp
347 12799 jmvivo
348 1691 fjp
}