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 @ 40435
History | View | Annotate | Download (7.64 KB)
1 | 40435 | jjdelcerro | /* 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.slf4j.Logger; |
||
35 | import org.slf4j.LoggerFactory; |
||
36 | |||
37 | import org.gvsig.fmap.dal.exception.DataException; |
||
38 | import org.gvsig.fmap.dal.exception.WriteException; |
||
39 | import org.gvsig.fmap.dal.feature.Feature; |
||
40 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
42 | import org.gvsig.fmap.dal.store.dbf.DBFFeatureWriter; |
||
43 | import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
||
44 | import org.gvsig.fmap.dal.store.shp.utils.SHPFileWrite; |
||
45 | import org.gvsig.fmap.geom.Geometry; |
||
46 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
47 | import org.gvsig.fmap.geom.GeometryLocator; |
||
48 | import org.gvsig.fmap.geom.GeometryManager; |
||
49 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
50 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
51 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
52 | |||
53 | public class SHPFeatureWriter extends DBFFeatureWriter { |
||
54 | |||
55 | private static final GeometryManager geomManager = GeometryLocator |
||
56 | .getGeometryManager(); |
||
57 | private static final Logger logger = LoggerFactory |
||
58 | .getLogger(GeometryManager.class); |
||
59 | |||
60 | private SHPFileWrite shpWrite;
|
||
61 | private Envelope envelope = null; |
||
62 | private int[] supportedGeometryTypes; |
||
63 | private Geometry defaultGeometry;
|
||
64 | private int fileSize; |
||
65 | private FeatureType shpFeatureType;
|
||
66 | |||
67 | protected SHPFeatureWriter(String name) { |
||
68 | super(name);
|
||
69 | try {
|
||
70 | this.defaultGeometry =
|
||
71 | geomManager.createNullGeometry(SUBTYPES.GEOM2D); |
||
72 | } catch (CreateGeometryException e) {
|
||
73 | logger.error("Creating a NullGeometry", e);
|
||
74 | } |
||
75 | } |
||
76 | |||
77 | public void begin(DBFStoreParameters dbfParameters, |
||
78 | FeatureType featureType, FeatureType dbfFeatureType, long numRows)
|
||
79 | throws DataException {
|
||
80 | SHPStoreParameters shpParameters = (SHPStoreParameters) dbfParameters; |
||
81 | File shpFile = shpParameters.getSHPFile();
|
||
82 | File shxFile = shpParameters.getSHXFile();
|
||
83 | |||
84 | FileChannel shpChannel = null; |
||
85 | FileChannel shxChannel = null; |
||
86 | |||
87 | try {
|
||
88 | shpChannel = |
||
89 | (FileChannel) getWriteChannel(shpFile.getAbsolutePath());
|
||
90 | shxChannel = |
||
91 | (FileChannel) getWriteChannel(shxFile.getAbsolutePath());
|
||
92 | } catch (IOException e) { |
||
93 | throw new WriteException(this.name, e); |
||
94 | } |
||
95 | |||
96 | shpWrite = new SHPFileWrite(shpChannel, shxChannel);
|
||
97 | int shapeType = getShapeTypeAndSetSupportedGeometries(featureType);
|
||
98 | try {
|
||
99 | shpWrite.writeHeaders( |
||
100 | geomManager.createEnvelope(0, 0, 0, 0, SUBTYPES.GEOM2D), |
||
101 | shapeType, 0, 0); |
||
102 | } catch (CreateEnvelopeException e) {
|
||
103 | logger.error("Error creating the envelope", e);
|
||
104 | } |
||
105 | |||
106 | this.shpFeatureType = featureType;
|
||
107 | super.begin(dbfParameters, dbfFeatureType, numRows);
|
||
108 | |||
109 | } |
||
110 | |||
111 | private int getShapeTypeAndSetSupportedGeometries(FeatureType featureType) { |
||
112 | |||
113 | FeatureAttributeDescriptor geometryAttr = |
||
114 | featureType.getAttributeDescriptor(featureType |
||
115 | .getDefaultGeometryAttributeIndex()); |
||
116 | int gvSIG_geometryType = geometryAttr.getGeometryType();
|
||
117 | int gvSIG_geometrySubType = geometryAttr.getGeometrySubType();
|
||
118 | this.setSupportedGeometryTypes(gvSIG_geometryType);
|
||
119 | int shapeType = 0; |
||
120 | shapeType = |
||
121 | shpWrite.getShapeType(gvSIG_geometryType, gvSIG_geometrySubType); |
||
122 | return shapeType;
|
||
123 | } |
||
124 | |||
125 | public void dispose() { |
||
126 | super.dispose();
|
||
127 | this.envelope = null; |
||
128 | this.shpWrite = null; |
||
129 | } |
||
130 | |||
131 | public void end() throws DataException { |
||
132 | if (envelope == null) { |
||
133 | try {
|
||
134 | envelope = |
||
135 | geomManager.createEnvelope(0, 0, 0, 0, SUBTYPES.GEOM2D); |
||
136 | } catch (CreateEnvelopeException e) {
|
||
137 | logger.error("Error creating the envelope", e);
|
||
138 | } |
||
139 | } |
||
140 | int shapeType = getShapeTypeAndSetSupportedGeometries(shpFeatureType);
|
||
141 | shpWrite.writeHeaders(envelope, shapeType, super.getRowCount(),
|
||
142 | fileSize); |
||
143 | super.end();
|
||
144 | shpWrite.close(); |
||
145 | } |
||
146 | |||
147 | public void append(Feature feature) throws DataException { |
||
148 | |||
149 | Geometry theGeom = feature.getDefaultGeometry(); |
||
150 | if (theGeom == null) { |
||
151 | theGeom = this.defaultGeometry;
|
||
152 | } |
||
153 | if (!canWriteGeometry(theGeom.getType())) {
|
||
154 | throw new WriteException(this.name, // FIXME Excepcion correcta |
||
155 | new RuntimeException("UnsupportedGeometryType: " |
||
156 | + theGeom.getGeometryType().getName())); |
||
157 | } |
||
158 | // numRows++;
|
||
159 | super.append(feature);
|
||
160 | |||
161 | // fileSize = shpWrite.writeIGeometry(theGeom);
|
||
162 | fileSize = shpWrite.writeIGeometry(theGeom); |
||
163 | Envelope envelope = theGeom.getEnvelope(); |
||
164 | if (envelope != null) { |
||
165 | if (this.envelope != null) { |
||
166 | this.envelope.add(envelope);
|
||
167 | } else {
|
||
168 | this.envelope = envelope;
|
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
173 | private void setSupportedGeometryTypes(int gvSIG_geometryType) { |
||
174 | switch (gvSIG_geometryType) {
|
||
175 | case Geometry.TYPES.POINT:
|
||
176 | supportedGeometryTypes = |
||
177 | new int[] { Geometry.TYPES.POINT, Geometry.TYPES.NULL }; |
||
178 | break;
|
||
179 | case Geometry.TYPES.MULTIPOINT:
|
||
180 | supportedGeometryTypes = |
||
181 | new int[] { Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL }; |
||
182 | break;
|
||
183 | case Geometry.TYPES.MULTICURVE:
|
||
184 | supportedGeometryTypes = |
||
185 | new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE, |
||
186 | Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE, |
||
187 | Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTICURVE, |
||
188 | Geometry.TYPES.SPLINE, Geometry.TYPES.NULL }; |
||
189 | break;
|
||
190 | case Geometry.TYPES.MULTISURFACE:
|
||
191 | supportedGeometryTypes = |
||
192 | new int[] { Geometry.TYPES.SURFACE, Geometry.TYPES.CIRCLE, |
||
193 | Geometry.TYPES.ELLIPSE, Geometry.TYPES.MULTISURFACE, |
||
194 | Geometry.TYPES.SPLINE, Geometry.TYPES.NULL }; |
||
195 | break;
|
||
196 | |||
197 | default:
|
||
198 | supportedGeometryTypes = new int[] {}; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | public boolean canWriteGeometry(int gvSIGgeometryType) { |
||
203 | for (int i = 0; i < supportedGeometryTypes.length; i++) { |
||
204 | if (gvSIGgeometryType == supportedGeometryTypes[i]) {
|
||
205 | return true; |
||
206 | } |
||
207 | } |
||
208 | return false; |
||
209 | } |
||
210 | |||
211 | public void begin(DBFStoreParameters storeParameters, |
||
212 | FeatureType featureType, long numRows) throws DataException { |
||
213 | throw new UnsupportedOperationException(); |
||
214 | } |
||
215 | |||
216 | } |