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 / utils / SHPFileWrite.java @ 40435
History | View | Annotate | Download (9.6 KB)
1 | 40435 | jjdelcerro | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package org.gvsig.fmap.dal.store.shp.utils; |
||
42 | |||
43 | import java.io.IOException; |
||
44 | import java.nio.ByteBuffer; |
||
45 | import java.nio.ByteOrder; |
||
46 | import java.nio.channels.FileChannel; |
||
47 | |||
48 | import org.gvsig.fmap.dal.exception.WriteException; |
||
49 | import org.gvsig.fmap.geom.Geometry; |
||
50 | import org.gvsig.fmap.geom.GeometryLocator; |
||
51 | import org.gvsig.fmap.geom.GeometryManager; |
||
52 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
53 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
54 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
55 | import org.slf4j.Logger; |
||
56 | import org.slf4j.LoggerFactory; |
||
57 | |||
58 | |||
59 | |||
60 | /**
|
||
61 | * DOCUMENT ME!
|
||
62 | *
|
||
63 | * @author Vicente Caballero Navarro
|
||
64 | */
|
||
65 | public class SHPFileWrite { |
||
66 | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
67 | private static final Logger logger = LoggerFactory.getLogger(SHPFileWrite.class); |
||
68 | private SHPShape m_shape = null; |
||
69 | private ByteBuffer m_bb = null; |
||
70 | private ByteBuffer m_indexBuffer = null; |
||
71 | private int m_pos = 0; |
||
72 | private int m_offset; |
||
73 | // private int m_type;
|
||
74 | private int m_cnt; |
||
75 | private FileChannel shpChannel; |
||
76 | private FileChannel shxChannel; |
||
77 | // private double flatness;
|
||
78 | |||
79 | /**
|
||
80 | * Crea un nuevo SHPFileWrite.
|
||
81 | *
|
||
82 | * @param shpChannel DOCUMENT ME!
|
||
83 | * @param shxChannel DOCUMENT ME!
|
||
84 | */
|
||
85 | public SHPFileWrite(FileChannel shpChannel, FileChannel shxChannel) { |
||
86 | this.shpChannel = shpChannel;
|
||
87 | this.shxChannel = shxChannel;
|
||
88 | } |
||
89 | |||
90 | /**
|
||
91 | * Make sure our buffer is of size.
|
||
92 | *
|
||
93 | * @param size DOCUMENT ME!
|
||
94 | */
|
||
95 | private void checkShapeBuffer(int size) { |
||
96 | if (m_bb.capacity() < size) {
|
||
97 | m_bb = ByteBuffer.allocateDirect(size);
|
||
98 | } |
||
99 | } |
||
100 | |||
101 | /**
|
||
102 | * Drain internal buffers into underlying channels.
|
||
103 | * @throws WriteException
|
||
104 | *
|
||
105 | * @throws IOException DOCUMENT ME!
|
||
106 | */
|
||
107 | private void drain() throws WriteException { |
||
108 | m_bb.flip(); |
||
109 | m_indexBuffer.flip(); |
||
110 | try{
|
||
111 | while (m_bb.remaining() > 0) { |
||
112 | shpChannel.write(m_bb); |
||
113 | } |
||
114 | |||
115 | while (m_indexBuffer.remaining() > 0) { |
||
116 | shxChannel.write(m_indexBuffer); |
||
117 | } |
||
118 | }catch (IOException e) { |
||
119 | throw new WriteException("SHP File Write Drain", e); |
||
120 | } |
||
121 | m_bb.flip().limit(m_bb.capacity()); |
||
122 | m_indexBuffer.flip().limit(m_indexBuffer.capacity()); |
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * DOCUMENT ME!
|
||
127 | */
|
||
128 | private void allocateBuffers() { |
||
129 | m_bb = ByteBuffer.allocateDirect(16 * 1024); |
||
130 | m_indexBuffer = ByteBuffer.allocateDirect(100); |
||
131 | } |
||
132 | |||
133 | /**
|
||
134 | * Close the underlying Channels.
|
||
135 | */
|
||
136 | public void close() throws WriteException { |
||
137 | try {
|
||
138 | shpChannel.close(); |
||
139 | shxChannel.close(); |
||
140 | } catch (IOException e) { |
||
141 | throw new WriteException("SHP File Write Close", e); |
||
142 | } |
||
143 | shpChannel = null;
|
||
144 | shxChannel = null;
|
||
145 | m_shape = null;
|
||
146 | |||
147 | if (m_indexBuffer instanceof ByteBuffer) { |
||
148 | if (m_indexBuffer != null) { |
||
149 | ///NIOUtilities.clean(m_indexBuffer);
|
||
150 | } |
||
151 | } |
||
152 | |||
153 | if (m_indexBuffer instanceof ByteBuffer) { |
||
154 | if (m_indexBuffer != null) { |
||
155 | ///NIOUtilities.clean(m_bb);
|
||
156 | } |
||
157 | } |
||
158 | |||
159 | m_indexBuffer = null;
|
||
160 | m_bb = null;
|
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * DOCUMENT ME!
|
||
165 | *
|
||
166 | * @param geometries DOCUMENT ME!
|
||
167 | * @param type DOCUMENT ME!
|
||
168 | *
|
||
169 | */
|
||
170 | public void write(Geometry[] geometries, int type) |
||
171 | throws WriteException {
|
||
172 | m_shape = SHP.create(type); |
||
173 | // m_shape.setFlatness(flatness);
|
||
174 | writeHeaders(geometries, type); |
||
175 | |||
176 | m_pos = m_bb.position(); |
||
177 | |||
178 | for (int i = 0, ii = geometries.length; i < ii; i++) { |
||
179 | writeGeometry(geometries[i]); |
||
180 | } |
||
181 | |||
182 | close(); |
||
183 | } |
||
184 | |||
185 | /**
|
||
186 | * DOCUMENT ME!
|
||
187 | *
|
||
188 | * @param geometries DOCUMENT ME!
|
||
189 | * @param type DOCUMENT ME!
|
||
190 | * @throws WriteException
|
||
191 | *
|
||
192 | */
|
||
193 | private void writeHeaders(Geometry[] geometries, int type) |
||
194 | throws WriteException {
|
||
195 | int fileLength = 100; |
||
196 | Envelope envelope = null;
|
||
197 | try {
|
||
198 | envelope = geomManager.createEnvelope(SUBTYPES.GEOM2D); |
||
199 | } catch (CreateEnvelopeException e) {
|
||
200 | logger.error("Error creating the envelope", e);
|
||
201 | } |
||
202 | |||
203 | for (int i = geometries.length - 1; i >= 0; i--) { |
||
204 | Geometry fgeometry = geometries[i]; |
||
205 | m_shape.obtainsPoints(fgeometry); |
||
206 | int size = m_shape.getLength(fgeometry) + 8; |
||
207 | fileLength += size; |
||
208 | envelope.add(fgeometry.getEnvelope()); |
||
209 | } |
||
210 | |||
211 | writeHeaders(envelope, type, geometries.length, fileLength); |
||
212 | } |
||
213 | |||
214 | /**
|
||
215 | * Writes shape header (100 bytes)
|
||
216 | *
|
||
217 | * @param bounds DOCUMENT ME!
|
||
218 | * @param type DOCUMENT ME!
|
||
219 | * @param numberOfGeometries DOCUMENT ME!
|
||
220 | * @param fileLength DOCUMENT ME!
|
||
221 | */
|
||
222 | public void writeHeaders(Envelope bounds, int type, |
||
223 | int numberOfGeometries,
|
||
224 | int fileLength) throws WriteException { |
||
225 | /*try {
|
||
226 | handler = type.getShapeHandler();
|
||
227 | } catch (ShapefileException se) {
|
||
228 | throw new RuntimeException("unexpected Exception",se);
|
||
229 | }
|
||
230 | */
|
||
231 | if (m_bb == null) { |
||
232 | allocateBuffers(); |
||
233 | } |
||
234 | // Posicionamos al principio.
|
||
235 | m_bb.position(0);
|
||
236 | m_indexBuffer.position(0);
|
||
237 | |||
238 | ShapeFileHeader2 header = new ShapeFileHeader2();
|
||
239 | |||
240 | header.write(m_bb, type, numberOfGeometries, fileLength / 2,
|
||
241 | bounds |
||
242 | .getMinimum(0), bounds.getMinimum(1), bounds.getMaximum(0), |
||
243 | bounds.getMaximum(1), 0, 0, 0, 0); |
||
244 | |||
245 | header.write(m_indexBuffer, type, numberOfGeometries, |
||
246 | 50 + (4 * numberOfGeometries), bounds.getMinimum(0), bounds.getMinimum(1), |
||
247 | bounds.getMaximum(0), bounds.getMaximum(1), 0, 0, 0, 0); |
||
248 | |||
249 | m_offset = 50;
|
||
250 | // m_type = type;
|
||
251 | m_cnt = 0;
|
||
252 | |||
253 | try {
|
||
254 | shpChannel.position(0);
|
||
255 | shxChannel.position(0);
|
||
256 | } catch (IOException e) { |
||
257 | throw new WriteException("SHP File Write Headers", e); |
||
258 | } |
||
259 | drain(); |
||
260 | } |
||
261 | public int writeIGeometry(Geometry g) throws WriteException { |
||
262 | int shapeType = getShapeType(g.getType(), g.getGeometryType().getSubType());
|
||
263 | m_shape = SHP.create(shapeType); |
||
264 | // m_shape.setFlatness(flatness);
|
||
265 | // System.out.println("writeIGeometry: type="+ g.getType());
|
||
266 | return writeGeometry(g);
|
||
267 | } |
||
268 | |||
269 | /**
|
||
270 | * Writes a single Geometry.
|
||
271 | *
|
||
272 | * @param g
|
||
273 | * @return the position of buffer (after the last geometry, it will allow you to
|
||
274 | * write the file size in the header.
|
||
275 | */
|
||
276 | public synchronized int writeGeometry(Geometry g) |
||
277 | throws WriteException {
|
||
278 | if (m_bb == null) { |
||
279 | allocateBuffers(); |
||
280 | m_offset = 50;
|
||
281 | m_cnt = 0;
|
||
282 | |||
283 | try {
|
||
284 | shpChannel.position(0);
|
||
285 | shxChannel.position(0);
|
||
286 | } catch (IOException e) { |
||
287 | throw new WriteException("SHP File Write", e); |
||
288 | } |
||
289 | // throw new IOException("Must write headers first");
|
||
290 | } |
||
291 | |||
292 | m_pos = m_bb.position(); |
||
293 | m_shape.obtainsPoints(g); |
||
294 | int length = m_shape.getLength(g);
|
||
295 | |||
296 | // must allocate enough for shape + header (2 ints)
|
||
297 | checkShapeBuffer(length + 8);
|
||
298 | |||
299 | length /= 2;
|
||
300 | |||
301 | m_bb.order(ByteOrder.BIG_ENDIAN);
|
||
302 | m_bb.putInt(++m_cnt); |
||
303 | m_bb.putInt(length); |
||
304 | m_bb.order(ByteOrder.LITTLE_ENDIAN);
|
||
305 | m_bb.putInt(m_shape.getShapeType()); |
||
306 | m_shape.write(m_bb, g); |
||
307 | |||
308 | ///assert (length * 2 == (m_bb.position() - m_pos) - 8);
|
||
309 | m_pos = m_bb.position(); |
||
310 | |||
311 | // write to the shx
|
||
312 | m_indexBuffer.putInt(m_offset); |
||
313 | m_indexBuffer.putInt(length); |
||
314 | m_offset += (length + 4);
|
||
315 | drain(); |
||
316 | |||
317 | ///assert(m_bb.position() == 0);
|
||
318 | return m_pos; // Devolvemos hasta donde hemos escrito |
||
319 | } |
||
320 | |||
321 | /**
|
||
322 | * Returns a shapeType compatible with shapeFile constants from a gvSIG's IGeometry type
|
||
323 | * @param geometryType
|
||
324 | * @return a shapeType compatible with shapeFile constants from a gvSIG's IGeometry type
|
||
325 | */
|
||
326 | public int getShapeType(int geometryType, int geometrySubType) { |
||
327 | |||
328 | if (geometrySubType == Geometry.SUBTYPES.GEOM3D){
|
||
329 | switch (geometryType) {
|
||
330 | case Geometry.TYPES.POINT:
|
||
331 | return SHP.POINT3D;
|
||
332 | |||
333 | case Geometry.TYPES.CURVE:
|
||
334 | case Geometry.TYPES.MULTICURVE:
|
||
335 | case Geometry.TYPES.ELLIPSE:
|
||
336 | case Geometry.TYPES.CIRCLE:
|
||
337 | case Geometry.TYPES.ARC:
|
||
338 | case Geometry.TYPES.SPLINE:
|
||
339 | return SHP.POLYLINE3D;
|
||
340 | |||
341 | case Geometry.TYPES.SURFACE:
|
||
342 | case Geometry.TYPES.MULTISURFACE:
|
||
343 | return SHP.POLYGON3D;
|
||
344 | |||
345 | case Geometry.TYPES.MULTIPOINT:
|
||
346 | return SHP.MULTIPOINT3D; //TODO falta aclarar cosas aqu?. |
||
347 | } |
||
348 | |||
349 | }else{
|
||
350 | switch (geometryType) {
|
||
351 | case Geometry.TYPES.POINT:
|
||
352 | return SHP.POINT2D;
|
||
353 | |||
354 | case Geometry.TYPES.CURVE:
|
||
355 | case Geometry.TYPES.MULTICURVE:
|
||
356 | case Geometry.TYPES.ELLIPSE:
|
||
357 | case Geometry.TYPES.CIRCLE:
|
||
358 | case Geometry.TYPES.ARC:
|
||
359 | case Geometry.TYPES.SPLINE:
|
||
360 | return SHP.POLYLINE2D;
|
||
361 | |||
362 | case Geometry.TYPES.SURFACE:
|
||
363 | case Geometry.TYPES.MULTISURFACE:
|
||
364 | return SHP.POLYGON2D;
|
||
365 | |||
366 | case Geometry.TYPES.MULTIPOINT:
|
||
367 | return SHP.MULTIPOINT2D; //TODO falta aclarar cosas aqu?. |
||
368 | } |
||
369 | } |
||
370 | return SHP.NULL;
|
||
371 | } |
||
372 | } |