Statistics
| Revision:

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 / SHPFile2.java @ 41873

History | View | Annotate | Download (24.4 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.store.shp.utils;
24

    
25
import java.io.BufferedReader;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileReader;
29
import java.io.IOException;
30
import java.nio.ByteOrder;
31
import java.nio.channels.FileChannel;
32

    
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36
import org.gvsig.fmap.dal.exception.CloseException;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.FileNotFoundException;
39
import org.gvsig.fmap.dal.exception.ReadException;
40
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters;
41
import org.gvsig.fmap.geom.Geometry;
42
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
43
import org.gvsig.fmap.geom.Geometry.TYPES;
44
import org.gvsig.fmap.geom.GeometryLocator;
45
import org.gvsig.fmap.geom.GeometryManager;
46
import org.gvsig.fmap.geom.aggregate.MultiPoint;
47
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
48
import org.gvsig.fmap.geom.exception.CreateGeometryException;
49
import org.gvsig.fmap.geom.primitive.Curve;
50
import org.gvsig.fmap.geom.primitive.Envelope;
51
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
52
import org.gvsig.fmap.geom.primitive.Point;
53
import org.gvsig.fmap.geom.primitive.PointGeometryType;
54
import org.gvsig.fmap.geom.primitive.Surface;
55
import org.gvsig.fmap.geom.type.GeometryType;
56
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
57
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
58
import org.gvsig.utils.bigfile.BigByteBuffer2;
59

    
60
/**
61
 * @author jmvivo
62
 * 
63
 */
64
public class SHPFile2 implements ISHPFile {
65

    
66
    private static final Logger logger = LoggerFactory.getLogger(SHPFile2.class);
67
    private Envelope extent;
68
    private int type;
69
    private int subType;
70
    private String srsParameters;
71

    
72
    private FileInputStream fin;
73
    private FileChannel channel;
74
    private BigByteBuffer2 bb;
75
    private FileInputStream finShx;
76
    private FileChannel channelShx;
77
    private BigByteBuffer2 bbShx;
78

    
79
    private SHPStoreParameters params;
80

    
81
    private int[] supportedGeometryTypes;
82
    private final GeometryManager gManager = GeometryLocator
83
        .getGeometryManager();
84

    
85
    private GeometryType gtypeNull;
86
    private PointGeometryType gtypePoint2D;
87
    private GeometryType gtypeCurve2D;
88
    private GeometryType gtypeSurface2D;
89
    private GeometryType gtypeMultiPoint2D;
90

    
91
    private boolean useNullGeometry = false;
92
    
93
    private boolean allowInconsistenciesInGeometryTypeWarningShow = false;
94
    
95
    public SHPFile2(SHPStoreParameters params) {
96
        this.params = params;
97
        try {
98
            gtypeNull = gManager.getGeometryType(TYPES.NULL, SUBTYPES.GEOM2D);
99
            gtypePoint2D
100
                    = (PointGeometryType) gManager.getGeometryType(TYPES.POINT,
101
                    SUBTYPES.GEOM2D);
102
            gtypeCurve2D
103
                    = gManager.getGeometryType(TYPES.CURVE, SUBTYPES.GEOM2D);
104
            gtypeSurface2D
105
                    = gManager.getGeometryType(TYPES.SURFACE, SUBTYPES.GEOM2D);
106
            gtypeMultiPoint2D
107
                    = gManager.getGeometryType(TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
108

    
109
        } catch (GeometryTypeNotSupportedException e) {
110
            throw new RuntimeException(
111
                "Unable to get the 2D geometry types to use", e);
112
        } catch (GeometryTypeNotValidException e) {
113
            throw new RuntimeException(
114
                "Unable to get the 2D geometry types to use", e);
115
        }
116
    }
117

    
118
    public void setUseNullGeometry(boolean useNullGeometry) {
119
        this.useNullGeometry = useNullGeometry;
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
        logger.debug("Closing shp/shx file '"+this.params.getSHPFileName()+"'");
131
        
132
        // FIXME: Arreglar esto para que se acumulen los errores
133
        try {
134
            channel.close();
135
            channelShx.close();
136
        } catch (IOException e) {
137
            ret = new CloseException("SHPFile.close", e);
138
        } finally {
139
            try {
140
                fin.close();
141
                finShx.close();
142
            } catch (IOException e1) {
143
                ret = new CloseException("SHPFile.close", e1);
144
            }
145
        }
146

    
147
        if (ret != null) {
148
            throw ret;
149
        }
150
        bb = null;
151
        bbShx = null;
152
        fin = null;
153
        finShx = null;
154
        channel = null;
155
        channelShx = null;
156
        srsParameters = null;
157
    }
158

    
159
    public boolean isOpen() {
160
        return this.fin != null;
161
    }
162

    
163
    public synchronized void open() throws DataException {
164
        try {
165
            fin = new FileInputStream(this.params.getSHPFile());
166
        } catch (java.io.FileNotFoundException e) {
167
            throw new FileNotFoundException(this.params.getSHPFileName());
168
        }
169

    
170
        // Open the file and then get a channel from the stream
171
        channel = fin.getChannel();
172

    
173
        // long size = channel.size();
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
        // 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.warn("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
    public Geometry getNullGeometry() throws CreateGeometryException {
341
        if (this.useNullGeometry) {
342
            return gtypeNull.create();
343
        }
344
        return null;
345
    }
346
    
347
    /**
348
     * Gets the geometry with the index provided. Set to synchronized to prevent
349
     * concurrent threads issue (?)
350
     * 
351
     * @param position
352
     * @return
353
     * @throws ReadException
354
     * @throws CreateGeometryException
355
     */
356
    public synchronized Geometry getGeometry(long position)
357
        throws ReadException, CreateGeometryException {
358

    
359
        int shapeType;
360
        bb.position(getPositionForRecord(position));
361
        bb.order(ByteOrder.LITTLE_ENDIAN);
362
        shapeType = bb.getInt();
363
        
364
        if (shapeType == SHP.NULL) {
365
            return getNullGeometry();
366
        }
367
        
368
        /*
369
         * Inconsistency: this particular shape is not
370
         * of the expected type. This can be because the SHP file
371
         * is corrupt and it can cause "out of memory error"
372
         * because the code will possibly try to instantiate a
373
         * huge (absurd) array, so it's safer to return a null geometry
374
         */
375
        if (shapeType != type) {
376
            if( !allowInconsistenciesInGeometryTypeWarningShow ) {
377
                logger.warn("Geometry type of Shape ("+type+") does not match the geometry found ("+shapeType+") in the shape '"+this.params.getSHPFileName()+".");
378
                allowInconsistenciesInGeometryTypeWarningShow = true;
379
            }
380
            if( ! this.params.getAllowInconsistenciesInGeometryType() ) {
381
                return getNullGeometry();
382
            }
383
        }
384

    
385
        // retrieve that shape.
386
        // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
387
        switch (type) {
388
        case (SHP.POINT2D):
389
        case (SHP.POINT3D):
390
        case (SHP.POINTM):
391
            Point point = readPoint(bb);
392
            fillPoint(point);
393
            return point;
394

    
395
        case (SHP.POLYLINE2D):
396
            Curve curve = (Curve) gtypeCurve2D.create();
397
            fillCurve(curve);
398
            if( curve.getNumVertices()==0 ) {
399
                return getNullGeometry();
400
            }
401
            return curve;
402

    
403
        case (SHP.POLYGON2D):
404
            Surface surface = (Surface) gtypeSurface2D.create();
405
            fillSurface(surface);
406
            if( surface.getNumVertices()==0 ) {
407
                return getNullGeometry();
408
            }
409
            return surface;
410

    
411
        case (SHP.POLYLINE3D):
412
            Curve curve3D =
413
                (Curve) gManager.create(TYPES.CURVE, SUBTYPES.GEOM3D);
414
            fillCurve(curve3D);
415
            fillZ(curve3D);
416
            if( curve3D.getNumVertices()==0 ) {
417
                return getNullGeometry();
418
            }
419
            return curve3D;
420

    
421
        case (SHP.POLYGON3D):
422
            Surface surface3D =
423
                (Surface) gManager.create(TYPES.SURFACE, SUBTYPES.GEOM3D);
424
            fillSurface(surface3D);
425
            fillZ(surface3D);
426
            if( surface3D.getNumVertices()==0 ) {
427
                return getNullGeometry();
428
            }
429
            return surface3D;
430

    
431
        case (SHP.MULTIPOINT2D):
432

    
433
            Point p = null;
434
            int numPoints;
435
            int i;
436
            int j;
437
            bb.position(bb.position() + 32);
438
            numPoints = bb.getInt();
439

    
440
            Point[] points = new Point[numPoints];
441

    
442
            for (i = 0; i < numPoints; i++) {
443
                points[i] =
444
                    (Point) gManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
445
                points[i].setX(bb.getDouble());
446
                points[i].setY(bb.getDouble());
447
            }
448

    
449
            MultiPoint multipoint = (MultiPoint) gtypeMultiPoint2D.create();
450
            // MultiPoint multipoint =
451
            // (MultiPoint)GeometryLocator.getGeometryManager().create(TYPES.MULTIPOINT,
452
            // SUBTYPES.GEOM2D);
453
            for (int k = 0; k < points.length; k++) {
454
                multipoint.addPoint(points[k]);
455
            }
456

    
457
            if( multipoint.getPrimitivesNumber()==0 ) {
458
                return getNullGeometry();
459
            }
460
            return multipoint;
461

    
462
        case (SHP.MULTIPOINT3D):
463
            bb.position(bb.position() + 32);
464
            numPoints = bb.getInt();
465

    
466
            double[] temX = new double[numPoints];
467
            double[] temY = new double[numPoints];
468
            double[] temZ = new double[numPoints];
469

    
470
            for (i = 0; i < numPoints; i++) {
471
                temX[i] = bb.getDouble();
472
                temY[i] = bb.getDouble();
473
                // temZ[i] = bb.getDouble();
474
            }
475

    
476
            for (i = 0; i < numPoints; i++) {
477
                temZ[i] = bb.getDouble();
478
            }
479

    
480
            MultiPoint multipoint3D =
481
                (MultiPoint) gManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM3D);
482
            for (int k = 0; k < temX.length; k++) {
483
                Point pointAux =
484
                    (Point) gManager.create(TYPES.POINT, SUBTYPES.GEOM3D);
485
                pointAux.setX(temX[k]);
486
                pointAux.setY(temY[k]);
487
                pointAux.setCoordinateAt(2, temZ[k]);
488
                multipoint3D.addPoint(pointAux);
489
            }
490
            if( multipoint3D.getPrimitivesNumber()==0 ) {
491
                return getNullGeometry();
492
            }
493
            return multipoint3D;
494
        }
495

    
496
        return null;
497
    }
498

    
499
    private void fillPoint(Point point) throws ReadException {
500
        if (subType == Geometry.SUBTYPES.GEOM3D) {
501
            point.setCoordinateAt(Geometry.DIMENSIONS.Z, bb.getDouble());
502
        } else
503
            if (subType == Geometry.SUBTYPES.GEOM2DM) {
504
                point.setCoordinateAt(2, bb.getDouble());
505
            }
506
    }
507

    
508
    private void fillCurve(Curve curve)
509
        throws CreateGeometryException, ReadException {
510
        Point p = null;
511
        int numParts;
512
        int numPoints;
513
        int i;
514
        int j;
515

    
516
        bb.position(bb.position() + 32);
517
        numParts = bb.getInt();
518
        numPoints = bb.getInt();
519

    
520
        int[] tempParts = new int[numParts];
521

    
522
        for (i = 0; i < numParts; i++) {
523
            tempParts[i] = bb.getInt();
524
        }
525

    
526
        j = 0;
527

    
528
        for (i = 0; i < numPoints; i++) {
529
            p = readPoint(bb);
530

    
531
            if (i == tempParts[j]) {
532
                curve.addMoveToVertex(p);
533

    
534
                if (j < (numParts - 1)) {
535
                    j++;
536
                }
537
            } else {
538
                curve.addVertex(p);
539
            }
540
        }
541
    }
542
    
543
    private void fillSurface(Surface surface)
544
        throws CreateGeometryException, ReadException {
545
        Point p = null;
546
        int numParts;
547
        int numPoints;
548
        int i;
549
        int partIndex;
550

    
551
        bb.position(bb.position() + 32);
552
        numParts = bb.getInt();
553
        numPoints = bb.getInt();
554

    
555
        int[] tempParts = new int[numParts];
556

    
557
        for (i = 0; i < numParts; i++) {
558
            tempParts[i] = bb.getInt();
559
        }
560

    
561
        partIndex = 0;
562

    
563
        for (i = 0; i < numPoints; i++) {
564
            p = readPoint(bb);
565

    
566
            if (i == tempParts[partIndex]) {
567
                surface.addMoveToVertex(p);
568
                if (partIndex < (numParts - 1)) {
569
                    partIndex++;
570
                }
571
            } else {
572
                if ((i == tempParts[partIndex] - 1) || (i == numPoints - 1)) {
573
                    surface.closePrimitive();
574
                } else {
575
                    surface.addVertex(p);
576
                }
577
            }
578
        }
579
    }
580

    
581
    private void fillZ(OrientablePrimitive orientablePrimitive)
582
        throws CreateGeometryException, ReadException {
583
        double[] boxZ = new double[2];
584
        boxZ[0] = bb.getDouble();
585
        boxZ[1] = bb.getDouble();
586

    
587
        for (int i = 0; i < orientablePrimitive.getNumVertices(); i++) {
588
            orientablePrimitive.setCoordinateAt(i, 2, bb.getDouble());
589
        }
590
    }
591

    
592
    private long getPositionForRecord(long numRec) {
593
        // shx file has a 100 bytes header, then, records
594
        // 8 bytes length, one for each entity.
595
        // first 4 bytes are the offset
596
        // next 4 bytes are length
597

    
598
        int posIndex = 100 + ((int) numRec * 8);
599
        // bbShx.position(posIndex);
600
        long pos = 8 + 2 * bbShx.getInt(posIndex);
601

    
602
        return pos;
603
    }
604

    
605
    /**
606
     * Reads the Point from the shape file.
607
     * 
608
     * @param in ByteBuffer.
609
     * 
610
     * @return Point2D.
611
     * @throws ReadException
612
     * @throws CreateGeometryException
613
     */
614
    private Point readPoint(BigByteBuffer2 in)
615
        throws CreateGeometryException, ReadException {
616
        return readPoint(subType, in);
617
    }
618

    
619
    private Point readPoint(int subtype, BigByteBuffer2 in)
620
            throws CreateGeometryException, ReadException {
621
        // bytes 1 to 4 are the type and have already been read.
622
        // bytes 4 to 12 are the X coordinate
623
        in.order(ByteOrder.LITTLE_ENDIAN);
624
        double x = in.getDouble();
625
        double y = in.getDouble();
626
        Point p = gManager.createPoint(x, y, subtype);
627
        return p;
628
    }
629

    
630
    /**
631
     * Lee un rect?ngulo del fichero.
632
     * 
633
     * @param in ByteBuffer.
634
     * 
635
     * @return Rect?ngulo.
636
     * @throws CreateEnvelopeException
637
     * 
638
     * @throws IOException
639
     */
640
    private Envelope readRectangle(BigByteBuffer2 in)
641
        throws CreateEnvelopeException {
642
        in.order(ByteOrder.LITTLE_ENDIAN);
643
        double x = in.getDouble();
644
        double y = in.getDouble();
645

    
646
        double x2 = in.getDouble();
647

    
648
        if (x2 - x == 0) {
649
            x2 += 0.2;
650
            x -= 0.1;
651
        }
652

    
653
        double y2 = in.getDouble();
654

    
655
        if (y2 - y == 0) {
656
            y2 += 0.2;
657
            y -= 0.1;
658
        }
659
        Envelope tempEnvelope
660
                = gManager.createEnvelope(x, y, x2, y2, SUBTYPES.GEOM2D);
661
        return tempEnvelope;
662
    }
663

    
664
    /**
665
     * Gets the geometry bbox with the index provided. Set to synchronized to
666
     * prevent concurrent threads issue (?)
667
     * 
668
     * @param featureIndex
669
     * @return
670
     * @throws ReadException
671
     * @throws CreateEnvelopeException
672
     * @throws CreateGeometryException
673
     */
674
    public synchronized Envelope getBoundingBox(long featureIndex)
675
        throws ReadException, CreateEnvelopeException, CreateGeometryException {
676
        Point p;
677
        Envelope BoundingBox = null;
678
        try {
679
            bb.position(getPositionForRecord(featureIndex));
680
        } catch (Exception e) {
681
            throw new ReadException("getBondingBox (" + featureIndex + ")", e);
682
            // logger.error(" Shapefile is corrupted. Drawing aborted. ="+e+
683
            // "  "+"index = "+index);
684
        }
685
        bb.order(ByteOrder.LITTLE_ENDIAN);
686

    
687
        int tipoShape = bb.getInt();
688

    
689
        // AZABALA: si tipoShape viene con valores erroneos deja de funcionar
690
        // el metodo getShape(i)
691
        // if (tipoShape != SHP.NULL) {
692
        // type = tipoShape;
693
        // }
694
        // retrieve that shape.
695
        // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
696
        switch (tipoShape) {
697
        case (SHP.POINT2D):
698
        case (SHP.POINT3D):
699
            p = readPoint(bb);
700
                BoundingBox
701
                        = gManager.createEnvelope(p.getX() - 0.1, p.getY() - 0.1,
702
                    p.getX() + 0.2, p.getY() + 0.2, SUBTYPES.GEOM2D);
703
            // new Rectangle2D.Double(p.getX() - 0.1,
704
            // p.getY() - 0.1, 0.2, 0.2);
705

    
706
            break;
707

    
708
        case (SHP.POLYLINE2D):
709
        case (SHP.POLYGON2D):
710
        case (SHP.MULTIPOINT2D):
711
        case (SHP.POLYLINE3D):
712
        case (SHP.POLYGON3D):
713
        case (SHP.MULTIPOINT3D):
714

    
715
            // BoundingBox
716
            BoundingBox = readRectangle(bb);
717

    
718
            break;
719
        }
720

    
721
        return BoundingBox;
722
    }
723

    
724
    /**
725
     * @return
726
     */
727
    public String getSRSParameters() {
728
        return this.srsParameters;
729
    }
730

    
731
    private void initSupportedGeometryTypes() throws ReadException {
732
        switch (this.getGeometryType()) {
733
        case Geometry.TYPES.POINT:
734
                supportedGeometryTypes
735
                        = new int[]{Geometry.TYPES.POINT, Geometry.TYPES.NULL};
736
            break;
737
        case Geometry.TYPES.MULTIPOINT:
738
                supportedGeometryTypes
739
                        = new int[]{Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL};
740
            break;
741
        case Geometry.TYPES.MULTICURVE:
742
                supportedGeometryTypes
743
                        = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE,
744
                    Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE,
745
                    Geometry.TYPES.SURFACE, Geometry.TYPES.NULL,
746
                            Geometry.TYPES.MULTICURVE};
747
            break;
748
        case Geometry.TYPES.MULTISURFACE:
749
                supportedGeometryTypes
750
                        = new int[]{Geometry.TYPES.ELLIPSE, Geometry.TYPES.CIRCLE,
751
                    Geometry.TYPES.SURFACE, Geometry.TYPES.NULL,
752
                            Geometry.TYPES.MULTISURFACE};
753
            break;
754

    
755
        default:
756
                supportedGeometryTypes = new int[]{};
757
        }
758
    }
759

    
760
    public boolean canWriteGeometry(int gvSIGgeometryType) {
761
        for (int i = 0; i < supportedGeometryTypes.length; i++) {
762
            if (gvSIGgeometryType == supportedGeometryTypes[i]) {
763
                return true;
764
            }
765
        }
766
        return false;
767
    }
768
}