Statistics
| Revision:

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

History | View | Annotate | Download (11.4 KB)

1
/*
2
 * Created on 08-mar-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 * 
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 * 
8
 * 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
 *  
13
 * 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
 * 
18
 * 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
 *  
22
 * 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
 * 
34
 *    or
35
 * 
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 * 
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.cit.gvsig.fmap.operations.strategies;
45

    
46
import java.awt.Graphics2D;
47
import java.awt.geom.Rectangle2D;
48
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
import com.iver.cit.gvsig.fmap.core.IFeature;
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.drivers.IFeatureIterator;
60
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
61
import com.iver.cit.gvsig.fmap.layers.FBitSet;
62
import com.iver.cit.gvsig.fmap.layers.FLayer;
63
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
65
import com.iver.cit.gvsig.fmap.layers.SpatialCache;
66
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
67
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
68
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
69
import com.iver.utiles.swing.threads.Cancellable;
70
import com.iver.utiles.swing.threads.CancellableMonitorable;
71

    
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
        // Nos aprovechamos del SQL para lanzar la consulta
90
        // teniendo en cuenta el boundingbox que toca.
91
        FLyrVect lyr = (FLyrVect) getCapa();
92
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
93
        VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) dbAdapter.getDriver();
94
        try {
95
                        dbAdapter.start();
96
                Selectable selectable=lyr.getRecordset();
97
                        ICoordTrans ct = lyr.getCoordTrans();
98
                        FBitSet bitSet = selectable.getSelection();
99
                
100
                String strEPSG = viewPort.getProjection().getAbrev().substring(5);
101
                // TODO: EXPLORAR LAS LEYENDAS PARA SABER LOS CAMPOS QUE VAMOS
102
                // A NECESITAR RECUPERAR, PARA INCLUIR EN LA CONSULTA SOLO
103
                // AQUELLOS QUE VAMOS A NECESITAR. CON ESO GANAREMOS VELOCIDAD.
104
                // Ejemplo:
105
                // En ArcSDE:
106
                // con Vias cogiendo un campo solo: 5 segundos
107
                // con todos los campos de Vias: 11 segundos.
108
                // => MODIFICAR EL getFeatureIterator para que admita los nombres
109
                // de los campos adem?s del rect?ngulo que pides.
110
                String[] usedFields = null;
111
                VectorialLegend l = null;
112
                if (lyr.getLegend() instanceof VectorialLegend)
113
                {
114
                    l = (VectorialLegend) lyr.getLegend();
115
                }
116
                
117
                Rectangle2D rectAux = viewPort.getAdjustedExtent();
118
                if (ct != null) {
119
                    ICoordTrans invertedCT = ct.getInverted();
120
                    rectAux = invertedCT.convert(rectAux);            
121
                }
122
        
123
                
124
                usedFields = l.getUsedFields();
125
        
126
                
127
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, usedFields);
128
                if (geomIt == null)
129
                {
130
                    // dbAdapter.stop();
131
                    return;
132
                }
133
                
134
                
135
                DriverAttributes attr = dbAdapter.getDriverAttributes();
136
                boolean bMustClone = false;
137
                if (attr != null)
138
                {
139
                    if (attr.isLoadedInMemory())
140
                    {
141
                        bMustClone = attr.isLoadedInMemory();               
142
                    }
143
                }
144
        
145
                
146
                int i;
147
                FSymbol symbol;
148
                SpatialCache cache = lyr.createSpatialCache();
149
                while (geomIt.hasNext())
150
                {
151
                        if (cancel.isCanceled()) {
152
                            geomIt.closeIterator();
153
                                break;
154
                        }
155
                        IFeature feat = geomIt.next();
156
                        if (feat == null)
157
                        {
158
                                continue;
159
                        }
160
                    IGeometry geom = feat.getGeometry();
161
                    
162
                    // System.out.println("PINTANDO FEATURE " + feat.getID());
163
                    
164
                    if (ct != null) {
165
                        if (bMustClone)
166
                            geom = geom.cloneGeometry();
167
                        geom.reProject(ct);
168
                    }
169
        
170
                    i = dbAdapter.getRowIndexByFID(feat);
171
                    // System.out.println("Antes de pintar " + i);
172
                    // Value val = feat.getAttribute(0);                
173
                    symbol = l.getSymbolByFeature(feat);
174
                    // symbol = l.getSymbol(i);
175
                    if (symbol == null) continue;
176
                    if (bitSet.get(i)) {
177
                        symbol = FSymbol.getSymbolForSelection(symbol);
178
                    } 
179
        
180
                                if (cache != null)
181
                                {
182
                                        // TODO: Comprobar aqu? tambi?n si esto es necesario.
183
                                        if (lyr.isSpatialCacheEnabled())
184
                                        {
185
                                                if (cache.getMaxFeatures() < cache.size())
186
                                                {
187
                                                        // lyr.setSpatialCache(null);
188
                                                        // cache = null;
189
                                                }
190
                                                else
191
                                                {
192
                                                        //         Ya reproyectado todo                                                        
193
                                                        cache.insert(geom.getBounds2D(), geom);
194
                                                }
195
                                        }
196
                                }  
197

    
198
                    // symbol = l.getDefaultSymbol();
199
                    geom.drawInts(g,viewPort, symbol);
200
                    
201
                } 
202
                dbAdapter.stop();
203
                } catch (DriverIOException e) {
204
                        throw new DriverException(e);
205
                }
206
        
207
    }
208
    /**
209
     * Processes features of layer whose geometries would
210
     * intersect rectangle passed as param by calling visitor's
211
     * visit method.
212
     * 
213
     * FIXME FeatureVisitor is designed to work with memory and
214
     * file drivers (visit(IGeometry, index), and to work with 
215
     * geometries (thats the reason for not to create Values, instead
216
     * of pass an index)
217
     * 
218
     * But DBDrivers recovers IGeometry and Values in dbAdapter.getFeatureIterator
219
     * method. We must add a visit(Feature) method to FeatureVisitor.
220
     * 
221
     * 
222
     */
223
    public void process(FeatureVisitor visitor, Rectangle2D rect, CancellableMonitorable cancel) 
224
                                            throws DriverException, VisitException{
225
             FLyrVect lyr = (FLyrVect) getCapa();
226
         ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
227
         VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) dbAdapter.getDriver();
228
         try {
229
                         dbAdapter.start();
230
                         ICoordTrans ct = lyr.getCoordTrans();
231
                         String strEPSG = lyr.getProjection().getAbrev().substring(5);                
232
                 Rectangle2D rectAux = rect;
233
                 if (ct != null) {
234
                     ICoordTrans invertedCT = ct.getInverted();
235
                     rectAux = invertedCT.convert(rectAux);            
236
                 }
237
                 IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
238
                 if (geomIt == null){
239
                     return;
240
                 }
241
                 DriverAttributes attr = dbAdapter.getDriverAttributes();
242
                 boolean bMustClone = false;
243
                 if (attr != null)
244
                 {
245
                     if (attr.isLoadedInMemory())
246
                     {
247
                         bMustClone = attr.isLoadedInMemory();               
248
                     }//if
249
                 }//if
250
                 int i;
251
                 while (geomIt.hasNext())
252
                 {
253
                         if(cancel != null){
254
                                        cancel.reportStep();
255
                                }
256
                         IFeature feat = geomIt.next();
257
                     IGeometry geom = feat.getGeometry();
258
                     if (ct != null) {
259
                         if (bMustClone){
260
                             geom = geom.cloneGeometry();
261
                         }//if
262
                         geom.reProject(ct);
263
                     }//if
264
                     i = dbDriver.getRowIndexByFID(feat);
265
                     if (geom.intersects(rect)){
266
                             visitor.visit(geom, i);
267
                     }
268
                 }
269
                 dbAdapter.stop();
270
                 } catch (DriverIOException e) {
271
                         throw new DriverException(e);
272
                 }
273
    }
274

    
275
        /* (non-Javadoc)
276
         * @see com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy#queryByRect(java.awt.geom.Rectangle2D)
277
         */
278
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) throws DriverException {
279
        // Nos aprovechamos del SQL para lanzar la consulta
280
        // teniendo en cuenta el boundingbox que toca.
281
        FLyrVect lyr = (FLyrVect) getCapa();
282
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
283
        VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) dbAdapter.getDriver();
284
        try {
285
                        dbAdapter.start();
286
                        ICoordTrans ct = lyr.getCoordTrans();
287
                        String strEPSG = lyr.getProjection().getAbrev().substring(5);                
288
                Rectangle2D rectAux = rect;
289
                if (ct != null) {
290
                    ICoordTrans invertedCT = ct.getInverted();
291
                    rectAux = invertedCT.convert(rectAux);            
292
                }
293
        
294
                FBitSet selection = new FBitSet();
295
                
296
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
297
                if (geomIt == null)
298
                {
299
                    return selection;
300
                }
301
                
302
                
303
                DriverAttributes attr = dbAdapter.getDriverAttributes();
304
                boolean bMustClone = false;
305
                if (attr != null)
306
                {
307
                    if (attr.isLoadedInMemory())
308
                    {
309
                        bMustClone = attr.isLoadedInMemory();               
310
                    }
311
                }
312
        
313
                
314
                int i;
315
                
316
                while (geomIt.hasNext())
317
                {
318
                        if(cancel != null)
319
                        {        
320
                                cancel.reportStep();
321
                                if(cancel.isCanceled())
322
                                {
323
                                        dbAdapter.stop();
324
                                        return selection;
325
                                }                                
326
                        }
327
                        IFeature feat = geomIt.next();
328
                    IGeometry geom = feat.getGeometry();
329
                    
330
                    if (ct != null) {
331
                        if (bMustClone)
332
                            geom = geom.cloneGeometry();
333
                        geom.reProject(ct);
334
                    }
335
        
336
                    i = dbDriver.getRowIndexByFID(feat);
337
                    if (geom.intersects(rect))
338
                            selection.set(i, true);
339
                }
340
                dbAdapter.stop();
341
                
342
                return selection;
343
                } catch (DriverIOException e) {
344
                        throw new DriverException(e);
345
                }
346

    
347
        }
348
    
349

    
350
}