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