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