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