Statistics
| Revision:

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

History | View | Annotate | Download (11.6 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.hardcode.gdbms.driver.exceptions.InitializeDriverException;
53
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
54
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
55
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
56
import com.iver.cit.gvsig.fmap.ViewPort;
57
import com.iver.cit.gvsig.fmap.core.IFeature;
58
import com.iver.cit.gvsig.fmap.core.IGeometry;
59
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
60
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
61
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
62
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
63
import com.iver.cit.gvsig.fmap.layers.FBitSet;
64
import com.iver.cit.gvsig.fmap.layers.FLayer;
65
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
66
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
67
import com.iver.cit.gvsig.fmap.layers.SpatialCache;
68
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
69
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
70
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend;
71
import com.iver.utiles.swing.threads.Cancellable;
72
import com.iver.utiles.swing.threads.CancellableMonitorable;
73

    
74
/**
75
 * @author FJP
76
 *
77
 * TODO To change the template for this generated type comment go to
78
 * Window - Preferences - Java - Code Generation - Code and Comments
79
 */
80
public class DBStrategy extends DefaultStrategy {
81

    
82
        public DBStrategy(FLayer capa) {
83
                super(capa);
84
        }
85

    
86

    
87
    /* (non-Javadoc)
88
     * @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)
89
     */
90
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel) throws ReadDriverException {
91
        // Nos aprovechamos del SQL para lanzar la consulta
92
        // teniendo en cuenta el boundingbox que toca.
93
        FLyrVect lyr = (FLyrVect) getCapa();
94
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
95
        //IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver();
96
        try {
97
                        dbAdapter.start();
98
                Selectable selectable=lyr.getRecordset();
99
                        ICoordTrans ct = lyr.getCoordTrans();
100
                        FBitSet bitSet = selectable.getSelection();
101

    
102
                String strEPSG = viewPort.getProjection().getAbrev();
103
                // TODO: EXPLORAR LAS LEYENDAS PARA SABER LOS CAMPOS QUE VAMOS
104
                // A NECESITAR RECUPERAR, PARA INCLUIR EN LA CONSULTA SOLO
105
                // AQUELLOS QUE VAMOS A NECESITAR. CON ESO GANAREMOS VELOCIDAD.
106
                // Ejemplo:
107
                // En ArcSDE:
108
                // con Vias cogiendo un campo solo: 5 segundos
109
                // con todos los campos de Vias: 11 segundos.
110
                // => MODIFICAR EL getFeatureIterator para que admita los nombres
111
                // de los campos adem?s del rect?ngulo que pides.
112
                String[] usedFields = null;
113
                IClassifiedVectorLegend l = null;
114
                if (lyr.getLegend() instanceof IClassifiedVectorLegend)
115
                {
116
                    l = (IClassifiedVectorLegend) lyr.getLegend();
117

    
118
                        usedFields = l.getClassifyingFieldNames();
119

    
120
                }
121

    
122
                Rectangle2D rectAux = viewPort.getAdjustedExtent();
123
                if (ct != null) {
124
                    ICoordTrans invertedCT = ct.getInverted();
125
                    rectAux = invertedCT.convert(rectAux);
126
                }
127

    
128

    
129

    
130
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, usedFields);
131
                if (geomIt == null)
132
                {
133
                    // dbAdapter.stop();
134
                    return;
135
                }
136

    
137

    
138
                DriverAttributes attr = dbAdapter.getDriverAttributes();
139
                boolean bMustClone = false;
140
                if (attr != null)
141
                {
142
                    if (attr.isLoadedInMemory())
143
                    {
144
                        bMustClone = attr.isLoadedInMemory();
145
                    }
146
                }
147

    
148

    
149
                int i;
150
                ISymbol symbol;
151
                SpatialCache cache = lyr.getSpatialCache();
152
                cache.clearAll();
153
                while (geomIt.hasNext())
154
                {
155
                        if (cancel.isCanceled()) {
156
                            geomIt.closeIterator();
157
                                break;
158
                        }
159
                        IFeature feat = geomIt.next();
160
                        if (feat == null)
161
                        {
162
                                continue;
163
                        }
164
                    IGeometry geom = feat.getGeometry();
165

    
166
                    // System.out.println("PINTANDO FEATURE " + feat.getID());
167

    
168
                    if (ct != null) {
169
                        if (bMustClone)
170
                            geom = geom.cloneGeometry();
171
                        geom.reProject(ct);
172
                    }
173

    
174
                    i = dbAdapter.getRowIndexByFID(feat);
175
                    // System.out.println("Antes de pintar " + i);
176
                    // Value val = feat.getAttribute(0);
177
                    symbol = l.getSymbolByFeature(feat);
178
                    // symbol = l.getSymbol(i);
179
                    if (symbol == null) continue;
180
                    if (bitSet.get(i)) {
181
                        symbol = symbol.getSymbolForSelection();
182
                    }
183

    
184
                                if (lyr.isSpatialCacheEnabled()) {
185
                                        if (cache.getMaxFeatures() >= cache.size()) {
186
                                                // Ya reproyectado todo
187
                                                cache.insert(geom.getBounds2D(), geom);
188
                                        }
189
                                }
190

    
191
                    // symbol = l.getDefaultSymbol();
192
                    geom.drawInts(g,viewPort, symbol, null);
193

    
194
                }
195
                dbAdapter.stop();
196
                } catch (InitializeDriverException e) {
197
                        throw new ReadDriverException(getCapa().getName(),e);
198
                }
199

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

    
269
        /* (non-Javadoc)
270
         * @see com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy#queryByRect(java.awt.geom.Rectangle2D)
271
         */
272
        public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) throws ReadDriverException, VisitorException {
273
        // Nos aprovechamos del SQL para lanzar la consulta
274
        // teniendo en cuenta el boundingbox que toca.
275
        FLyrVect lyr = (FLyrVect) getCapa();
276
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
277
//        IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver();
278
        try {
279
                        dbAdapter.start();
280
                        ICoordTrans ct = lyr.getCoordTrans();
281
                        String strEPSG = lyr.getProjection().getAbrev();
282
                Rectangle2D rectAux = rect;
283
                if (ct != null) {
284
                    ICoordTrans invertedCT = ct.getInverted();
285
                    rectAux = invertedCT.convert(rectAux);
286
                }
287

    
288
                FBitSet selection = new FBitSet();
289

    
290
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
291
                if (geomIt == null)
292
                {
293
                    return selection;
294
                }
295

    
296

    
297
                DriverAttributes attr = dbAdapter.getDriverAttributes();
298
                boolean bMustClone = false;
299
                if (attr != null)
300
                {
301
                    if (attr.isLoadedInMemory())
302
                    {
303
                        bMustClone = attr.isLoadedInMemory();
304
                    }
305
                }
306

    
307

    
308
                int i;
309

    
310
                while (geomIt.hasNext())
311
                {
312
                        if(cancel != null)
313
                        {
314
                                cancel.reportStep();
315
                                if(cancel.isCanceled())
316
                                {
317
                                        dbAdapter.stop();
318
                                        return selection;
319
                                }
320
                        }
321
                        IFeature feat = geomIt.next();
322
                    IGeometry geom = feat.getGeometry();
323

    
324
                    if (ct != null) {
325
                        if (bMustClone)
326
                            geom = geom.cloneGeometry();
327
                        geom.reProject(ct);
328
                    }
329

    
330
                    i = dbAdapter.getRowIndexByFID(feat);
331
                    if (geom.intersects(rect))
332
                            selection.set(i, true);
333
                }
334
                dbAdapter.stop();
335

    
336
                return selection;
337
                } catch (InitializeDriverException e) {
338
                        throw new ReadDriverException(getCapa().getName(),e);
339
                }
340

    
341
        }
342

    
343

    
344
}