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 / SHPFeatureWriter.java @ 44669

History | View | Annotate | Download (8.35 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.dal.store.shp;
26

    
27
import java.io.File;
28
import java.io.IOException;
29
import java.nio.channels.FileChannel;
30

    
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.exception.WriteException;
36
import org.gvsig.fmap.dal.feature.EditableFeatureType;
37
import org.gvsig.fmap.dal.feature.Feature;
38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39
import org.gvsig.fmap.dal.feature.FeatureType;
40
import org.gvsig.fmap.dal.store.dbf.DBFFeatureWriter;
41
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
42
import org.gvsig.fmap.dal.store.shp.utils.SHPFileWrite;
43
import org.gvsig.fmap.geom.Geometry;
44
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
45
import org.gvsig.fmap.geom.GeometryLocator;
46
import org.gvsig.fmap.geom.GeometryManager;
47
import org.gvsig.fmap.geom.GeometryUtils;
48
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
49
import org.gvsig.fmap.geom.primitive.Envelope;
50
import org.gvsig.fmap.geom.type.GeometryType;
51

    
52
public class SHPFeatureWriter extends DBFFeatureWriter {
53

    
54
    private static final GeometryManager GEOM_MANAGER = GeometryLocator.getGeometryManager();
55
    private static final Logger LOGGER = LoggerFactory.getLogger(SHPFeatureWriter.class);
56

    
57
    private SHPFileWrite shpWrite;
58
    private Envelope envelope = null;
59
    private int[] supportedGeometryTypes;
60
    private int fileSize;
61
    private FeatureType shpFeatureType;
62

    
63
    protected SHPFeatureWriter(String name) {
64
        super(name);
65
    }
66

    
67
    public void begin(DBFStoreParameters dbfParameters, FeatureType featureType, long numRows)
68
        throws DataException {
69
        SHPStoreParameters shpParameters = (SHPStoreParameters) dbfParameters;
70
        File shpFile = shpParameters.getSHPFile();
71
        File shxFile = shpParameters.getSHXFile();
72

    
73
        FileChannel shpChannel = null;
74
        FileChannel shxChannel = null;
75

    
76
        try {
77
            shpChannel =
78
                (FileChannel) getWriteChannel(shpFile.getAbsolutePath());
79
            shxChannel =
80
                (FileChannel) getWriteChannel(shxFile.getAbsolutePath());
81
        } catch (IOException e) {
82
            throw new WriteException(this.name, e);
83
        }
84

    
85
        shpWrite = new SHPFileWrite(shpChannel, shxChannel);
86
        int shapeType = getShapeTypeAndSetSupportedGeometries(featureType);
87
        try {
88
            shpWrite.writeHeaders(
89
                GEOM_MANAGER.createEnvelope(0, 0, 0, 0, featureType.getDefaultGeometryAttribute().getGeomType().getSubType()),
90
                shapeType, 0, 0);
91
        } catch (CreateEnvelopeException e) {
92
            LOGGER.warn("Error creating the envelope", e);
93
        }
94

    
95
        this.shpFeatureType = featureType;
96

    
97
        EditableFeatureType efType = featureType.getEditable();
98
        efType.remove(efType.getDefaultGeometryAttributeIndex());
99
        efType.setDefaultGeometryAttributeName(null);
100
        super.begin(dbfParameters, efType, numRows);
101

    
102
    }
103

    
104
    private int getShapeTypeAndSetSupportedGeometries(FeatureType featureType) {
105

    
106
        FeatureAttributeDescriptor geometryAttr = featureType.getAttributeDescriptor(
107
                featureType.getDefaultGeometryAttributeIndex()
108
        );
109
        int gvSIG_geometryType;
110
        int gvSIG_geometrySubType;
111
        try {
112
            GeometryType geomType = geometryAttr.getGeomType();
113
            gvSIG_geometryType = geomType.getType();
114
            gvSIG_geometrySubType = geomType.getSubType();
115
        } catch(Exception e){
116
            gvSIG_geometryType = geometryAttr.getGeometryType();
117
            gvSIG_geometrySubType = geometryAttr.getGeometrySubType();
118
        }
119
        this.setSupportedGeometryTypes(gvSIG_geometryType);
120
        int shapeType = 0;
121
        shapeType =
122
            shpWrite.getShapeType(gvSIG_geometryType, gvSIG_geometrySubType);
123
        return shapeType;
124
    }
125

    
126
    public void dispose() {
127
        super.dispose();
128
        this.envelope = null;
129
        this.shpWrite = null;
130
    }
131

    
132
    public void end() throws DataException {
133
        if (envelope == null) {
134
            try {
135
                envelope =
136
                    GEOM_MANAGER.createEnvelope(0, 0, 0, 0, SUBTYPES.GEOM2D);
137
            } catch (CreateEnvelopeException e) {
138
                LOGGER.error("Error creating the envelope", e);
139
            }
140
        }
141
        int shapeType = getShapeTypeAndSetSupportedGeometries(shpFeatureType);
142
        shpWrite.writeHeaders(envelope, shapeType, super.getRowCount(),
143
            fileSize);
144
        super.end();
145
        shpWrite.close();
146
    }
147

    
148
    public void append(Feature feature) throws DataException {
149

    
150
        Geometry theGeom = feature.getDefaultGeometry();
151
        if (theGeom != null) {
152
            if (!canWriteGeometry(theGeom.getType())) {
153
                throw new WriteException(this.name, // FIXME Excepcion correcta
154
                    new RuntimeException("UnsupportedGeometryType: "
155
                        + theGeom.getGeometryType().getName()));
156
            }
157
            super.append(feature);
158
            fileSize = shpWrite.writeIGeometry(theGeom);
159
            Envelope envelope = theGeom.getEnvelope();
160
            if (envelope != null) {
161
                if (this.envelope != null) {
162
                    this.envelope.add(envelope);
163
                } else {
164
                    this.envelope = envelope;
165
                }
166
            }
167
        } else {
168
            super.append(feature);
169
            fileSize = shpWrite.writeIGeometry(theGeom);
170
        }
171
    }
172

    
173
    private void setSupportedGeometryTypes(int gvSIG_geometryType) {
174
        if( GeometryUtils.isSubtype(Geometry.TYPES.POINT, gvSIG_geometryType) ) {
175
            supportedGeometryTypes =
176
                new int[] { Geometry.TYPES.POINT, Geometry.TYPES.NULL };
177
            return;
178
        }
179
        if( GeometryUtils.isSubtype(Geometry.TYPES.MULTIPOINT, gvSIG_geometryType) ) {
180
            supportedGeometryTypes =
181
                new int[] { Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL };
182
            return;
183
        }
184
        if( GeometryUtils.isSubtype(Geometry.TYPES.CURVE, gvSIG_geometryType) ) {
185
            supportedGeometryTypes =
186
                new int[] { Geometry.TYPES.MULTICURVE, Geometry.TYPES.CURVE, Geometry.TYPES.NULL };
187
            return;
188
        }
189
        if( GeometryUtils.isSubtype(Geometry.TYPES.MULTICURVE, gvSIG_geometryType) ) {
190
            supportedGeometryTypes =
191
                new int[] { Geometry.TYPES.MULTICURVE, Geometry.TYPES.CURVE, Geometry.TYPES.NULL };
192
            return;
193
        }
194
        if( GeometryUtils.isSubtype(Geometry.TYPES.SURFACE, gvSIG_geometryType) ) {
195
            supportedGeometryTypes =
196
                new int[] { Geometry.TYPES.MULTISURFACE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL };
197
            return;
198
        }
199
        if( GeometryUtils.isSubtype(Geometry.TYPES.MULTISURFACE, gvSIG_geometryType) ) {
200
            supportedGeometryTypes =
201
                new int[] { Geometry.TYPES.MULTISURFACE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL };
202
            return;
203
        }
204
        supportedGeometryTypes = new int[] {};
205
    }
206

    
207
    public boolean canWriteGeometry(int gvSIGgeometryType) {
208
        for (int i = 0; i < supportedGeometryTypes.length; i++) {
209
            if( GeometryUtils.isSubtype(supportedGeometryTypes[i], gvSIGgeometryType)) {
210
                return true;
211
            }
212
        }
213
        return false;
214
    }
215

    
216
//    public void begin(DBFStoreParameters storeParameters,
217
//        FeatureType featureType, long numRows) throws DataException {
218
//        throw new UnsupportedOperationException();
219
//    }
220

    
221
}