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