svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / shp / ShpFeaturesWriter.java @ 22373
History | View | Annotate | Download (6.06 KB)
1 |
package org.gvsig.fmap.data.feature.file.shp; |
---|---|
2 |
|
3 |
import java.awt.geom.Rectangle2D; |
4 |
import java.io.File; |
5 |
import java.io.IOException; |
6 |
import java.nio.channels.FileChannel; |
7 |
|
8 |
import org.gvsig.fmap.data.DataException; |
9 |
import org.gvsig.fmap.data.OpenException; |
10 |
import org.gvsig.fmap.data.ReadException; |
11 |
import org.gvsig.fmap.data.WriteException; |
12 |
import org.gvsig.fmap.data.feature.Feature; |
13 |
import org.gvsig.fmap.data.feature.FeatureStore; |
14 |
import org.gvsig.fmap.data.feature.FeatureType; |
15 |
import org.gvsig.fmap.data.feature.InitializeWriterException; |
16 |
import org.gvsig.fmap.data.feature.UnsupportedEncodingException; |
17 |
import org.gvsig.fmap.data.feature.file.dbf.DBFFeaturesWriter; |
18 |
import org.gvsig.fmap.data.feature.file.dbf.utils.DbaseFileHeader; |
19 |
import org.gvsig.fmap.data.feature.file.shp.utils.SHP; |
20 |
import org.gvsig.fmap.data.feature.file.shp.utils.SHPFileWrite; |
21 |
import org.gvsig.fmap.geom.Geometry; |
22 |
import org.gvsig.fmap.geom.primitive.DefaultEnvelope; |
23 |
import org.gvsig.fmap.geom.primitive.Envelope; |
24 |
import org.gvsig.fmap.geom.primitive.NullGeometry; |
25 |
|
26 |
public class ShpFeaturesWriter extends DBFFeaturesWriter { |
27 |
private File shpFile; |
28 |
private String shxPath; |
29 |
private String shpPath; |
30 |
private int shapeType; |
31 |
private int gvSIG_geometryType; |
32 |
private SHPFileWrite shpWrite;
|
33 |
private int[] supportedGeometryTypes; |
34 |
private boolean bWriteHeaders=true; |
35 |
private Envelope envelope;
|
36 |
private int fileSize; |
37 |
|
38 |
public void init(FeatureStore store) throws InitializeWriterException { |
39 |
super.init(store);
|
40 |
SHPStoreParameters parameters=(SHPStoreParameters)store.getParameters(); |
41 |
shpFile=parameters.getFile(); |
42 |
setFile(shpFile); |
43 |
|
44 |
try {
|
45 |
shpWrite = new SHPFileWrite((FileChannel) getWriteChannel(shpPath), |
46 |
(FileChannel) super.getWriteChannel(shxPath)); |
47 |
} catch (IOException e) { |
48 |
throw new InitializeWriterException(SHPStore.DATASTORE_NAME,e); |
49 |
} |
50 |
updateFeatureType(store.getDefaultFeatureType()); |
51 |
} |
52 |
|
53 |
public void postProcess() throws OpenException, WriteException { |
54 |
super.postProcess();
|
55 |
if (this.envelope == null) |
56 |
this.envelope = new DefaultEnvelope(2); |
57 |
shpWrite.writeHeaders(this.envelope,
|
58 |
shapeType, numRows, fileSize); |
59 |
} |
60 |
|
61 |
public void preProcess() throws WriteException, ReadException { |
62 |
super.preProcess();
|
63 |
if (bWriteHeaders){
|
64 |
shpWrite.writeHeaders(new DefaultEnvelope(2), |
65 |
shapeType, 0, 0); |
66 |
} |
67 |
this.envelope = null; |
68 |
} |
69 |
|
70 |
public void insertFeature(Feature feature) throws WriteException, UnsupportedEncodingException { |
71 |
|
72 |
Geometry theGeom = (Geometry)feature.getDefaultGeometry(); |
73 |
if (theGeom==null){ |
74 |
FeatureType type = feature.getType(); |
75 |
theGeom=(Geometry) type.get(type.getDefaultGeometry()).getDefaultValue(); |
76 |
if (theGeom == null){ |
77 |
theGeom = new NullGeometry();
|
78 |
} |
79 |
} |
80 |
if (!canWriteGeometry(theGeom.getType())){
|
81 |
throw new WriteException("UnsupportedGeometryType: "+ theGeom.getGeometryType().getName(),SHPStore.DATASTORE_NAME); |
82 |
} |
83 |
super.insertFeature(feature);
|
84 |
fileSize = shpWrite.writeIGeometry(theGeom); |
85 |
Envelope envelope = theGeom.getEnvelope(); |
86 |
if (envelope!=null){ |
87 |
if (this.envelope == null) { |
88 |
this.envelope = envelope;
|
89 |
} else {
|
90 |
this.envelope.add(envelope);
|
91 |
} |
92 |
} |
93 |
|
94 |
} |
95 |
|
96 |
public void updateFeatureType(FeatureType featureType) { |
97 |
super.updateFeatureType(featureType);
|
98 |
int type=featureType.getGeometryTypes()[0]; |
99 |
shapeType = shpWrite.getShapeType(type); |
100 |
gvSIG_geometryType = type; |
101 |
setSupportedGeometryTypes(); |
102 |
} |
103 |
|
104 |
public void setFile(File f) |
105 |
{ |
106 |
shpPath = f.getAbsolutePath(); |
107 |
|
108 |
shxPath = SHP.getShxFile(f).getAbsolutePath(); |
109 |
|
110 |
super.setFile(SHP.getDbfFile(f));
|
111 |
|
112 |
// dbfPath = SHP.getDbfFile(f).getAbsolutePath();
|
113 |
|
114 |
shpFile = f; |
115 |
} |
116 |
private void setSupportedGeometryTypes() { |
117 |
switch (gvSIG_geometryType % Geometry.TYPES.Z)
|
118 |
{ |
119 |
case Geometry.TYPES.POINT:
|
120 |
supportedGeometryTypes = new int[] {Geometry.TYPES.POINT, Geometry.TYPES.NULL }; |
121 |
break;
|
122 |
case Geometry.TYPES.MULTIPOINT:
|
123 |
supportedGeometryTypes = new int[] {Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL }; |
124 |
break;
|
125 |
case Geometry.TYPES.CURVE:
|
126 |
supportedGeometryTypes = new int[] {Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE, |
127 |
Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL }; |
128 |
break;
|
129 |
case Geometry.TYPES.SURFACE:
|
130 |
supportedGeometryTypes = new int[] {Geometry.TYPES.ELLIPSE, |
131 |
Geometry.TYPES.CIRCLE, Geometry.TYPES.SURFACE, Geometry.TYPES.NULL }; |
132 |
break;
|
133 |
|
134 |
default:
|
135 |
supportedGeometryTypes = new int[] {}; |
136 |
} |
137 |
} |
138 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
139 |
for (int i=0; i < supportedGeometryTypes.length; i++) |
140 |
{ |
141 |
if (gvSIGgeometryType == supportedGeometryTypes[i] ||
|
142 |
gvSIGgeometryType == (supportedGeometryTypes[i] | Geometry.TYPES.Z)) |
143 |
return true; |
144 |
} |
145 |
return false; |
146 |
} |
147 |
public static void create(SHPStoreParameters parameters, FeatureType featureType)throws OpenException, WriteException { |
148 |
int fileLength = 100; |
149 |
DbaseFileHeader myHeader = DbaseFileHeader.createDbaseHeader(featureType); |
150 |
try {
|
151 |
DBFFeaturesWriter.create(parameters,featureType); |
152 |
FileChannel shpChannel=(FileChannel) getWriteChannel(parameters.getFile().getAbsolutePath()); |
153 |
FileChannel shxChannel=(FileChannel) getWriteChannel(SHP.getShxFile(parameters.getFile()).getAbsolutePath()); |
154 |
SHPFileWrite shpWrite = new SHPFileWrite(shpChannel,shxChannel);
|
155 |
int shapeType = 0; |
156 |
if (featureType.getGeometryTypes().length > 0){ |
157 |
shapeType= shpWrite.getShapeType(featureType.getGeometryTypes()[0]);
|
158 |
} |
159 |
myHeader.setNumRecords(0);
|
160 |
Envelope envelope = new DefaultEnvelope(2); |
161 |
shpWrite.writeHeaders(envelope, |
162 |
shapeType, 0, fileLength);
|
163 |
shpChannel.close(); |
164 |
shxChannel.close(); |
165 |
} catch (IOException e) { |
166 |
throw new InitializeWriterException("SHP AbstractFeature Writer",e); |
167 |
} |
168 |
} |
169 |
|
170 |
/* (non-Javadoc)
|
171 |
* @see org.gvsig.fmap.data.feature.file.dbf.DBFFeaturesWriter#dispose()
|
172 |
*/
|
173 |
public void dispose() throws DataException { |
174 |
super.dispose();
|
175 |
if (this.shpWrite != null){ |
176 |
this.shpWrite.close();
|
177 |
this.shpWrite = null; |
178 |
} |
179 |
this.envelope = null; |
180 |
|
181 |
} |
182 |
} |