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