root / trunk / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / reader / GPEVectorialDriver.java @ 30721
History | View | Annotate | Download (11.6 KB)
1 | 27093 | jpiera | package org.gvsig.fmap.drivers.gpe.reader; |
---|---|---|---|
2 | |||
3 | import java.awt.Component; |
||
4 | import java.awt.geom.Rectangle2D; |
||
5 | import java.io.File; |
||
6 | import java.sql.Types; |
||
7 | 30721 | jpiera | import java.util.ArrayList; |
8 | 27093 | jpiera | import java.util.HashMap; |
9 | import java.util.Iterator; |
||
10 | |||
11 | import javax.swing.JOptionPane; |
||
12 | |||
13 | import org.cresques.cts.ICoordTrans; |
||
14 | import org.cresques.cts.IProjection; |
||
15 | import org.gvsig.fmap.drivers.gpe.handlers.DefaultFmapContentHandler; |
||
16 | import org.gvsig.fmap.drivers.gpe.handlers.FmapErrorHandler; |
||
17 | import org.gvsig.fmap.drivers.gpe.handlers.FmapHandlerFactory; |
||
18 | import org.gvsig.fmap.drivers.gpe.model.GPEElement; |
||
19 | import org.gvsig.fmap.drivers.gpe.model.GPEFeature; |
||
20 | import org.gvsig.fmap.drivers.gpe.model.GPEGeometry; |
||
21 | import org.gvsig.gpe.GPERegister; |
||
22 | import org.gvsig.gpe.parser.GPEParser; |
||
23 | |||
24 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
25 | import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
||
26 | import com.hardcode.gdbms.engine.data.edition.DataWare; |
||
27 | import com.hardcode.gdbms.engine.values.Value; |
||
28 | import com.hardcode.gdbms.engine.values.ValueFactory; |
||
29 | import com.iver.andami.PluginServices; |
||
30 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
31 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
32 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
33 | import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
||
34 | import com.iver.cit.gvsig.fmap.drivers.BoundedShapes; |
||
35 | import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
||
36 | import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
||
37 | |||
38 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
39 | *
|
||
40 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
41 | *
|
||
42 | * This program is free software; you can redistribute it and/or
|
||
43 | * modify it under the terms of the GNU General Public License
|
||
44 | * as published by the Free Software Foundation; either version 2
|
||
45 | * of the License, or (at your option) any later version.
|
||
46 | *
|
||
47 | * This program is distributed in the hope that it will be useful,
|
||
48 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
49 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
50 | * GNU General Public License for more details.
|
||
51 | *
|
||
52 | * You should have received a copy of the GNU General Public License
|
||
53 | * along with this program; if not, write to the Free Software
|
||
54 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
55 | *
|
||
56 | * For more information, contact:
|
||
57 | *
|
||
58 | * Generalitat Valenciana
|
||
59 | * Conselleria d'Infraestructures i Transport
|
||
60 | * Av. Blasco Ib??ez, 50
|
||
61 | * 46010 VALENCIA
|
||
62 | * SPAIN
|
||
63 | *
|
||
64 | * +34 963862235
|
||
65 | * gvsig@gva.es
|
||
66 | * www.gvsig.gva.es
|
||
67 | *
|
||
68 | * or
|
||
69 | *
|
||
70 | * IVER T.I. S.A
|
||
71 | * Salamanca 50
|
||
72 | * 46005 Valencia
|
||
73 | * Spain
|
||
74 | *
|
||
75 | * +34 963163400
|
||
76 | * dac@iver.es
|
||
77 | */
|
||
78 | /* CVS MESSAGES:
|
||
79 | *
|
||
80 | * $Id$
|
||
81 | * $Log$
|
||
82 | *
|
||
83 | */
|
||
84 | /**
|
||
85 | * @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
||
86 | */
|
||
87 | public abstract class GPEVectorialDriver implements IGPEDriver, VectorialDriver, ObjectDriver, |
||
88 | BoundedShapes{ |
||
89 | private Rectangle2D extent = null; |
||
90 | //The data
|
||
91 | private HashMap features = null; |
||
92 | private int numFeatures = 0; |
||
93 | 30721 | jpiera | private ArrayList parsers = null; |
94 | 27093 | jpiera | private IProjection projection = null; |
95 | private File m_Fich; |
||
96 | private boolean isWarningShowed = false; |
||
97 | private DriverAttributes attributes = null; |
||
98 | |||
99 | GPEVectorialDriver() { |
||
100 | super();
|
||
101 | features = new HashMap(); |
||
102 | 30721 | jpiera | GPEParser[] registeredParsers = GPERegister.getAllParsers();
|
103 | parsers = new ArrayList(); |
||
104 | for (int i=0 ; i<registeredParsers.length ; i++){ |
||
105 | for (int j=0 ; j<getGPEParsers().size() ; j++){ |
||
106 | if (registeredParsers[i].getClass() == getGPEParsers().get(j)){
|
||
107 | parsers.add(registeredParsers[i]); |
||
108 | } |
||
109 | 27093 | jpiera | } |
110 | } |
||
111 | } |
||
112 | |||
113 | /**
|
||
114 | * @return the parser
|
||
115 | */
|
||
116 | 30721 | jpiera | public ArrayList getParsers() { |
117 | return parsers;
|
||
118 | 27093 | jpiera | } |
119 | |||
120 | /**
|
||
121 | * @return the projection
|
||
122 | */
|
||
123 | public IProjection getProjection() {
|
||
124 | return projection;
|
||
125 | } |
||
126 | |||
127 | /**
|
||
128 | * @param projection the projection to set
|
||
129 | */
|
||
130 | public void setProjection(IProjection projection) { |
||
131 | this.projection = projection;
|
||
132 | } |
||
133 | |||
134 | /**
|
||
135 | * Add a new feature in the layer
|
||
136 | * @param feature
|
||
137 | * The feature to add
|
||
138 | */
|
||
139 | public void addFeature(GPEFeature feature) { |
||
140 | IGeometry geometry = getGeometry(feature); |
||
141 | //if the geometry exists
|
||
142 | if (geometry != null){ |
||
143 | //Update the extent
|
||
144 | Rectangle2D boundsShp = geometry.getBounds();
|
||
145 | if (extent == null) { |
||
146 | extent = boundsShp; |
||
147 | } else {
|
||
148 | extent.add(boundsShp); |
||
149 | } |
||
150 | //Set the geometry
|
||
151 | feature.getGeometry().setReprojectedGeometry(geometry); |
||
152 | //Set the attributes
|
||
153 | features.put(new Integer(numFeatures), feature); |
||
154 | numFeatures++; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /**
|
||
159 | * Gets the geometry
|
||
160 | * @param feature
|
||
161 | * The feature to add
|
||
162 | */
|
||
163 | private IGeometry getGeometry(GPEFeature feature){
|
||
164 | GPEGeometry gpeGeometry = ((GPEFeature)feature).getGeometry(); |
||
165 | if (gpeGeometry != null){ |
||
166 | IProjection geomProj = null;
|
||
167 | if (gpeGeometry.getSrs() != null){ |
||
168 | try{
|
||
169 | geomProj = CRSFactory.getCRS(gpeGeometry.getSrs()); |
||
170 | }catch(Exception e){ |
||
171 | //If the CRS factory has an error.
|
||
172 | } |
||
173 | } |
||
174 | if (geomProj == null){ |
||
175 | return gpeGeometry.getIGeometry();
|
||
176 | }else{
|
||
177 | if (projection == null){ |
||
178 | return gpeGeometry.getIGeometry();
|
||
179 | }else{
|
||
180 | if (geomProj.getAbrev().compareTo(projection.getAbrev()) == 0){ |
||
181 | return gpeGeometry.getIGeometry();
|
||
182 | }else{
|
||
183 | ICoordTrans coordTrans = geomProj.getCT(projection); |
||
184 | FShape shape = (FShape)gpeGeometry.getIGeometry().getInternalShape(); |
||
185 | shape.reProject(coordTrans); |
||
186 | return ShapeFactory.createGeometry(shape);
|
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | return null; |
||
192 | } |
||
193 | |||
194 | /*
|
||
195 | * (non-Javadoc)
|
||
196 | * @see com.iver.cit.gvsig.fmap.drivers.gpe.reader.IGPEDriver#setExtent(java.awt.geom.Rectangle2D)
|
||
197 | */
|
||
198 | public void setExtent(Rectangle2D extent) { |
||
199 | this.extent = extent;
|
||
200 | } |
||
201 | |||
202 | /*
|
||
203 | * (non-Javadoc)
|
||
204 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
||
205 | */
|
||
206 | public DriverAttributes getDriverAttributes() {
|
||
207 | if (attributes == null){ |
||
208 | attributes = new DriverAttributes();
|
||
209 | attributes.setLoadedInMemory(true);
|
||
210 | } |
||
211 | return attributes;
|
||
212 | } |
||
213 | |||
214 | /*
|
||
215 | * (non-Javadoc)
|
||
216 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
|
||
217 | */
|
||
218 | public Rectangle2D getFullExtent(){ |
||
219 | return extent;
|
||
220 | } |
||
221 | |||
222 | /*
|
||
223 | * (non-Javadoc)
|
||
224 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
|
||
225 | */
|
||
226 | public IGeometry getShape(int index) { |
||
227 | return ((GPEFeature)features.get(new Integer(index))).getGeometry().getReprojectedGeometry(); |
||
228 | } |
||
229 | |||
230 | /*
|
||
231 | * (non-Javadoc)
|
||
232 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
|
||
233 | */
|
||
234 | public int getShapeCount() { |
||
235 | return features.size();
|
||
236 | } |
||
237 | |||
238 | /*
|
||
239 | * (non-Javadoc)
|
||
240 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
|
||
241 | */
|
||
242 | public int getShapeType() { |
||
243 | return FShape.MULTI;
|
||
244 | } |
||
245 | |||
246 | /*
|
||
247 | * (non-Javadoc)
|
||
248 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#isWritable()
|
||
249 | */
|
||
250 | public boolean isWritable() { |
||
251 | return true; |
||
252 | } |
||
253 | |||
254 | /*
|
||
255 | * (non-Javadoc)
|
||
256 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reload()
|
||
257 | */
|
||
258 | public void reload() { |
||
259 | numFeatures = 0;
|
||
260 | features.clear(); |
||
261 | extent = null;
|
||
262 | } |
||
263 | |||
264 | /*
|
||
265 | * (non-Javadoc)
|
||
266 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
||
267 | */
|
||
268 | public int[] getPrimaryKeys() { |
||
269 | return null; |
||
270 | } |
||
271 | |||
272 | /*
|
||
273 | * (non-Javadoc)
|
||
274 | * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
||
275 | */
|
||
276 | public void write(DataWare dataWare) { |
||
277 | |||
278 | } |
||
279 | |||
280 | /*
|
||
281 | * (non-Javadoc)
|
||
282 | * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
||
283 | */
|
||
284 | public void setDataSourceFactory(DataSourceFactory dsf) { |
||
285 | // TODO Auto-generated method stub
|
||
286 | |||
287 | } |
||
288 | |||
289 | /*
|
||
290 | * (non-Javadoc)
|
||
291 | * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
|
||
292 | */
|
||
293 | public int getFieldCount() { |
||
294 | if (features.size() > 0){ |
||
295 | GPEFeature feature = (GPEFeature)features.get(new Integer(0)); |
||
296 | return feature.getelements().size() + 1; |
||
297 | } |
||
298 | return 1; |
||
299 | } |
||
300 | |||
301 | /*
|
||
302 | * (non-Javadoc)
|
||
303 | * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
|
||
304 | */
|
||
305 | public String getFieldName(int fieldId) { |
||
306 | if (fieldId == getFieldCount()-1){ |
||
307 | return "fid"; |
||
308 | } |
||
309 | if (features.size() > 0){ |
||
310 | GPEFeature feature = (GPEFeature)features.get(new Integer(0)); |
||
311 | Iterator it = feature.getelements().keySet().iterator();
|
||
312 | String fieldName = null; |
||
313 | for (int i=0 ; i<=fieldId ; i++){ |
||
314 | fieldName = (String)it.next();
|
||
315 | } |
||
316 | return fieldName;
|
||
317 | } |
||
318 | return null; |
||
319 | } |
||
320 | |||
321 | /*
|
||
322 | * (non-Javadoc)
|
||
323 | * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
|
||
324 | */
|
||
325 | public int getFieldType(int i) { |
||
326 | if (i == getFieldCount()-1){ |
||
327 | return Types.VARCHAR; |
||
328 | } |
||
329 | if (getRowCount() > 1){ |
||
330 | Value value = getFieldValue(0,i);
|
||
331 | return value.getSQLType();
|
||
332 | } |
||
333 | return Types.VARCHAR; |
||
334 | } |
||
335 | |||
336 | /*
|
||
337 | * (non-Javadoc)
|
||
338 | * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long, int)
|
||
339 | */
|
||
340 | public Value getFieldValue(long rowIndex, int fieldId) { |
||
341 | GPEFeature feature = (GPEFeature)features.get(new Integer((int)rowIndex)); |
||
342 | if (fieldId == getFieldCount()-1){ |
||
343 | return feature.getId();
|
||
344 | } |
||
345 | String attName = getFieldName(fieldId);
|
||
346 | GPEElement element = (GPEElement)feature.getelements().get(attName); |
||
347 | if (element != null){ |
||
348 | return element.getValue();
|
||
349 | } |
||
350 | return ValueFactory.createValue(""); |
||
351 | } |
||
352 | |||
353 | /*
|
||
354 | * (non-Javadoc)
|
||
355 | * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldWidth(int)
|
||
356 | */
|
||
357 | public int getFieldWidth(int i) { |
||
358 | return 50; |
||
359 | } |
||
360 | |||
361 | /*
|
||
362 | * (non-Javadoc)
|
||
363 | * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
|
||
364 | */
|
||
365 | public long getRowCount() { |
||
366 | return features.size();
|
||
367 | } |
||
368 | |||
369 | /*
|
||
370 | * (non-Javadoc)
|
||
371 | * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds(int)
|
||
372 | */
|
||
373 | public Rectangle2D getShapeBounds(int index){ |
||
374 | return ((GPEFeature)features.get(new Integer(index))).getGeometry().getShapeBounds(); |
||
375 | } |
||
376 | |||
377 | /*
|
||
378 | * (non-Javadoc)
|
||
379 | * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
|
||
380 | */
|
||
381 | public int getShapeType(int index){ |
||
382 | return FShape.MULTI;
|
||
383 | } |
||
384 | |||
385 | /*
|
||
386 | * (non-Javadoc)
|
||
387 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
|
||
388 | */
|
||
389 | public void close() { |
||
390 | |||
391 | } |
||
392 | |||
393 | /*
|
||
394 | * (non-Javadoc)
|
||
395 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFile()
|
||
396 | */
|
||
397 | public File getFile() { |
||
398 | return m_Fich;
|
||
399 | } |
||
400 | |||
401 | /*
|
||
402 | * (non-Javadoc)
|
||
403 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
|
||
404 | */
|
||
405 | public void initialize() { |
||
406 | FmapErrorHandler errorHandler = FmapHandlerFactory.createErrorHandler(); |
||
407 | DefaultFmapContentHandler contentHandler = FmapHandlerFactory.createContentHandler(errorHandler, |
||
408 | this);
|
||
409 | 30721 | jpiera | |
410 | GPEParser parser = null;
|
||
411 | for (int i=0 ; i<parsers.size() ; i++){ |
||
412 | if (((GPEParser)parsers.get(i)).accept(getFile().toURI())){
|
||
413 | parser = (GPEParser)parsers.get(i); |
||
414 | } |
||
415 | } |
||
416 | if (parser == null){ |
||
417 | parser = (GPEParser)parsers.get(0);
|
||
418 | } |
||
419 | 27093 | jpiera | parser.parse(contentHandler, |
420 | errorHandler, |
||
421 | getFile().toURI()); |
||
422 | //TODO patch to support multilayer on KML
|
||
423 | if (getName().equals(KMLVectorialDriver.DRIVERNAME)){
|
||
424 | if (isWarningShowed == false){ |
||
425 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
||
426 | PluginServices.getText(this,"gpe_gvsig_dont_support_multilayer")); |
||
427 | isWarningShowed = true;
|
||
428 | } |
||
429 | } |
||
430 | } |
||
431 | |||
432 | /*
|
||
433 | * (non-Javadoc)
|
||
434 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
|
||
435 | */
|
||
436 | public void open(File f) { |
||
437 | m_Fich = f; |
||
438 | } |
||
439 | |||
440 | /*
|
||
441 | * (non-Javadoc)
|
||
442 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
|
||
443 | */
|
||
444 | public boolean accept(File f) { |
||
445 | if (f.isDirectory()){
|
||
446 | return true; |
||
447 | } |
||
448 | 30721 | jpiera | for (int i=0 ; i<parsers.size() ; i++){ |
449 | if (((GPEParser)parsers.get(i)).accept(f.toURI())){
|
||
450 | return true; |
||
451 | } |
||
452 | } |
||
453 | return false; |
||
454 | 27093 | jpiera | } |
455 | |||
456 | /* (non-Javadoc)
|
||
457 | * @see com.iver.cit.gvsig.fmap.drivers.gpe.reader.IGPEDriver#getTypeName()
|
||
458 | */
|
||
459 | public String getTypeName() { |
||
460 | if (features.size() > 0){ |
||
461 | GPEFeature feature = (GPEFeature)features.get(new Integer(0)); |
||
462 | return feature.getName();
|
||
463 | } |
||
464 | return null; |
||
465 | } |
||
466 | 30721 | jpiera | |
467 | |||
468 | 27093 | jpiera | } |