svn-gvsig-desktop / tags / v1_1_Build_1013 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DBStrategy.java @ 13521
History | View | Annotate | Download (10.9 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.IVectorialDatabaseDriver; |
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 |
|
94 |
boolean isJoined = lyr.isJoined();
|
95 |
|
96 |
//IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver();
|
97 |
try {
|
98 |
dbAdapter.start(); |
99 |
Selectable selectable=lyr.getRecordset(); |
100 |
ICoordTrans ct = lyr.getCoordTrans(); |
101 |
FBitSet bitSet = selectable.getSelection(); |
102 |
|
103 |
String strEPSG = viewPort.getProjection().getAbrev().substring(5); |
104 |
// TODO: EXPLORAR LAS LEYENDAS PARA SABER LOS CAMPOS QUE VAMOS
|
105 |
// A NECESITAR RECUPERAR, PARA INCLUIR EN LA CONSULTA SOLO
|
106 |
// AQUELLOS QUE VAMOS A NECESITAR. CON ESO GANAREMOS VELOCIDAD.
|
107 |
// Ejemplo:
|
108 |
// En ArcSDE:
|
109 |
// con Vias cogiendo un campo solo: 5 segundos
|
110 |
// con todos los campos de Vias: 11 segundos.
|
111 |
// => MODIFICAR EL getFeatureIterator para que admita los nombres
|
112 |
// de los campos adem?s del rect?ngulo que pides.
|
113 |
String[] usedFields = null; |
114 |
VectorialLegend l = null;
|
115 |
if (lyr.getLegend() instanceof VectorialLegend) |
116 |
{ |
117 |
l = (VectorialLegend) lyr.getLegend(); |
118 |
} |
119 |
|
120 |
Rectangle2D rectAux = viewPort.getAdjustedExtent();
|
121 |
if (ct != null) { |
122 |
ICoordTrans invertedCT = ct.getInverted(); |
123 |
rectAux = invertedCT.convert(rectAux); |
124 |
} |
125 |
|
126 |
|
127 |
if (!isJoined){
|
128 |
usedFields = l.getUsedFields(); |
129 |
} |
130 |
|
131 |
|
132 |
IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, usedFields); |
133 |
if (geomIt == null) |
134 |
{ |
135 |
// dbAdapter.stop();
|
136 |
return;
|
137 |
} |
138 |
|
139 |
|
140 |
DriverAttributes attr = dbAdapter.getDriverAttributes(); |
141 |
boolean bMustClone = false; |
142 |
if (attr != null) |
143 |
{ |
144 |
if (attr.isLoadedInMemory())
|
145 |
{ |
146 |
bMustClone = attr.isLoadedInMemory(); |
147 |
} |
148 |
} |
149 |
|
150 |
|
151 |
int i;
|
152 |
ISymbol symbol; |
153 |
SpatialCache cache = lyr.getSpatialCache(); |
154 |
cache.clearAll(); |
155 |
while (geomIt.hasNext())
|
156 |
{ |
157 |
if (cancel.isCanceled()) {
|
158 |
geomIt.closeIterator(); |
159 |
break;
|
160 |
} |
161 |
IFeature feat = geomIt.next(); |
162 |
if (feat == null) |
163 |
{ |
164 |
continue;
|
165 |
} |
166 |
IGeometry geom = feat.getGeometry(); |
167 |
|
168 |
// System.out.println("PINTANDO FEATURE " + feat.getID());
|
169 |
|
170 |
if (ct != null) { |
171 |
if (bMustClone)
|
172 |
geom = geom.cloneGeometry(); |
173 |
geom.reProject(ct); |
174 |
} |
175 |
|
176 |
i = dbAdapter.getRowIndexByFID(feat); |
177 |
|
178 |
if (isJoined){
|
179 |
symbol = l.getSymbol(i); |
180 |
} else {
|
181 |
symbol = l.getSymbolByFeature(feat); |
182 |
} |
183 |
|
184 |
if (symbol == null) continue; |
185 |
if (bitSet.get(i)) {
|
186 |
symbol = symbol.getSymbolForSelection(); |
187 |
} |
188 |
|
189 |
if (lyr.isSpatialCacheEnabled()) {
|
190 |
if (cache.getMaxFeatures() >= cache.size()) {
|
191 |
// Ya reproyectado todo
|
192 |
cache.insert(geom.getBounds2D(), geom); |
193 |
} |
194 |
} |
195 |
|
196 |
// symbol = l.getDefaultSymbol();
|
197 |
geom.drawInts(g,viewPort, symbol); |
198 |
|
199 |
} |
200 |
dbAdapter.stop(); |
201 |
} catch (DriverIOException e) {
|
202 |
throw new DriverException(e); |
203 |
} |
204 |
|
205 |
} |
206 |
/**
|
207 |
* Processes features of layer whose geometries would
|
208 |
* intersect rectangle passed as param by calling visitor's
|
209 |
* visit method.
|
210 |
*
|
211 |
* FIXME FeatureVisitor is designed to work with memory and
|
212 |
* file drivers (visit(IGeometry, index), and to work with
|
213 |
* geometries (thats the reason for not to create Values, instead
|
214 |
* of pass an index)
|
215 |
*
|
216 |
* But DBDrivers recovers IGeometry and Values in dbAdapter.getFeatureIterator
|
217 |
* method. We must add a visit(Feature) method to FeatureVisitor.
|
218 |
*
|
219 |
*
|
220 |
*/
|
221 |
public void process(FeatureVisitor visitor, Rectangle2D rect, CancellableMonitorable cancel) |
222 |
throws DriverException, VisitException{
|
223 |
FLyrVect lyr = (FLyrVect) getCapa(); |
224 |
ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource(); |
225 |
IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver(); |
226 |
try {
|
227 |
dbAdapter.start(); |
228 |
ICoordTrans ct = lyr.getCoordTrans(); |
229 |
String strEPSG = lyr.getProjection().getAbrev().substring(5); |
230 |
Rectangle2D rectAux = rect;
|
231 |
if (ct != null) { |
232 |
ICoordTrans invertedCT = ct.getInverted(); |
233 |
rectAux = invertedCT.convert(rectAux); |
234 |
} |
235 |
IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
|
236 |
if (geomIt == null){ |
237 |
return;
|
238 |
} |
239 |
DriverAttributes attr = dbAdapter.getDriverAttributes(); |
240 |
boolean bMustClone = false; |
241 |
if (attr != null) |
242 |
{ |
243 |
if (attr.isLoadedInMemory())
|
244 |
{ |
245 |
bMustClone = attr.isLoadedInMemory(); |
246 |
}//if
|
247 |
}//if
|
248 |
int i;
|
249 |
while (geomIt.hasNext())
|
250 |
{ |
251 |
if(cancel != null){ |
252 |
cancel.reportStep(); |
253 |
} |
254 |
IFeature feat = geomIt.next(); |
255 |
IGeometry geom = feat.getGeometry(); |
256 |
if (ct != null) { |
257 |
if (bMustClone){
|
258 |
geom = geom.cloneGeometry(); |
259 |
}//if
|
260 |
geom.reProject(ct); |
261 |
}//if
|
262 |
i = dbDriver.getRowIndexByFID(feat); |
263 |
if (geom.intersects(rect)){
|
264 |
visitor.visit(geom, i); |
265 |
} |
266 |
} |
267 |
dbAdapter.stop(); |
268 |
} catch (DriverIOException e) {
|
269 |
throw new DriverException(e); |
270 |
} |
271 |
} |
272 |
|
273 |
/* (non-Javadoc)
|
274 |
* @see com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy#queryByRect(java.awt.geom.Rectangle2D)
|
275 |
*/
|
276 |
public FBitSet queryByRect(Rectangle2D rect, CancellableMonitorable cancel) throws DriverException { |
277 |
// Nos aprovechamos del SQL para lanzar la consulta
|
278 |
// teniendo en cuenta el boundingbox que toca.
|
279 |
FLyrVect lyr = (FLyrVect) getCapa(); |
280 |
ISpatialDB dbAdapter = (ISpatialDB) ((SingleLayer) capa).getSource(); |
281 |
IVectorialDatabaseDriver dbDriver = (IVectorialDatabaseDriver) dbAdapter.getDriver(); |
282 |
try {
|
283 |
dbAdapter.start(); |
284 |
ICoordTrans ct = lyr.getCoordTrans(); |
285 |
String strEPSG = lyr.getProjection().getAbrev().substring(5); |
286 |
Rectangle2D rectAux = rect;
|
287 |
if (ct != null) { |
288 |
ICoordTrans invertedCT = ct.getInverted(); |
289 |
rectAux = invertedCT.convert(rectAux); |
290 |
} |
291 |
|
292 |
FBitSet selection = new FBitSet();
|
293 |
|
294 |
IFeatureIterator geomIt = dbAdapter.getFeatureIterator(rectAux, strEPSG, null);
|
295 |
if (geomIt == null) |
296 |
{ |
297 |
return selection;
|
298 |
} |
299 |
|
300 |
|
301 |
DriverAttributes attr = dbAdapter.getDriverAttributes(); |
302 |
boolean bMustClone = false; |
303 |
if (attr != null) |
304 |
{ |
305 |
if (attr.isLoadedInMemory())
|
306 |
{ |
307 |
bMustClone = attr.isLoadedInMemory(); |
308 |
} |
309 |
} |
310 |
|
311 |
|
312 |
int i;
|
313 |
|
314 |
while (geomIt.hasNext())
|
315 |
{ |
316 |
if(cancel != null) |
317 |
{ |
318 |
cancel.reportStep(); |
319 |
if(cancel.isCanceled())
|
320 |
{ |
321 |
dbAdapter.stop(); |
322 |
return selection;
|
323 |
} |
324 |
} |
325 |
IFeature feat = geomIt.next(); |
326 |
IGeometry geom = feat.getGeometry(); |
327 |
|
328 |
if (ct != null) { |
329 |
if (bMustClone)
|
330 |
geom = geom.cloneGeometry(); |
331 |
geom.reProject(ct); |
332 |
} |
333 |
|
334 |
i = dbDriver.getRowIndexByFID(feat); |
335 |
if (geom.intersects(rect))
|
336 |
selection.set(i, true);
|
337 |
} |
338 |
dbAdapter.stop(); |
339 |
|
340 |
return selection;
|
341 |
} catch (DriverIOException e) {
|
342 |
throw new DriverException(e); |
343 |
} |
344 |
|
345 |
} |
346 |
|
347 |
|
348 |
} |