Statistics
| Revision:

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

History | View | Annotate | Download (11.3 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
                                                        //         Ya reproyectado todo                                                        
188
                                                        cache.insert(geom.getBounds2D(), geom);
189
                                                }
190
                                        }
191
                                }  
192

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

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

    
342
        }
343
    
344

    
345
}