Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / SHPStoreProvider.java @ 42574

History | View | Annotate | Download (21.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.store.shp;
24

    
25
import java.io.File;
26
import java.io.IOException;
27
import java.util.Iterator;
28

    
29
import org.apache.commons.io.FileUtils;
30
import org.apache.commons.io.FilenameUtils;
31
import org.cresques.cts.ICRSFactory;
32
import org.cresques.cts.IProjection;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36
import org.gvsig.fmap.dal.DataStore;
37
import org.gvsig.fmap.dal.DataTypes;
38
import org.gvsig.fmap.dal.FileHelper;
39
import org.gvsig.fmap.dal.exception.CloseException;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.exception.InitializeException;
42
import org.gvsig.fmap.dal.exception.OpenException;
43
import org.gvsig.fmap.dal.exception.ReadException;
44
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
45
import org.gvsig.fmap.dal.feature.EditableFeatureType;
46
import org.gvsig.fmap.dal.feature.Feature;
47
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
48
import org.gvsig.fmap.dal.feature.FeatureSet;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.dal.feature.FeatureType;
51
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
52
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
53
import org.gvsig.fmap.dal.resource.ResourceAction;
54
import org.gvsig.fmap.dal.resource.exception.ResourceException;
55
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
56
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
57
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
58
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
59
import org.gvsig.fmap.dal.resource.file.FileResource;
60
import org.gvsig.fmap.dal.resource.spi.MultiResource;
61
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
62
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
63
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
64
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider;
65
import org.gvsig.fmap.dal.store.shp.utils.ISHPFile;
66
import org.gvsig.fmap.dal.store.shp.utils.SHP;
67
import org.gvsig.fmap.dal.store.shp.utils.SHPFile2;
68
import org.gvsig.fmap.geom.Geometry;
69
import org.gvsig.fmap.geom.GeometryLocator;
70
import org.gvsig.fmap.geom.GeometryManager;
71
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
72
import org.gvsig.fmap.geom.exception.CreateGeometryException;
73
import org.gvsig.fmap.geom.primitive.Envelope;
74
import org.gvsig.tools.dispose.DisposableIterator;
75
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
76
import org.gvsig.tools.exception.BaseException;
77

    
78
/**
79
 *
80
 */
81
public class SHPStoreProvider extends DBFStoreProvider {
82

    
83
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
84
    private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class);
85
    public static String NAME = "Shape";
86
    public static String DESCRIPTION = "Shape file";
87
    private ISHPFile shpFile;
88

    
89
    private MultiResource resource;
90

    
91
    protected static final String GEOMETRY_ATTIBUTE_NAME = "GEOMETRY";
92

    
93
    public static final String METADATA_DEFINITION_NAME = NAME;
94

    
95
    private SHPFeatureWriter writer = null;
96

    
97
    private boolean loTengoEnUso;
98

    
99
    /**
100
     * @param params
101
     * @param storeServices
102
     * @throws InitializeException
103
     */
104
    public SHPStoreProvider(SHPStoreParameters params, DataStoreProviderServices storeServices)
105
        throws InitializeException {
106
        super(params, storeServices, FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME));
107
    }
108

    
109
    protected void init(DBFStoreParameters params, DataStoreProviderServices storeServices) throws InitializeException {
110

    
111
        this.shpFile = new SHPFile2((SHPStoreParameters) params);
112
        super.init(params, storeServices);
113
        this.shpFile.setUseNullGeometry(this.getShpParameters().getUseNullGeometry());
114
    }
115

    
116
    public Object getDynValue(String name) throws DynFieldNotFoundException {
117
        if (DataStore.METADATA_CRS.equalsIgnoreCase(name)) {
118

    
119
            return this.getShpParameters().getCRS();
120

    
121
        } else if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) {
122
            try {
123
                return this.shpFile.getFullExtent();
124
            } catch (ReadException e) {
125
                return null;
126
            }
127
        }
128
        return super.getDynValue(name);
129
    }
130

    
131
    protected void initResource(DBFStoreParameters params, DataStoreProviderServices storeServices)
132
        throws InitializeException {
133

    
134
        SHPStoreParameters shpParams = (SHPStoreParameters) params;
135
        resource = (MultiResource) createResource(MultiResource.TYPE_NAME, new Object[] { shpParams.getSHPFileName() });
136

    
137
        resource.addResource(FileResource.NAME, new Object[] { shpParams.getSHPFileName() }, true);
138
        resource.addResource(FileResource.NAME, new Object[] { shpParams.getSHXFileName() }, true);
139
        resource.addResource(FileResource.NAME, new Object[] { shpParams.getDBFFileName() }, true);
140

    
141
        resource.frozen();
142
        resource.addMultiResourceConsumer(this);
143
    }
144

    
145
    ;
146

    
147
    public ResourceProvider getResource() {
148
        return resource;
149
    }
150

    
151
    /**
152
     *
153
     * @throws ResourceNotifyChangesException
154
     */
155
    protected void resourcesNotifyChanges() throws ResourceNotifyChangesException {
156
        getResource().notifyChanges();
157
        // TODO .prj
158

    
159
    }
160

    
161
    /**
162
     * @throws ResourceNotifyCloseException
163
     *
164
     */
165
    protected void resourcesNotifyClose() throws ResourceNotifyCloseException {
166
        getResource().notifyClose();
167
        // TODO .prj
168

    
169
    }
170

    
171
    @Override
172
    protected void doDispose() throws BaseException {
173
        super.doDispose();
174
        getResource().removeConsumer(this);
175
        this.writer = null;
176
        this.shpFile = null;
177
    }
178

    
179
    protected void disposeResource() {
180
        getResource().removeConsumer(this);
181
    }
182

    
183
    /**
184
     * @throws ResourceNotifyOpenException
185
     *
186
     */
187
    protected void resourcesOpen() throws ResourceNotifyOpenException {
188
        getResource().notifyOpen();
189
    }
190

    
191
    protected static EditableFeatureAttributeDescriptor addGeometryColumn(EditableFeatureType fType) {
192

    
193
        EditableFeatureAttributeDescriptor attrTmp = null;
194
        EditableFeatureAttributeDescriptor attr = null;
195
        Iterator<?> iter = fType.iterator();
196
        while (iter.hasNext()) {
197
            attrTmp = (EditableFeatureAttributeDescriptor) iter.next();
198
            if (attrTmp.getType() == DataTypes.GEOMETRY) {
199
                if (attr != null) {
200
                    // Two geom fields not allowed
201
                    fType.remove(attrTmp.getName());
202
                } else {
203
                    attr = attrTmp;
204
                }
205
            }
206
        }
207

    
208
        if (attr == null) {
209
            String geofield = createGeometryFieldName(fType);
210
            attr = fType.add(geofield, DataTypes.GEOMETRY);
211
            attr.setDefaultValue(null);
212
        }
213

    
214
        attr.setObjectClass(Geometry.class);
215
        fType.setDefaultGeometryAttributeName(attr.getName());
216
        return attr;
217

    
218
    }
219

    
220
    private static String createGeometryFieldName(FeatureType ft) {
221

    
222
        if (ft.getAttributeDescriptor(GEOMETRY_ATTIBUTE_NAME) == null) {
223
            return GEOMETRY_ATTIBUTE_NAME;
224
        }
225

    
226
        int i = 0;
227
        String candidate = GEOMETRY_ATTIBUTE_NAME + i;
228
        while (ft.getAttributeDescriptor(candidate) != null) {
229
            i++;
230
            candidate = GEOMETRY_ATTIBUTE_NAME + i;
231
        }
232
        return candidate;
233
    }
234

    
235
    protected static FeatureType removeGeometryColumn(EditableFeatureType fType) {
236
        Iterator<?> iter = fType.iterator();
237
        FeatureAttributeDescriptor attr;
238
        while (iter.hasNext()) {
239
            attr = (FeatureAttributeDescriptor) iter.next();
240
            if (attr.getType() == DataTypes.GEOMETRY) {
241
                iter.remove();
242
            }
243
        }
244
        fType.setDefaultGeometryAttributeName(null);
245
        return fType.getNotEditableCopy();
246
    }
247

    
248
    protected EditableFeatureType getTheFeatureType() throws InitializeException, OpenException {
249
        final EditableFeatureType fType = super.getTheFeatureType();
250
        this.open();
251
        try {
252
            getResource().execute(new ResourceAction() {
253

    
254
                public Object run() throws Exception {
255
                    EditableFeatureAttributeDescriptor attr = addGeometryColumn(fType);
256

    
257
                    attr.setGeometryType(geomManager.getGeometryType(shpFile.getGeometryType(),
258
                        shpFile.getGeometrySubType()));
259

    
260
                    IProjection srs = getShpParameters().getCRS();
261
                    attr.setSRS(srs);
262

    
263
                    return null;
264
                }
265
            });
266
            return fType;
267
        } catch (ResourceExecuteException e) {
268
            throw new InitializeException(e);
269
        }
270
    }
271

    
272
    protected SHPStoreParameters getShpParameters() {
273
        return (SHPStoreParameters) getParameters();
274
    }
275

    
276
    public String getProviderName() {
277
        return NAME;
278
    }
279

    
280
    public boolean allowWrite() {
281
        return this.shpFile.isEditable();
282
    }
283

    
284
    /**
285
     *
286
     * @param index
287
     * @param featureType
288
     * @return
289
     * @throws ReadException
290
     */
291
    protected FeatureProvider getFeatureProviderByIndex(long index, FeatureType featureType) throws DataException {
292
        this.open();
293
        try {
294

    
295
            FeatureProvider featureProvider = super.getFeatureProviderByIndex(index, featureType);
296
            featureProvider.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
297
            return featureProvider;
298
        } catch (DataException e) {
299
            throw e;
300
        } catch (CreateEnvelopeException e) {
301
            throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e);
302
        } catch (CreateGeometryException e) {
303
            throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e);
304
        }
305

    
306
    }
307

    
308
    protected void initFeatureProviderByIndex(FeatureProvider featureProvider, long index, FeatureType featureType)
309
        throws DataException {
310
        try {
311
            super.initFeatureProviderByIndex(featureProvider, index, featureType);
312
            featureProvider.setDefaultEnvelope(this.shpFile.getBoundingBox(index));
313
        } catch (CreateEnvelopeException e) {
314
            throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e);
315
        } catch (CreateGeometryException e) {
316
            throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e);
317
        }
318
    }
319

    
320
    /**
321
     *
322
     * @param featureProvider
323
     * @throws DataException
324
     */
325
    protected void loadFeatureProviderByIndex(FeatureProvider featureProvider) throws DataException {
326

    
327
        FeatureType featureType = featureProvider.getType();
328
        long index = ((Long) featureProvider.getOID()).longValue();
329
        boolean hasGeometry = false;
330
        int i = featureType.getDefaultGeometryAttributeIndex();
331
        if (i >= 0) {
332
            if (!featureProvider.isReadOnly(i)) {
333
                try {
334
                    Geometry geom = this.shpFile.getGeometry(index);
335
                    featureProvider.set(i, geom);
336
                } catch (CreateGeometryException e) {
337
                    throw new ReadException(getProviderName(), e);
338
                }
339
            }
340
            hasGeometry = true;
341
        }
342
        if (hasDBFAttributes(featureType, hasGeometry)) {
343
            super.loadFeatureProviderByIndex(featureProvider);
344
        }
345

    
346
    }
347

    
348
    private boolean hasDBFAttributes(FeatureType featureType, boolean hasGeometry) {
349
        FeatureAttributeDescriptor[] attributes = featureType.getAttributeDescriptors();
350
        // If there aren't any attributes, nor has any DBF attributes
351
        if (attributes == null || attributes.length == 0) {
352
            return false;
353
        }
354
        // If there is only one attribute and it is the geometry one
355
        if (attributes.length == 1 && hasGeometry) {
356
            return false;
357
        }
358
        // In any other case
359
        return true;
360
    }
361

    
362
    protected void loadValue(FeatureProvider featureProvider, int rowIndex, FeatureAttributeDescriptor descriptor)
363
        throws ReadException {
364
        if (descriptor.getType() == DataTypes.GEOMETRY) {
365
            return;
366
        } else {
367
            super.loadValue(featureProvider, rowIndex, descriptor);
368
        }
369
    }
370

    
371
    public FeatureProvider createFeatureProvider(FeatureType type) throws DataException {
372
        FeatureProvider data = new SHPFeatureProvider(this, type);
373
        return data;
374
    }
375

    
376
    protected void openFile() throws IOException, DataException {
377
        super.openFile();
378
        this.shpFile.open();
379

    
380
    }
381

    
382
    protected void closeFile() throws CloseException {
383
        super.closeFile();
384
        if (!this.shpFile.isOpen()) {
385
            return;
386
        }
387
        this.shpFile.close();
388
    }
389

    
390
    public boolean canWriteGeometry(final int geometryType, int geometrySubType) throws DataException {
391
        this.open();
392
        return ((Boolean) getResource().execute(new ResourceAction() {
393

    
394
            public Object run() throws Exception {
395
                boolean value = shpFile.canWriteGeometry(geometryType);
396
                return value ? Boolean.TRUE : Boolean.FALSE;
397
            }
398
        })).booleanValue();
399
    }
400

    
401
    @SuppressWarnings("rawtypes")
402
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds,
403
        Iterator originalFeatureTypesUpdated) throws PerformEditingException {
404

    
405
        /*
406
         * This will throw an exception if there are new fields
407
         * with names too long
408
         */
409
        checkNewFieldsNameSize(originalFeatureTypesUpdated);
410

    
411
        final FeatureType fType;
412
        try {
413
            fType = this.getStoreServices().getDefaultFeatureType();
414
        } catch (DataException e) {
415
            throw new PerformEditingException(this.getProviderName(), e);
416
        }
417
        // TODO Comprobar el campo de geometria
418

    
419
        final EditableFeatureType dbfFtype = fType.getEditable();
420

    
421
        removeGeometryColumn(dbfFtype);
422

    
423
        try {
424
            // TODO repasar el concepto de enUso de un recurso.
425
            loTengoEnUso = true;
426
            resourceCloseRequest();
427

    
428
            getResource().execute(new ResourceAction() {
429

    
430
                public Object run() throws Exception {
431
                    FeatureSet set = null;
432
                    DisposableIterator iter = null;
433
                    try {
434
                        set = getFeatureStore().getFeatureSet();
435
                        writer = new SHPFeatureWriter(getProviderName());
436

    
437
                        SHPStoreParameters shpParams = getShpParameters();
438
                        SHPStoreParameters tmpParams = (SHPStoreParameters) shpParams.getCopy();
439

    
440
                        File tmp_base = File.createTempFile("tmp_" + System.currentTimeMillis(), null);
441
                        String str_base = tmp_base.getCanonicalPath();
442

    
443
                        tmpParams.setDBFFile(str_base + ".dbf");
444
                        tmpParams.setSHPFile(str_base + ".shp");
445
                        tmpParams.setSHXFile(str_base + ".shx");
446

    
447
                        writer.begin(tmpParams, fType, dbfFtype, set.getSize());
448

    
449
                        iter = set.fastIterator();
450
                        while (iter.hasNext()) {
451
                            Feature feature = (Feature) iter.next();
452
                            writer.append(feature);
453
                        }
454

    
455
                        writer.end();
456
                        loTengoEnUso = false;
457
                        close();
458

    
459
                        File tmpPrjFile = SHP.getPrjFile(tmpParams.getSHPFile());
460
                        try {
461
                            FileUtils.writeStringToFile(tmpPrjFile,
462
                                tmpParams.getCRS().export(ICRSFactory.FORMAT_WKT_ESRI));
463
                        } catch (Exception e) {
464
                            logger.info("Can't write prj file '" + tmpPrjFile.getAbsolutePath() + "'.");
465
                        }
466

    
467
                        if (!shpParams.getDBFFile().delete()) {
468
                            logger.debug("Can't delete dbf file '" + shpParams.getDBFFile() + "'.");
469
                            throw new IOException("Can't delete dbf '"
470
                                + FilenameUtils.getBaseName(shpParams.getDBFFileName())
471
                                + "' file to replace with the new dbf.\nThe new dbf is in temporary file '" + str_base
472
                                + "'");
473
                        }
474
                        if (!shpParams.getSHPFile().delete()) {
475
                            logger.debug("Can't delete dbf file '" + shpParams.getSHPFile() + "'.");
476
                            throw new IOException("Can't delete shp '"
477
                                + FilenameUtils.getBaseName(shpParams.getSHPFileName())
478
                                + "' file to replace with the new shp.\nThe new shp is in temporary file '" + str_base
479
                                + "'");
480
                        }
481
                        if (!shpParams.getSHXFile().delete()) {
482
                            logger.debug("Can't delete dbf file '" + shpParams.getSHXFile() + "'.");
483
                            throw new IOException("Can't delete shx '"
484
                                + FilenameUtils.getBaseName(shpParams.getSHXFileName())
485
                                + "' file to replace with the new shx.\nThe new shx is in temporary file '" + str_base
486
                                + "'");
487
                        }
488

    
489
                        File prjFile = SHP.getPrjFile(shpParams.getSHPFile());
490
                        if (prjFile.exists()) {
491
                            if (!prjFile.delete()) {
492
                                logger.debug("Can't delete prj file '" + prjFile + "'.");
493
                                throw new IOException("Can't delete shx '"
494
                                    + FilenameUtils.getBaseName(prjFile.getPath())
495
                                    + "' file to replace with the new shx.\nThe new shx is in temporary file '"
496
                                    + str_base + "'");
497
                            }
498
                        }
499
                        FileUtils.moveFile(tmpParams.getDBFFile(), shpParams.getDBFFile());
500
                        FileUtils.moveFile(tmpParams.getSHPFile(), shpParams.getSHPFile());
501
                        FileUtils.moveFile(tmpParams.getSHXFile(), shpParams.getSHXFile());
502

    
503
                        FileUtils.moveFile(tmpPrjFile, SHP.getPrjFile(shpParams.getSHPFile()));
504

    
505
                        resourcesNotifyChanges();
506
                        initFeatureType();
507
                        return null;
508
                    } finally {
509
                        loTengoEnUso = false;
510
                        dispose(set);
511
                        dispose(iter);
512
                    }
513
                }
514
            });
515

    
516
        } catch (Exception e) {
517
            throw new PerformEditingException(this.getProviderName(), e);
518
        }
519

    
520
    }
521

    
522
    protected void resourceCloseRequest() throws ResourceException {
523
        getResource().closeRequest();
524
    }
525

    
526
    public Envelope getEnvelope() throws DataException {
527
        this.open();
528
        return (Envelope) this.getDynValue("Envelope");
529
    }
530

    
531
    public void append(final FeatureProvider featureProvider) throws DataException {
532
        getResource().execute(new ResourceAction() {
533

    
534
            public Object run() throws Exception {
535
                writer.append(getStoreServices().createFeature(featureProvider));
536
                return null;
537
            }
538
        });
539
    }
540

    
541
    public void beginAppend() throws DataException {
542
        getResource().execute(new ResourceAction() {
543

    
544
            public Object run() throws Exception {
545
                FeatureStore store = getFeatureStore();
546
                FeatureType fType = store.getDefaultFeatureType();
547

    
548
                // TODO Comprobar el campo de geometria
549
                EditableFeatureType dbfFtype = fType.getEditable();
550

    
551
                removeGeometryColumn(dbfFtype);
552
                FeatureSet set = store.getFeatureSet();
553

    
554
                writer = new SHPFeatureWriter(getProviderName());
555

    
556
                writer.begin(getShpParameters(), fType, dbfFtype, set.getSize());
557
                return null;
558
            }
559
        });
560
    }
561

    
562
    public void endAppend() throws DataException {
563
        getResource().execute(new ResourceAction() {
564

    
565
            public Object run() throws Exception {
566
                writer.end();
567
                close();
568

    
569
                SHPStoreParameters shpParameters = SHPStoreProvider.this.getShpParameters();
570
                File prjFile = SHP.getPrjFile(shpParameters.getFile());
571
                try {
572
                    FileUtils.writeStringToFile(prjFile, shpParameters.getCRS().export(ICRSFactory.FORMAT_WKT_ESRI));
573
                } catch (Exception e) {
574
                    logger.info("Can't write prj file '" + prjFile.getAbsolutePath() + "'.");
575
                }
576

    
577
                resourcesNotifyChanges();
578
                return null;
579
            }
580
        });
581
    }
582

    
583
    public Object getSourceId() {
584
        return this.getShpParameters().getFile();
585
    }
586
}