svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / utils / SHPFile.java @ 40596
History | View | Annotate | Download (21.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.shp.utils; |
25 |
|
26 |
import java.io.BufferedReader; |
27 |
import java.io.File; |
28 |
import java.io.FileInputStream; |
29 |
import java.io.FileReader; |
30 |
import java.io.IOException; |
31 |
import java.nio.ByteOrder; |
32 |
import java.nio.channels.FileChannel; |
33 |
|
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
import org.gvsig.fmap.dal.exception.CloseException; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
40 |
import org.gvsig.fmap.dal.exception.ReadException; |
41 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
42 |
import org.gvsig.fmap.geom.Geometry; |
43 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
44 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
45 |
import org.gvsig.fmap.geom.GeometryLocator; |
46 |
import org.gvsig.fmap.geom.GeometryManager; |
47 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
48 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
49 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
50 |
import org.gvsig.fmap.geom.primitive.Curve; |
51 |
import org.gvsig.fmap.geom.primitive.Envelope; |
52 |
import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
53 |
import org.gvsig.fmap.geom.primitive.Point; |
54 |
import org.gvsig.fmap.geom.primitive.PointGeometryType; |
55 |
import org.gvsig.fmap.geom.primitive.Surface; |
56 |
import org.gvsig.fmap.geom.type.GeometryType; |
57 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
58 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
59 |
import org.gvsig.utils.bigfile.BigByteBuffer2; |
60 |
|
61 |
/**
|
62 |
* @author jmvivo
|
63 |
*
|
64 |
*/
|
65 |
public class SHPFile { |
66 |
|
67 |
private static final Logger logger = LoggerFactory.getLogger(SHPFile.class); |
68 |
private Envelope extent;
|
69 |
private int type; |
70 |
private int subType; |
71 |
private String srsParameters; |
72 |
|
73 |
private FileInputStream fin; |
74 |
private FileChannel channel; |
75 |
private BigByteBuffer2 bb;
|
76 |
private FileInputStream finShx; |
77 |
private FileChannel channelShx; |
78 |
private BigByteBuffer2 bbShx;
|
79 |
|
80 |
private SHPStoreParameters params;
|
81 |
|
82 |
private int[] supportedGeometryTypes; |
83 |
private final GeometryManager gManager = GeometryLocator |
84 |
.getGeometryManager(); |
85 |
|
86 |
private GeometryType gtypeNull;
|
87 |
private PointGeometryType gtypePoint2D;
|
88 |
private GeometryType gtypeCurve2D;
|
89 |
private GeometryType gtypeSurface2D;
|
90 |
private GeometryType gtypeMultiPoint2D;
|
91 |
|
92 |
public SHPFile(SHPStoreParameters params) {
|
93 |
this.params = params;
|
94 |
try {
|
95 |
gtypeNull = gManager.getGeometryType(TYPES.NULL, SUBTYPES.GEOM2D); |
96 |
gtypePoint2D = |
97 |
(PointGeometryType) gManager.getGeometryType(TYPES.POINT, |
98 |
SUBTYPES.GEOM2D); |
99 |
gtypeCurve2D = |
100 |
gManager.getGeometryType(TYPES.CURVE, SUBTYPES.GEOM2D); |
101 |
gtypeSurface2D = |
102 |
gManager.getGeometryType(TYPES.SURFACE, SUBTYPES.GEOM2D); |
103 |
gtypeMultiPoint2D = |
104 |
gManager.getGeometryType(TYPES.MULTIPOINT, SUBTYPES.GEOM2D); |
105 |
|
106 |
} catch (GeometryTypeNotSupportedException e) {
|
107 |
throw new RuntimeException( |
108 |
"Unable to get the 2D geometry types to use", e);
|
109 |
} catch (GeometryTypeNotValidException e) {
|
110 |
throw new RuntimeException( |
111 |
"Unable to get the 2D geometry types to use", e);
|
112 |
} |
113 |
} |
114 |
|
115 |
/*
|
116 |
* (non-Javadoc)
|
117 |
*
|
118 |
* @see org.gvsig.fmap.dal.Resource#doClose()
|
119 |
*/
|
120 |
public void close() throws CloseException { |
121 |
CloseException ret = null;
|
122 |
|
123 |
// FIXME: Arreglar esto para que se acumulen los errores
|
124 |
try {
|
125 |
channel.close(); |
126 |
channelShx.close(); |
127 |
} catch (IOException e) { |
128 |
ret = new CloseException("SHPFile.close", e); |
129 |
} finally {
|
130 |
try {
|
131 |
fin.close(); |
132 |
finShx.close(); |
133 |
} catch (IOException e1) { |
134 |
ret = new CloseException("SHPFile.close", e1); |
135 |
} |
136 |
} |
137 |
|
138 |
if (ret != null) { |
139 |
throw ret;
|
140 |
} |
141 |
bb = null;
|
142 |
bbShx = null;
|
143 |
fin = null;
|
144 |
finShx = null;
|
145 |
channel = null;
|
146 |
channelShx = null;
|
147 |
srsParameters = null;
|
148 |
} |
149 |
|
150 |
public boolean isOpen() { |
151 |
return this.fin != null; |
152 |
} |
153 |
|
154 |
public synchronized void open() throws DataException { |
155 |
try {
|
156 |
fin = new FileInputStream(this.params.getSHPFile()); |
157 |
} catch (java.io.FileNotFoundException e) {
|
158 |
throw new FileNotFoundException(this.params.getSHPFileName()); |
159 |
} |
160 |
|
161 |
// Open the file and then get a channel from the stream
|
162 |
channel = fin.getChannel(); |
163 |
|
164 |
// long size = channel.size();
|
165 |
|
166 |
// Get the file's size and then map it into memory
|
167 |
// bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
168 |
try {
|
169 |
bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY); |
170 |
} catch (IOException e) { |
171 |
throw new ReadException(this.params.getSHPFileName(), e); |
172 |
|
173 |
} |
174 |
try {
|
175 |
finShx = new FileInputStream(this.params.getSHXFile()); |
176 |
} catch (java.io.FileNotFoundException e) {
|
177 |
throw new FileNotFoundException(this.params.getSHXFileName()); |
178 |
} |
179 |
|
180 |
// Open the file and then get a channel from the stream
|
181 |
channelShx = finShx.getChannel(); |
182 |
|
183 |
// long sizeShx = channelShx.size();
|
184 |
|
185 |
// Get the file's size and then map it into memory
|
186 |
// bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
|
187 |
// bbShx = channelShx.map(FileChannel.MapMode.READ_ONLY, 0, sizeShx);
|
188 |
try {
|
189 |
bbShx = |
190 |
new BigByteBuffer2(channelShx, FileChannel.MapMode.READ_ONLY); |
191 |
} catch (IOException e) { |
192 |
throw new ReadException(this.params.getSHXFileName(), e); |
193 |
|
194 |
} |
195 |
bbShx.order(ByteOrder.BIG_ENDIAN);
|
196 |
|
197 |
// create a new header.
|
198 |
ShapeFileHeader2 myHeader = new ShapeFileHeader2();
|
199 |
|
200 |
bb.position(0);
|
201 |
|
202 |
// read the header
|
203 |
myHeader.readHeader(bb); |
204 |
|
205 |
double[] min = new double[2]; |
206 |
min[0] = myHeader.myXmin;
|
207 |
min[1] = myHeader.myYmin;
|
208 |
double[] max = new double[2]; |
209 |
max[0] = myHeader.myXmax;
|
210 |
max[1] = myHeader.myYmax;
|
211 |
|
212 |
try {
|
213 |
extent = |
214 |
gManager.createEnvelope(min[0], min[1], max[0], max[1], |
215 |
SUBTYPES.GEOM2D); |
216 |
} catch (CreateEnvelopeException e1) {
|
217 |
logger.error("Error creating the envelope", e1);
|
218 |
} |
219 |
// extent = new Rectangle2D.Double(myHeader.myXmin, myHeader.myYmin,
|
220 |
// myHeader.myXmax - myHeader.myXmin,
|
221 |
// myHeader.myYmax - myHeader.myYmin);
|
222 |
|
223 |
type = myHeader.myShapeType; |
224 |
subType = getGeometrySubType(); |
225 |
|
226 |
this.initSupportedGeometryTypes();
|
227 |
|
228 |
double x = myHeader.myXmin;
|
229 |
double y = myHeader.myYmin;
|
230 |
double w = myHeader.myXmax - myHeader.myXmin;
|
231 |
double h = myHeader.myYmax - myHeader.myYmin;
|
232 |
|
233 |
if (w == 0) { |
234 |
x -= 0.1;
|
235 |
w = 0.2;
|
236 |
} |
237 |
|
238 |
if (h == 0) { |
239 |
y -= 0.1;
|
240 |
h = 0.2;
|
241 |
} |
242 |
|
243 |
// TODO: SRS
|
244 |
File prjFile = SHP.getPrjFile(this.params.getSHPFile()); |
245 |
if (prjFile.exists()) {
|
246 |
BufferedReader input = null; |
247 |
try {
|
248 |
input = new BufferedReader(new FileReader(prjFile)); |
249 |
} catch (java.io.FileNotFoundException e) {
|
250 |
throw new FileNotFoundException(prjFile.getAbsolutePath()); |
251 |
} |
252 |
|
253 |
try {
|
254 |
this.srsParameters = input.readLine();
|
255 |
} catch (IOException e) { |
256 |
throw new ReadException("SHPFile.open prj", e); |
257 |
} finally {
|
258 |
try {
|
259 |
input.close(); |
260 |
} catch (IOException e) { |
261 |
// TODO ???
|
262 |
} |
263 |
} |
264 |
|
265 |
} else {
|
266 |
this.srsParameters = null; |
267 |
} |
268 |
} |
269 |
|
270 |
public Envelope getFullExtent() throws ReadException { |
271 |
return this.extent; |
272 |
} |
273 |
|
274 |
public boolean isEditable() { |
275 |
return this.params.getDBFFile().canWrite() |
276 |
&& this.params.getSHPFile().canWrite()
|
277 |
&& this.params.getSHXFile().canWrite();
|
278 |
} |
279 |
|
280 |
public int getGeometryType() throws ReadException { |
281 |
int auxType = 0; |
282 |
|
283 |
switch (type) {
|
284 |
case (SHP.POINT2D):
|
285 |
case (SHP.POINT3D):
|
286 |
auxType = auxType | Geometry.TYPES.POINT; |
287 |
|
288 |
break;
|
289 |
|
290 |
case (SHP.POLYLINE2D):
|
291 |
case (SHP.POLYLINE3D):
|
292 |
auxType = auxType | Geometry.TYPES.MULTICURVE; |
293 |
|
294 |
break;
|
295 |
|
296 |
case (SHP.POLYGON2D):
|
297 |
case (SHP.POLYGON3D):
|
298 |
auxType = auxType | Geometry.TYPES.MULTISURFACE; |
299 |
|
300 |
break;
|
301 |
case (SHP.MULTIPOINT2D):
|
302 |
case (SHP.MULTIPOINT3D):
|
303 |
auxType = auxType | Geometry.TYPES.MULTIPOINT; |
304 |
|
305 |
break;
|
306 |
} |
307 |
|
308 |
return auxType;
|
309 |
} |
310 |
|
311 |
public int getGeometrySubType() throws ReadException { |
312 |
switch (type) {
|
313 |
case (SHP.POINT2D):
|
314 |
case (SHP.POLYLINE2D):
|
315 |
case (SHP.POLYGON2D):
|
316 |
case (SHP.MULTIPOINT2D):
|
317 |
return SUBTYPES.GEOM2D;
|
318 |
case (SHP.POINT3D):
|
319 |
case (SHP.POLYLINE3D):
|
320 |
case (SHP.POLYGON3D):
|
321 |
case (SHP.MULTIPOINT3D):
|
322 |
return SUBTYPES.GEOM3D;
|
323 |
case (SHP.POINTM):
|
324 |
case (SHP.POLYLINEM):
|
325 |
case (SHP.POLYGONM):
|
326 |
case (SHP.MULTIPOINTM):
|
327 |
return SUBTYPES.GEOM2DM;
|
328 |
} |
329 |
|
330 |
return SUBTYPES.UNKNOWN;
|
331 |
} |
332 |
|
333 |
/**
|
334 |
* Gets the geometry with the index provided.
|
335 |
* Set to synchronized to prevent concurrent threads issue (?)
|
336 |
*
|
337 |
* @param position
|
338 |
* @return
|
339 |
* @throws ReadException
|
340 |
* @throws CreateGeometryException
|
341 |
*/
|
342 |
public synchronized Geometry getGeometry(long position) |
343 |
throws ReadException, CreateGeometryException {
|
344 |
|
345 |
int shapeType;
|
346 |
bb.position(getPositionForRecord(position)); |
347 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
348 |
shapeType = bb.getInt(); |
349 |
|
350 |
// el shape tal con tema tal y n?mro tal es null
|
351 |
if (shapeType == SHP.NULL) {
|
352 |
return gtypeNull.create();
|
353 |
} |
354 |
|
355 |
// retrieve that shape.
|
356 |
// tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
|
357 |
switch (type) {
|
358 |
case (SHP.POINT2D):
|
359 |
case (SHP.POINT3D):
|
360 |
case (SHP.POINTM):
|
361 |
Point point = readPoint(bb);
|
362 |
fillPoint(point); |
363 |
return point;
|
364 |
|
365 |
case (SHP.POLYLINE2D):
|
366 |
Curve curve = (Curve) gtypeCurve2D.create(); |
367 |
fillCurve(curve); |
368 |
return curve;
|
369 |
|
370 |
case (SHP.POLYGON2D):
|
371 |
Surface surface = (Surface) gtypeSurface2D.create(); |
372 |
fillSurface(surface); |
373 |
return surface;
|
374 |
|
375 |
case (SHP.POLYLINE3D):
|
376 |
Curve curve3D = |
377 |
(Curve) gManager.create(TYPES.CURVE, SUBTYPES.GEOM3D); |
378 |
fillCurve(curve3D); |
379 |
fillZ(curve3D); |
380 |
return curve3D;
|
381 |
|
382 |
case (SHP.POLYGON3D):
|
383 |
Surface surface3D = |
384 |
(Surface) gManager.create(TYPES.SURFACE, SUBTYPES.GEOM3D); |
385 |
fillSurface(surface3D); |
386 |
fillZ(surface3D); |
387 |
return surface3D;
|
388 |
|
389 |
case (SHP.MULTIPOINT2D):
|
390 |
|
391 |
Point p = null; |
392 |
int numPoints;
|
393 |
int i;
|
394 |
int j;
|
395 |
bb.position(bb.position() + 32);
|
396 |
numPoints = bb.getInt(); |
397 |
|
398 |
Point[] points = new Point[numPoints]; |
399 |
|
400 |
for (i = 0; i < numPoints; i++) { |
401 |
points[i] = |
402 |
(Point) gManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
|
403 |
points[i].setX(bb.getDouble()); |
404 |
points[i].setY(bb.getDouble()); |
405 |
} |
406 |
|
407 |
MultiPoint multipoint = (MultiPoint) gtypeMultiPoint2D.create(); |
408 |
// MultiPoint multipoint =
|
409 |
// (MultiPoint)GeometryLocator.getGeometryManager().create(TYPES.MULTIPOINT,
|
410 |
// SUBTYPES.GEOM2D);
|
411 |
for (int k = 0; k < points.length; k++) { |
412 |
multipoint.addPoint(points[k]); |
413 |
} |
414 |
|
415 |
return multipoint;
|
416 |
|
417 |
case (SHP.MULTIPOINT3D):
|
418 |
bb.position(bb.position() + 32);
|
419 |
numPoints = bb.getInt(); |
420 |
|
421 |
double[] temX = new double[numPoints]; |
422 |
double[] temY = new double[numPoints]; |
423 |
double[] temZ = new double[numPoints]; |
424 |
|
425 |
for (i = 0; i < numPoints; i++) { |
426 |
temX[i] = bb.getDouble(); |
427 |
temY[i] = bb.getDouble(); |
428 |
// temZ[i] = bb.getDouble();
|
429 |
} |
430 |
|
431 |
for (i = 0; i < numPoints; i++) { |
432 |
temZ[i] = bb.getDouble(); |
433 |
} |
434 |
|
435 |
MultiPoint multipoint3D = |
436 |
(MultiPoint) gManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM3D); |
437 |
for (int k = 0; k < temX.length; k++) { |
438 |
Point pointAux =
|
439 |
(Point) gManager.create(TYPES.POINT, SUBTYPES.GEOM3D);
|
440 |
pointAux.setX(temX[k]); |
441 |
pointAux.setY(temY[k]); |
442 |
pointAux.setCoordinateAt(2, temZ[k]);
|
443 |
multipoint3D.addPoint(pointAux); |
444 |
} |
445 |
return multipoint3D;
|
446 |
} |
447 |
|
448 |
return null; |
449 |
} |
450 |
|
451 |
private void fillPoint(Point point) throws ReadException { |
452 |
if (subType == Geometry.SUBTYPES.GEOM3D) {
|
453 |
point.setCoordinateAt(Geometry.DIMENSIONS.Z, bb.getDouble()); |
454 |
} else
|
455 |
if (subType == Geometry.SUBTYPES.GEOM2DM) {
|
456 |
point.setCoordinateAt(2, bb.getDouble());
|
457 |
} |
458 |
} |
459 |
|
460 |
private void fillCurve(Curve curve) |
461 |
throws CreateGeometryException, ReadException {
|
462 |
Point p = null; |
463 |
int numParts;
|
464 |
int numPoints;
|
465 |
int i;
|
466 |
int j;
|
467 |
|
468 |
bb.position(bb.position() + 32);
|
469 |
numParts = bb.getInt(); |
470 |
numPoints = bb.getInt(); |
471 |
|
472 |
int[] tempParts = new int[numParts]; |
473 |
|
474 |
for (i = 0; i < numParts; i++) { |
475 |
tempParts[i] = bb.getInt(); |
476 |
} |
477 |
|
478 |
j = 0;
|
479 |
|
480 |
for (i = 0; i < numPoints; i++) { |
481 |
p = readPoint(bb); |
482 |
|
483 |
if (i == tempParts[j]) {
|
484 |
curve.addMoveToVertex(p); |
485 |
|
486 |
if (j < (numParts - 1)) { |
487 |
j++; |
488 |
} |
489 |
} else {
|
490 |
curve.addVertex(p); |
491 |
} |
492 |
} |
493 |
} |
494 |
|
495 |
private void fillSurface(Surface surface) |
496 |
throws CreateGeometryException, ReadException {
|
497 |
Point p = null; |
498 |
int numParts;
|
499 |
int numPoints;
|
500 |
int i;
|
501 |
int partIndex;
|
502 |
|
503 |
bb.position(bb.position() + 32);
|
504 |
numParts = bb.getInt(); |
505 |
numPoints = bb.getInt(); |
506 |
|
507 |
int[] tempParts = new int[numParts]; |
508 |
|
509 |
for (i = 0; i < numParts; i++) { |
510 |
tempParts[i] = bb.getInt(); |
511 |
} |
512 |
|
513 |
partIndex = 0;
|
514 |
|
515 |
for (i = 0; i < numPoints; i++) { |
516 |
p = readPoint(bb); |
517 |
|
518 |
if (i == tempParts[partIndex]) {
|
519 |
surface.addMoveToVertex(p); |
520 |
if (partIndex < (numParts - 1)) { |
521 |
partIndex++; |
522 |
} |
523 |
} else {
|
524 |
if ((i == tempParts[partIndex] - 1) || (i == numPoints - 1)) { |
525 |
surface.closePrimitive(); |
526 |
} else {
|
527 |
surface.addVertex(p); |
528 |
} |
529 |
} |
530 |
} |
531 |
} |
532 |
|
533 |
private void fillZ(OrientablePrimitive orientablePrimitive) |
534 |
throws CreateGeometryException, ReadException {
|
535 |
double[] boxZ = new double[2]; |
536 |
boxZ[0] = bb.getDouble();
|
537 |
boxZ[1] = bb.getDouble();
|
538 |
|
539 |
for (int i = 0; i < orientablePrimitive.getNumVertices(); i++) { |
540 |
orientablePrimitive.setCoordinateAt(i, 2, bb.getDouble());
|
541 |
} |
542 |
} |
543 |
|
544 |
private long getPositionForRecord(long numRec) { |
545 |
// shx file has a 100 bytes header, then, records
|
546 |
// 8 bytes length, one for each entity.
|
547 |
// first 4 bytes are the offset
|
548 |
// next 4 bytes are length
|
549 |
|
550 |
int posIndex = 100 + ((int) numRec * 8); |
551 |
// bbShx.position(posIndex);
|
552 |
long pos = 8 + 2 * bbShx.getInt(posIndex); |
553 |
|
554 |
return pos;
|
555 |
} |
556 |
|
557 |
/**
|
558 |
* Reads the Point from the shape file.
|
559 |
*
|
560 |
* @param in
|
561 |
* ByteBuffer.
|
562 |
*
|
563 |
* @return Point2D.
|
564 |
* @throws ReadException
|
565 |
* @throws CreateGeometryException
|
566 |
*/
|
567 |
private Point readPoint(BigByteBuffer2 in) |
568 |
throws CreateGeometryException, ReadException {
|
569 |
// bytes 1 to 4 are the type and have already been read.
|
570 |
// bytes 4 to 12 are the X coordinate
|
571 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
572 |
|
573 |
return Geometry.SUBTYPES.GEOM2D == subType ? gtypePoint2D.createPoint(
|
574 |
in.getDouble(), in.getDouble()) : gManager.createPoint( |
575 |
in.getDouble(), |
576 |
in.getDouble(), subType); |
577 |
} |
578 |
|
579 |
/**
|
580 |
* Lee un rect?ngulo del fichero.
|
581 |
*
|
582 |
* @param in
|
583 |
* ByteBuffer.
|
584 |
*
|
585 |
* @return Rect?ngulo.
|
586 |
* @throws CreateEnvelopeException
|
587 |
*
|
588 |
* @throws IOException
|
589 |
*/
|
590 |
private Envelope readRectangle(BigByteBuffer2 in)
|
591 |
throws CreateEnvelopeException {
|
592 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
593 |
double x = in.getDouble();
|
594 |
double y = in.getDouble();
|
595 |
|
596 |
double x2 = in.getDouble();
|
597 |
|
598 |
if (x2 - x == 0) { |
599 |
x2 += 0.2;
|
600 |
x -= 0.1;
|
601 |
} |
602 |
|
603 |
double y2 = in.getDouble();
|
604 |
|
605 |
if (y2 - y == 0) { |
606 |
y2 += 0.2;
|
607 |
y -= 0.1;
|
608 |
} |
609 |
Envelope tempEnvelope = |
610 |
gManager.createEnvelope(x, y, x2, y2, SUBTYPES.GEOM2D); |
611 |
return tempEnvelope;
|
612 |
} |
613 |
|
614 |
/**
|
615 |
* Gets the geometry bbox with the index provided.
|
616 |
* Set to synchronized to prevent concurrent threads issue (?)
|
617 |
*
|
618 |
* @param featureIndex
|
619 |
* @return
|
620 |
* @throws ReadException
|
621 |
* @throws CreateEnvelopeException
|
622 |
* @throws CreateGeometryException
|
623 |
*/
|
624 |
public synchronized Envelope getBoundingBox(long featureIndex) |
625 |
throws ReadException, CreateEnvelopeException, CreateGeometryException {
|
626 |
Point p = null; |
627 |
Envelope BoundingBox = null;
|
628 |
try {
|
629 |
bb.position(getPositionForRecord(featureIndex)); |
630 |
} catch (Exception e) { |
631 |
throw new ReadException("getBondingBox (" + featureIndex + ")", e); |
632 |
// logger.error(" Shapefile is corrupted. Drawing aborted. ="+e+
|
633 |
// " "+"index = "+index);
|
634 |
} |
635 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
636 |
|
637 |
int tipoShape = bb.getInt();
|
638 |
|
639 |
// AZABALA: si tipoShape viene con valores erroneos deja de funcionar
|
640 |
// el metodo getShape(i)
|
641 |
// if (tipoShape != SHP.NULL) {
|
642 |
// type = tipoShape;
|
643 |
|
644 |
// }
|
645 |
|
646 |
// retrieve that shape.
|
647 |
// tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
|
648 |
switch (tipoShape) {
|
649 |
case (SHP.POINT2D):
|
650 |
case (SHP.POINT3D):
|
651 |
p = readPoint(bb); |
652 |
BoundingBox = |
653 |
gManager.createEnvelope(p.getX() - 0.1, p.getY() - 0.1, |
654 |
p.getX() + 0.2, p.getY() + 0.2, SUBTYPES.GEOM2D); |
655 |
// new Rectangle2D.Double(p.getX() - 0.1,
|
656 |
// p.getY() - 0.1, 0.2, 0.2);
|
657 |
|
658 |
break;
|
659 |
|
660 |
case (SHP.POLYLINE2D):
|
661 |
case (SHP.POLYGON2D):
|
662 |
case (SHP.MULTIPOINT2D):
|
663 |
case (SHP.POLYLINE3D):
|
664 |
case (SHP.POLYGON3D):
|
665 |
case (SHP.MULTIPOINT3D):
|
666 |
|
667 |
// BoundingBox
|
668 |
BoundingBox = readRectangle(bb); |
669 |
|
670 |
break;
|
671 |
} |
672 |
|
673 |
return BoundingBox;
|
674 |
} |
675 |
|
676 |
/**
|
677 |
* @return
|
678 |
*/
|
679 |
public String getSRSParameters() { |
680 |
return this.srsParameters; |
681 |
} |
682 |
|
683 |
private void initSupportedGeometryTypes() throws ReadException { |
684 |
switch (this.getGeometryType()) { |
685 |
case Geometry.TYPES.POINT:
|
686 |
supportedGeometryTypes = |
687 |
new int[] { Geometry.TYPES.POINT, Geometry.TYPES.NULL }; |
688 |
break;
|
689 |
case Geometry.TYPES.MULTIPOINT:
|
690 |
supportedGeometryTypes = |
691 |
new int[] { Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL }; |
692 |
break;
|
693 |
case Geometry.TYPES.MULTICURVE:
|
694 |
supportedGeometryTypes = |
695 |
new int[] { Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE, |
696 |
Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE, |
697 |
Geometry.TYPES.SURFACE, Geometry.TYPES.NULL, |
698 |
Geometry.TYPES.MULTICURVE }; |
699 |
break;
|
700 |
case Geometry.TYPES.MULTISURFACE:
|
701 |
supportedGeometryTypes = |
702 |
new int[] { Geometry.TYPES.ELLIPSE, Geometry.TYPES.CIRCLE, |
703 |
Geometry.TYPES.SURFACE, Geometry.TYPES.NULL, |
704 |
Geometry.TYPES.MULTISURFACE }; |
705 |
break;
|
706 |
|
707 |
default:
|
708 |
supportedGeometryTypes = new int[] {}; |
709 |
} |
710 |
} |
711 |
|
712 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
713 |
for (int i = 0; i < supportedGeometryTypes.length; i++) { |
714 |
if (gvSIGgeometryType == supportedGeometryTypes[i]) {
|
715 |
return true; |
716 |
} |
717 |
} |
718 |
return false; |
719 |
} |
720 |
} |