svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / shp / utils / SHPFileWrite.java @ 22373
History | View | Annotate | Download (9.44 KB)
1 |
/* 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.data.feature.file.shp.utils; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.io.IOException; |
45 |
import java.nio.ByteBuffer; |
46 |
import java.nio.ByteOrder; |
47 |
import java.nio.channels.FileChannel; |
48 |
|
49 |
import org.gvsig.fmap.data.WriteException; |
50 |
import org.gvsig.fmap.data.feature.InitializeWriterException; |
51 |
import org.gvsig.fmap.geom.Geometry; |
52 |
import org.gvsig.fmap.geom.primitive.DefaultEnvelope; |
53 |
import org.gvsig.fmap.geom.primitive.Envelope; |
54 |
|
55 |
|
56 |
|
57 |
/**
|
58 |
* DOCUMENT ME!
|
59 |
*
|
60 |
* @author Vicente Caballero Navarro
|
61 |
*/
|
62 |
public class SHPFileWrite { |
63 |
private SHPShape m_shape = null; |
64 |
private ByteBuffer m_bb = null; |
65 |
private ByteBuffer m_indexBuffer = null; |
66 |
private int m_pos = 0; |
67 |
private int m_offset; |
68 |
private int m_type; |
69 |
private int m_cnt; |
70 |
private FileChannel shpChannel; |
71 |
private FileChannel shxChannel; |
72 |
// private double flatness;
|
73 |
|
74 |
/**
|
75 |
* Crea un nuevo SHPFileWrite.
|
76 |
*
|
77 |
* @param shpChannel DOCUMENT ME!
|
78 |
* @param shxChannel DOCUMENT ME!
|
79 |
*/
|
80 |
public SHPFileWrite(FileChannel shpChannel, FileChannel shxChannel) { |
81 |
this.shpChannel = shpChannel;
|
82 |
this.shxChannel = shxChannel;
|
83 |
} |
84 |
|
85 |
/**
|
86 |
* Make sure our buffer is of size.
|
87 |
*
|
88 |
* @param size DOCUMENT ME!
|
89 |
*/
|
90 |
private void checkShapeBuffer(int size) { |
91 |
if (m_bb.capacity() < size) {
|
92 |
m_bb = ByteBuffer.allocateDirect(size);
|
93 |
} |
94 |
} |
95 |
|
96 |
/**
|
97 |
* Drain internal buffers into underlying channels.
|
98 |
* @throws WriteException
|
99 |
*
|
100 |
* @throws IOException DOCUMENT ME!
|
101 |
*/
|
102 |
private void drain() throws InitializeWriterException { |
103 |
m_bb.flip(); |
104 |
m_indexBuffer.flip(); |
105 |
try{
|
106 |
while (m_bb.remaining() > 0) |
107 |
shpChannel.write(m_bb); |
108 |
|
109 |
while (m_indexBuffer.remaining() > 0) |
110 |
shxChannel.write(m_indexBuffer); |
111 |
}catch (IOException e) { |
112 |
throw new InitializeWriterException("SHP File Write Drain",e); |
113 |
} |
114 |
m_bb.flip().limit(m_bb.capacity()); |
115 |
m_indexBuffer.flip().limit(m_indexBuffer.capacity()); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* DOCUMENT ME!
|
120 |
*/
|
121 |
private void allocateBuffers() { |
122 |
m_bb = ByteBuffer.allocateDirect(16 * 1024); |
123 |
m_indexBuffer = ByteBuffer.allocateDirect(100); |
124 |
} |
125 |
|
126 |
/**
|
127 |
* Close the underlying Channels.
|
128 |
* @throws WriteException
|
129 |
*
|
130 |
* @throws IOException DOCUMENT ME!
|
131 |
*/
|
132 |
public void close() throws InitializeWriterException { |
133 |
try {
|
134 |
shpChannel.close(); |
135 |
shxChannel.close(); |
136 |
} catch (IOException e) { |
137 |
throw new InitializeWriterException("SHP File Write Close",e); |
138 |
} |
139 |
shpChannel = null;
|
140 |
shxChannel = null;
|
141 |
m_shape = null;
|
142 |
|
143 |
if (m_indexBuffer instanceof ByteBuffer) { |
144 |
if (m_indexBuffer != null) { |
145 |
///NIOUtilities.clean(m_indexBuffer);
|
146 |
} |
147 |
} |
148 |
|
149 |
if (m_indexBuffer instanceof ByteBuffer) { |
150 |
if (m_indexBuffer != null) { |
151 |
///NIOUtilities.clean(m_bb);
|
152 |
} |
153 |
} |
154 |
|
155 |
m_indexBuffer = null;
|
156 |
m_bb = null;
|
157 |
} |
158 |
|
159 |
/**
|
160 |
* DOCUMENT ME!
|
161 |
*
|
162 |
* @param geometries DOCUMENT ME!
|
163 |
* @param type DOCUMENT ME!
|
164 |
* @throws WriteException
|
165 |
*
|
166 |
* @throws IOException DOCUMENT ME!
|
167 |
* @throws ShapefileException DOCUMENT ME!
|
168 |
*/
|
169 |
public void write(Geometry[] geometries, int type) throws InitializeWriterException { |
170 |
m_shape = SHP.create(type); |
171 |
// m_shape.setFlatness(flatness);
|
172 |
writeHeaders(geometries, type); |
173 |
|
174 |
m_pos = m_bb.position(); |
175 |
|
176 |
for (int i = 0, ii = geometries.length; i < ii; i++) { |
177 |
writeGeometry(geometries[i]); |
178 |
} |
179 |
|
180 |
close(); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* DOCUMENT ME!
|
185 |
*
|
186 |
* @param geometries DOCUMENT ME!
|
187 |
* @param type DOCUMENT ME!
|
188 |
* @throws WriteException
|
189 |
*
|
190 |
* @throws IOException DOCUMENT ME!
|
191 |
*/
|
192 |
private void writeHeaders(Geometry[] geometries, int type) throws InitializeWriterException{ |
193 |
int fileLength = 100; |
194 |
Envelope extent = null;
|
195 |
|
196 |
for (int i = geometries.length - 1; i >= 0; i--) { |
197 |
Geometry fgeometry = geometries[i]; |
198 |
m_shape.obtainsPoints(fgeometry); |
199 |
int size = m_shape.getLength(fgeometry) + 8; |
200 |
fileLength += size; |
201 |
|
202 |
if (extent == null) { |
203 |
extent = new DefaultEnvelope(2); |
204 |
} else {
|
205 |
extent.add(fgeometry.getEnvelope()); |
206 |
} |
207 |
} |
208 |
|
209 |
writeHeaders(extent, type, geometries.length, fileLength); |
210 |
} |
211 |
|
212 |
/**
|
213 |
* Writes shape header (100 bytes)
|
214 |
*
|
215 |
* @param bounds DOCUMENT ME!
|
216 |
* @param type DOCUMENT ME!
|
217 |
* @param numberOfGeometries DOCUMENT ME!
|
218 |
* @param fileLength DOCUMENT ME!
|
219 |
* @throws WriteException
|
220 |
*
|
221 |
* @throws IOException DOCUMENT ME!
|
222 |
*/
|
223 |
public void writeHeaders(Envelope bounds, int type, |
224 |
int numberOfGeometries, int fileLength) throws InitializeWriterException { |
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.getMinimum(0), bounds.getMaximum(1), bounds.getMaximum(0), |
242 |
bounds.getMaximum(1), 0, 0, 0, 0); |
243 |
|
244 |
header.write(m_indexBuffer, type, numberOfGeometries, |
245 |
50 + (4 * numberOfGeometries), bounds.getMinimum(0), bounds.getMinimum(1), |
246 |
bounds.getMaximum(0), bounds.getMaximum(1), 0, 0, 0, 0); |
247 |
|
248 |
m_offset = 50;
|
249 |
m_type = type; |
250 |
m_cnt = 0;
|
251 |
|
252 |
try {
|
253 |
shpChannel.position(0);
|
254 |
shxChannel.position(0);
|
255 |
} catch (IOException e) { |
256 |
throw new InitializeWriterException("SHP File Write Headers",e); |
257 |
} |
258 |
drain(); |
259 |
} |
260 |
public int writeIGeometry(Geometry g) throws InitializeWriterException{ |
261 |
int shapeType = getShapeType(g.getType());
|
262 |
m_shape = SHP.create(shapeType); |
263 |
// m_shape.setFlatness(flatness);
|
264 |
System.out.println("writeIGeometry: type="+ g.getType()); |
265 |
return writeGeometry(g);
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* Writes a single Geometry.
|
270 |
*
|
271 |
* @param g
|
272 |
* @return the position of buffer (after the last geometry, it will allow you to
|
273 |
* write the file size in the header.
|
274 |
* @throws WriteException
|
275 |
* @throws IOException
|
276 |
*/
|
277 |
public synchronized int writeGeometry(Geometry g) throws InitializeWriterException{ |
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 InitializeWriterException("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) { |
327 |
|
328 |
if (geometryType>=Geometry.TYPES.Z){
|
329 |
switch (geometryType - Geometry.TYPES.Z) {
|
330 |
case Geometry.TYPES.POINT:
|
331 |
return SHP.POINT3D;
|
332 |
|
333 |
case Geometry.TYPES.CURVE:
|
334 |
case Geometry.TYPES.ELLIPSE:
|
335 |
case Geometry.TYPES.CIRCLE:
|
336 |
case Geometry.TYPES.ARC:
|
337 |
return SHP.POLYLINE3D;
|
338 |
|
339 |
case Geometry.TYPES.SURFACE:
|
340 |
return SHP.POLYGON3D;
|
341 |
|
342 |
case Geometry.TYPES.MULTIPOINT:
|
343 |
return SHP.MULTIPOINT3D; //TODO falta aclarar cosas aqu?. |
344 |
} |
345 |
|
346 |
}else{
|
347 |
switch (geometryType) {
|
348 |
case Geometry.TYPES.POINT:
|
349 |
return SHP.POINT2D;
|
350 |
|
351 |
case Geometry.TYPES.CURVE:
|
352 |
case Geometry.TYPES.ELLIPSE:
|
353 |
case Geometry.TYPES.CIRCLE:
|
354 |
case Geometry.TYPES.ARC:
|
355 |
return SHP.POLYLINE2D;
|
356 |
|
357 |
case Geometry.TYPES.SURFACE:
|
358 |
return SHP.POLYGON2D;
|
359 |
|
360 |
case Geometry.TYPES.MULTIPOINT:
|
361 |
return SHP.MULTIPOINT2D; //TODO falta aclarar cosas aqu?. |
362 |
} |
363 |
} |
364 |
return SHP.NULL;
|
365 |
} |
366 |
|
367 |
// public void setFlatness(double flatness) {
|
368 |
// this.flatness=flatness;
|
369 |
// }
|
370 |
|
371 |
} |