Statistics
| Revision:

root / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / file / shp / SHPStore.java @ 20501

History | View | Annotate | Download (21 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.data.vectorial.IsNotFeatureSettingException;
42
import org.gvsig.exceptions.BaseException;
43
import org.gvsig.metadata.IMetadata;
44
import org.gvsig.metadata.IMetadataManager;
45
import org.gvsig.metadata.MetadataManager;
46

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

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

    
68

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

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

    
86
                SHPStoreParameters shpParameters=(SHPStoreParameters)parameters;
87

    
88
                        fileShp = shpParameters.getSHPFile();
89

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

    
93

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

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

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

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

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

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

    
121
            }
122

    
123

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

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

    
166
        }
167

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

    
182
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
183
                if (this.alterMode){
184
                    if (featureManager.contains(id)) {
185
                            try {
186
                                        return featureManager.getFeature(id,this);
187
                                } catch (IsNotFeatureSettingException e) {
188
                                        throw new ReadException(this.getName(),e);
189
                                }
190
                    }
191
            }
192
                long position=((ShpFeatureID)id).getIndex();
193
                ShpFeature feature=new ShpFeature(getDefaultFeatureType(),this,position);
194
                return feature;
195
//                return getFeatureByPosition(featureType,((ShpFeatureID)id).getIndex());
196
        }
197

    
198
        public List getFeatureTypes() {
199
                featureTypes.set(0,getDefaultFeatureType());
200
        return featureTypes;
201
        }
202

    
203
        public IFeatureType getDefaultFeatureType() {
204
                if (isEditing()){
205
//                    Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
206
                    return attributeManager.getFeatureType();
207
            }
208
        return featureType;
209
        }
210

    
211
        public boolean isWithDefaultLegend() {
212
                return false;
213
        }
214

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

    
219
        public Object getDefaultLabelingStrategy() {
220
                return null;
221
        }
222

    
223
        public boolean canAlterFeatureType() {
224
                return true;
225
        }
226

    
227
        public String getName() {
228
                return DATASTORE_NAME;
229
        }
230

    
231
        public void open() throws OpenException {
232
                this.observable.notifyObservers(
233
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_OPEN)
234
                );
235
                super.open();
236
//                 create a new header.
237
                ShapeFileHeader2 myHeader = new ShapeFileHeader2();
238

    
239
                bb.position(0);
240

    
241
                // read the header
242
                myHeader.readHeader(bb);
243

    
244
                extent = new Extent(myHeader.myXmin, myHeader.myYmin,myHeader.myXmax,myHeader.myYmax);
245
//                extent = new Rectangle2D.Double(myHeader.myXmin, myHeader.myYmin,
246
//                                myHeader.myXmax - myHeader.myXmin,
247
//                                myHeader.myYmax - myHeader.myYmin);
248

    
249
                type = myHeader.myShapeType;
250

    
251
                double x = myHeader.myXmin;
252
                double y = myHeader.myYmin;
253
                double w = myHeader.myXmax - myHeader.myXmin;
254
                double h = myHeader.myYmax - myHeader.myYmin;
255

    
256
                if (w == 0) {
257
                        x -= 0.1;
258
                        w = 0.2;
259
                }
260

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

    
282
        }
283

    
284
        public void close() throws CloseException {
285
                this.observable.notifyObservers(
286
                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CLOSE)
287
            );
288
                super.close();
289
                CloseException ret = null;
290

    
291
                try {
292
                        channel.close();
293
            channelShx.close();
294
                } catch (IOException e) {
295
                        ret = new CloseException(getName(),e);
296
                } finally {
297
                        try {
298
                                fin.close();
299
                        } catch (IOException e1) {
300
                                ret = new CloseException(getName(),e1);
301
                        }
302
                }
303

    
304
//                if (ret != null) {
305
//                        throw ret;
306
//                }
307
                bb = null;
308
                bbShx = null;
309
        this.observable.notifyObservers(
310
                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CLOSE)
311
            );
312

    
313
        }
314

    
315
        public void dispose() {
316
                this.observable.notifyObservers(
317
                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DISPOSE)
318
            );
319
                super.dispose();
320
        this.observable.notifyObservers(
321
                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DISPOSE)
322
            );
323
        }
324

    
325
        public boolean isEditable() {
326
                if (super.isEditable() && fileShp.canWrite() && fileShx.canWrite()) return true;
327
                return false;
328
        }
329

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

    
364
        protected IFeaturesWriter getFeaturesWriter() {
365
                IFeaturesWriter writer = new ShpFeaturesWriter();
366
//                writer.init(this);
367
                return writer;
368
        }
369
        protected long getFeatureCount(){
370
                return dbf.getRecordCount();
371
        }
372

    
373

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

    
385
            bb.position(getPositionForRecord(position));
386
                bb.order(ByteOrder.LITTLE_ENDIAN);
387

    
388
                shapeType = bb.getInt();
389
                //el shape tal con tema tal y n?mro tal es null
390
                if (shapeType==SHP.NULL){
391
                        return new FNullGeometry();
392
                }
393

    
394
                // retrieve that shape.
395
                // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
396
                switch (type) {
397
                        case (SHP.POINT2D):
398
                                p = readPoint(bb);
399

    
400
                                return ShapeFactory.createPoint2D(p.getX(), p.getY());
401

    
402
                        case (SHP.POLYLINE2D):
403

    
404
                                bb.position(bb.position() + 32);
405
                                numParts = bb.getInt();
406
                                numPoints = bb.getInt();
407

    
408
                                // part indexes.
409
                                // Geometry geom = GeometryFactory.toGeometryArray();
410
                                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
411
                                                numPoints);
412

    
413
                                int[] tempParts = new int[numParts];
414

    
415
                                for (i = 0; i < numParts; i++) {
416
                                        tempParts[i] = bb.getInt();
417
                                }
418

    
419
                                j = 0;
420

    
421
                                for (i = 0; i < numPoints; i++) {
422
                                        p = readPoint(bb);
423

    
424
                                        if (i == tempParts[j]) {
425
                                                elShape.moveTo(p.getX(), p.getY());
426

    
427
                                                if (j < (numParts - 1)) {
428
                                                        j++;
429
                                                }
430
                                        } else {
431
                                                elShape.lineTo(p.getX(), p.getY());
432
                                        }
433
                                }
434

    
435
                                return ShapeFactory.createPolyline2D(elShape);
436

    
437
                        case (SHP.POLYGON2D):
438

    
439
                                //                            BoundingBox = readRectangle(bb);
440
//                                bb.getDouble();
441
//                                bb.getDouble();
442
//                                bb.getDouble();
443
//                                bb.getDouble();
444
                                bb.position(bb.position() + 32);
445
                                numParts = bb.getInt();
446

    
447
                                numPoints = bb.getInt();
448

    
449
                                // part indexes.
450
                                // Geometry geom = GeometryFactory.toGeometryArray();
451
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
452

    
453
                                tempParts = new int[numParts];
454

    
455
                                for (i = 0; i < numParts; i++) {
456
                                        tempParts[i] = bb.getInt();
457
                                }
458

    
459
                                j = 0;
460

    
461
                                for (i = 0; i < numPoints; i++) {
462
                                        p = readPoint(bb);
463

    
464
                                        if (i == tempParts[j]) {
465
                                                elShape.moveTo(p.getX(), p.getY());
466

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

    
479
                                return ShapeFactory.createPolygon2D(elShape);
480

    
481
                        case (SHP.POINT3D):
482

    
483
                                double x = bb.getDouble();
484
                                double y = bb.getDouble();
485
                                double z = bb.getDouble();
486

    
487
                                return ShapeFactory.createPoint3D(x, y, z);
488

    
489
                        case (SHP.POLYLINE3D):
490
                                bb.position(bb.position() + 32);
491
                                numParts = bb.getInt();
492
                                numPoints = bb.getInt();
493
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
494
                                tempParts = new int[numParts];
495

    
496
                                for (i = 0; i < numParts; i++) {
497
                                        tempParts[i] = bb.getInt();
498
                                }
499

    
500
                                j = 0;
501

    
502
                                for (i = 0; i < numPoints; i++) {
503
                                        p = readPoint(bb);
504

    
505
                                        if (i == tempParts[j]) {
506
                                                elShape.moveTo(p.getX(), p.getY());
507

    
508
                                                if (j < (numParts - 1)) {
509
                                                        j++;
510
                                                }
511
                                        } else {
512
                                                elShape.lineTo(p.getX(), p.getY());
513
                                        }
514
                                }
515

    
516
                                double[] boxZ = new double[2];
517
                                boxZ[0] = bb.getDouble();
518
                                boxZ[1] = bb.getDouble();
519

    
520
                                double[] pZ = new double[numPoints];
521

    
522
                                for (i = 0; i < numPoints; i++) {
523
                                        pZ[i] = bb.getDouble();
524
                                }
525

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

    
534
                        for (i = 0; i < numParts; i++) {
535
                                tempParts[i] = bb.getInt();
536
                        }
537

    
538
                        j = 0;
539

    
540
                        for (i = 0; i < numPoints; i++) {
541
                                p = readPoint(bb);
542

    
543
                                if (i == tempParts[j]) {
544
                                        elShape.moveTo(p.getX(), p.getY());
545

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

    
558
                        double[] boxpoZ = new double[2];
559
                        boxpoZ[0] = bb.getDouble();
560
                        boxpoZ[1] = bb.getDouble();
561

    
562
                        double[] poZ = new double[numPoints];
563

    
564
                        for (i = 0; i < numPoints; i++) {
565
                                poZ[i] = bb.getDouble();
566
                        }
567

    
568
                        return ShapeFactory.createPolygon3D(elShape, poZ);
569

    
570
                        case (SHP.MULTIPOINT2D):
571
                                bb.position(bb.position() + 32);
572
                                numPoints = bb.getInt();
573

    
574
                                double[] tempX = new double[numPoints];
575
                                double[] tempY = new double[numPoints];
576

    
577
                                for (i = 0; i < numPoints; i++) {
578
                                        tempX[i] = bb.getDouble();
579
                                        tempY[i] = bb.getDouble();
580
                                }
581

    
582
                                return ShapeFactory.createMultipoint2D(tempX, tempY);
583

    
584
                        case (SHP.MULTIPOINT3D):
585
                                bb.position(bb.position() + 32);
586
                                numPoints = bb.getInt();
587

    
588
                                double[] temX = new double[numPoints];
589
                                double[] temY = new double[numPoints];
590
                                double[] temZ = new double[numPoints];
591

    
592
                                for (i = 0; i < numPoints; i++) {
593
                                        temX[i] = bb.getDouble();
594
                                        temY[i] = bb.getDouble();
595
                                        //temZ[i] = bb.getDouble();
596
                                }
597

    
598
                                for (i = 0; i < numPoints; i++) {
599
                                        temZ[i] = bb.getDouble();
600
                                }
601
                                return ShapeFactory.createMultipoint3D(temX, temY, temZ);
602
                }
603

    
604
                return null;
605

    
606

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

    
615
        int posIndex = 100 + ((int)numRec * 8);
616
        // bbShx.position(posIndex);
617
        long pos = 8 + 2* bbShx.getInt(posIndex);
618

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

    
632
                // bytes 1 to 4 are the type and have already been read.
633
                // bytes 4 to 12 are the X coordinate
634
                in.order(ByteOrder.LITTLE_ENDIAN);
635
                tempPoint.setLocation(in.getDouble(), in.getDouble());
636

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

    
650
                in.order(ByteOrder.LITTLE_ENDIAN);
651
                double x = in.getDouble();
652
                double y = in.getDouble();
653

    
654
                double x2 = in.getDouble();
655

    
656
                if (x2-x == 0) {
657
                        x2 = 0.2;
658
                        x -= 0.1;
659
                }
660

    
661
                double y2 = in.getDouble();
662

    
663
                if (y2-y == 0) {
664
                        y2 = 0.2;
665
                        y -= 0.1;
666
                }
667
                IExtent tempRect = new Extent(x,y,x2,y2);
668
                return tempRect;
669
        }
670
        public IExtent getBoundingBox(long featureIndex) {
671
                Point2D p = new Point2D.Double();
672
                IExtent BoundingBox = null;
673
                try {
674
                        bb.position(getPositionForRecord(featureIndex));
675
                }catch (Exception e) {
676
                        e.printStackTrace();
677
//                        logger.error(" Shapefile is corrupted. Drawing aborted. ="+e+ "  "+"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
//                }
689

    
690
                // retrieve that shape.
691
                // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
692
                switch (tipoShape) {
693
                        case (SHP.POINT2D):
694
                        case (SHP.POINT3D):
695
                                p = readPoint(bb);
696
                                BoundingBox = new Extent(p.getX() - 0.1, p.getY() - 0.1,p.getX()+0.2,p.getY()+0.2);
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
private int getGeometryType() {
718
        int auxType = 0;
719

    
720
        switch (type) {
721
                case (SHP.POINT2D):
722
                case (SHP.POINT3D):
723
                        auxType = auxType | FShape.POINT;
724

    
725
                        break;
726

    
727
                case (SHP.POLYLINE2D):
728
                case (SHP.POLYLINE3D):
729
                        auxType = auxType | FShape.LINE;
730

    
731
                        break;
732

    
733
                case (SHP.POLYGON2D):
734
                case (SHP.POLYGON3D):
735
                        auxType = auxType | FShape.POLYGON;
736

    
737
                        break;
738
                case (SHP.MULTIPOINT2D):
739
                case (SHP.MULTIPOINT3D):
740
                        auxType = auxType | FShape.MULTIPOINT;
741

    
742
                        break;
743
        }
744

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

    
763

    
764
public IDataExplorer getExplorer() {
765
        DataManager dsm=DataManager.getManager();
766
        IDataExplorerParameters dsp = dsm.createDataExplorerParameters(
767
                        SHPDataExplorer.DATASOURCE_NAME);
768
        ((DataExplorerFileParameters)dsp).setSource(shpParameters.getFile().getParentFile());
769

    
770
        IDataExplorer src=null;
771
        try {
772
                src = dsm.createDataExplorer(dsp);
773
        } catch (InitializeException e1) {
774
                e1.printStackTrace();
775
        }
776
        return src;
777
}
778
}