svn-gvsig-desktop / tags / v1_0_2_Build_896 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DBStrategy.java @ 10391
History | View | Annotate | Download (11.2 KB)
1 | 1691 | fjp | /*
|
---|---|---|---|
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 | 3095 | fjp | import java.awt.geom.Rectangle2D; |
48 | 1691 | fjp | 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 | 2183 | fernando | import com.iver.cit.gvsig.fmap.core.IFeature; |
55 | 1691 | fjp | import com.iver.cit.gvsig.fmap.core.IGeometry; |
56 | 8765 | jjdelcerro | import com.iver.cit.gvsig.fmap.core.ISymbol; |
57 | 3095 | fjp | import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
58 | 4171 | fjp | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
59 | 2183 | fernando | 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 | 1691 | fjp | import com.iver.cit.gvsig.fmap.layers.FLayer; |
63 | 3236 | fjp | import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
64 | 4171 | fjp | import com.iver.cit.gvsig.fmap.layers.ISpatialDB; |
65 | 5236 | fjp | import com.iver.cit.gvsig.fmap.layers.SpatialCache; |
66 | 2183 | fernando | import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable; |
67 | 1691 | fjp | import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer; |
68 | 1704 | fjp | import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
69 | 5317 | fjp | import com.iver.utiles.swing.threads.Cancellable; |
70 | import com.iver.utiles.swing.threads.CancellableMonitorable; |
||
71 | 1691 | fjp | |
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 | 1704 | fjp | // Nos aprovechamos del SQL para lanzar la consulta
|
90 | // teniendo en cuenta el boundingbox que toca.
|
||
91 | 3236 | fjp | FLyrVect lyr = (FLyrVect) getCapa(); |
92 | 4171 | fjp | ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource(); |
93 | 2183 | fernando | VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) dbAdapter.getDriver(); |
94 | 4171 | fjp | 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 | 4191 | fjp | VectorialLegend l = null;
|
112 | 4171 | fjp | if (lyr.getLegend() instanceof VectorialLegend) |
113 | { |
||
114 | 4191 | fjp | l = (VectorialLegend) lyr.getLegend(); |
115 | 4171 | fjp | } |
116 | |||
117 | Rectangle2D rectAux = viewPort.getAdjustedExtent();
|
||
118 | if (ct != null) { |
||
119 | ICoordTrans invertedCT = ct.getInverted(); |
||
120 | rectAux = invertedCT.convert(rectAux); |
||
121 | } |
||
122 | |||
123 | 4191 | fjp | |
124 | 4171 | fjp | 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 | 8765 | jjdelcerro | ISymbol symbol; |
148 | 5813 | fjp | SpatialCache cache = lyr.getSpatialCache(); |
149 | cache.clearAll(); |
||
150 | 4171 | fjp | while (geomIt.hasNext())
|
151 | { |
||
152 | if (cancel.isCanceled()) {
|
||
153 | geomIt.closeIterator(); |
||
154 | break;
|
||
155 | } |
||
156 | IFeature feat = geomIt.next(); |
||
157 | 4191 | fjp | if (feat == null) |
158 | { |
||
159 | continue;
|
||
160 | } |
||
161 | 4171 | fjp | IGeometry geom = feat.getGeometry(); |
162 | |||
163 | 5008 | fjp | // System.out.println("PINTANDO FEATURE " + feat.getID());
|
164 | 4181 | fjp | |
165 | 4171 | fjp | if (ct != null) { |
166 | if (bMustClone)
|
||
167 | geom = geom.cloneGeometry(); |
||
168 | geom.reProject(ct); |
||
169 | } |
||
170 | |||
171 | 4181 | fjp | i = dbAdapter.getRowIndexByFID(feat); |
172 | 4171 | fjp | // 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 | 8765 | jjdelcerro | symbol = symbol.getSymbolForSelection(); |
179 | 4171 | fjp | } |
180 | 5236 | fjp | |
181 | 5813 | fjp | if (lyr.isSpatialCacheEnabled()) {
|
182 | if (cache.getMaxFeatures() >= cache.size()) {
|
||
183 | // Ya reproyectado todo
|
||
184 | cache.insert(geom.getBounds2D(), geom); |
||
185 | 5236 | fjp | } |
186 | 5813 | fjp | } |
187 | 5236 | fjp | |
188 | 4171 | fjp | // symbol = l.getDefaultSymbol();
|
189 | 5236 | fjp | geom.drawInts(g,viewPort, symbol); |
190 | 4171 | fjp | |
191 | 5574 | fjp | } |
192 | 4171 | fjp | dbAdapter.stop(); |
193 | } catch (DriverIOException e) {
|
||
194 | throw new DriverException(e); |
||
195 | } |
||
196 | 1691 | fjp | |
197 | } |
||
198 | 4200 | azabala | /**
|
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 | 4347 | azabala | public void process(FeatureVisitor visitor, Rectangle2D rect, CancellableMonitorable cancel) |
214 | 4200 | azabala | 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 | 4419 | azabala | if(cancel != null){ |
244 | cancel.reportStep(); |
||
245 | } |
||
246 | 4200 | azabala | 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 | 4115 | fjp | |
265 | /* (non-Javadoc)
|
||
266 | * @see com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy#queryByRect(java.awt.geom.Rectangle2D)
|
||
267 | */
|
||
268 | 4347 | azabala | public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) throws DriverException { |
269 | 4115 | fjp | // Nos aprovechamos del SQL para lanzar la consulta
|
270 | // teniendo en cuenta el boundingbox que toca.
|
||
271 | FLyrVect lyr = (FLyrVect) getCapa(); |
||
272 | 4171 | fjp | ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource(); |
273 | 4115 | fjp | VectorialDatabaseDriver dbDriver = (VectorialDatabaseDriver) dbAdapter.getDriver(); |
274 | 4171 | fjp | 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 | 4341 | fjp | if(cancel != null) |
309 | 4419 | azabala | { |
310 | cancel.reportStep(); |
||
311 | 4341 | fjp | if(cancel.isCanceled())
|
312 | { |
||
313 | dbAdapter.stop(); |
||
314 | return selection;
|
||
315 | } |
||
316 | 4311 | azabala | } |
317 | 4171 | fjp | 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 | 4115 | fjp | |
337 | } |
||
338 | 1691 | fjp | |
339 | |||
340 | } |