svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / IndexedShpDriver.java @ 4430
History | View | Annotate | Download (23.7 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 com.iver.cit.gvsig.fmap.drivers.shp; |
42 |
|
43 |
import java.awt.geom.Point2D; |
44 |
import java.awt.geom.Rectangle2D; |
45 |
import java.io.File; |
46 |
import java.io.FileInputStream; |
47 |
import java.io.IOException; |
48 |
import java.nio.ByteBuffer; |
49 |
import java.nio.ByteOrder; |
50 |
import java.nio.channels.FileChannel; |
51 |
import java.util.Properties; |
52 |
|
53 |
import org.apache.log4j.Logger; |
54 |
|
55 |
import com.iver.cit.gvsig.fmap.core.FShape; |
56 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
57 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
58 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
59 |
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes; |
60 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
61 |
import com.iver.cit.gvsig.fmap.drivers.ExternalData; |
62 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
63 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
64 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
65 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
66 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
67 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
68 |
import com.iver.utiles.bigfile.BigByteBuffer2; |
69 |
|
70 |
|
71 |
/**
|
72 |
* Driver del formato SHP. Usa ByteBuffer2, que no consume
|
73 |
* memoria (bueno, 8KBytes... !) y no le importa que le pidan
|
74 |
* entidades desordenadas del fichero, lo cual es indispensable
|
75 |
* para trabajar con ?ndices espaciales.
|
76 |
* Adem?s, el tiempo de espera para abrir un fichero y empezar a pintar es
|
77 |
* pr?cticamente nulo, tanto desde disco duro como por red.
|
78 |
* La ?nica pega es que en ficheros grandes puede ser unos
|
79 |
* milisegundos un poco m?s lento que el otro, pero muy poco.
|
80 |
*
|
81 |
* @author Francisco Jos? Pe?arrubia
|
82 |
*/
|
83 |
public class IndexedShpDriver implements VectorialFileDriver, BoundedShapes, |
84 |
ExternalData, ISpatialWriter { |
85 |
private static Logger logger = Logger.getLogger(IndexedShpDriver.class.getName()); |
86 |
private File file; |
87 |
private BigByteBuffer2 bb;
|
88 |
private FileChannel channel; |
89 |
private FileInputStream fin; |
90 |
private int type; |
91 |
// private long[] m_posShapes;
|
92 |
private int numReg; |
93 |
private Rectangle2D extent; |
94 |
|
95 |
// private File shxFile;
|
96 |
private ByteBuffer bbShx; |
97 |
private FileChannel channelShx; |
98 |
private FileInputStream finShx; |
99 |
|
100 |
private ShpWriter shpWriter = new ShpWriter(); |
101 |
|
102 |
/**
|
103 |
* Cierra el fichero.
|
104 |
*
|
105 |
* @throws IOException
|
106 |
*
|
107 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
|
108 |
*/
|
109 |
public void close() throws IOException { |
110 |
IOException ret = null; |
111 |
|
112 |
try {
|
113 |
channel.close(); |
114 |
channelShx.close(); |
115 |
} catch (IOException e) { |
116 |
ret = e; |
117 |
} finally {
|
118 |
try {
|
119 |
fin.close(); |
120 |
} catch (IOException e1) { |
121 |
ret = e1; |
122 |
} |
123 |
} |
124 |
|
125 |
if (ret != null) { |
126 |
throw ret;
|
127 |
} |
128 |
else // Si todo ha ido bien, preparamos para liberar memoria. |
129 |
{ |
130 |
bb = null;
|
131 |
bbShx = null;
|
132 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
|
137 |
*/
|
138 |
public void open(File f) throws IOException { |
139 |
file = f; |
140 |
|
141 |
fin = new FileInputStream(f); |
142 |
|
143 |
// Open the file and then get a channel from the stream
|
144 |
channel = fin.getChannel(); |
145 |
|
146 |
// long size = channel.size();
|
147 |
|
148 |
// Get the file's size and then map it into memory
|
149 |
// bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
150 |
bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY); |
151 |
|
152 |
finShx = new FileInputStream(getShxFile(f)); |
153 |
|
154 |
// Open the file and then get a channel from the stream
|
155 |
channelShx = finShx.getChannel(); |
156 |
|
157 |
long sizeShx = channelShx.size();
|
158 |
|
159 |
// Get the file's size and then map it into memory
|
160 |
// bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
161 |
bbShx = channelShx.map(FileChannel.MapMode.READ_ONLY, 0, sizeShx); |
162 |
bbShx.order(ByteOrder.BIG_ENDIAN);
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* DOCUMENT ME!
|
167 |
*
|
168 |
* @param index DOCUMENT ME!
|
169 |
*
|
170 |
* @return DOCUMENT ME!
|
171 |
*
|
172 |
* @throws IOException
|
173 |
*
|
174 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShape(int)
|
175 |
*/
|
176 |
|
177 |
/* public FShape getShapeByID(int ID) {
|
178 |
Point2D.Double p = new Point2D.Double();
|
179 |
Point2D.Double pAnt = null;
|
180 |
int numParts;
|
181 |
int numPoints;
|
182 |
int i;
|
183 |
int j;
|
184 |
int numReg;
|
185 |
int numeroPuntos;
|
186 |
int hasta;
|
187 |
int desde;
|
188 |
Rectangle2D.Double BoundingBox = new Rectangle2D.Double();
|
189 |
|
190 |
SHPShape shapeShp=null;
|
191 |
FShape resulShape = null;
|
192 |
try {
|
193 |
bb.position(m_posShapes[ID]);
|
194 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
195 |
int tipoShape = bb.getInt();
|
196 |
m_shapeType = tipoShape;
|
197 |
// retrieve that shape.
|
198 |
// tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
|
199 |
if (tipoShape == FConstant.SHAPE_TYPE_POINT) {
|
200 |
p = readPoint(bb);
|
201 |
resulShape = new FShape(new FPoint(p.getX(),p.getY()),FConstant.SHAPE_TYPE_POINT);
|
202 |
//Comprobaci?n punto.
|
203 |
//System.err.println("p.x = "+p.x);
|
204 |
//System.err.println("p.y = "+p.y);
|
205 |
|
206 |
} else if ((tipoShape == FConstant.SHAPE_TYPE_POLYLINE) ||
|
207 |
(tipoShape == FConstant.SHAPE_TYPE_POLYGON)) {
|
208 |
// BoundingBox
|
209 |
BoundingBox = readRectangle(bb);
|
210 |
numParts = bb.getInt();
|
211 |
numPoints = bb.getInt();
|
212 |
// part indexes.
|
213 |
// Geometry geom = GeometryFactory.toGeometryArray();
|
214 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
215 |
numPoints);
|
216 |
int[] tempParts = new int[numParts];
|
217 |
for (i = 0; i < numParts; i++) {
|
218 |
tempParts[i] = bb.getInt();
|
219 |
}
|
220 |
j = 0;
|
221 |
///Line2D.Double line2D;
|
222 |
FPoint[] points=new FPoint[numPoints];
|
223 |
for (i = 0; i < numPoints; i++) {
|
224 |
p=readPoint(bb);
|
225 |
points[i] = new FPoint(p.x,p.y);
|
226 |
// System.out.println("x= " + p.x + " y=" + p.y);
|
227 |
// System.out.println("x= " + (float) p.x + " y=" + (float) p.y);
|
228 |
if (i == tempParts[j]) {
|
229 |
elShape.moveTo(p.x, p.y);
|
230 |
if (j < (numParts - 1)) {
|
231 |
j++;
|
232 |
}
|
233 |
} else {
|
234 |
elShape.lineTo(p.x, p.y);
|
235 |
}
|
236 |
}
|
237 |
//FGeometry pol=new FPolyLine(points,tempParts,BoundingBox);
|
238 |
|
239 |
resulShape = new FShape(tipoShape,elShape);
|
240 |
} else if (tipoShape == FConstant.SHAPE_TYPE_MULTIPOINT) {
|
241 |
// BoundingBox
|
242 |
BoundingBox = readRectangle(bb);
|
243 |
numPoints = bb.getInt();
|
244 |
FPoint[] tempPoints = new FPoint[numPoints];
|
245 |
for (i = 0; i < numPoints; i++) {
|
246 |
Point2D p2=readPoint(bb);
|
247 |
tempPoints[i] = new FPoint(p2.getX(),p2.getY(),0);
|
248 |
}
|
249 |
FMultiPoint multipoint = new FMultiPoint(tempPoints,BoundingBox);
|
250 |
resulShape = new FShape(multipoint,tipoShape);
|
251 |
} else if (tipoShape == FConstant.SHAPE_TYPE_POINTZ) {
|
252 |
FPoint p3d = new FPoint();
|
253 |
p3d.read(bb);
|
254 |
resulShape = new FShape(p3d,tipoShape);
|
255 |
} else if ((tipoShape == FConstant.SHAPE_TYPE_POLYLINEZ) ||
|
256 |
(tipoShape == FConstant.SHAPE_TYPE_POLYGONZ)) {
|
257 |
// BoundingBox
|
258 |
BoundingBox = readRectangle(bb);
|
259 |
numParts = bb.getInt();
|
260 |
numPoints = bb.getInt();
|
261 |
// part indexes.
|
262 |
// Geometry geom = GeometryFactory.toGeometryArray();
|
263 |
|
264 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
265 |
numPoints);
|
266 |
int[] tempParts = new int[numParts];
|
267 |
for (i = 0; i < numParts; i++) {
|
268 |
tempParts[i] = bb.getInt();
|
269 |
}
|
270 |
j = 0;
|
271 |
//Line2D.Double line2D;
|
272 |
FPoint[] points=new FPoint[numPoints];
|
273 |
for (i = 0; i < numPoints; i++) {
|
274 |
p = readPoint(bb);
|
275 |
points[i]=new FPoint(p.x,p.y);
|
276 |
|
277 |
if (i == tempParts[j]) {
|
278 |
elShape.moveTo(p.x, p.y);
|
279 |
if (j < (numParts - 1)) {
|
280 |
j++;
|
281 |
}
|
282 |
} else {
|
283 |
elShape.lineTo(p.x, p.y);
|
284 |
}
|
285 |
|
286 |
}
|
287 |
|
288 |
double[] boxZ = new double[2];
|
289 |
boxZ[0] = bb.getDouble();
|
290 |
boxZ[1] = bb.getDouble();
|
291 |
double[] pZ = new double[numPoints];
|
292 |
for (i = 0; i < numPoints; i++) {
|
293 |
pZ[i] = bb.getDouble();
|
294 |
}
|
295 |
//FGeometry pol=new FPolyLine(points,tempParts,BoundingBox);
|
296 |
resulShape = new FShape(tipoShape, elShape, pZ);
|
297 |
} else if (tipoShape == FConstant.SHAPE_TYPE_MULTIPOINTZ) {
|
298 |
// BoundingBox
|
299 |
BoundingBox = readRectangle(bb);
|
300 |
numPoints = bb.getInt();
|
301 |
FPoint[] tempPoints3D = new FPoint[numPoints];
|
302 |
for (i = 0; i < numPoints; i++) {
|
303 |
tempPoints3D[i] = new FPoint();
|
304 |
tempPoints3D[i].read(bb);
|
305 |
}
|
306 |
FMultiPoint multipoint3D = new FMultiPoint(tempPoints3D,BoundingBox);
|
307 |
resulShape = new FShape(multipoint3D,tipoShape);
|
308 |
}
|
309 |
} catch (Exception e) {
|
310 |
System.err.println("Fallo en getShapeByID. ID=" + ID +
|
311 |
" m_posShapes[ID]=" + m_posShapes[ID]);
|
312 |
System.err.println("getShapeByID: " + e.getMessage());
|
313 |
e.printStackTrace();
|
314 |
}
|
315 |
return resulShape;
|
316 |
}
|
317 |
*/
|
318 |
|
319 |
/**
|
320 |
* Devuelve la geometria a partir de un ?ndice.
|
321 |
*
|
322 |
* @param index DOCUMENT ME!
|
323 |
*
|
324 |
* @return DOCUMENT ME!
|
325 |
*
|
326 |
* @throws IOException DOCUMENT ME!
|
327 |
*/
|
328 |
public IGeometry getShape(int index) throws IOException { |
329 |
Point2D.Double p = new Point2D.Double(); |
330 |
int numParts;
|
331 |
int numPoints;
|
332 |
int i;
|
333 |
int j;
|
334 |
/* int numReg;
|
335 |
int numeroPuntos;
|
336 |
int hasta;
|
337 |
int desde; */
|
338 |
int shapeType;
|
339 |
|
340 |
//Rectangle2D.Double BoundingBox;
|
341 |
// if (m_posShapes[index] == 0)
|
342 |
|
343 |
// bb.position(m_posShapes[index]);
|
344 |
bb.position(getPositionForRecord(index)); |
345 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
346 |
|
347 |
///bb.position(bb.position()+4);
|
348 |
shapeType = bb.getInt(); |
349 |
//el shape tal con tema tal y n?mro tal es null
|
350 |
if (shapeType==SHP.NULL){
|
351 |
logger.info("El shape ="+index+ " del tema ="+this.toString()+" es null"); |
352 |
return null; |
353 |
} |
354 |
|
355 |
// retrieve that shape.
|
356 |
// tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
|
357 |
switch (type) {
|
358 |
case (SHP.POINT2D):
|
359 |
p = readPoint(bb); |
360 |
|
361 |
return ShapeFactory.createPoint2D(p.getX(), p.getY());
|
362 |
|
363 |
case (SHP.POLYLINE2D):
|
364 |
|
365 |
//BoundingBox = readRectangle(bb);
|
366 |
//bb.getDouble();
|
367 |
//bb.getDouble();
|
368 |
//bb.getDouble();
|
369 |
//bb.getDouble();
|
370 |
bb.position(bb.position() + 32);
|
371 |
numParts = bb.getInt(); |
372 |
numPoints = bb.getInt(); |
373 |
|
374 |
// part indexes.
|
375 |
// Geometry geom = GeometryFactory.toGeometryArray();
|
376 |
GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
377 |
numPoints); |
378 |
|
379 |
int[] tempParts = new int[numParts]; |
380 |
|
381 |
for (i = 0; i < numParts; i++) { |
382 |
tempParts[i] = bb.getInt(); |
383 |
} |
384 |
|
385 |
j = 0;
|
386 |
|
387 |
for (i = 0; i < numPoints; i++) { |
388 |
p = readPoint(bb); |
389 |
|
390 |
if (i == tempParts[j]) {
|
391 |
elShape.moveTo(p.x, p.y); |
392 |
|
393 |
if (j < (numParts - 1)) { |
394 |
j++; |
395 |
} |
396 |
} else {
|
397 |
elShape.lineTo(p.x, p.y); |
398 |
} |
399 |
} |
400 |
|
401 |
return ShapeFactory.createPolyline2D(elShape);
|
402 |
|
403 |
case (SHP.POLYGON2D):
|
404 |
|
405 |
// BoundingBox = readRectangle(bb);
|
406 |
bb.getDouble(); |
407 |
bb.getDouble(); |
408 |
bb.getDouble(); |
409 |
bb.getDouble(); |
410 |
|
411 |
numParts = bb.getInt(); |
412 |
|
413 |
numPoints = bb.getInt(); |
414 |
|
415 |
// part indexes.
|
416 |
// Geometry geom = GeometryFactory.toGeometryArray();
|
417 |
elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
|
418 |
|
419 |
tempParts = new int[numParts]; |
420 |
|
421 |
for (i = 0; i < numParts; i++) { |
422 |
tempParts[i] = bb.getInt(); |
423 |
} |
424 |
|
425 |
j = 0;
|
426 |
|
427 |
for (i = 0; i < numPoints; i++) { |
428 |
p = readPoint(bb); |
429 |
|
430 |
if (i == tempParts[j]) {
|
431 |
elShape.moveTo(p.x, p.y); |
432 |
|
433 |
if (j < (numParts - 1)) { |
434 |
j++; |
435 |
} |
436 |
} else {
|
437 |
elShape.lineTo(p.x, p.y); |
438 |
} |
439 |
} |
440 |
|
441 |
return ShapeFactory.createPolygon2D(elShape);
|
442 |
|
443 |
case (SHP.POINT3D):
|
444 |
|
445 |
double x = bb.getDouble();
|
446 |
double y = bb.getDouble();
|
447 |
double z = bb.getDouble();
|
448 |
|
449 |
return ShapeFactory.createPoint3D(x, y, z);
|
450 |
|
451 |
case (SHP.POLYLINE3D):
|
452 |
bb.position(bb.position() + 32);
|
453 |
numParts = bb.getInt(); |
454 |
numPoints = bb.getInt(); |
455 |
elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
|
456 |
tempParts = new int[numParts]; |
457 |
|
458 |
for (i = 0; i < numParts; i++) { |
459 |
tempParts[i] = bb.getInt(); |
460 |
} |
461 |
|
462 |
j = 0;
|
463 |
|
464 |
for (i = 0; i < numPoints; i++) { |
465 |
p = readPoint(bb); |
466 |
|
467 |
if (i == tempParts[j]) {
|
468 |
elShape.moveTo(p.x, p.y); |
469 |
|
470 |
if (j < (numParts - 1)) { |
471 |
j++; |
472 |
} |
473 |
} else {
|
474 |
elShape.lineTo(p.x, p.y); |
475 |
} |
476 |
} |
477 |
|
478 |
double[] boxZ = new double[2]; |
479 |
boxZ[0] = bb.getDouble();
|
480 |
boxZ[1] = bb.getDouble();
|
481 |
|
482 |
double[] pZ = new double[numPoints]; |
483 |
|
484 |
for (i = 0; i < numPoints; i++) { |
485 |
pZ[i] = bb.getDouble(); |
486 |
} |
487 |
|
488 |
return ShapeFactory.createPolyline3D(elShape, pZ);
|
489 |
case (SHP.POLYGON3D):
|
490 |
bb.position(bb.position() + 32);
|
491 |
numParts = bb.getInt(); |
492 |
numPoints = bb.getInt(); |
493 |
elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
|
494 |
tempParts = new int[numParts]; |
495 |
|
496 |
for (i = 0; i < numParts; i++) { |
497 |
tempParts[i] = bb.getInt(); |
498 |
} |
499 |
|
500 |
j = 0;
|
501 |
|
502 |
for (i = 0; i < numPoints; i++) { |
503 |
p = readPoint(bb); |
504 |
|
505 |
if (i == tempParts[j]) {
|
506 |
elShape.moveTo(p.x, p.y); |
507 |
|
508 |
if (j < (numParts - 1)) { |
509 |
j++; |
510 |
} |
511 |
} else {
|
512 |
elShape.lineTo(p.x, p.y); |
513 |
} |
514 |
} |
515 |
|
516 |
double[] boxpoZ = new double[2]; |
517 |
boxpoZ[0] = bb.getDouble();
|
518 |
boxpoZ[1] = bb.getDouble();
|
519 |
|
520 |
double[] poZ = new double[numPoints]; |
521 |
|
522 |
for (i = 0; i < numPoints; i++) { |
523 |
poZ[i] = bb.getDouble(); |
524 |
} |
525 |
|
526 |
return ShapeFactory.createPolygon3D(elShape, poZ);
|
527 |
|
528 |
case (SHP.MULTIPOINT2D):
|
529 |
bb.position(bb.position() + 32);
|
530 |
numPoints = bb.getInt(); |
531 |
|
532 |
double[] tempX = new double[numPoints]; |
533 |
double[] tempY = new double[numPoints]; |
534 |
|
535 |
for (i = 0; i < numPoints; i++) { |
536 |
tempX[i] = bb.getDouble(); |
537 |
tempY[i] = bb.getDouble(); |
538 |
} |
539 |
|
540 |
return ShapeFactory.createMultipoint2D(tempX, tempY);
|
541 |
|
542 |
case (SHP.MULTIPOINT3D):
|
543 |
bb.position(bb.position() + 32);
|
544 |
numPoints = bb.getInt(); |
545 |
|
546 |
double[] temX = new double[numPoints]; |
547 |
double[] temY = new double[numPoints]; |
548 |
double[] temZ = new double[numPoints]; |
549 |
|
550 |
for (i = 0; i < numPoints; i++) { |
551 |
temX[i] = bb.getDouble(); |
552 |
temY[i] = bb.getDouble(); |
553 |
//temZ[i] = bb.getDouble();
|
554 |
} |
555 |
|
556 |
for (i = 0; i < numPoints; i++) { |
557 |
temZ[i] = bb.getDouble(); |
558 |
} |
559 |
return ShapeFactory.createMultipoint3D(temX, temY, temZ);
|
560 |
} |
561 |
|
562 |
return null; |
563 |
} |
564 |
|
565 |
/**
|
566 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShapeCount()
|
567 |
*/
|
568 |
public int getShapeCount() { |
569 |
return numReg;
|
570 |
} |
571 |
|
572 |
/**
|
573 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
|
574 |
*/
|
575 |
public int getShapeType() { |
576 |
int auxType = 0; |
577 |
|
578 |
switch (type) {
|
579 |
case (SHP.POINT2D):
|
580 |
case (SHP.POINT3D):
|
581 |
auxType = auxType | FShape.POINT; |
582 |
|
583 |
break;
|
584 |
|
585 |
case (SHP.POLYLINE2D):
|
586 |
case (SHP.POLYLINE3D):
|
587 |
auxType = auxType | FShape.LINE; |
588 |
|
589 |
break;
|
590 |
|
591 |
case (SHP.POLYGON2D):
|
592 |
case (SHP.POLYGON3D):
|
593 |
auxType = auxType | FShape.POLYGON; |
594 |
|
595 |
break;
|
596 |
case (SHP.MULTIPOINT2D):
|
597 |
case (SHP.MULTIPOINT3D):
|
598 |
auxType = auxType | FShape.MULTIPOINT; |
599 |
|
600 |
break;
|
601 |
} |
602 |
|
603 |
return auxType;
|
604 |
} |
605 |
|
606 |
|
607 |
/**
|
608 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
|
609 |
*/
|
610 |
public void initialize() throws IOException { |
611 |
// create a new header.
|
612 |
ShapeFileHeader2 myHeader = new ShapeFileHeader2();
|
613 |
|
614 |
// read the header
|
615 |
myHeader.readHeader(bb); |
616 |
|
617 |
extent = new Rectangle2D.Double(myHeader.myXmin, myHeader.myYmin, |
618 |
myHeader.myXmax - myHeader.myXmin, |
619 |
myHeader.myYmax - myHeader.myYmin); |
620 |
|
621 |
type = myHeader.myShapeType; |
622 |
|
623 |
double x = myHeader.myXmin;
|
624 |
double y = myHeader.myYmin;
|
625 |
double w = myHeader.myXmax - myHeader.myXmin;
|
626 |
double h = myHeader.myYmax - myHeader.myYmin;
|
627 |
|
628 |
if (w == 0) { |
629 |
x -= 0.1;
|
630 |
w = 0.2;
|
631 |
} |
632 |
|
633 |
if (h == 0) { |
634 |
y -= 0.1;
|
635 |
h = 0.2;
|
636 |
} |
637 |
|
638 |
// String strFichDbf = m_Path.toLowerCase().replaceAll("\\.shp", ".dbf");
|
639 |
String strFichDbf = file.getAbsolutePath().replaceAll("\\.shp", ".dbf"); |
640 |
strFichDbf = strFichDbf.replaceAll("\\.SHP", ".DBF"); |
641 |
|
642 |
DbaseFileNIO m_FichDbf = new DbaseFileNIO();
|
643 |
|
644 |
m_FichDbf.open(new File(strFichDbf)); |
645 |
numReg = m_FichDbf.getRecordCount(); |
646 |
|
647 |
// shpWriter.initialize(file);
|
648 |
// m_posShapes = new long[numReg];
|
649 |
|
650 |
// read the records.
|
651 |
/* int tempCurrentLength = myHeader.getHeaderLength();
|
652 |
int numReg = 0;
|
653 |
|
654 |
long pos1;
|
655 |
|
656 |
while (tempCurrentLength < myHeader.myFileLength) {
|
657 |
// read the record header
|
658 |
// ShapeFileRecord tempRecord = new ShapeFileRecord();
|
659 |
// Bytes 0 to 4 represent the record number in the file, these may be out of order.
|
660 |
bb.order(ByteOrder.BIG_ENDIAN);
|
661 |
|
662 |
// tempRecord.setIndex(in.readInt());
|
663 |
bb.getInt();
|
664 |
|
665 |
// read the content length of this record in 16 bit words, excluding the index.
|
666 |
// in.setLittleEndianMode(false);
|
667 |
int tempContentLength = bb.getInt();
|
668 |
|
669 |
pos1 = bb.position();
|
670 |
|
671 |
m_posShapes[numReg] = bb.position();
|
672 |
|
673 |
// Posicionamos
|
674 |
bb.position((pos1 + (2 * tempContentLength)));
|
675 |
numReg = numReg + 1;
|
676 |
|
677 |
// update the current length the 4 is for the index, and content length.
|
678 |
tempCurrentLength = tempCurrentLength + 4 + tempContentLength;
|
679 |
} */
|
680 |
} |
681 |
|
682 |
/**
|
683 |
* Reads the Point from the shape file.
|
684 |
*
|
685 |
* @param in ByteBuffer.
|
686 |
*
|
687 |
* @return Point2D.
|
688 |
*/
|
689 |
private Point2D.Double readPoint(BigByteBuffer2 in) { |
690 |
// create a new point
|
691 |
Point2D.Double tempPoint = new Point2D.Double(); |
692 |
|
693 |
// bytes 1 to 4 are the type and have already been read.
|
694 |
// bytes 4 to 12 are the X coordinate
|
695 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
696 |
tempPoint.setLocation(in.getDouble(), in.getDouble()); |
697 |
|
698 |
return tempPoint;
|
699 |
} |
700 |
|
701 |
/**
|
702 |
* Lee un rect?ngulo del fichero.
|
703 |
*
|
704 |
* @param in ByteBuffer.
|
705 |
*
|
706 |
* @return Rect?ngulo.
|
707 |
*
|
708 |
* @throws IOException
|
709 |
*/
|
710 |
private Rectangle2D.Double readRectangle(BigByteBuffer2 in) |
711 |
throws IOException { |
712 |
Rectangle2D.Double tempRect = new Rectangle2D.Double(); |
713 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
714 |
tempRect.x = in.getDouble(); |
715 |
tempRect.y = in.getDouble(); |
716 |
|
717 |
tempRect.width = in.getDouble() - tempRect.x; |
718 |
|
719 |
if (tempRect.width == 0) { |
720 |
tempRect.width = 0.2;
|
721 |
tempRect.x -= 0.1;
|
722 |
} |
723 |
|
724 |
tempRect.height = in.getDouble() - tempRect.y; |
725 |
|
726 |
if (tempRect.height == 0) { |
727 |
tempRect.height = 0.2;
|
728 |
tempRect.y -= 0.1;
|
729 |
} |
730 |
|
731 |
return tempRect;
|
732 |
} |
733 |
|
734 |
/**
|
735 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFullExtent()
|
736 |
*/
|
737 |
public Rectangle2D getFullExtent() throws IOException { |
738 |
return extent;
|
739 |
} |
740 |
|
741 |
/**
|
742 |
* Obtiene el extent del shape a partir de un ?ndice.
|
743 |
*
|
744 |
* @param index ?ndice.
|
745 |
*
|
746 |
* @return Rect?ngulo.
|
747 |
*
|
748 |
* @throws IOException
|
749 |
*
|
750 |
* @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds()
|
751 |
*/
|
752 |
public Rectangle2D getShapeBounds(int index) throws IOException { |
753 |
Point2D p = new Point2D.Double(); |
754 |
Rectangle2D BoundingBox = new Rectangle2D.Double(); |
755 |
bb.position(getPositionForRecord(index)); |
756 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
757 |
|
758 |
int tipoShape = bb.getInt();
|
759 |
|
760 |
if (tipoShape != SHP.NULL) {
|
761 |
type = tipoShape; |
762 |
} |
763 |
|
764 |
// retrieve that shape.
|
765 |
// tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
|
766 |
switch (tipoShape) {
|
767 |
case (SHP.POINT2D):
|
768 |
case (SHP.POINT3D):
|
769 |
p = readPoint(bb); |
770 |
BoundingBox = new Rectangle2D.Double(p.getX() - 0.1, |
771 |
p.getY() - 0.1, 0.2, 0.2); |
772 |
|
773 |
break;
|
774 |
|
775 |
case (SHP.POLYLINE2D):
|
776 |
case (SHP.POLYGON2D):
|
777 |
case (SHP.MULTIPOINT2D):
|
778 |
case (SHP.POLYLINE3D):
|
779 |
case (SHP.POLYGON3D):
|
780 |
case (SHP.MULTIPOINT3D):
|
781 |
|
782 |
// BoundingBox
|
783 |
BoundingBox = readRectangle(bb); |
784 |
|
785 |
break;
|
786 |
} |
787 |
|
788 |
return BoundingBox;
|
789 |
} |
790 |
|
791 |
/**
|
792 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
|
793 |
*/
|
794 |
public boolean accept(File f) { |
795 |
return (f.getName().toUpperCase().endsWith("SHP")); |
796 |
} |
797 |
|
798 |
/**
|
799 |
* @see com.hardcode.driverManager.Driver#getType()
|
800 |
*/
|
801 |
public String getName() { |
802 |
// Para que se use este driver en lugar del viejo DemoShpDriver.
|
803 |
|
804 |
// return "gvSIG indexed shp driver";
|
805 |
return "gvSIG shp driver"; |
806 |
} |
807 |
|
808 |
/**
|
809 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getDataDriverName()
|
810 |
*/
|
811 |
public String getDataDriverName() { |
812 |
return "gdbms dbf driver"; |
813 |
} |
814 |
|
815 |
/**
|
816 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getDataFile(java.io.File)
|
817 |
*/
|
818 |
public File getDataFile(File f) { |
819 |
String str = f.getAbsolutePath();
|
820 |
|
821 |
return new File(str.substring(0, str.length() - 3) + "dbf"); |
822 |
} |
823 |
|
824 |
public File getShxFile(File f) { |
825 |
String str = f.getAbsolutePath();
|
826 |
|
827 |
return new File(str.substring(0, str.length() - 3) + "shx"); |
828 |
} |
829 |
|
830 |
|
831 |
/* (non-Javadoc)
|
832 |
* @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
|
833 |
*/
|
834 |
public int getShapeType(int index) { |
835 |
// Por ahora todos los fichero .shp contienen
|
836 |
// entidades del mismo tipo. Si trabajamos con
|
837 |
// alguno mixto, tendremos que cambiar esta funci?n.
|
838 |
return getShapeType();
|
839 |
} |
840 |
|
841 |
/* (non-Javadoc)
|
842 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
843 |
*/
|
844 |
public DriverAttributes getDriverAttributes() {
|
845 |
return null; |
846 |
} |
847 |
|
848 |
private long getPositionForRecord(int numRec) |
849 |
{ |
850 |
// shx file has a 100 bytes header, then, records
|
851 |
// 8 bytes length, one for each entity.
|
852 |
// first 4 bytes are the offset
|
853 |
// next 4 bytes are length
|
854 |
|
855 |
int posIndex = 100 + (numRec * 8); |
856 |
// bbShx.position(posIndex);
|
857 |
long pos = 8 + 2*bbShx.getInt(posIndex); |
858 |
|
859 |
return pos;
|
860 |
} |
861 |
|
862 |
public File getFile() { |
863 |
return file;
|
864 |
} |
865 |
|
866 |
public void reLoad() throws IOException { |
867 |
initialize(); |
868 |
} |
869 |
|
870 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
871 |
return shpWriter.canWriteGeometry(gvSIGgeometryType);
|
872 |
} |
873 |
|
874 |
/* (non-Javadoc)
|
875 |
* @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
|
876 |
*/
|
877 |
public void preProcess() throws EditionException { |
878 |
shpWriter.preProcess(); |
879 |
|
880 |
} |
881 |
|
882 |
public void process(IRowEdited row) throws EditionException { |
883 |
shpWriter.process(row); |
884 |
|
885 |
} |
886 |
|
887 |
public void postProcess() throws EditionException { |
888 |
shpWriter.postProcess(); |
889 |
|
890 |
} |
891 |
|
892 |
public String getCapability(String capability) { |
893 |
return shpWriter.getCapability(capability);
|
894 |
} |
895 |
|
896 |
public void setCapabilities(Properties capabilities) { |
897 |
shpWriter.setCapabilities(capabilities); |
898 |
|
899 |
} |
900 |
|
901 |
public boolean canWriteAttribute(int sqlType) { |
902 |
return shpWriter.canWriteAttribute(sqlType);
|
903 |
} |
904 |
|
905 |
public ShpWriter getShpWriter() {
|
906 |
return shpWriter;
|
907 |
} |
908 |
|
909 |
public void setShpWriter(ShpWriter shpWriter) { |
910 |
this.shpWriter = shpWriter;
|
911 |
} |
912 |
|
913 |
public void initialize(VectorialEditableAdapter vea) throws EditionException { |
914 |
shpWriter.setFile(file); |
915 |
shpWriter.initialize(vea); |
916 |
|
917 |
} |
918 |
|
919 |
} |