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

History | View | Annotate | Download (7.69 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
 * AUTHORS (In addition to CIT):
26
 * 2008 IVER T.I. S.A.   {{Task}}
27
 */
28

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

    
31
import java.io.File;
32
import java.io.IOException;
33
import java.nio.channels.FileChannel;
34

    
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.fmap.dal.exception.DataException;
39
import org.gvsig.fmap.dal.exception.WriteException;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.store.dbf.DBFFeatureWriter;
44
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
45
import org.gvsig.fmap.dal.store.shp.utils.SHPFileWrite;
46
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.GeometryManager;
50
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
51
import org.gvsig.fmap.geom.exception.CreateGeometryException;
52
import org.gvsig.fmap.geom.primitive.Envelope;
53

    
54
public class SHPFeatureWriter extends DBFFeatureWriter {
55

    
56
    private static final GeometryManager geomManager = GeometryLocator
57
        .getGeometryManager();
58
    private static final Logger logger = LoggerFactory
59
        .getLogger(GeometryManager.class);
60

    
61
    private SHPFileWrite shpWrite;
62
    private Envelope envelope = null;
63
    private int[] supportedGeometryTypes;
64
    private Geometry defaultGeometry;
65
    private int fileSize;
66
    private FeatureType shpFeatureType;
67

    
68
    protected SHPFeatureWriter(String name) {
69
        super(name);
70
        try {
71
            this.defaultGeometry =
72
                geomManager.createNullGeometry(SUBTYPES.GEOM2D);
73
        } catch (CreateGeometryException e) {
74
            logger.error("Creating a NullGeometry", e);
75
        }
76
    }
77

    
78
    public void begin(DBFStoreParameters dbfParameters,
79
        FeatureType featureType, FeatureType dbfFeatureType, long numRows)
80
        throws DataException {
81
        SHPStoreParameters shpParameters = (SHPStoreParameters) dbfParameters;
82
        File shpFile = shpParameters.getSHPFile();
83
        File shxFile = shpParameters.getSHXFile();
84

    
85
        FileChannel shpChannel = null;
86
        FileChannel shxChannel = null;
87

    
88
        try {
89
            shpChannel =
90
                (FileChannel) getWriteChannel(shpFile.getAbsolutePath());
91
            shxChannel =
92
                (FileChannel) getWriteChannel(shxFile.getAbsolutePath());
93
        } catch (IOException e) {
94
            throw new WriteException(this.name, e);
95
        }
96

    
97
        shpWrite = new SHPFileWrite(shpChannel, shxChannel);
98
        int shapeType = getShapeTypeAndSetSupportedGeometries(featureType);
99
        try {
100
            shpWrite.writeHeaders(
101
                geomManager.createEnvelope(0, 0, 0, 0, SUBTYPES.GEOM2D),
102
                shapeType, 0, 0);
103
        } catch (CreateEnvelopeException e) {
104
            logger.error("Error creating the envelope", e);
105
        }
106

    
107
        this.shpFeatureType = featureType;
108
        super.begin(dbfParameters, dbfFeatureType, numRows);
109

    
110
    }
111

    
112
    private int getShapeTypeAndSetSupportedGeometries(FeatureType featureType) {
113

    
114
        FeatureAttributeDescriptor geometryAttr =
115
            featureType.getAttributeDescriptor(featureType
116
                .getDefaultGeometryAttributeIndex());
117
        int gvSIG_geometryType = geometryAttr.getGeometryType();
118
        int gvSIG_geometrySubType = geometryAttr.getGeometrySubType();
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
                    geomManager.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
            theGeom = this.defaultGeometry;
153
        }
154
        if (!canWriteGeometry(theGeom.getType())) {
155
            throw new WriteException(this.name, // FIXME Excepcion correcta
156
                new RuntimeException("UnsupportedGeometryType: "
157
                    + theGeom.getGeometryType().getName()));
158
        }
159
        // numRows++;
160
        super.append(feature);
161

    
162
        // fileSize = shpWrite.writeIGeometry(theGeom);
163
        fileSize = shpWrite.writeIGeometry(theGeom);
164
        Envelope envelope = theGeom.getEnvelope();
165
        if (envelope != null) {
166
            if (this.envelope != null) {
167
                this.envelope.add(envelope);
168
            } else {
169
                this.envelope = envelope;
170
            }
171
        }
172
    }
173

    
174
    private void setSupportedGeometryTypes(int gvSIG_geometryType) {
175
        switch (gvSIG_geometryType) {
176
        case Geometry.TYPES.POINT:
177
            supportedGeometryTypes =
178
                new int[] { Geometry.TYPES.POINT, Geometry.TYPES.NULL };
179
            break;
180
        case Geometry.TYPES.MULTIPOINT:
181
            supportedGeometryTypes =
182
                new int[] { Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL };
183
            break;
184
        case Geometry.TYPES.MULTICURVE:
185
            supportedGeometryTypes =
186
                new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE,
187
                    Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE,
188
                    Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTICURVE,
189
                    Geometry.TYPES.SPLINE, Geometry.TYPES.NULL };
190
            break;
191
        case Geometry.TYPES.MULTISURFACE:
192
            supportedGeometryTypes =
193
                new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE,
194
                    Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE,
195
                    Geometry.TYPES.SPLINE, Geometry.TYPES.NULL };
196
            break;
197

    
198
        default:
199
            supportedGeometryTypes = new int[] {};
200
        }
201
    }
202

    
203
    public boolean canWriteGeometry(int gvSIGgeometryType) {
204
        for (int i = 0; i < supportedGeometryTypes.length; i++) {
205
            if (gvSIGgeometryType == supportedGeometryTypes[i]) {
206
                return true;
207
            }
208
        }
209
        return false;
210
    }
211

    
212
    public void begin(DBFStoreParameters storeParameters,
213
        FeatureType featureType, long numRows) throws DataException {
214
        throw new UnsupportedOperationException();
215
    }
216

    
217
}