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