Statistics
| Revision:

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

History | View | Annotate | Download (6.65 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.io.File;
31
import java.io.IOException;
32
import java.nio.channels.FileChannel;
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.FeatureType;
38
import org.gvsig.fmap.dal.store.dbf.DBFFeatureWriter;
39
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
40
import org.gvsig.fmap.dal.store.shp.utils.SHPFileWrite;
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.fmap.geom.GeometryLocator;
43
import org.gvsig.fmap.geom.GeometryManager;
44
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
45
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
46
import org.gvsig.fmap.geom.exception.CreateGeometryException;
47
import org.gvsig.fmap.geom.primitive.Envelope;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
public class SHPFeatureWriter extends DBFFeatureWriter {
52
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
53
        private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class);
54

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

    
62

    
63
        protected SHPFeatureWriter(String name) {
64
                super(name);
65
                try {
66
                        this.defaultGeometry = geomManager.createNullGeometry(SUBTYPES.GEOM2D);
67
                } catch (CreateGeometryException e) {
68
                        logger.error("Creating a NullGeometry", e);
69
                }
70
        }
71

    
72
        public void begin(DBFStoreParameters dbfParameters,
73
                        FeatureType featureType, FeatureType dbfFeatureType, long numRows)
74
                        throws DataException {
75
                SHPStoreParameters shpParameters = (SHPStoreParameters) dbfParameters;
76
                File shpFile = shpParameters.getSHPFile();
77
                File shxFile = shpParameters.getSHXFile();
78

    
79
                FileChannel shpChannel = null;
80
                FileChannel shxChannel = null;
81

    
82
                try {
83
                        shpChannel = (FileChannel) getWriteChannel(shpFile
84
                                        .getAbsolutePath());
85
                        shxChannel = (FileChannel) getWriteChannel(shxFile
86
                                        .getAbsolutePath());
87
                } catch (IOException e) {
88
                        throw new WriteException(this.name, e);
89
                }
90

    
91
                shpWrite = new SHPFileWrite(shpChannel, shxChannel);
92
                int gvSIG_geometryType =featureType.getAttributeDescriptor(
93
                                featureType.getDefaultGeometryAttributeName())
94
                                .getGeometryType();
95
                int gvSIG_geometrySubType = featureType.getAttributeDescriptor(
96
                                featureType.getDefaultGeometryAttributeName())
97
                                .getGeometrySubType();
98
                this.setSupportedGeometryTypes(gvSIG_geometryType);
99
                int shapeType = 0;
100
                shapeType = shpWrite
101
                                .getShapeType(gvSIG_geometryType, gvSIG_geometrySubType);
102
                try {
103
                        shpWrite.writeHeaders(geomManager.createEnvelope(SUBTYPES.GEOM2D),
104
                                                shapeType, 0, 0);
105
                } catch (CreateEnvelopeException e) {
106
                        logger.error("Error creating the envelope", e);
107
                }
108

    
109
                this.shpFeatureType = featureType;
110
                super.begin(dbfParameters, dbfFeatureType, numRows);
111

    
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 = geomManager.createEnvelope(SUBTYPES.GEOM2D);
124
                                } catch (CreateEnvelopeException e) {
125
                                        logger.error("Error creating the envelope", e);
126
                                }
127
                        }
128
                        int gvSIG_geometryType = shpFeatureType.getAttributeDescriptor(
129
                                shpFeatureType.getDefaultGeometryAttributeIndex())
130
                                        .getGeometryType();
131
                        int gvSIG_geometrySubType = shpFeatureType.getAttributeDescriptor(
132
                                        shpFeatureType.getDefaultGeometryAttributeIndex())
133
                                                .getGeometryType();
134
                        this.setSupportedGeometryTypes(gvSIG_geometryType);
135
                        int shapeType = shpWrite
136
                                        .getShapeType(gvSIG_geometryType, gvSIG_geometrySubType);
137
                        shpWrite.writeHeaders(envelope,
138
                                        shapeType, super.getRowCount(),
139
                                fileSize);
140
                        super.end();
141
                        shpWrite.close();
142
        }
143

    
144
        public void append(Feature feature) throws DataException {
145

    
146
                Geometry theGeom = feature.getDefaultGeometry();
147
                if (theGeom==null){
148
                        theGeom=this.defaultGeometry;
149
                }
150
                if (!canWriteGeometry(theGeom.getType())){
151
                        throw new WriteException(this.name, // FIXME Excepcion correcta
152
                                        new RuntimeException("UnsupportedGeometryType: "+ theGeom.getGeometryType().getName()));
153
                }
154
//                numRows++;
155
                super.append(feature);
156

    
157
//                fileSize = shpWrite.writeIGeometry(theGeom);
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
        }
168

    
169
        private void setSupportedGeometryTypes(int gvSIG_geometryType) {
170
                switch (gvSIG_geometryType)
171
                {
172
                case Geometry.TYPES.POINT:
173
                        supportedGeometryTypes = new int[] {Geometry.TYPES.POINT, Geometry.TYPES.NULL };
174
                        break;
175
                case Geometry.TYPES.MULTIPOINT:
176
                        supportedGeometryTypes = new int[] {Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL };
177
                        break;
178
                case Geometry.TYPES.MULTICURVE:
179
                        supportedGeometryTypes = new int[] {Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE,
180
                                Geometry.TYPES.ARC,
181
                                        Geometry.TYPES.CIRCLE, Geometry.TYPES.MULTICURVE,
182
                                        Geometry.TYPES.NULL };
183
                        break;
184
                case Geometry.TYPES.MULTISURFACE:
185
                        supportedGeometryTypes = new int[] { Geometry.TYPES.SURFACE,
186
                                        Geometry.TYPES.CIRCLE,        Geometry.TYPES.MULTISURFACE, 
187
                                        Geometry.TYPES.NULL };
188
                        break;
189

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

    
204
        public void begin(DBFStoreParameters storeParameters,
205
                        FeatureType featureType, long numRows) throws DataException {
206
                throw new UnsupportedOperationException();
207
        }
208

    
209
}