root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialAdapter.java @ 37951
History | View | Annotate | Download (9.21 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.layers; |
42 |
|
43 |
import java.awt.Image; |
44 |
import java.awt.geom.Rectangle2D; |
45 |
|
46 |
import org.cresques.cts.IProjection; |
47 |
|
48 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
49 |
import com.hardcode.gdbms.engine.data.DataSource; |
50 |
import com.hardcode.gdbms.engine.values.Value; |
51 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
52 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
53 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
54 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
55 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
56 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
57 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
58 |
import com.iver.cit.gvsig.fmap.drivers.featureiterators.AttrQueryFeatureIterator; |
59 |
import com.iver.cit.gvsig.fmap.drivers.featureiterators.AttrQuerySelectionFeatureIterator; |
60 |
import com.iver.cit.gvsig.fmap.drivers.featureiterators.DefaultFeatureIterator; |
61 |
import com.iver.cit.gvsig.fmap.drivers.featureiterators.IndexedSptQueryFeatureIterator; |
62 |
import com.iver.cit.gvsig.fmap.drivers.featureiterators.SpatialQueryFeatureIterator; |
63 |
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex; |
64 |
|
65 |
/**
|
66 |
* Clase padre de los adaptadores de los drivers. De momento mantiene solo el
|
67 |
* ?ndice creado sobre la capa
|
68 |
*/
|
69 |
public abstract class VectorialAdapter implements ReadableVectorial { |
70 |
protected VectorialDriver driver;
|
71 |
/**
|
72 |
* Spatial index of the driver's data source
|
73 |
* */
|
74 |
protected ISpatialIndex spatialIndex;
|
75 |
/**
|
76 |
* Projection of the data in the data source
|
77 |
*/
|
78 |
protected IProjection projection;
|
79 |
|
80 |
/**
|
81 |
* Establece el driver sobre el que act?a el adaptador
|
82 |
*
|
83 |
* @param driver
|
84 |
*/
|
85 |
public void setDriver(VectorialDriver driver) { |
86 |
this.driver = driver;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Obtiene una referencia al objeto que implementa la interfaz vectorial con
|
91 |
* el fin de que las Strategy puedan optimizar en funci?n del driver.
|
92 |
*
|
93 |
* @return VectorialDriver
|
94 |
*/
|
95 |
public VectorialDriver getDriver() {
|
96 |
return driver;
|
97 |
} |
98 |
|
99 |
/**
|
100 |
* Devuelve el DataSource a pasrtir del nombre.
|
101 |
*
|
102 |
* @return DataSource.
|
103 |
*
|
104 |
* @throws ReadDriverException
|
105 |
*/
|
106 |
public abstract SelectableDataSource getRecordset() |
107 |
throws ReadDriverException;
|
108 |
|
109 |
/**
|
110 |
* Por defecto devuelve null, y se le pone el icono por defecto. Si el
|
111 |
* driver reescribe este m?todo, se usar? este icono en el TOC.
|
112 |
*
|
113 |
* @return
|
114 |
*/
|
115 |
public Image getImageIcon() { |
116 |
return null; |
117 |
} |
118 |
|
119 |
public DriverAttributes getDriverAttributes() {
|
120 |
if (driver != null) |
121 |
return driver.getDriverAttributes();
|
122 |
return null; |
123 |
} |
124 |
|
125 |
/**
|
126 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
|
127 |
*/
|
128 |
public int getShapeCount() throws ReadDriverException { |
129 |
return getDriver().getShapeCount();
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
134 |
*/
|
135 |
public Rectangle2D getFullExtent() throws ReadDriverException { |
136 |
try {
|
137 |
Rectangle2D aux = getDriver().getFullExtent();
|
138 |
// Para evitar que una nueva capa a?adida a una vista vac?a no
|
139 |
// tenga un lienzo para pintar.
|
140 |
if (aux == null) |
141 |
aux = new Rectangle2D.Double(1,1,10,10); |
142 |
return aux;
|
143 |
} catch (ExpansionFileReadException e) {
|
144 |
throw new ReadDriverException(getDriver().getName(),e); |
145 |
} |
146 |
} |
147 |
|
148 |
/**
|
149 |
* En la implementaci?n por defecto podemos hacer que cada feature tenga ID =
|
150 |
* numero de registro. En el DBAdapter podr?amos "overrride" este m?todo y
|
151 |
* poner como ID de la Feature el campo ?nico escogido en la base de datos.
|
152 |
*
|
153 |
* @param numReg
|
154 |
* @return
|
155 |
*/
|
156 |
public IFeature getFeature(int numReg) throws ReadDriverException { |
157 |
IGeometry geom; |
158 |
IFeature feat = null;
|
159 |
try {
|
160 |
geom = getShape(numReg); |
161 |
DataSource rs = getRecordset();
|
162 |
Value[] regAtt = new Value[rs.getFieldCount()]; |
163 |
for (int fieldId = 0; fieldId < rs.getFieldCount(); fieldId++) { |
164 |
regAtt[fieldId] = rs.getFieldValue(numReg, fieldId); |
165 |
} |
166 |
|
167 |
feat = new DefaultFeature(geom, regAtt, numReg + ""); |
168 |
} catch (ExpansionFileReadException e) {
|
169 |
throw new ReadDriverException(getDriver().getName(),e); |
170 |
} |
171 |
return feat;
|
172 |
} |
173 |
|
174 |
public IFeatureIterator getFeatureIterator() throws ReadDriverException{ |
175 |
return new DefaultFeatureIterator(this, projection, null, null); |
176 |
} |
177 |
|
178 |
public IFeatureIterator getFeatureIterator(String[] fields, IProjection newProjection) |
179 |
throws ReadDriverException{
|
180 |
return new DefaultFeatureIterator(this, projection, newProjection, fields); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* Return a feature iterator from a given sql statement.
|
185 |
* <br>
|
186 |
* In this case, the statement will have the "projection" operator
|
187 |
* (select campo1, campo2, ...etc) and the "selection" operator (where ....)
|
188 |
* @param sql statement which define a filter
|
189 |
* @return feature iterator
|
190 |
* */
|
191 |
public IFeatureIterator getFeatureIterator(String sql, |
192 |
IProjection newProjection) throws ReadDriverException{
|
193 |
|
194 |
return new AttrQueryFeatureIterator(this, projection, newProjection, sql); |
195 |
} |
196 |
|
197 |
|
198 |
/**
|
199 |
* Makes an spatial query returning a feature iterator over the features which intersects
|
200 |
* or are contained in the rectangle query. Applies a restriction to the alphanumeric fields
|
201 |
* returned by the iterator.
|
202 |
* @param rect
|
203 |
* @param fields
|
204 |
* @return
|
205 |
* @throws ReadDriverException
|
206 |
*/
|
207 |
public IFeatureIterator getFeatureIterator(Rectangle2D rect, String[] fields, |
208 |
IProjection newProjection, |
209 |
boolean fastIteration) throws ReadDriverException{ |
210 |
if(spatialIndex != null){ |
211 |
try {
|
212 |
if(isSpatialIndexNecessary(rect)) {
|
213 |
IndexedSptQueryFeatureIterator it = new IndexedSptQueryFeatureIterator(this, projection, newProjection, fields, rect, spatialIndex, fastIteration); |
214 |
if (it == null) { |
215 |
setSpatialIndex(null);
|
216 |
} |
217 |
else
|
218 |
return it;
|
219 |
} |
220 |
} catch (ExpansionFileReadException e) {
|
221 |
e.printStackTrace(); |
222 |
throw new ReadDriverException("Error al iterar la capa", e); |
223 |
} |
224 |
catch (Exception e) { |
225 |
e.printStackTrace(); |
226 |
setSpatialIndex(null);
|
227 |
} |
228 |
|
229 |
}//if
|
230 |
return new SpatialQueryFeatureIterator(this, projection, newProjection, fields, rect, fastIteration); |
231 |
|
232 |
|
233 |
} |
234 |
|
235 |
|
236 |
/*
|
237 |
* this method is copied from ShpStrategy
|
238 |
* */
|
239 |
/**
|
240 |
* Decides if for a given Rectangle2D extent, is worthy to use an spatial index
|
241 |
* (or a secuential scan)
|
242 |
* @param extent Rectangle2D used to filter features
|
243 |
* @return true if spatial index search is worthy, or false
|
244 |
*/
|
245 |
protected boolean isSpatialIndexNecessary(Rectangle2D extent) throws ReadDriverException, ExpansionFileReadException { |
246 |
Rectangle2D driverExtent = getFullExtent();
|
247 |
double areaExtent = extent.getWidth() * extent.getHeight();
|
248 |
double areaFullExtent = driverExtent.getWidth() *
|
249 |
driverExtent.getHeight(); |
250 |
return areaExtent < (areaFullExtent / 4.0); |
251 |
|
252 |
} |
253 |
|
254 |
public ISpatialIndex getSpatialIndex(){
|
255 |
return spatialIndex;
|
256 |
|
257 |
} |
258 |
public void setSpatialIndex(ISpatialIndex spatialIndex){ |
259 |
this.spatialIndex = spatialIndex;
|
260 |
} |
261 |
|
262 |
public void setProjection(IProjection projection){ |
263 |
this.projection = projection;
|
264 |
} |
265 |
|
266 |
public IProjection getProjection(){
|
267 |
return projection;
|
268 |
} |
269 |
|
270 |
public IFeatureIterator getFeatureIterator(String sql, IProjection newProjection, boolean withSelection) throws ReadDriverException { |
271 |
if (withSelection)
|
272 |
return new AttrQuerySelectionFeatureIterator(this, projection, newProjection, sql); |
273 |
else
|
274 |
return getFeatureIterator(sql,newProjection);
|
275 |
} |
276 |
/*
|
277 |
* (non-Javadoc)
|
278 |
*
|
279 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFeatureIterator(java.awt.geom.Rectangle2D,
|
280 |
* java.lang.String) Lo sobreescribir?n los adapters para base de datos
|
281 |
* espacial. Por defecto, suponemos un buen acceso aleatorio y usamos
|
282 |
* getFeature(i)
|
283 |
*/
|
284 |
/* public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG)
|
285 |
throws DriverException {
|
286 |
try {
|
287 |
return new RandomAccessFeatureIterator(driver, getRecordset(), r,
|
288 |
strEPSG);
|
289 |
} catch (DriverLoadException e) {
|
290 |
throw new DriverException(e);
|
291 |
}
|
292 |
}
|
293 |
|
294 |
public IFeatureIterator getFeatureIterator(String strEPSG)
|
295 |
throws DriverException {
|
296 |
try {
|
297 |
return new RandomAccessFeatureIterator(driver, getRecordset(), strEPSG);
|
298 |
} catch (DriverLoadException e) {
|
299 |
throw new DriverException(e);
|
300 |
}
|
301 |
} */
|
302 |
|
303 |
} |