svn-gvsig-desktop / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / shp / ShpFeaturesWriter.java @ 20744
History | View | Annotate | Download (6.16 KB)
1 |
package org.gvsig.data.datastores.vectorial.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.data.datastores.vectorial.file.dbf.DBFFeaturesWriter; |
9 |
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFileHeader; |
10 |
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHP; |
11 |
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHPFileWrite; |
12 |
import org.gvsig.data.exception.DataException; |
13 |
import org.gvsig.data.exception.InitializeWriterException; |
14 |
import org.gvsig.data.exception.OpenException; |
15 |
import org.gvsig.data.exception.ReadException; |
16 |
import org.gvsig.data.exception.UnsupportedEncodingException; |
17 |
import org.gvsig.data.exception.WriteException; |
18 |
import org.gvsig.data.vectorial.IFeature; |
19 |
import org.gvsig.data.vectorial.IFeatureStore; |
20 |
import org.gvsig.data.vectorial.IFeatureType; |
21 |
|
22 |
import com.iver.cit.gvsig.fmap.core.FNullGeometry; |
23 |
import com.iver.cit.gvsig.fmap.core.FShape; |
24 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
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 Rectangle2D fullExtent; |
36 |
private int fileSize; |
37 |
|
38 |
public void init(IFeatureStore 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 (fullExtent == null) |
56 |
fullExtent = new Rectangle2D.Double(); |
57 |
shpWrite.writeHeaders(fullExtent, |
58 |
shapeType, numRows, fileSize); |
59 |
} |
60 |
|
61 |
public void preProcess() throws WriteException, ReadException { |
62 |
super.preProcess();
|
63 |
if (bWriteHeaders){
|
64 |
shpWrite.writeHeaders(new Rectangle2D.Double(), |
65 |
shapeType, 0, 0); |
66 |
} |
67 |
fullExtent = null;
|
68 |
} |
69 |
|
70 |
public void insertFeature(IFeature feature) throws WriteException, UnsupportedEncodingException { |
71 |
/* System.out.println("Intento escribir el registro " +
|
72 |
numRows + " de la capa " + lyrVect.getName()); */
|
73 |
|
74 |
IGeometry theGeom = (IGeometry)feature.getDefaultGeometry(); |
75 |
if (theGeom==null){ |
76 |
theGeom=new FNullGeometry();
|
77 |
} |
78 |
// Revisamos que podemos escribir esa entidad
|
79 |
// En un shpFile, podemos meter pol?gonos, pero que sean como
|
80 |
// lineas. En cambio, en uno de puntos solo se pueden meter puntos
|
81 |
// Con capas de anotaciones ?nicamente se pueden salvar los puntos,
|
82 |
// de momento no hay problema porque est? limitado
|
83 |
// y no se puede tener anotaciones de otro tipo de shape.
|
84 |
if (canWriteGeometry(theGeom.getGeometryType()))// || canWriteGeometry(gvSIG_geometryType)) |
85 |
{ |
86 |
System.out.println(feature.get("NOMBRE")); |
87 |
super.insertFeature(feature);
|
88 |
fileSize = shpWrite.writeIGeometry(theGeom); |
89 |
Rectangle2D boundsShp = theGeom.getBounds2D();
|
90 |
if (boundsShp!=null){ |
91 |
if (fullExtent == null) { |
92 |
fullExtent = boundsShp; |
93 |
} else {
|
94 |
fullExtent.add(boundsShp); |
95 |
} |
96 |
} |
97 |
|
98 |
} |
99 |
} |
100 |
|
101 |
public void updateFeatureType(IFeatureType featureType) { |
102 |
super.updateFeatureType(featureType);
|
103 |
int type=featureType.getGeometryTypes()[0]; |
104 |
shapeType = shpWrite.getShapeType(type); |
105 |
gvSIG_geometryType = type; |
106 |
setSupportedGeometryTypes(); |
107 |
} |
108 |
|
109 |
public void setFile(File f) |
110 |
{ |
111 |
shpPath = f.getAbsolutePath(); |
112 |
|
113 |
shxPath = SHP.getShxFile(f).getAbsolutePath(); |
114 |
|
115 |
super.setFile(SHP.getDbfFile(f));
|
116 |
|
117 |
// dbfPath = SHP.getDbfFile(f).getAbsolutePath();
|
118 |
|
119 |
shpFile = f; |
120 |
} |
121 |
private void setSupportedGeometryTypes() { |
122 |
switch (gvSIG_geometryType % FShape.Z)
|
123 |
{ |
124 |
case FShape.POINT:
|
125 |
supportedGeometryTypes = new int[] {FShape.POINT, FShape.NULL }; |
126 |
break;
|
127 |
case FShape.MULTIPOINT:
|
128 |
supportedGeometryTypes = new int[] {FShape.MULTIPOINT, FShape.NULL }; |
129 |
break;
|
130 |
case FShape.LINE:
|
131 |
supportedGeometryTypes = new int[] {FShape.LINE, FShape.ELLIPSE, |
132 |
FShape.ARC, FShape.CIRCLE, FShape.POLYGON, FShape.NULL }; |
133 |
break;
|
134 |
case FShape.POLYGON:
|
135 |
supportedGeometryTypes = new int[] {FShape.ELLIPSE, |
136 |
FShape.CIRCLE, FShape.POLYGON, FShape.NULL }; |
137 |
break;
|
138 |
|
139 |
default:
|
140 |
supportedGeometryTypes = new int[] {}; |
141 |
} |
142 |
} |
143 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
144 |
for (int i=0; i < supportedGeometryTypes.length; i++) |
145 |
{ |
146 |
if (gvSIGgeometryType == supportedGeometryTypes[i] ||
|
147 |
gvSIGgeometryType == (supportedGeometryTypes[i] | FShape.Z)) |
148 |
return true; |
149 |
} |
150 |
return false; |
151 |
} |
152 |
public static void create(SHPStoreParameters parameters, IFeatureType featureType)throws OpenException, WriteException { |
153 |
int fileLength = 100; |
154 |
DbaseFileHeader myHeader = DbaseFileHeader.createDbaseHeader(featureType); |
155 |
try {
|
156 |
DBFFeaturesWriter.create(parameters,featureType); |
157 |
FileChannel shpChannel=(FileChannel) getWriteChannel(parameters.getFile().getAbsolutePath()); |
158 |
FileChannel shxChannel=(FileChannel) getWriteChannel(SHP.getShxFile(parameters.getFile()).getAbsolutePath()); |
159 |
SHPFileWrite shpWrite = new SHPFileWrite(shpChannel,shxChannel);
|
160 |
|
161 |
int shapeType = shpWrite.getShapeType(featureType.getGeometryTypes()[0]); |
162 |
myHeader.setNumRecords(0);
|
163 |
Rectangle2D fullExtent = new Rectangle2D.Double(); |
164 |
shpWrite.writeHeaders(fullExtent, |
165 |
shapeType, 0, fileLength);
|
166 |
shpChannel.close(); |
167 |
shxChannel.close(); |
168 |
} catch (IOException e) { |
169 |
throw new InitializeWriterException("SHP Feature Writer",e); |
170 |
} |
171 |
} |
172 |
|
173 |
/* (non-Javadoc)
|
174 |
* @see org.gvsig.data.datastores.vectorial.file.dbf.DBFFeaturesWriter#dispose()
|
175 |
*/
|
176 |
public void dispose() throws DataException { |
177 |
super.dispose();
|
178 |
if (this.shpWrite != null){ |
179 |
this.shpWrite.close();
|
180 |
this.shpWrite = null; |
181 |
} |
182 |
this.fullExtent = null; |
183 |
|
184 |
} |
185 |
} |