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