Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / file / shp / SHPStore.java @ 20437

History | View | Annotate | Download (20.8 KB)

1
package org.gvsig.data.datastores.vectorial.file.shp;
2

    
3
import java.awt.geom.Point2D;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.IOException;
8
import java.lang.ref.WeakReference;
9
import java.nio.ByteOrder;
10
import java.nio.channels.FileChannel;
11
import java.util.Collection;
12
import java.util.Iterator;
13
import java.util.List;
14

    
15
import org.gvsig.data.DataManager;
16
import org.gvsig.data.IDataCollection;
17
import org.gvsig.data.IDataExplorer;
18
import org.gvsig.data.IDataExplorerParameters;
19
import org.gvsig.data.IDataStoreParameters;
20
import org.gvsig.data.datastores.vectorial.IFeaturesWriter;
21
import org.gvsig.data.datastores.vectorial.file.DataExplorerFileParameters;
22
import org.gvsig.data.datastores.vectorial.file.dbf.DBFDataExplorer;
23
import org.gvsig.data.datastores.vectorial.file.dbf.DBFStore;
24
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHP;
25
import org.gvsig.data.datastores.vectorial.file.shp.utils.ShapeFileHeader2;
26
import org.gvsig.data.exception.CloseException;
27
import org.gvsig.data.exception.InitializeException;
28
import org.gvsig.data.exception.OpenException;
29
import org.gvsig.data.exception.ReadException;
30
import org.gvsig.data.exception.WriteException;
31
import org.gvsig.data.spatialprovisional.Extent;
32
import org.gvsig.data.spatialprovisional.IExtent;
33
import org.gvsig.data.vectorial.DefaultAttributeDescriptor;
34
import org.gvsig.data.vectorial.FeatureStoreNotification;
35
import org.gvsig.data.vectorial.IFeature;
36
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
37
import org.gvsig.data.vectorial.IFeatureCollection;
38
import org.gvsig.data.vectorial.IFeatureID;
39
import org.gvsig.data.vectorial.IFeatureType;
40
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
41
import org.gvsig.exceptions.BaseException;
42
import org.gvsig.metadata.IMetadata;
43
import org.gvsig.metadata.IMetadataManager;
44
import org.gvsig.metadata.MetadataManager;
45

    
46
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
47
import com.iver.cit.gvsig.fmap.core.FShape;
48
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
49
import com.iver.cit.gvsig.fmap.core.IGeometry;
50
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
51
import com.iver.utiles.bigfile.BigByteBuffer2;
52

    
53
public class SHPStore extends DBFStore{
54
        public static String DATASTORE_NAME = "SHPStore";
55
        private SHPStoreParameters shpParameters=null;
56
        private File fileShp;
57
        private FileInputStream fin;
58
        private FileChannel channel;
59
        private BigByteBuffer2 bb;
60
        private File fileShx;
61
        private FileInputStream finShx;
62
        private FileChannel channelShx;
63
        private BigByteBuffer2 bbShx;
64
        private Extent extent;
65
        private int type;
66

    
67

    
68
         public void init(IDataStoreParameters parameters) throws InitializeException {
69
                 super.init(parameters);
70
                        featureType=super.getDefaultFeatureType();
71

    
72
                        DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor();
73
                        try {
74
                                dad.loading();
75
                                dad.setName("GEOMETRY");
76
                                dad.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY);
77
                                dad.stopLoading();
78
                        } catch (IsNotAttributeSettingException e1) {
79
                                // TODO Auto-generated catch block
80
                                e1.printStackTrace();
81
                        }
82
                featureType.add(dad);
83
                featureType.setDefaultGeometry("GEOMETRY");
84

    
85
                SHPStoreParameters shpParameters=(SHPStoreParameters)parameters;
86

    
87
                        fileShp = shpParameters.getFile();
88

    
89
                        try {
90
                                fin = new FileInputStream(fileShp);
91

    
92

    
93
                        // Open the file and then get a channel from the stream
94
                        channel = fin.getChannel();
95

    
96
                        // long size = channel.size();
97

    
98
                        // Get the file's size and then map it into memory
99
                        // bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
100
                bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY);
101
                fileShx=SHP.getShxFile(shpParameters.getFile());
102
                finShx = new FileInputStream(fileShx);
103

    
104
                // Open the file and then get a channel from the stream
105
                channelShx = finShx.getChannel();
106

    
107
//                long sizeShx = channelShx.size();
108

    
109
                // Get the file's size and then map it into memory
110
                // bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
111
                // bbShx = channelShx.map(FileChannel.MapMode.READ_ONLY, 0, sizeShx);
112
                bbShx = new BigByteBuffer2(channelShx, FileChannel.MapMode.READ_ONLY);
113
                bbShx.order(ByteOrder.BIG_ENDIAN);
114
                        } catch (FileNotFoundException e) {
115
                                throw new OpenException(getName(),e);
116
                        } catch (IOException e) {
117
                                throw new OpenException(getName(),e);
118
                        }
119

    
120
            }
121

    
122

    
123
//         private IFeatureAttributeDescriptor createFeatureAttribute(int i) {
124
//                char fieldType = dbf.getFieldType(i);
125
//                DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor();
126
//                dad.setOrdinal(i);
127
//                dad.setName(dbf.getFieldName(i));
128
//                dad.setSize(dbf.getFieldLength(i));
129
//                if (fieldType == 'L') {
130
//                        dad.setType(IFeatureAttributeDescriptor.TYPE_BOOLEAN);
131
//
132
//                } else if ((fieldType == 'F') || (fieldType == 'N')) {
133
//                        int precision = dbf.getFieldDecimalLength(i);
134
//                        if (precision > 0){
135
//                                dad.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
136
//                                dad.setPrecision(precision);
137
//                        } else{
138
//                                dad.setType(IFeatureAttributeDescriptor.TYPE_INT);
139
//                        }
140
//                } else if (fieldType == 'C') {
141
//                        dad.setType(IFeatureAttributeDescriptor.TYPE_STRING);
142
//                } else if (fieldType == 'D') {
143
//                        dad.setType(IFeatureAttributeDescriptor.TYPE_DATE);
144
//                } else {
145
////                    throw new BadFieldDriverException(getName(),null,String.valueOf(fieldType));
146
//                }
147
//                      return dad;
148
//
149
//            }
150

    
151
        protected void doFinishEdition() throws WriteException, ReadException {
152
                IFeaturesWriter writer = getFeaturesWriter();
153
        writer.init(this);
154
        writer.updateFeatureType(featureType);
155
        writer.preProcess();
156
        Collection collection=getDataCollection();
157
        Iterator iterator=collection.iterator();
158
        IFeature feature;
159
        while (iterator.hasNext()) {
160
                feature= (IFeature) iterator.next();
161
                        writer.insertFeature(feature);
162
                }
163
        writer.postProcess();
164

    
165
        }
166

    
167
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
168
                if (type==null){
169
                        type=getDefaultFeatureType();
170
                }
171
                IFeatureCollection coll;
172
                if (order == null){
173
                        coll=new ShpFeatureCollection(featureManager,this,type,filter);
174
                }else{
175
                        coll=new ShpFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
176
                }
177
                this.addObserver(new WeakReference(coll));
178
                return coll;
179
        }
180

    
181
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
182
                if (this.alterMode){
183
                    if (featureManager.contains(id)) {
184
                            return featureManager.getFeature(id);
185
                    }
186
            }
187
                long position=((ShpFeatureID)id).getIndex();
188
                ShpFeature feature=new ShpFeature(featureType,this,position);
189
                return feature;
190
//                return getFeatureByPosition(featureType,((ShpFeatureID)id).getIndex());
191
        }
192

    
193
        public List getFeatureTypes() {
194
                featureTypes.set(0,getDefaultFeatureType());
195
        return featureTypes;
196
        }
197

    
198
        public IFeatureType getDefaultFeatureType() {
199
                IFeatureType ft = featureType;
200
            if (isEditing()){
201
//                    Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
202
                    return attributeManager.getFeatureType(ft);
203
            }
204
        return ft;
205
        }
206

    
207
        public boolean isWithDefaultLegend() {
208
                return false;
209
        }
210

    
211
        public Object getDefaultLegend() {
212
                return null;
213
        }
214

    
215
        public Object getDefaultLabelingStrategy() {
216
                return null;
217
        }
218

    
219
        public boolean canAlterFeatureType() {
220
                return true;
221
        }
222

    
223
        public String getName() {
224
                return DATASTORE_NAME;
225
        }
226

    
227
        public void open() throws OpenException {
228
                this.observable.notifyObservers(
229
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_OPEN)
230
                );
231
                super.open();
232
//                 create a new header.
233
                ShapeFileHeader2 myHeader = new ShapeFileHeader2();
234

    
235
                bb.position(0);
236

    
237
                // read the header
238
                myHeader.readHeader(bb);
239

    
240
                extent = new Extent(myHeader.myXmin, myHeader.myYmin,myHeader.myXmax,myHeader.myYmax);
241
//                extent = new Rectangle2D.Double(myHeader.myXmin, myHeader.myYmin,
242
//                                myHeader.myXmax - myHeader.myXmin,
243
//                                myHeader.myYmax - myHeader.myYmin);
244

    
245
                type = myHeader.myShapeType;
246

    
247
                double x = myHeader.myXmin;
248
                double y = myHeader.myYmin;
249
                double w = myHeader.myXmax - myHeader.myXmin;
250
                double h = myHeader.myYmax - myHeader.myYmin;
251

    
252
                if (w == 0) {
253
                        x -= 0.1;
254
                        w = 0.2;
255
                }
256

    
257
                if (h == 0) {
258
                        y -= 0.1;
259
                        h = 0.2;
260
                }
261
                featureType.setGeometryTypes(new int[]{getGeometryType()});
262
                // String strFichDbf = m_Path.toLowerCase().replaceAll("\\.shp", ".dbf");
263
//                String strFichDbf = fileShp.getAbsolutePath().replaceAll("\\.shp", ".dbf");
264
//                strFichDbf = strFichDbf.replaceAll("\\.SHP", ".DBF");
265
//
266
//                DbaseFileNIO m_FichDbf = new DbaseFileNIO();
267
//
268
//                try {
269
//                        m_FichDbf.open(new File(strFichDbf));
270
//                } catch (IOException e) {
271
////                        throw new FileNotFoundDriverException(getName(),e,strFichDbf);
272
//                }
273
//                numReg = m_FichDbf.getRecordCount();
274
                this.observable.notifyObservers(
275
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_OPEN)
276
                );
277

    
278
        }
279

    
280
        public void close() throws CloseException {
281
                this.observable.notifyObservers(
282
                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CLOSE)
283
            );
284
                super.close();
285
                CloseException ret = null;
286

    
287
                try {
288
                        channel.close();
289
            channelShx.close();
290
                } catch (IOException e) {
291
                        ret = new CloseException(getName(),e);
292
                } finally {
293
                        try {
294
                                fin.close();
295
                        } catch (IOException e1) {
296
                                ret = new CloseException(getName(),e1);
297
                        }
298
                }
299

    
300
//                if (ret != null) {
301
//                        throw ret;
302
//                }
303
                bb = null;
304
                bbShx = null;
305
        this.observable.notifyObservers(
306
                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CLOSE)
307
            );
308

    
309
        }
310

    
311
        public void dispose() {
312
                this.observable.notifyObservers(
313
                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DISPOSE)
314
            );
315
                super.dispose();
316
        this.observable.notifyObservers(
317
                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DISPOSE)
318
            );
319
        }
320

    
321
        public boolean isEditable() {
322
                if (super.isEditable() && fileShp.canWrite() && fileShx.canWrite()) return true;
323
                return false;
324
        }
325

    
326
        public IMetadata getMetadata() throws BaseException {
327
                if (metadata==null){
328
                        IMetadataManager manager=MetadataManager.getManager();
329
                        metadata=manager.create(DATASTORE_NAME);
330
                        IExtent extent=getFullExtent();
331
                        metadata.set("extent",extent);
332
                        String srs=getSRS();
333
                        metadata.set("srs",srs);
334
                }
335
                if (this.alterMode){
336
                        IExtent extent=(IExtent)metadata.get("extent");
337
                        IFeatureCollection featureCollection=(IFeatureCollection)getDataCollection();
338
                    if (spatialManager.isFullExtentDirty()){
339
                            if (!featureCollection.isEmpty()){
340
                                    Iterator featureIterator=featureCollection.iterator();
341
                                    extent = ((IFeature)featureIterator.next()).getExtent();
342
                                    while(featureIterator.hasNext()){
343
                                            IFeature feature=(IFeature)featureIterator.next();
344
                                            IExtent boundExtent=feature.getExtent();
345
                                            if (boundExtent!=null)
346
                                                    extent.add(boundExtent);
347
                                    }
348
                            }
349
                    }
350
                    metadata.set("extent",extent);
351
                }
352
                return metadata;
353
        }
354
//        protected IFeature getFeatureByPosition(IFeatureType featureType,long position) throws ReadException {
355
//                ShpFeature feature=new ShpFeature(featureType,this,position);
356
//                feature.load(dbf, this.getGeometry(position));
357
//                return feature;
358
//        }
359

    
360
        protected IFeaturesWriter getFeaturesWriter() {
361
                IFeaturesWriter writer = new ShpFeaturesWriter();
362
//                writer.init(this);
363
                return writer;
364
        }
365
        protected long getFeatureCount(){
366
                return dbf.getRecordCount();
367
        }
368

    
369

    
370
        public IDataStoreParameters getParameters() {
371
                return parameters;
372
        }
373
        synchronized IGeometry getGeometry(long position) {
374
                Point2D p = new Point2D.Double();
375
                int numParts;
376
                int numPoints;
377
                int i;
378
                int j;
379
                int shapeType;
380

    
381
            bb.position(getPositionForRecord(position));
382
                bb.order(ByteOrder.LITTLE_ENDIAN);
383

    
384
                shapeType = bb.getInt();
385
                //el shape tal con tema tal y n?mro tal es null
386
                if (shapeType==SHP.NULL){
387
                        return new FNullGeometry();
388
                }
389

    
390
                // retrieve that shape.
391
                // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
392
                switch (type) {
393
                        case (SHP.POINT2D):
394
                                p = readPoint(bb);
395

    
396
                                return ShapeFactory.createPoint2D(p.getX(), p.getY());
397

    
398
                        case (SHP.POLYLINE2D):
399

    
400
                                bb.position(bb.position() + 32);
401
                                numParts = bb.getInt();
402
                                numPoints = bb.getInt();
403

    
404
                                // part indexes.
405
                                // Geometry geom = GeometryFactory.toGeometryArray();
406
                                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
407
                                                numPoints);
408

    
409
                                int[] tempParts = new int[numParts];
410

    
411
                                for (i = 0; i < numParts; i++) {
412
                                        tempParts[i] = bb.getInt();
413
                                }
414

    
415
                                j = 0;
416

    
417
                                for (i = 0; i < numPoints; i++) {
418
                                        p = readPoint(bb);
419

    
420
                                        if (i == tempParts[j]) {
421
                                                elShape.moveTo(p.getX(), p.getY());
422

    
423
                                                if (j < (numParts - 1)) {
424
                                                        j++;
425
                                                }
426
                                        } else {
427
                                                elShape.lineTo(p.getX(), p.getY());
428
                                        }
429
                                }
430

    
431
                                return ShapeFactory.createPolyline2D(elShape);
432

    
433
                        case (SHP.POLYGON2D):
434

    
435
                                //                            BoundingBox = readRectangle(bb);
436
//                                bb.getDouble();
437
//                                bb.getDouble();
438
//                                bb.getDouble();
439
//                                bb.getDouble();
440
                                bb.position(bb.position() + 32);
441
                                numParts = bb.getInt();
442

    
443
                                numPoints = bb.getInt();
444

    
445
                                // part indexes.
446
                                // Geometry geom = GeometryFactory.toGeometryArray();
447
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
448

    
449
                                tempParts = new int[numParts];
450

    
451
                                for (i = 0; i < numParts; i++) {
452
                                        tempParts[i] = bb.getInt();
453
                                }
454

    
455
                                j = 0;
456

    
457
                                for (i = 0; i < numPoints; i++) {
458
                                        p = readPoint(bb);
459

    
460
                                        if (i == tempParts[j]) {
461
                                                elShape.moveTo(p.getX(), p.getY());
462

    
463
                                                if (j < (numParts - 1)) {
464
                                                        j++;
465
                                                }
466
                                        } else {
467
                                                if (i==numPoints-1){
468
                                                        elShape.closePath();
469
                                                }else{
470
                                                        elShape.lineTo(p.getX(), p.getY());
471
                                                }
472
                                        }
473
                                }
474

    
475
                                return ShapeFactory.createPolygon2D(elShape);
476

    
477
                        case (SHP.POINT3D):
478

    
479
                                double x = bb.getDouble();
480
                                double y = bb.getDouble();
481
                                double z = bb.getDouble();
482

    
483
                                return ShapeFactory.createPoint3D(x, y, z);
484

    
485
                        case (SHP.POLYLINE3D):
486
                                bb.position(bb.position() + 32);
487
                                numParts = bb.getInt();
488
                                numPoints = bb.getInt();
489
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
490
                                tempParts = new int[numParts];
491

    
492
                                for (i = 0; i < numParts; i++) {
493
                                        tempParts[i] = bb.getInt();
494
                                }
495

    
496
                                j = 0;
497

    
498
                                for (i = 0; i < numPoints; i++) {
499
                                        p = readPoint(bb);
500

    
501
                                        if (i == tempParts[j]) {
502
                                                elShape.moveTo(p.getX(), p.getY());
503

    
504
                                                if (j < (numParts - 1)) {
505
                                                        j++;
506
                                                }
507
                                        } else {
508
                                                elShape.lineTo(p.getX(), p.getY());
509
                                        }
510
                                }
511

    
512
                                double[] boxZ = new double[2];
513
                                boxZ[0] = bb.getDouble();
514
                                boxZ[1] = bb.getDouble();
515

    
516
                                double[] pZ = new double[numPoints];
517

    
518
                                for (i = 0; i < numPoints; i++) {
519
                                        pZ[i] = bb.getDouble();
520
                                }
521

    
522
                                return ShapeFactory.createPolyline3D(elShape, pZ);
523
                        case (SHP.POLYGON3D):
524
                        bb.position(bb.position() + 32);
525
                        numParts = bb.getInt();
526
                        numPoints = bb.getInt();
527
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
528
                        tempParts = new int[numParts];
529

    
530
                        for (i = 0; i < numParts; i++) {
531
                                tempParts[i] = bb.getInt();
532
                        }
533

    
534
                        j = 0;
535

    
536
                        for (i = 0; i < numPoints; i++) {
537
                                p = readPoint(bb);
538

    
539
                                if (i == tempParts[j]) {
540
                                        elShape.moveTo(p.getX(), p.getY());
541

    
542
                                        if (j < (numParts - 1)) {
543
                                                j++;
544
                                        }
545
                                } else {
546
                                        if (i==numPoints-1){
547
                                                elShape.closePath();
548
                                        }else{
549
                                                elShape.lineTo(p.getX(), p.getY());
550
                                        }
551
                                }
552
                        }
553

    
554
                        double[] boxpoZ = new double[2];
555
                        boxpoZ[0] = bb.getDouble();
556
                        boxpoZ[1] = bb.getDouble();
557

    
558
                        double[] poZ = new double[numPoints];
559

    
560
                        for (i = 0; i < numPoints; i++) {
561
                                poZ[i] = bb.getDouble();
562
                        }
563

    
564
                        return ShapeFactory.createPolygon3D(elShape, poZ);
565

    
566
                        case (SHP.MULTIPOINT2D):
567
                                bb.position(bb.position() + 32);
568
                                numPoints = bb.getInt();
569

    
570
                                double[] tempX = new double[numPoints];
571
                                double[] tempY = new double[numPoints];
572

    
573
                                for (i = 0; i < numPoints; i++) {
574
                                        tempX[i] = bb.getDouble();
575
                                        tempY[i] = bb.getDouble();
576
                                }
577

    
578
                                return ShapeFactory.createMultipoint2D(tempX, tempY);
579

    
580
                        case (SHP.MULTIPOINT3D):
581
                                bb.position(bb.position() + 32);
582
                                numPoints = bb.getInt();
583

    
584
                                double[] temX = new double[numPoints];
585
                                double[] temY = new double[numPoints];
586
                                double[] temZ = new double[numPoints];
587

    
588
                                for (i = 0; i < numPoints; i++) {
589
                                        temX[i] = bb.getDouble();
590
                                        temY[i] = bb.getDouble();
591
                                        //temZ[i] = bb.getDouble();
592
                                }
593

    
594
                                for (i = 0; i < numPoints; i++) {
595
                                        temZ[i] = bb.getDouble();
596
                                }
597
                                return ShapeFactory.createMultipoint3D(temX, temY, temZ);
598
                }
599

    
600
                return null;
601

    
602

    
603
        }
604
        private long getPositionForRecord(long numRec)
605
    {
606
        // shx file has a 100 bytes header, then, records
607
        // 8 bytes length, one for each entity.
608
        // first 4 bytes are the offset
609
        // next 4 bytes are length
610

    
611
        int posIndex = 100 + ((int)numRec * 8);
612
        // bbShx.position(posIndex);
613
        long pos = 8 + 2* bbShx.getInt(posIndex);
614

    
615
        return pos;
616
    }
617
        /**
618
         * Reads the Point from the shape file.
619
         *
620
         * @param in ByteBuffer.
621
         *
622
         * @return Point2D.
623
         */
624
        private synchronized Point2D readPoint(BigByteBuffer2 in) {
625
                // create a new point
626
                Point2D.Double tempPoint = new Point2D.Double();
627

    
628
                // bytes 1 to 4 are the type and have already been read.
629
                // bytes 4 to 12 are the X coordinate
630
                in.order(ByteOrder.LITTLE_ENDIAN);
631
                tempPoint.setLocation(in.getDouble(), in.getDouble());
632

    
633
                return tempPoint;
634
        }
635
        /**
636
         * Lee un rect?ngulo del fichero.
637
         *
638
         * @param in ByteBuffer.
639
         *
640
         * @return Rect?ngulo.
641
         *
642
         * @throws IOException
643
         */
644
        private synchronized IExtent readRectangle(BigByteBuffer2 in){
645

    
646
                in.order(ByteOrder.LITTLE_ENDIAN);
647
                double x = in.getDouble();
648
                double y = in.getDouble();
649

    
650
                double x2 = in.getDouble();
651

    
652
                if (x2-x == 0) {
653
                        x2 = 0.2;
654
                        x -= 0.1;
655
                }
656

    
657
                double y2 = in.getDouble();
658

    
659
                if (y2-y == 0) {
660
                        y2 = 0.2;
661
                        y -= 0.1;
662
                }
663
                IExtent tempRect = new Extent(x,y,x2,y2);
664
                return tempRect;
665
        }
666
        public IExtent getBoundingBox(long featureIndex) {
667
                Point2D p = new Point2D.Double();
668
                IExtent BoundingBox = null;
669
                try {
670
                        bb.position(getPositionForRecord(featureIndex));
671
                }catch (Exception e) {
672
                        e.printStackTrace();
673
//                        logger.error(" Shapefile is corrupted. Drawing aborted. ="+e+ "  "+"index = "+index);
674
                }
675
                bb.order(ByteOrder.LITTLE_ENDIAN);
676

    
677
                int tipoShape = bb.getInt();
678

    
679
                //AZABALA: si tipoShape viene con valores erroneos deja de funcionar
680
                //el metodo getShape(i)
681
//                if (tipoShape != SHP.NULL) {
682
//                        type = tipoShape;
683
//
684
//                }
685

    
686
                // retrieve that shape.
687
                // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
688
                switch (tipoShape) {
689
                        case (SHP.POINT2D):
690
                        case (SHP.POINT3D):
691
                                p = readPoint(bb);
692
                                BoundingBox = new Extent(p.getX() - 0.1, p.getY() - 0.1,p.getX()+0.2,p.getY()+0.2);
693
//                                        new Rectangle2D.Double(p.getX() - 0.1,
694
//                                                p.getY() - 0.1, 0.2, 0.2);
695

    
696
                                break;
697

    
698
                        case (SHP.POLYLINE2D):
699
                        case (SHP.POLYGON2D):
700
                        case (SHP.MULTIPOINT2D):
701
                        case (SHP.POLYLINE3D):
702
                        case (SHP.POLYGON3D):
703
                        case (SHP.MULTIPOINT3D):
704

    
705
                                // BoundingBox
706
                                BoundingBox = readRectangle(bb);
707

    
708
                                break;
709
                }
710

    
711
                return BoundingBox;
712
}
713
private int getGeometryType() {
714
        int auxType = 0;
715

    
716
        switch (type) {
717
                case (SHP.POINT2D):
718
                case (SHP.POINT3D):
719
                        auxType = auxType | FShape.POINT;
720

    
721
                        break;
722

    
723
                case (SHP.POLYLINE2D):
724
                case (SHP.POLYLINE3D):
725
                        auxType = auxType | FShape.LINE;
726

    
727
                        break;
728

    
729
                case (SHP.POLYGON2D):
730
                case (SHP.POLYGON3D):
731
                        auxType = auxType | FShape.POLYGON;
732

    
733
                        break;
734
                case (SHP.MULTIPOINT2D):
735
                case (SHP.MULTIPOINT3D):
736
                        auxType = auxType | FShape.MULTIPOINT;
737

    
738
                        break;
739
        }
740

    
741
        return auxType;
742
}
743
/**
744
 * @deprecated
745
 * @return
746
 */
747
private IExtent getFullExtent() {
748
        return extent;
749
}
750
/**
751
 * @deprecated
752
 * @return
753
 */
754
private String getSRS() {
755
        // TODO Auto-generated method stub
756
        return null;
757
}
758

    
759

    
760
public IDataExplorer getExplorer() {
761
        DataManager dsm=DataManager.getManager();
762
        IDataExplorerParameters dsp = dsm.createDataExplorerParameters(
763
                        SHPDataExplorer.DATASOURCE_NAME);
764
        ((DataExplorerFileParameters)dsp).setSource(shpParameters.getFile().getParentFile());
765

    
766
        IDataExplorer src=null;
767
        try {
768
                src = dsm.createDataExplorer(dsp);
769
        } catch (InitializeException e1) {
770
                e1.printStackTrace();
771
        }
772
        return src;
773
}
774
}