Statistics
| Revision:

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

History | View | Annotate | Download (11.2 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.ISymbol;
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
                ISymbol symbol;
148
                SpatialCache cache = lyr.getSpatialCache();
149
                cache.clearAll();
150
                while (geomIt.hasNext())
151
                {
152
                        if (cancel.isCanceled()) {
153
                            geomIt.closeIterator();
154
                                break;
155
                        }
156
                        IFeature feat = geomIt.next();
157
                        if (feat == null)
158
                        {
159
                                continue;
160
                        }
161
                    IGeometry geom = feat.getGeometry();
162
                    
163
                    // System.out.println("PINTANDO FEATURE " + feat.getID());
164
                    
165
                    if (ct != null) {
166
                        if (bMustClone)
167
                            geom = geom.cloneGeometry();
168
                        geom.reProject(ct);
169
                    }
170
        
171
                    i = dbAdapter.getRowIndexByFID(feat);
172
                    // System.out.println("Antes de pintar " + i);
173
                    // Value val = feat.getAttribute(0);                
174
                    symbol = l.getSymbolByFeature(feat);
175
                    // symbol = l.getSymbol(i);
176
                    if (symbol == null) continue;
177
                    if (bitSet.get(i)) {
178
                        symbol = symbol.getSymbolForSelection();
179
                    } 
180
        
181
                                if (lyr.isSpatialCacheEnabled()) {
182
                                        if (cache.getMaxFeatures() >= cache.size()) {
183
                                                // Ya reproyectado todo
184
                                                cache.insert(geom.getBounds2D(), geom);
185
                                        }
186
                                }
187

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

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

    
337
        }
338
    
339

    
340
}