Statistics
| Revision:

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

History | View | Annotate | Download (8.75 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.Handler;
55
import com.iver.cit.gvsig.fmap.core.IFeature;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
58
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
59
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
60
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
61
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
62
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
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.layerOperations.ClassifiableVectorial;
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.operations.Cancellable;
71
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
72

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

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

    
85

    
86
    /* (non-Javadoc)
87
     * @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)
88
     */
89
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel) throws DriverException {
90
        // Nos aprovechamos del SQL para lanzar la consulta
91
        // teniendo en cuenta el boundingbox que toca.
92
        FLyrVect lyr = (FLyrVect) getCapa();
93
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
94
        VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) dbAdapter.getDriver();
95
        try {
96
                        dbAdapter.start();
97
                Selectable selectable=lyr.getRecordset();
98
                        ICoordTrans ct = lyr.getCoordTrans();
99
                        FBitSet bitSet = selectable.getSelection();
100
                
101
                String strEPSG = viewPort.getProjection().getAbrev().substring(5);
102
                // TODO: EXPLORAR LAS LEYENDAS PARA SABER LOS CAMPOS QUE VAMOS
103
                // A NECESITAR RECUPERAR, PARA INCLUIR EN LA CONSULTA SOLO
104
                // AQUELLOS QUE VAMOS A NECESITAR. CON ESO GANAREMOS VELOCIDAD.
105
                // Ejemplo:
106
                // En ArcSDE:
107
                // con Vias cogiendo un campo solo: 5 segundos
108
                // con todos los campos de Vias: 11 segundos.
109
                // => MODIFICAR EL getFeatureIterator para que admita los nombres
110
                // de los campos adem?s del rect?ngulo que pides.
111
                String[] usedFields = null;
112
                VectorialLegend l = null;
113
                if (lyr.getLegend() instanceof VectorialLegend)
114
                {
115
                    l = (VectorialLegend) lyr.getLegend();
116
                }
117
                
118
                Rectangle2D rectAux = viewPort.getAdjustedExtent();
119
                if (ct != null) {
120
                    ICoordTrans invertedCT = ct.getInverted();
121
                    rectAux = invertedCT.convert(rectAux);            
122
                }
123
        
124
                
125
                usedFields = l.getUsedFields();
126
        
127
                
128
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, usedFields);
129
                if (geomIt == null)
130
                {
131
                    // dbAdapter.stop();
132
                    return;
133
                }
134
                
135
                
136
                DriverAttributes attr = dbAdapter.getDriverAttributes();
137
                boolean bMustClone = false;
138
                if (attr != null)
139
                {
140
                    if (attr.isLoadedInMemory())
141
                    {
142
                        bMustClone = attr.isLoadedInMemory();               
143
                    }
144
                }
145
        
146
                
147
                int i;
148
                FSymbol symbol;
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
                    // symbol = l.getDefaultSymbol();
181
                    geom.draw(g,viewPort, symbol);
182
                    
183
                    if (lyr.isEditing())
184
                    {
185
                            if (bitSet.get(i))
186
                            {
187
                                    Handler[] handlers = geom.getHandlers(IGeometry.SELECTHANDLER);
188
                                    FGraphicUtilities.DrawHandlers(g, viewPort.getAffineTransform(), handlers);
189
                            }                    
190
                    }
191
                }
192
                dbAdapter.stop();
193
                } catch (DriverIOException e) {
194
                        throw new DriverException(e);
195
                }
196
        
197
    }
198

    
199

    
200
        /* (non-Javadoc)
201
         * @see com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy#queryByRect(java.awt.geom.Rectangle2D)
202
         */
203
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
204
        // Nos aprovechamos del SQL para lanzar la consulta
205
        // teniendo en cuenta el boundingbox que toca.
206
        FLyrVect lyr = (FLyrVect) getCapa();
207
        ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource();
208
        VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) dbAdapter.getDriver();
209
        try {
210
                        dbAdapter.start();
211
                        ICoordTrans ct = lyr.getCoordTrans();
212
                        String strEPSG = lyr.getProjection().getAbrev().substring(5);                
213
                Rectangle2D rectAux = rect;
214
                if (ct != null) {
215
                    ICoordTrans invertedCT = ct.getInverted();
216
                    rectAux = invertedCT.convert(rectAux);            
217
                }
218
        
219
                FBitSet selection = new FBitSet();
220
                
221
                IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
222
                if (geomIt == null)
223
                {
224
                    return selection;
225
                }
226
                
227
                
228
                DriverAttributes attr = dbAdapter.getDriverAttributes();
229
                boolean bMustClone = false;
230
                if (attr != null)
231
                {
232
                    if (attr.isLoadedInMemory())
233
                    {
234
                        bMustClone = attr.isLoadedInMemory();               
235
                    }
236
                }
237
        
238
                
239
                int i;
240
                
241
                while (geomIt.hasNext())
242
                {
243
                        IFeature feat = geomIt.next();
244
                    IGeometry geom = feat.getGeometry();
245
                    
246
                    if (ct != null) {
247
                        if (bMustClone)
248
                            geom = geom.cloneGeometry();
249
                        geom.reProject(ct);
250
                    }
251
        
252
                    i = dbDriver.getRowIndexByFID(feat);
253
                    if (geom.intersects(rect))
254
                            selection.set(i, true);
255
                }
256
                dbAdapter.stop();
257
                
258
                return selection;
259
                } catch (DriverIOException e) {
260
                        throw new DriverException(e);
261
                }
262

    
263
        }
264
    
265

    
266
}