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 @ 41860

History | View | Annotate | Download (25.1 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.Aggregate;
47
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
48
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
49
import org.gvsig.fmap.geom.exception.CreateGeometryException;
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
        Geometry geometry;
386
        Point point;
387
        switch (type) {
388
            case (SHP.POINT2D):
389
                point = readPoint(bb);
390
                return point;
391

    
392
            case (SHP.POINT3D):
393
                point = readPoint(bb);
394
                point.setCoordinateAt(
395
                        Geometry.DIMENSIONS.Z,
396
                        bb.getDouble()
397
                );
398
                return point;
399

    
400
            case (SHP.POINTM):
401
                point = readPoint(bb);
402
                point.setCoordinateAt(2, bb.getDouble());
403
                return point;
404

    
405
            case (SHP.POLYLINE2D):
406
                geometry = readGeometry(
407
                        Geometry.TYPES.CURVE,
408
                        Geometry.TYPES.MULTICURVE,
409
                        Geometry.SUBTYPES.GEOM2D);
410
                if (geometry == null) {
411
                    return getNullGeometry();
412
                }
413
                return geometry;
414

    
415
            case (SHP.POLYGON2D):
416
                geometry = readGeometry(
417
                        Geometry.TYPES.SURFACE,
418
                        Geometry.TYPES.MULTISURFACE,
419
                        Geometry.SUBTYPES.GEOM2D);
420
                if (geometry == null) {
421
                    return getNullGeometry();
422
                }
423
                return geometry;
424

    
425
            case (SHP.POLYLINE3D):
426
                geometry = readGeometry(
427
                        Geometry.TYPES.CURVE,
428
                        Geometry.TYPES.MULTICURVE,
429
                        Geometry.SUBTYPES.GEOM3D);
430
                if (geometry == null) {
431
                    return getNullGeometry();
432
                }
433
                return geometry;
434

    
435
            case (SHP.POLYGON3D):
436
                geometry = readGeometry(
437
                        Geometry.TYPES.SURFACE,
438
                        Geometry.TYPES.MULTISURFACE,
439
                        Geometry.SUBTYPES.GEOM3D);
440
                if (geometry == null) {
441
                    return getNullGeometry();
442
                }
443
                return geometry;
444

    
445
            case (SHP.MULTIPOINT2D):
446
                geometry = readMultiPoint(SUBTYPES.GEOM2D);
447
                if (geometry == null) {
448
                    return getNullGeometry();
449
                }
450
                return geometry;
451

    
452
            case (SHP.MULTIPOINT3D):
453
                geometry = readMultiPoint(SUBTYPES.GEOM3D);
454
                if (geometry == null) {
455
                    return getNullGeometry();
456
                }
457
                return geometry;
458
        }
459

    
460
        return null;
461
    }
462

    
463
    private Geometry readMultiPoint(int subtype) throws CreateGeometryException, ReadException {
464
        Point point;
465

    
466
        bb.position(bb.position() + 32);
467
        int numPoints = bb.getInt();
468
        
469
        if( numPoints < 1 ) {
470
            return null;
471
        }
472

    
473
//        Point[] points = new Point[numPoints];
474

    
475
        MultiPrimitive multi = (MultiPrimitive) gManager.create(Geometry.TYPES.MULTIPOINT, subtype);
476
        multi.ensureCapacity(numPoints);
477

    
478
        for (int i = 0; i < numPoints; i++) {
479
            point = (Point) gManager.create(TYPES.POINT, subtype);
480
            point.setX(bb.getDouble());
481
            point.setY(bb.getDouble());
482
            multi.addPrimitive(point);
483
        }
484

    
485
        if (subtype == Geometry.SUBTYPES.GEOM3D) {
486
            for (int i = 0; i < numPoints; i++) {
487
                double z = bb.getDouble();
488
                point = (Point) multi.getPrimitiveAt(i);
489
                point.setCoordinateAt(Geometry.DIMENSIONS.Z, z);
490
            }
491
        }
492
        if (multi.getPrimitivesNumber() == 0) {
493
            return null;
494
        }
495
        return multi;
496
    }
497

    
498
    private Geometry readGeometry(int type, int typemulti, int subtype)
499
            throws CreateGeometryException, ReadException {
500

    
501
        bb.position(bb.position() + 32);
502

    
503
        int numberOfMultis = bb.getInt();
504
        int numberOfPoints = bb.getInt();
505
        
506
        if( numberOfPoints<1  ) {
507
            return null;
508
        }
509
        if( numberOfMultis<1 ) {
510
            numberOfMultis=0;
511
        }
512

    
513
        int[] indexOfPointsOfMulti = new int[numberOfMultis+1];
514
        for (int i = 0; i < numberOfMultis; i++) {
515
            indexOfPointsOfMulti[i] = bb.getInt();
516
        }
517
        indexOfPointsOfMulti[numberOfMultis] = -1;
518
                
519
        MultiPrimitive multi = null;
520
        if (numberOfMultis > 1) {
521
            multi = (MultiPrimitive) gManager.create(typemulti, subtype);
522
        }
523
        OrientablePrimitive primitive = null;
524

    
525
        int nextSurface = 0;
526

    
527
        for (int i = 0; i < numberOfPoints; i++) {
528
            Point p = readPoint(subtype, bb);
529
            if (i == indexOfPointsOfMulti[nextSurface]) {
530
                // Cambiamos de poligono
531
                if( primitive != null ) {
532
                    if( multi != null ) {
533
                        multi.addPrimitive(primitive);
534
                    }
535
                }
536
                primitive = (OrientablePrimitive) gManager.create(type, subtype);
537
                primitive.addVertex(p);
538
                nextSurface++;
539
            } else {
540
                primitive.addVertex(p);
541
            }
542
        }
543
        if (multi != null) {
544
            multi.addPrimitive(primitive);
545
            if (subtype == Geometry.SUBTYPES.GEOM3D) {
546
                fillZ(multi);
547
            }
548
            return multi;
549
        }
550
        if (primitive==null || primitive.getNumVertices() < 1) {
551
            return null;
552
        }
553
        if (subtype == Geometry.SUBTYPES.GEOM3D) {
554
            fillZ(primitive);
555
        }
556
        return primitive;
557
    }
558

    
559
    private void fillZ(Geometry geometry)
560
            throws CreateGeometryException, ReadException {
561
        double[] boxZ = new double[2];
562
        boxZ[0] = bb.getDouble();
563
        boxZ[1] = bb.getDouble();
564

    
565
        if (geometry == null) {
566
            return;
567
        }
568
        if (geometry instanceof Aggregate) {
569
            Aggregate multi = (Aggregate) geometry;
570
            for (int i = 0; i < multi.getPrimitivesNumber(); i++) {
571
                OrientablePrimitive primitive = (OrientablePrimitive) geometry;
572
                for (int p = 0; p < primitive.getNumVertices(); p++) {
573
                    primitive.setCoordinateAt(p, 2, bb.getDouble());
574
                }
575
            }
576
        } else if (geometry instanceof OrientablePrimitive) {
577
            OrientablePrimitive primitive = (OrientablePrimitive) geometry;
578
            for (int p = 0; p < primitive.getNumVertices(); p++) {
579
                primitive.setCoordinateAt(p, 2, bb.getDouble());
580
            }
581
        } else {
582
            logger.warn("Geoemtry type '" + geometry.getClass().getName() + "'unexpected ");
583
        }
584
    }
585

    
586
    private long getPositionForRecord(long numRec) {
587
        // shx file has a 100 bytes header, then, records
588
        // 8 bytes length, one for each entity.
589
        // first 4 bytes are the offset
590
        // next 4 bytes are length
591

    
592
        int posIndex = 100 + ((int) numRec * 8);
593
        // bbShx.position(posIndex);
594
        long pos = 8 + 2 * bbShx.getInt(posIndex);
595

    
596
        return pos;
597
    }
598

    
599
    /**
600
     * Reads the Point from the shape file.
601
     *
602
     * @param in ByteBuffer.
603
     *
604
     * @return Point2D.
605
     * @throws ReadException
606
     * @throws CreateGeometryException
607
     */
608
    private Point readPoint(BigByteBuffer2 in)
609
            throws CreateGeometryException, ReadException {
610
        return readPoint(subType, in);
611
    }
612

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

    
624
    /**
625
     * Lee un rect?ngulo del fichero.
626
     *
627
     * @param in ByteBuffer.
628
     *
629
     * @return Rect?ngulo.
630
     * @throws CreateEnvelopeException
631
     *
632
     * @throws IOException
633
     */
634
    private Envelope readRectangle(BigByteBuffer2 in)
635
            throws CreateEnvelopeException {
636
        in.order(ByteOrder.LITTLE_ENDIAN);
637
        double x = in.getDouble();
638
        double y = in.getDouble();
639

    
640
        double x2 = in.getDouble();
641

    
642
        if (x2 - x == 0) {
643
            x2 += 0.2;
644
            x -= 0.1;
645
        }
646

    
647
        double y2 = in.getDouble();
648

    
649
        if (y2 - y == 0) {
650
            y2 += 0.2;
651
            y -= 0.1;
652
        }
653
        Envelope tempEnvelope
654
                = gManager.createEnvelope(x, y, x2, y2, SUBTYPES.GEOM2D);
655
        return tempEnvelope;
656
    }
657

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

    
681
        int tipoShape = bb.getInt();
682

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

    
700
                break;
701

    
702
            case (SHP.POLYLINE2D):
703
            case (SHP.POLYGON2D):
704
            case (SHP.MULTIPOINT2D):
705
            case (SHP.POLYLINE3D):
706
            case (SHP.POLYGON3D):
707
            case (SHP.MULTIPOINT3D):
708

    
709
                // BoundingBox
710
                BoundingBox = readRectangle(bb);
711

    
712
                break;
713
        }
714

    
715
        return BoundingBox;
716
    }
717

    
718
    /**
719
     * @return
720
     */
721
    public String getSRSParameters() {
722
        return this.srsParameters;
723
    }
724

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

    
749
            default:
750
                supportedGeometryTypes = new int[]{};
751
        }
752
    }
753

    
754
    public boolean canWriteGeometry(int gvSIGgeometryType) {
755
        for (int i = 0; i < supportedGeometryTypes.length; i++) {
756
            if (gvSIGgeometryType == supportedGeometryTypes[i]) {
757
                return true;
758
            }
759
        }
760
        return false;
761
    }
762
}