Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / reader / GPEVectorialDriver.java @ 18292

History | View | Annotate | Download (10.8 KB)

1
package org.gvsig.fmap.drivers.gpe.reader;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.File;
5
import java.io.IOException;
6
import java.sql.Types;
7
import java.util.HashMap;
8
import java.util.Iterator;
9

    
10
import org.cresques.cts.ICoordTrans;
11
import org.cresques.cts.IProjection;
12
import org.gvsig.fmap.drivers.gpe.handlers.DefaultFmapContentHandler;
13
import org.gvsig.fmap.drivers.gpe.handlers.FmapErrorHandler;
14
import org.gvsig.fmap.drivers.gpe.handlers.FmapHandlerFactory;
15
import org.gvsig.fmap.drivers.gpe.model.GPEElement;
16
import org.gvsig.fmap.drivers.gpe.model.GPEFeature;
17
import org.gvsig.fmap.drivers.gpe.model.GPEGeometry;
18
import org.gvsig.gpe.GPEParser;
19
import org.gvsig.gpe.GPERegister;
20

    
21
import com.hardcode.gdbms.engine.data.DataSourceFactory;
22
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
23
import com.hardcode.gdbms.engine.data.edition.DataWare;
24
import com.hardcode.gdbms.engine.values.Value;
25
import com.hardcode.gdbms.engine.values.ValueFactory;
26
import com.iver.cit.gvsig.fmap.core.FShape;
27
import com.iver.cit.gvsig.fmap.core.IGeometry;
28
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
29
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
30
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
31
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
32
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
33

    
34
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
35
 *
36
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
37
 *
38
 * This program is free software; you can redistribute it and/or
39
 * modify it under the terms of the GNU General Public License
40
 * as published by the Free Software Foundation; either version 2
41
 * of the License, or (at your option) any later version.
42
 *
43
 * This program is distributed in the hope that it will be useful,
44
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46
 * GNU General Public License for more details.
47
 *
48
 * You should have received a copy of the GNU General Public License
49
 * along with this program; if not, write to the Free Software
50
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
51
 *
52
 * For more information, contact:
53
 *
54
 *  Generalitat Valenciana
55
 *   Conselleria d'Infraestructures i Transport
56
 *   Av. Blasco Ib??ez, 50
57
 *   46010 VALENCIA
58
 *   SPAIN
59
 *
60
 *      +34 963862235
61
 *   gvsig@gva.es
62
 *      www.gvsig.gva.es
63
 *
64
 *    or
65
 *
66
 *   IVER T.I. S.A
67
 *   Salamanca 50
68
 *   46005 Valencia
69
 *   Spain
70
 *
71
 *   +34 963163400
72
 *   dac@iver.es
73
 */
74
/* CVS MESSAGES:
75
 *
76
 * $Id$
77
 * $Log$
78
 *
79
 */
80
/**
81
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
82
 */
83
public abstract class GPEVectorialDriver implements IGPEDriver, VectorialDriver, ObjectDriver,
84
BoundedShapes{
85
        private Rectangle2D extent = null;
86
        //The data
87
        private HashMap features = null;
88
        private int numFeatures = 0; 
89
        private GPEParser parser = null;
90
        private IProjection projection = null;
91
        private File m_Fich;
92

    
93
        GPEVectorialDriver() {
94
                super();        
95
                features = new HashMap();
96
                GPEParser[] parsers = GPERegister.getAllParsers();
97
                for (int i=0 ; i<parsers.length ; i++){
98
                        if (parsers[i].getClass() == getGPEParser()){
99
                                setParser(parsers[i]);
100
                        }
101
                }
102
        }        
103

    
104
        /**
105
         * @return the parser
106
         */
107
        public GPEParser getParser() {
108
                return parser;
109
        }
110

    
111
        /**
112
         * @param parser the parser to set
113
         */
114
        public void setParser(GPEParser parser) {
115
                this.parser = parser;
116
        }
117

    
118
        /**
119
         * @return the projection
120
         */
121
        public IProjection getProjection() {
122
                return projection;
123
        }
124

    
125
        /**
126
         * @param projection the projection to set
127
         */
128
        public void setProjection(IProjection projection) {
129
                this.projection = projection;
130
        }
131

    
132
        /**
133
         * Add a new feature in the layer
134
         * @param feature
135
         * The feature to add
136
         */
137
        public void addFeature(GPEFeature feature) {
138
                IGeometry geometry = getGeometry(feature);
139
                //if the geometry exists
140
                if (geometry != null){
141
                        //Update the extent
142
                        Rectangle2D boundsShp = geometry.getBounds();
143
                        if (extent == null) {
144
                                extent = boundsShp;
145
                        } else {
146
                                extent.add(boundsShp);
147
                        }
148
                        //Set the geometry
149
                        feature.getGeometry().setReprojectedGeometry(geometry);
150
                        //Set the attributes
151
                        features.put(new Integer(numFeatures), feature);
152
                        numFeatures++;
153
                }
154
        }
155

    
156
        /**
157
         * Gets the geometry
158
         * @param feature
159
         * The feature to add
160
         */
161
        private IGeometry getGeometry(GPEFeature feature){
162
                GPEGeometry gpeGeometry = ((GPEFeature)feature).getGeometry();
163
                if (gpeGeometry != null){
164
                        IProjection geomProj = null;
165
                        if (gpeGeometry.getSrs() != null){
166
                                try{
167
                                        geomProj = CRSFactory.getCRS(gpeGeometry.getSrs());
168
                                }catch(Exception e){
169
                                        //If the CRS factory has an error.
170
                                }                                
171
                        }
172
                        if (geomProj == null){
173
                                return gpeGeometry.getIGeometry();
174
                        }else{
175
                                if (projection == null){
176
                                        return gpeGeometry.getIGeometry();
177
                                }else{
178
                                        if (geomProj.getAbrev().compareTo(projection.getAbrev()) == 0){
179
                                                return gpeGeometry.getIGeometry();
180
                                        }else{
181
                                                ICoordTrans coordTrans = geomProj.getCT(projection);
182
                                                FShape shape = (FShape)gpeGeometry.getIGeometry().getInternalShape();
183
                                                shape.reProject(coordTrans);
184
                                                return ShapeFactory.createGeometry(shape);
185
                                        }
186
                                }
187
                        }
188
                }
189
                return null;
190
        }
191

    
192
        /*
193
         * (non-Javadoc)
194
         * @see com.iver.cit.gvsig.fmap.drivers.gpe.reader.IGPEDriver#setExtent(java.awt.geom.Rectangle2D)
195
         */
196
        public void setExtent(Rectangle2D extent) {
197
                this.extent = extent;                
198
        }
199

    
200
        /*
201
         * (non-Javadoc)
202
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
203
         */
204
        public DriverAttributes getDriverAttributes() {
205
                return null;
206
        }
207

    
208
        /*
209
         * (non-Javadoc)
210
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
211
         */
212
        public Rectangle2D getFullExtent(){
213
                return extent;
214
        }
215

    
216
        /*
217
         * (non-Javadoc)
218
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
219
         */
220
        public IGeometry getShape(int index) {
221
                return ((GPEFeature)features.get(new Integer(index))).getGeometry().getReprojectedGeometry();
222
        }
223

    
224
        /*
225
         * (non-Javadoc)
226
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
227
         */
228
        public int getShapeCount() {
229
                return features.size();
230
        }
231

    
232
        /*
233
         * (non-Javadoc)
234
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
235
         */
236
        public int getShapeType() {
237
                return FShape.MULTI;
238
        }
239

    
240
        /*
241
         * (non-Javadoc)
242
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#isWritable()
243
         */
244
        public boolean isWritable() {
245
                return true;
246
        }
247

    
248
        /*
249
         * (non-Javadoc)
250
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reload()
251
         */
252
        public void reload() {
253
                numFeatures = 0; 
254
                features.clear();
255
                extent = null;
256
        }
257

    
258
        /*
259
         * (non-Javadoc)
260
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
261
         */
262
        public int[] getPrimaryKeys() {
263
                return null;
264
        }
265

    
266
        /*
267
         * (non-Javadoc)
268
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
269
         */
270
        public void write(DataWare dataWare) {                
271

    
272
        }
273

    
274
        /*
275
         * (non-Javadoc)
276
         * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
277
         */
278
        public void setDataSourceFactory(DataSourceFactory dsf) {
279
                // TODO Auto-generated method stub
280

    
281
        }
282

    
283
        /*
284
         * (non-Javadoc)
285
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
286
         */
287
        public int getFieldCount() {
288
                if (features.size() > 0){
289
                        GPEFeature feature = (GPEFeature)features.get(new Integer(0));
290
                        return feature.getelements().size() + 1;
291
                }
292
                return 1;
293
        }
294

    
295
        /*
296
         * (non-Javadoc)
297
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
298
         */
299
        public String getFieldName(int fieldId) {
300
                if (fieldId == getFieldCount()-1){
301
                        return "fid";
302
                }
303
                if (features.size() > 0){
304
                        GPEFeature feature = (GPEFeature)features.get(new Integer(0));
305
                        Iterator it = feature.getelements().keySet().iterator();
306
                        String fieldName = null;
307
                        for (int i=0 ; i<=fieldId ; i++){
308
                                fieldName = (String)it.next();
309
                        }
310
                        return fieldName;
311
                }
312
                return null;
313
        }
314

    
315
        /*
316
         * (non-Javadoc)
317
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
318
         */
319
        public int getFieldType(int i) {
320
                if (i == getFieldCount()-1){
321
                        return Types.VARCHAR;
322
                }
323
                if (getRowCount() > 1){
324
                        Value value = getFieldValue(0,i);
325
                        return value.getSQLType();
326
                }
327
                return Types.VARCHAR;
328
        }
329

    
330
        /*
331
         * (non-Javadoc)
332
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long, int)
333
         */
334
        public Value getFieldValue(long rowIndex, int fieldId) {
335
                GPEFeature feature = (GPEFeature)features.get(new Integer((int)rowIndex));
336
                if (fieldId == getFieldCount()-1){
337
                        return feature.getId();
338
                }
339
                String attName = getFieldName(fieldId);
340
                GPEElement element = (GPEElement)feature.getelements().get(attName);
341
                if (element != null){
342
                        return element.getValue();
343
                }
344
                return ValueFactory.createNullValue();
345
        }
346

    
347
        /*
348
         * (non-Javadoc)
349
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldWidth(int)
350
         */
351
        public int getFieldWidth(int i) {
352
                return 50;
353
        }
354

    
355
        /*
356
         * (non-Javadoc)
357
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
358
         */
359
        public long getRowCount() {
360
                return features.size();
361
        }
362

    
363
        /*
364
         * (non-Javadoc)
365
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds(int)
366
         */
367
        public Rectangle2D getShapeBounds(int index){
368
                return ((GPEFeature)features.get(new Integer(index))).getGeometry().getShapeBounds();
369
        }
370

    
371
        /*
372
         * (non-Javadoc)
373
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
374
         */
375
        public int getShapeType(int index){
376
                return FShape.MULTI;
377
        }
378

    
379
        /*
380
         * (non-Javadoc)
381
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
382
         */
383
        public void close() throws IOException {
384

    
385
        }
386

    
387
        /*
388
         * (non-Javadoc)
389
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFile()
390
         */
391
        public File getFile() {
392
                return m_Fich;
393
        }
394

    
395
        /*
396
         * (non-Javadoc)
397
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
398
         */
399
        public void initialize() throws IOException {
400
                FmapErrorHandler errorHandler = FmapHandlerFactory.createErrorHandler();
401
                DefaultFmapContentHandler contentHandler = FmapHandlerFactory.createContentHandler(errorHandler,
402
                                this);                
403
                GPEParser parser = getParser();
404
                parser.parse(contentHandler,
405
                                errorHandler,
406
                                getFile().toURI());                
407
        }
408

    
409
        /*
410
         * (non-Javadoc)
411
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
412
         */
413
        public void open(File f) throws IOException {
414
                m_Fich = f;
415
        }
416

    
417
        /*
418
         * (non-Javadoc)
419
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
420
         */
421
        public boolean accept(File f) {
422
                if (f.isDirectory()){
423
                        return true;
424
                }
425
                return (getParser().accept(f.toURI()));
426
        }
427
        
428
        /* (non-Javadoc)
429
         * @see com.iver.cit.gvsig.fmap.drivers.gpe.reader.IGPEDriver#getTypeName()
430
         */
431
        public String getTypeName() {
432
                if (features.size() > 0){
433
                        GPEFeature feature = (GPEFeature)features.get(new Integer(0));
434
                        return feature.getName();
435
                }
436
                return null;
437
        }
438
}