Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / shp / SHPStoreProvider.java @ 25677

History | View | Annotate | Download (9.76 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.Iterator;
31

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

    
60
public class SHPStoreProvider extends DBFStoreProvider {
61
        public static String NAME = "SHPStore";
62
        public static String DESCRIPTION = "SHP file";
63
        private SHPFile shpFile;
64
        private ResourceProvider shpResource;
65
        private SHPStoreParameters shpParams;
66
        protected static final String GEOMETRY_ATTIBUTE_NAME = "GEOMETRY";
67

    
68
        public SHPStoreProvider(SHPStoreParameters params)
69
                        throws InitializeException {
70
                super(params);
71
                this.shpParams = params;
72

    
73
                shpResource = this.createResource(FileResource.NAME,
74
                                new Object[] { this.shpParams.getSHPFileName() });
75
                shpResource.addConsumer(this);
76

    
77
                //DBFResource tmpResource = new DBFResource(dbfParameters);
78
                //
79
                //try {
80
                //        this.dbf = (DBFResource) this.store.addResource(tmpResource);
81
                //} catch (DataException e1) {
82
                //        throw new InitializeException(this.getName(), e1);
83
                //}
84

    
85
                this.shpFile = new SHPFile(this.shpParams);
86
        }
87

    
88
        protected static EditableFeatureAttributeDescriptor addGeometryColumn(
89
                        EditableFeatureType fType) {
90

    
91
                EditableFeatureAttributeDescriptor attr = fType.add(
92
                                GEOMETRY_ATTIBUTE_NAME, DataTypes.GEOMETRY);
93

    
94
                fType.setDefaultGeometryAttributeName(attr.getName());
95
                return attr;
96

    
97
        }
98

    
99
        protected static void removeGeometryColumn(
100
                        EditableFeatureType fType) {
101
                fType.setDefaultGeometryAttributeName(null);
102
                fType.remove(GEOMETRY_ATTIBUTE_NAME);
103
        }
104

    
105
        protected EditableFeatureType getTheFeatureType()
106
                        throws InitializeException {
107
                EditableFeatureType fType = super.getTheFeatureType();
108
                try {
109
                        this.open();
110
                        this.shpResource.begin();
111
                } catch (DataException e) {
112
                        throw new InitializeException(this.getName(), e);
113
                }
114
                try {
115

    
116
                        EditableFeatureAttributeDescriptor attr = addGeometryColumn(
117
                                        fType).setGeometryType(
118
                                        this.shpFile.getGeometryType());
119
                        String srs = this.getSRS(this.shpFile.getSRSParameters());
120
                        if (srs == null){
121
                                // TODO petar ??
122
                                srs = "EPSG:23030";
123
                        }
124
                        attr.setSRS(srs);
125

    
126

    
127
                        return fType;
128
                } catch (ReadException e) {
129
                        throw new InitializeException(e);
130
                } finally {
131
                        this.shpResource.end();
132
                }
133
        }
134

    
135
        private String getSRS(String srsParameters) {
136
                // TODO identificar que SRS hay que usar, ya sea
137
                // el que se recibe de los parametros o el que
138
                // conicida con el que se ha encontrado en el
139
                // prg... y si ninguna de las dos que?
140
                return null;
141
        }
142

    
143
        protected SHPStoreParameters getShpParameters() {
144
                return shpParams;
145
        }
146

    
147
        public String getName() {
148
                return NAME;
149
        }
150

    
151
        public boolean allowWrite() {
152
                return super.allowWrite() && this.shpFile.isEditable();
153
        }
154

    
155
        /**
156
         *
157
         * @param index
158
         * @param featureType
159
         * @return
160
         * @throws ReadException
161
         */
162
        protected FeatureData getFeatureDataByIndex(long index,
163
                        FeatureType featureType) throws DataException {
164
                this.open();
165
                this.shpResource.begin();
166
                try {
167

    
168
                        FeatureData featureData = super.getFeatureDataByIndex(index,
169
                                        featureType);
170
                        featureData.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
171
                        return featureData;
172
                } catch (DataException e) {
173
                        throw e;
174
                } finally {
175
                        this.shpResource.end();
176
                }
177

    
178
        }
179

    
180
        protected void initFeatureDataByIndex(FeatureData featureData,
181
                        long index, FeatureType featureType) throws DataException {
182
                this.open();
183
                this.shpResource.begin();
184
                try {
185
                        super.initFeatureDataByIndex(featureData, index, featureType);
186
                        featureData.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
187
                } finally {
188
                        this.shpResource.end();
189
                }
190

    
191
        }
192

    
193
        /**
194
         *
195
         * @param featureData
196
         * @throws DataException
197
         */
198
        protected void loadFeatureDataByIndex(FeatureData featureData)
199
                        throws DataException {
200
                this.open();
201
                this.shpResource.begin();
202
                try {
203
                        long index = ((Long) featureData.getOID()).longValue();
204
                        super.loadFeatureDataByIndex(featureData);
205
                        if (featureData.getType().getDefaultGeometryAttributeIndex() >= 0) {
206
                                featureData.setDefaultGeometry(this.shpFile.getGeometry(index));
207
                        }
208

    
209
                } finally {
210
                        this.shpResource.end();
211
                }
212
        }
213

    
214

    
215
        protected void loadValue(FeatureData featureData, int rowIndex,
216
                        FeatureAttributeDescriptor descriptor) throws ReadException {
217
                if (descriptor.getDataType() == DataTypes.GEOMETRY) {
218
                        return;
219
                } else {
220
                        super.loadValue(featureData, rowIndex, descriptor);
221
                }
222
        }
223

    
224
        public FeatureData createFeatureData(FeatureType type) throws DataException {
225
                FeatureData data = new SHPFeatureData(this, (DefaultFeatureType) type);
226
                return data;
227
        }
228
        public void open() throws OpenException {
229
                super.open();
230
                if (this.shpFile.isOpen()) {
231
                        return;
232
                }
233
                try {
234
                        this.shpResource.begin();
235
                } catch (ResourceBeginException e) {
236
                        throw new OpenException(this.getName(), e);
237
                }
238
                try {
239
                        this.shpFile.open();
240
                        this.shpResource.notifyOpen();
241

    
242
                } catch (UnsupportedVersionException e) {
243
                        throw new OpenException(this.getName(), e);
244
                } catch (ResourceNotifyOpenException e) {
245
                        throw new OpenException(this.getName(), e);
246
                } catch (FileNotFoundException e) {
247
                        throw new OpenException(this.getName(), e);
248
                } catch (DataException e) {
249
                        throw new OpenException(this.getName(), e);
250
                } finally {
251
                        this.shpResource.end();
252
                }
253
        }
254

    
255

    
256
        public void close() throws CloseException {
257
                super.close();
258
                if (!this.shpFile.isOpen()) {
259
                        return;
260
                }
261
                try {
262
                        this.shpResource.begin();
263
                } catch (ResourceBeginException e) {
264
                        throw new CloseException(this.getName(), e);
265
                }
266
                try {
267
                        this.shpFile.close();
268
                        this.shpResource.notifyClose();
269

    
270
                } catch (ResourceNotifyCloseException e) {
271
                        throw new CloseException(this.getName(), e);
272
                } finally {
273
                        this.shpResource.end();
274
                }
275
        }
276

    
277
        public boolean canWriteGeometry(int geometryType) throws DataException {
278
                this.open();
279
                this.shpResource.begin();
280
                try {
281
                        return this.shpFile.canWriteGeometry(geometryType);
282

    
283
                } finally {
284
                        this.shpResource.end();
285
                }
286
        }
287

    
288
        public void performEditing(Iterator deleteds, Iterator inserteds,
289
                        Iterator updateds) throws PerformEditingException {
290
                FeatureType fType;
291
                try {
292
                        fType = this.store.getDefaultFeatureType();
293
                } catch (DataException e) {
294
                        throw new PerformEditingException(this.getName(), e);
295
                }
296
                // TODO Comprobar el campo de geometria
297

    
298
                EditableFeatureType dbfFtype = fType.getEditable();
299

    
300
                removeGeometryColumn(dbfFtype);
301

    
302
                SHPFeatureWriter writer = null;
303
                try {
304
                        this.shpResource.begin();
305

    
306
                        FeatureSet set = this.store.getFeatureSet();
307

    
308
                        writer = new SHPFeatureWriter(this.getName());
309

    
310
                        writer.begin(this.shpParams, fType, dbfFtype, set.getSize());
311

    
312
                        Iterator iter = set.iterator();
313
                        while (iter.hasNext()) {
314
                                writer.write((Feature) iter.next());
315
                        }
316

    
317
                        writer.end();
318

    
319
                        shpResource.notifyChanges();
320
                } catch (Exception e) {
321
                        throw new PerformEditingException(this.getName(), e);
322
                } finally {
323

    
324
                        shpResource.end();
325
                }
326

    
327

    
328
        }
329

    
330
        public Envelope getEnvelope() throws DataException {
331
                this.open();
332
                this.shpResource.begin();
333
                try {
334
                        return this.shpFile.getFullExtent();
335
                } finally {
336
                        this.shpResource.end();
337
                }
338

    
339
        }
340

    
341
        public void setState(PersistentState state) throws PersistenceException {
342
                // TODO Auto-generated method stub
343
                super.setState(state);
344
        }
345

    
346
        public PersistentState getState() throws PersistenceException {
347
                // TODO Auto-generated method stub
348
                return super.getState();
349
        }
350

    
351
        public void append(Feature feature) {
352
                // TODO Auto-generated method stub
353
                super.append(feature);
354
        }
355

    
356
        public void beginAppend() {
357
                // TODO Auto-generated method stub
358
                super.beginAppend();
359
        }
360

    
361
        public void endAppend() {
362
                // TODO Auto-generated method stub
363
                super.endAppend();
364
        }
365
}