Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / shp / SHPStoreProvider.java @ 25789

History | View | Annotate | Download (11.9 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 IVER T.I. S.A.   {{Task}}
26
*/
27

    
28
package org.gvsig.fmap.dal.store.shp;
29

    
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.Iterator;
33

    
34
import org.gvsig.fmap.dal.DataTypes;
35
import org.gvsig.fmap.dal.exception.CloseException;
36
import org.gvsig.fmap.dal.exception.DataException;
37
import org.gvsig.fmap.dal.exception.FileNotFoundException;
38
import org.gvsig.fmap.dal.exception.InitializeException;
39
import org.gvsig.fmap.dal.exception.OpenException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.dal.exception.UnsupportedVersionException;
42
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
43
import org.gvsig.fmap.dal.feature.EditableFeatureType;
44
import org.gvsig.fmap.dal.feature.Feature;
45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
46
import org.gvsig.fmap.dal.feature.FeatureSet;
47
import org.gvsig.fmap.dal.feature.FeatureType;
48
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
49
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureType;
50
import org.gvsig.fmap.dal.feature.spi.FeatureData;
51
import org.gvsig.fmap.dal.resource.exception.ResourceBeginException;
52
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
53
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
54
import org.gvsig.fmap.dal.resource.file.FileResource;
55
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
56
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider;
57
import org.gvsig.fmap.dal.store.shp.utils.SHPFile;
58
import org.gvsig.fmap.geom.primitive.Envelope;
59
import org.gvsig.tools.ToolsLocator;
60
import org.gvsig.tools.dynobject.DelegatedDynObject;
61
import org.gvsig.tools.dynobject.DynClass;
62
import org.gvsig.tools.dynobject.DynField;
63
import org.gvsig.tools.dynobject.DynObjectManager;
64
import org.gvsig.tools.persistence.PersistenceException;
65
import org.gvsig.tools.persistence.PersistentState;
66

    
67
public class SHPStoreProvider extends DBFStoreProvider {
68
        public static String NAME = "SHPStore";
69
        public static String DESCRIPTION = "SHP file";
70
        private SHPFile shpFile;
71
        private ResourceProvider shpResource;
72
        private SHPStoreParameters shpParams;
73
        protected static final String GEOMETRY_ATTIBUTE_NAME = "GEOMETRY";
74
        protected static final String DYNFIELD_SRSORIGINALPARAMETERS_NAME = "SRSOriginalParameters";
75
        private static final String DYNCLASS_NAME = "SHPStore";
76
        protected static DynClass DYNCLASS = null;
77

    
78
        private SHPFeatureWriter writer = null;
79

    
80
        public SHPStoreProvider(SHPStoreParameters params)
81
                        throws InitializeException {
82
                super(params);
83
                this.shpParams = params;
84

    
85
                shpResource = this.createResource(FileResource.NAME,
86
                                new Object[] { this.shpParams.getSHPFileName() });
87
                shpResource.addConsumer(this);
88

    
89
                // FIXME
90
                this.dynObject = this.dynObject = (DelegatedDynObject) ToolsLocator
91
                                .getDynObjectManager().createDynObject(DYNCLASS);
92

    
93
                this.shpFile = new SHPFile(this.shpParams);
94
        }
95

    
96
        protected static void registerDynClass() {
97
                DynObjectManager dynman = ToolsLocator.getDynObjectManager();
98
                DynClass dynClass;
99
                DynField field;
100
                if (DYNCLASS == null) {
101

    
102
                        DynClass dbfDynClass = DBFStoreProvider.DYNCLASS;
103
                        dynClass = dynman.add(DYNCLASS_NAME);
104

    
105

    
106
                        field = dynClass.addDynField(DYNFIELD_SRSORIGINALPARAMETERS_NAME);
107

    
108
                        field.setType(DataTypes.STRING);
109
                        field.setDescription("SRS original parameters");
110

    
111
            // The SHP store parameters extend the DBF store parameters
112
                        dynClass.extend(dbfDynClass);
113

    
114
                        DYNCLASS = dynClass;
115
                }
116

    
117
        }
118

    
119

    
120
        protected static EditableFeatureAttributeDescriptor addGeometryColumn(
121
                        EditableFeatureType fType) {
122

    
123
                EditableFeatureAttributeDescriptor attr = fType.add(
124
                                GEOMETRY_ATTIBUTE_NAME, DataTypes.GEOMETRY);
125

    
126
                fType.setDefaultGeometryAttributeName(attr.getName());
127
                return attr;
128

    
129
        }
130

    
131
        protected static void removeGeometryColumn(
132
                        EditableFeatureType fType) {
133
                fType.setDefaultGeometryAttributeName(GEOMETRY_ATTIBUTE_NAME);
134
                fType.remove(GEOMETRY_ATTIBUTE_NAME);
135
        }
136

    
137
        protected EditableFeatureType getTheFeatureType()
138
                        throws InitializeException {
139
                EditableFeatureType fType = super.getTheFeatureType();
140
                try {
141
                        this.open();
142
                        this.shpResource.begin();
143
                } catch (DataException e) {
144
                        throw new InitializeException(this.getName(), e);
145
                }
146
                try {
147

    
148
                        EditableFeatureAttributeDescriptor attr = addGeometryColumn(
149
                                        fType).setGeometryType(
150
                                        this.shpFile.getGeometryType());
151
                        String srs = this.getSRS(this.shpFile.getSRSParameters());
152
                        if (srs == null){
153
                                // TODO petar ??
154
                                srs = "EPSG:23030";
155
                        }
156
                        attr.setSRS(srs);
157

    
158

    
159
                        return fType;
160
                } catch (ReadException e) {
161
                        throw new InitializeException(e);
162
                } finally {
163
                        this.shpResource.end();
164
                }
165
        }
166

    
167
        private String getSRS(String srsParameters) {
168
                // TODO identificar que SRS hay que usar, ya sea
169
                // el que se recibe de los parametros o el que
170
                // conicida con el que se ha encontrado en el
171
                // prg... y si ninguna de las dos que?
172
                return null;
173
        }
174

    
175
        protected SHPStoreParameters getShpParameters() {
176
                return shpParams;
177
        }
178

    
179
        public String getName() {
180
                return NAME;
181
        }
182

    
183
        public boolean allowWrite() {
184
                return super.allowWrite() && this.shpFile.isEditable();
185
        }
186

    
187
        /**
188
         *
189
         * @param index
190
         * @param featureType
191
         * @return
192
         * @throws ReadException
193
         */
194
        protected FeatureData getFeatureDataByIndex(long index,
195
                        FeatureType featureType) throws DataException {
196
                this.open();
197
                this.shpResource.begin();
198
                try {
199

    
200
                        FeatureData featureData = super.getFeatureDataByIndex(index,
201
                                        featureType);
202
                        featureData.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
203
                        return featureData;
204
                } catch (DataException e) {
205
                        throw e;
206
                } finally {
207
                        this.shpResource.end();
208
                }
209

    
210
        }
211

    
212
        protected void initFeatureDataByIndex(FeatureData featureData,
213
                        long index, FeatureType featureType) throws DataException {
214
                this.open();
215
                this.shpResource.begin();
216
                try {
217
                        super.initFeatureDataByIndex(featureData, index, featureType);
218
                        featureData.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
219
                } finally {
220
                        this.shpResource.end();
221
                }
222

    
223
        }
224

    
225
        /**
226
         *
227
         * @param featureData
228
         * @throws DataException
229
         */
230
        protected void loadFeatureDataByIndex(FeatureData featureData)
231
                        throws DataException {
232
                this.open();
233
                this.shpResource.begin();
234
                try {
235
                        long index = ((Long) featureData.getOID()).longValue();
236
                        super.loadFeatureDataByIndex(featureData);
237
                        if (featureData.getType().getDefaultGeometryAttributeIndex() >= 0) {
238
                                featureData.setDefaultGeometry(this.shpFile.getGeometry(index));
239
                        }
240

    
241
                } finally {
242
                        this.shpResource.end();
243
                }
244
        }
245

    
246

    
247
        protected void loadValue(FeatureData featureData, int rowIndex,
248
                        FeatureAttributeDescriptor descriptor) throws ReadException {
249
                if (descriptor.getDataType() == DataTypes.GEOMETRY) {
250
                        return;
251
                } else {
252
                        super.loadValue(featureData, rowIndex, descriptor);
253
                }
254
        }
255

    
256
        public FeatureData createFeatureData(FeatureType type) throws DataException {
257
                FeatureData data = new SHPFeatureData(this, (DefaultFeatureType) type);
258
                return data;
259
        }
260
        public void open() throws OpenException {
261
                super.open();
262
                if (this.shpFile.isOpen()) {
263
                        return;
264
                }
265
                try {
266
                        this.shpResource.begin();
267
                } catch (ResourceBeginException e) {
268
                        throw new OpenException(this.getName(), e);
269
                }
270
                try {
271
                        this.shpFile.open();
272
                        this.shpResource.notifyOpen();
273

    
274

    
275
                        // load metadata
276
                        this.dynObject.setDynValue(
277
                                        DYNFIELD_SRSORIGINALPARAMETERS_NAME,
278
                                        this.shpFile.getSRSParameters());
279

    
280
                        this.dynObject.setDynValue("Envelope", shpFile.getFullExtent());
281

    
282
                } catch (UnsupportedVersionException e) {
283
                        throw new OpenException(this.getName(), e);
284
                } catch (ResourceNotifyOpenException e) {
285
                        throw new OpenException(this.getName(), e);
286
                } catch (FileNotFoundException e) {
287
                        throw new OpenException(this.getName(), e);
288
                } catch (DataException e) {
289
                        throw new OpenException(this.getName(), e);
290
                } finally {
291
                        this.shpResource.end();
292
                }
293
        }
294

    
295

    
296
        public void close() throws CloseException {
297
                super.close();
298
                if (!this.shpFile.isOpen()) {
299
                        return;
300
                }
301
                try {
302
                        this.shpResource.begin();
303
                } catch (ResourceBeginException e) {
304
                        throw new CloseException(this.getName(), e);
305
                }
306
                try {
307
                        this.shpFile.close();
308
                        this.shpResource.notifyClose();
309

    
310
                } catch (ResourceNotifyCloseException e) {
311
                        throw new CloseException(this.getName(), e);
312
                } finally {
313
                        this.shpResource.end();
314
                }
315
        }
316

    
317
        public boolean canWriteGeometry(int geometryType) throws DataException {
318
                this.open();
319
                this.shpResource.begin();
320
                try {
321
                        return this.shpFile.canWriteGeometry(geometryType);
322

    
323
                } finally {
324
                        this.shpResource.end();
325
                }
326
        }
327

    
328
        public void performEditing(Iterator deleteds, Iterator inserteds,
329
                        Iterator updateds) throws PerformEditingException {
330
                FeatureType fType;
331
                try {
332
                        fType = this.store.getDefaultFeatureType();
333
                } catch (DataException e) {
334
                        throw new PerformEditingException(this.getName(), e);
335
                }
336
                // TODO Comprobar el campo de geometria
337

    
338
                EditableFeatureType dbfFtype = fType.getEditable();
339

    
340
                removeGeometryColumn(dbfFtype);
341

    
342

    
343
                try {
344
                        FeatureSet set = this.store.getFeatureSet();
345
//                        this.shpResource.begin();
346
                        writer = new SHPFeatureWriter(this.getName());
347

    
348
                        writer.begin(this.shpParams, fType, dbfFtype, set.getSize());
349

    
350
//                        Iterator iter1 = set.iterator();
351
//                        ArrayList<Feature> features=new ArrayList<Feature>();
352
//                        while (iter1.hasNext()) {
353
//                                features.add((Feature) iter1.next());
354
//                        }
355
//                        for (int i = features.size()-1; i >=0; i--) {
356
//                                writer.write(features.get(i));
357
//                        }
358
                        Iterator iter = set.iterator();
359
                        while (iter.hasNext()) {
360
                                Feature feature=(Feature) iter.next();
361
                                writer.write(feature);
362
                        }
363

    
364
                        writer.end();
365

    
366
                        shpResource.notifyChanges();
367
                } catch (Exception e) {
368
                        throw new PerformEditingException(this.getName(), e);
369
                } finally {
370

    
371
//                        shpResource.end();
372
                }
373

    
374

    
375
        }
376

    
377
        public Envelope getEnvelope() throws DataException {
378
                this.open();
379
                this.shpResource.begin();
380
                try {
381
                        return this.shpFile.getFullExtent();
382
                } finally {
383
                        this.shpResource.end();
384
                }
385

    
386
        }
387

    
388
        public void setState(PersistentState state) throws PersistenceException {
389
                // TODO Auto-generated method stub
390
                super.setState(state);
391
        }
392

    
393
        public PersistentState getState() throws PersistenceException {
394
                // TODO Auto-generated method stub
395
                return super.getState();
396
        }
397

    
398
        public void append(Feature feature) {
399
                try {
400
                        writer.write(feature);
401
                } catch (DataException e) {
402
                        // TODO Auto-generated catch block
403
                        e.printStackTrace();
404
                }
405
        }
406

    
407
        public void beginAppend() {
408
                try {
409
                        FeatureType fType = this.store.getDefaultFeatureType();
410

    
411
                        // TODO Comprobar el campo de geometria
412

    
413
                        EditableFeatureType dbfFtype = fType.getEditable();
414

    
415
                        removeGeometryColumn(dbfFtype);
416
                        FeatureSet set = this.store.getFeatureSet();
417

    
418
                        writer = new SHPFeatureWriter(this.getName());
419

    
420
                        writer.begin(this.shpParams, fType, dbfFtype, set.getSize());
421
                } catch (DataException e) {
422
                        // TODO Auto-generated catch block
423
                        e.printStackTrace();
424
                }
425

    
426
        }
427

    
428
        public void endAppend() {
429
                try {
430
                        writer.end();
431
                        shpResource.notifyChanges();
432
                } catch (DataException e) {
433
                        // TODO Auto-generated catch block
434
                        e.printStackTrace();
435
                }
436
        }
437

    
438
        /*
439
         * (non-Javadoc)
440
         * 
441
         * @see org.gvsig.metadata.Metadata#getMetadataName()
442
         */
443
        public String getMetadataName() {
444
                return this.shpParams.getSHPFile().getName();
445
        }
446

    
447
}