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 @ 41610

History | View | Annotate | Download (7.34 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.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.store.dbf.DBFFeatureWriter;
40
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
41
import org.gvsig.fmap.dal.store.shp.utils.SHPFileWrite;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
44
import org.gvsig.fmap.geom.GeometryLocator;
45
import org.gvsig.fmap.geom.GeometryManager;
46
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
47
import org.gvsig.fmap.geom.primitive.Envelope;
48

    
49
public class SHPFeatureWriter extends DBFFeatureWriter {
50

    
51
    private static final GeometryManager geomManager = GeometryLocator
52
        .getGeometryManager();
53
    private static final Logger logger = LoggerFactory
54
        .getLogger(GeometryManager.class);
55

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

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

    
66
    public void begin(DBFStoreParameters dbfParameters,
67
        FeatureType featureType, FeatureType dbfFeatureType, 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
                geomManager.createEnvelope(0, 0, 0, 0, SUBTYPES.GEOM2D),
90
                shapeType, 0, 0);
91
        } catch (CreateEnvelopeException e) {
92
            logger.error("Error creating the envelope", e);
93
        }
94

    
95
        this.shpFeatureType = featureType;
96
        super.begin(dbfParameters, dbfFeatureType, numRows);
97

    
98
    }
99

    
100
    private int getShapeTypeAndSetSupportedGeometries(FeatureType featureType) {
101

    
102
        FeatureAttributeDescriptor geometryAttr =
103
            featureType.getAttributeDescriptor(featureType
104
                .getDefaultGeometryAttributeIndex());
105
        int gvSIG_geometryType = geometryAttr.getGeometryType();
106
        int gvSIG_geometrySubType = geometryAttr.getGeometrySubType();
107
        this.setSupportedGeometryTypes(gvSIG_geometryType);
108
        int shapeType = 0;
109
        shapeType =
110
            shpWrite.getShapeType(gvSIG_geometryType, gvSIG_geometrySubType);
111
        return shapeType;
112
    }
113

    
114
    public void dispose() {
115
        super.dispose();
116
        this.envelope = null;
117
        this.shpWrite = null;
118
    }
119

    
120
    public void end() throws DataException {
121
        if (envelope == null) {
122
            try {
123
                envelope =
124
                    geomManager.createEnvelope(0, 0, 0, 0, SUBTYPES.GEOM2D);
125
            } catch (CreateEnvelopeException e) {
126
                logger.error("Error creating the envelope", e);
127
            }
128
        }
129
        int shapeType = getShapeTypeAndSetSupportedGeometries(shpFeatureType);
130
        shpWrite.writeHeaders(envelope, shapeType, super.getRowCount(),
131
            fileSize);
132
        super.end();
133
        shpWrite.close();
134
    }
135

    
136
    public void append(Feature feature) throws DataException {
137

    
138
        Geometry theGeom = feature.getDefaultGeometry();
139
        if (theGeom != null) {
140
            if (!canWriteGeometry(theGeom.getType())) {
141
                throw new WriteException(this.name, // FIXME Excepcion correcta
142
                    new RuntimeException("UnsupportedGeometryType: "
143
                        + theGeom.getGeometryType().getName()));
144
            }
145
            super.append(feature);
146
            fileSize = shpWrite.writeIGeometry(theGeom);
147
            Envelope envelope = theGeom.getEnvelope();
148
            if (envelope != null) {
149
                if (this.envelope != null) {
150
                    this.envelope.add(envelope);
151
                } else {
152
                    this.envelope = envelope;
153
                }
154
            }
155
        } else {
156
            super.append(feature);
157
            fileSize = shpWrite.writeIGeometry(theGeom);
158
        }
159
    }
160

    
161
    private void setSupportedGeometryTypes(int gvSIG_geometryType) {
162
        switch (gvSIG_geometryType) {
163
        case Geometry.TYPES.POINT:
164
            supportedGeometryTypes =
165
                new int[] { Geometry.TYPES.POINT, Geometry.TYPES.NULL };
166
            break;
167
        case Geometry.TYPES.MULTIPOINT:
168
            supportedGeometryTypes =
169
                new int[] { Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL };
170
            break;
171
        case Geometry.TYPES.MULTICURVE:
172
            supportedGeometryTypes =
173
                new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE,
174
                    Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE,
175
                    Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTICURVE,
176
                    Geometry.TYPES.SPLINE, Geometry.TYPES.NULL };
177
            break;
178
        case Geometry.TYPES.MULTISURFACE:
179
            supportedGeometryTypes =
180
                new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE,
181
                    Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE,
182
                    Geometry.TYPES.SPLINE, Geometry.TYPES.NULL };
183
            break;
184

    
185
        default:
186
            supportedGeometryTypes = new int[] {};
187
        }
188
    }
189

    
190
    public boolean canWriteGeometry(int gvSIGgeometryType) {
191
        for (int i = 0; i < supportedGeometryTypes.length; i++) {
192
            if (gvSIGgeometryType == supportedGeometryTypes[i]) {
193
                return true;
194
            }
195
        }
196
        return false;
197
    }
198

    
199
    public void begin(DBFStoreParameters storeParameters,
200
        FeatureType featureType, long numRows) throws DataException {
201
        throw new UnsupportedOperationException();
202
    }
203

    
204
}