Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / IndexedShpDriver.java @ 5258

History | View | Annotate | Download (25.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.drivers.shp;
42

    
43
import java.awt.geom.Point2D;
44
import java.awt.geom.Rectangle2D;
45
import java.io.File;
46
import java.io.FileInputStream;
47
import java.io.FileNotFoundException;
48
import java.io.FileOutputStream;
49
import java.io.IOException;
50
import java.nio.ByteBuffer;
51
import java.nio.ByteOrder;
52
import java.nio.channels.FileChannel;
53
import java.util.Properties;
54
import java.util.Random;
55

    
56
import org.apache.log4j.Logger;
57

    
58
import com.hardcode.gdbms.driver.DriverUtilities;
59
import com.iver.cit.gvsig.fmap.core.FShape;
60
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
63
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
64
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
65
import com.iver.cit.gvsig.fmap.drivers.ExternalData;
66
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
67
import com.iver.cit.gvsig.fmap.edition.EditionException;
68
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
69
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter;
70
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
71
import com.iver.cit.gvsig.fmap.layers.FLayer;
72
import com.iver.utiles.bigfile.BigByteBuffer2;
73

    
74

    
75
/**
76
 * Driver del formato SHP. Usa ByteBuffer2, que no consume
77
 * memoria (bueno, 8KBytes... !) y no le importa que le pidan
78
 * entidades desordenadas del fichero, lo cual es indispensable
79
 * para trabajar con ?ndices espaciales.
80
 * Adem?s, el tiempo de espera para abrir un fichero y empezar a pintar es
81
 * pr?cticamente nulo, tanto desde disco duro como por red.
82
 * La ?nica pega es que en ficheros grandes puede ser unos
83
 * milisegundos un poco m?s lento que el otro, pero muy poco.
84
 *
85
 * @author Francisco Jos? Pe?arrubia
86
 */
87
public class IndexedShpDriver implements VectorialFileDriver, BoundedShapes,
88
        ExternalData, ISpatialWriter {
89
        private static Logger logger = Logger.getLogger(IndexedShpDriver.class.getName());
90
        private static String tempDirectoryPath = System.getProperty("java.io.tmpdir");
91
        private File fileShp;
92
        private File fTemp;
93
        private BigByteBuffer2 bb;
94
        private FileChannel channel;
95
        private FileInputStream fin;
96
        private int type;
97
        // private long[] m_posShapes;
98
        private int numReg;
99
        private Rectangle2D extent;
100
    
101
    // private File shxFile;
102
    private BigByteBuffer2 bbShx;
103
    private FileChannel channelShx;
104
    private FileInputStream finShx;
105
    
106
    private ShpWriter shpWriter = new ShpWriter();
107
    
108

    
109
        /**
110
         * Cierra el fichero.
111
         *
112
         * @throws IOException
113
         *
114
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
115
         */
116
        public void close() throws IOException {
117
                IOException ret = null;
118
                
119
                try {
120
                        channel.close();
121
            channelShx.close();
122
                } catch (IOException e) {
123
                        ret = e;
124
                } finally {
125
                        try {
126
                                fin.close();
127
                        } catch (IOException e1) {
128
                                ret = e1;
129
                        }
130
                }
131

    
132
                if (ret != null) {
133
                        throw ret;
134
                }
135
                else // Si todo ha ido bien, preparamos para liberar memoria.
136
        {
137
                    bb = null;
138
            bbShx = null;
139
        }
140
        }
141

    
142
        /**
143
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
144
         */
145
        public void open(File f) throws IOException {
146
                fileShp = f;
147

    
148
                fin = new FileInputStream(f);
149

    
150
                // Open the file and then get a channel from the stream
151
                channel = fin.getChannel();
152

    
153
                // long size = channel.size();
154

    
155
                // Get the file's size and then map it into memory
156
                // bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
157
        bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY);
158
        
159
        finShx = new FileInputStream(getShxFile(f));
160

    
161
        // Open the file and then get a channel from the stream
162
        channelShx = finShx.getChannel();
163

    
164
        long sizeShx = channelShx.size();
165

    
166
        // Get the file's size and then map it into memory
167
        // bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
168
        // bbShx = channelShx.map(FileChannel.MapMode.READ_ONLY, 0, sizeShx);
169
        bbShx = new BigByteBuffer2(channelShx, FileChannel.MapMode.READ_ONLY);
170
        bbShx.order(ByteOrder.BIG_ENDIAN);
171
        }
172

    
173
        /**
174
         * DOCUMENT ME!
175
         *
176
         * @param index DOCUMENT ME!
177
         *
178
         * @return DOCUMENT ME!
179
         *
180
         * @throws IOException
181
         *
182
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShape(int)
183
         */
184

    
185
        /* public FShape getShapeByID(int ID) {
186
           Point2D.Double p = new Point2D.Double();
187
           Point2D.Double pAnt = null;
188
           int numParts;
189
           int numPoints;
190
           int i;
191
           int j;
192
           int numReg;
193
           int numeroPuntos;
194
           int hasta;
195
           int desde;
196
           Rectangle2D.Double BoundingBox = new Rectangle2D.Double();
197
        
198
                   SHPShape shapeShp=null;
199
           FShape resulShape = null;
200
           try {
201
               bb.position(m_posShapes[ID]);
202
               bb.order(ByteOrder.LITTLE_ENDIAN);
203
               int tipoShape = bb.getInt();
204
               m_shapeType = tipoShape;
205
               // retrieve that shape.
206
               // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
207
               if (tipoShape == FConstant.SHAPE_TYPE_POINT) {
208
                   p = readPoint(bb);
209
                   resulShape = new FShape(new FPoint(p.getX(),p.getY()),FConstant.SHAPE_TYPE_POINT);
210
                   //Comprobaci?n punto.
211
                   //System.err.println("p.x = "+p.x);
212
                   //System.err.println("p.y = "+p.y);
213
        
214
               } else if ((tipoShape == FConstant.SHAPE_TYPE_POLYLINE) ||
215
                       (tipoShape == FConstant.SHAPE_TYPE_POLYGON)) {
216
                   // BoundingBox
217
                   BoundingBox = readRectangle(bb);
218
                   numParts = bb.getInt();
219
                   numPoints = bb.getInt();
220
                   // part indexes.
221
                   // Geometry geom = GeometryFactory.toGeometryArray();
222
                   GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
223
                           numPoints);
224
                   int[] tempParts = new int[numParts];
225
                   for (i = 0; i < numParts; i++) {
226
                       tempParts[i] = bb.getInt();
227
                   }
228
                   j = 0;
229
                   ///Line2D.Double line2D;
230
                   FPoint[] points=new FPoint[numPoints];
231
                   for (i = 0; i < numPoints; i++) {
232
                           p=readPoint(bb);
233
                       points[i] = new FPoint(p.x,p.y);
234
                       // System.out.println("x= " + p.x + " y=" + p.y);
235
                       // System.out.println("x= " + (float) p.x + " y=" + (float) p.y);
236
                       if (i == tempParts[j]) {
237
                           elShape.moveTo(p.x, p.y);
238
                           if (j < (numParts - 1)) {
239
                               j++;
240
                           }
241
                       } else {
242
                           elShape.lineTo(p.x, p.y);
243
                       }
244
                   }
245
                   //FGeometry pol=new FPolyLine(points,tempParts,BoundingBox);
246
        
247
                   resulShape = new FShape(tipoShape,elShape);
248
               } else if (tipoShape == FConstant.SHAPE_TYPE_MULTIPOINT) {
249
                   // BoundingBox
250
                   BoundingBox = readRectangle(bb);
251
                   numPoints = bb.getInt();
252
                   FPoint[] tempPoints = new FPoint[numPoints];
253
                   for (i = 0; i < numPoints; i++) {
254
                           Point2D p2=readPoint(bb);
255
                       tempPoints[i] = new FPoint(p2.getX(),p2.getY(),0);
256
                   }
257
                   FMultiPoint multipoint = new FMultiPoint(tempPoints,BoundingBox);
258
                   resulShape = new FShape(multipoint,tipoShape);
259
               } else if (tipoShape == FConstant.SHAPE_TYPE_POINTZ) {
260
                   FPoint p3d = new FPoint();
261
                   p3d.read(bb);
262
                   resulShape = new FShape(p3d,tipoShape);
263
               } else if ((tipoShape == FConstant.SHAPE_TYPE_POLYLINEZ) ||
264
                       (tipoShape == FConstant.SHAPE_TYPE_POLYGONZ)) {
265
                   // BoundingBox
266
                   BoundingBox = readRectangle(bb);
267
                   numParts = bb.getInt();
268
                   numPoints = bb.getInt();
269
                   // part indexes.
270
                   // Geometry geom = GeometryFactory.toGeometryArray();
271
        
272
                   GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
273
                           numPoints);
274
                   int[] tempParts = new int[numParts];
275
                   for (i = 0; i < numParts; i++) {
276
                       tempParts[i] = bb.getInt();
277
                   }
278
                   j = 0;
279
                  //Line2D.Double line2D;
280
                   FPoint[] points=new FPoint[numPoints];
281
                   for (i = 0; i < numPoints; i++) {
282
                       p = readPoint(bb);
283
                       points[i]=new FPoint(p.x,p.y);
284
        
285
                       if (i == tempParts[j]) {
286
                           elShape.moveTo(p.x, p.y);
287
                           if (j < (numParts - 1)) {
288
                               j++;
289
                           }
290
                       } else {
291
                           elShape.lineTo(p.x, p.y);
292
                       }
293
        
294
                   }
295
        
296
                   double[] boxZ = new double[2];
297
                   boxZ[0] = bb.getDouble();
298
                   boxZ[1] = bb.getDouble();
299
                   double[] pZ = new double[numPoints];
300
                   for (i = 0; i < numPoints; i++) {
301
                       pZ[i] = bb.getDouble();
302
                   }
303
                   //FGeometry pol=new FPolyLine(points,tempParts,BoundingBox);
304
                   resulShape = new FShape(tipoShape, elShape, pZ);
305
               } else if (tipoShape == FConstant.SHAPE_TYPE_MULTIPOINTZ) {
306
                   // BoundingBox
307
                   BoundingBox = readRectangle(bb);
308
                   numPoints = bb.getInt();
309
                   FPoint[] tempPoints3D = new FPoint[numPoints];
310
                   for (i = 0; i < numPoints; i++) {
311
                       tempPoints3D[i] = new FPoint();
312
                       tempPoints3D[i].read(bb);
313
                   }
314
                   FMultiPoint multipoint3D = new FMultiPoint(tempPoints3D,BoundingBox);
315
                   resulShape = new FShape(multipoint3D,tipoShape);
316
               }
317
           } catch (Exception e) {
318
               System.err.println("Fallo en getShapeByID. ID=" + ID +
319
                   " m_posShapes[ID]=" + m_posShapes[ID]);
320
               System.err.println("getShapeByID: " + e.getMessage());
321
               e.printStackTrace();
322
           }
323
           return resulShape;
324
           }
325
         */
326

    
327
        /**
328
         * Devuelve la geometria a partir de un ?ndice.
329
         *
330
         * @param index DOCUMENT ME!
331
         *
332
         * @return DOCUMENT ME!
333
         *
334
         * @throws IOException DOCUMENT ME!
335
         */
336
        public synchronized IGeometry getShape(int index) throws IOException {
337
                Point2D.Double p = new Point2D.Double();
338
                int numParts;
339
                int numPoints;
340
                int i;
341
                int j;
342
                /* int numReg;
343
                int numeroPuntos;
344
                int hasta;
345
                int desde; */
346
                int shapeType;
347

    
348
                //Rectangle2D.Double BoundingBox;
349
        // if (m_posShapes[index] == 0)
350
            
351
                // bb.position(m_posShapes[index]);
352
        bb.position(getPositionForRecord(index));
353
                bb.order(ByteOrder.LITTLE_ENDIAN);
354

    
355
                ///bb.position(bb.position()+4);
356
                shapeType = bb.getInt();
357
                //el shape tal con tema tal y n?mro tal es null
358
                if (shapeType==SHP.NULL){
359
                        logger.info("El shape ="+index+ " de la capa ="+this.toString()+" es null");
360
                        return null;
361
                }
362
                        
363
                // retrieve that shape.
364
                // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
365
                switch (type) {
366
                        case (SHP.POINT2D):
367
                                p = readPoint(bb);
368

    
369
                                return ShapeFactory.createPoint2D(p.getX(), p.getY());
370

    
371
                        case (SHP.POLYLINE2D):
372

    
373
                                //BoundingBox = readRectangle(bb);
374
                                //bb.getDouble();
375
                                //bb.getDouble();
376
                                //bb.getDouble();
377
                                //bb.getDouble();
378
                                bb.position(bb.position() + 32);
379
                                numParts = bb.getInt();
380
                                numPoints = bb.getInt();
381

    
382
                                // part indexes.
383
                                // Geometry geom = GeometryFactory.toGeometryArray();
384
                                GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
385
                                                numPoints);
386

    
387
                                int[] tempParts = new int[numParts];
388

    
389
                                for (i = 0; i < numParts; i++) {
390
                                        tempParts[i] = bb.getInt();
391
                                }
392

    
393
                                j = 0;
394

    
395
                                for (i = 0; i < numPoints; i++) {
396
                                        p = readPoint(bb);
397

    
398
                                        if (i == tempParts[j]) {
399
                                                elShape.moveTo(p.x, p.y);
400

    
401
                                                if (j < (numParts - 1)) {
402
                                                        j++;
403
                                                }
404
                                        } else {
405
                                                elShape.lineTo(p.x, p.y);
406
                                        }
407
                                }
408

    
409
                                return ShapeFactory.createPolyline2D(elShape);
410

    
411
                        case (SHP.POLYGON2D):
412

    
413
                                //                            BoundingBox = readRectangle(bb);
414
                                bb.getDouble();
415
                                bb.getDouble();
416
                                bb.getDouble();
417
                                bb.getDouble();
418

    
419
                                numParts = bb.getInt();
420

    
421
                                numPoints = bb.getInt();
422

    
423
                                // part indexes.
424
                                // Geometry geom = GeometryFactory.toGeometryArray();
425
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
426

    
427
                                tempParts = new int[numParts];
428

    
429
                                for (i = 0; i < numParts; i++) {
430
                                        tempParts[i] = bb.getInt();
431
                                }
432

    
433
                                j = 0;
434

    
435
                                for (i = 0; i < numPoints; i++) {
436
                                        p = readPoint(bb);
437

    
438
                                        if (i == tempParts[j]) {
439
                                                elShape.moveTo(p.x, p.y);
440

    
441
                                                if (j < (numParts - 1)) {
442
                                                        j++;
443
                                                }
444
                                        } else {
445
                                                elShape.lineTo(p.x, p.y);
446
                                        }
447
                                }
448

    
449
                                return ShapeFactory.createPolygon2D(elShape);
450

    
451
                        case (SHP.POINT3D):
452

    
453
                                double x = bb.getDouble();
454
                                double y = bb.getDouble();
455
                                double z = bb.getDouble();
456

    
457
                                return ShapeFactory.createPoint3D(x, y, z);
458

    
459
                        case (SHP.POLYLINE3D):
460
                                bb.position(bb.position() + 32);
461
                                numParts = bb.getInt();
462
                                numPoints = bb.getInt();
463
                                elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
464
                                tempParts = new int[numParts];
465

    
466
                                for (i = 0; i < numParts; i++) {
467
                                        tempParts[i] = bb.getInt();
468
                                }
469

    
470
                                j = 0;
471

    
472
                                for (i = 0; i < numPoints; i++) {
473
                                        p = readPoint(bb);
474

    
475
                                        if (i == tempParts[j]) {
476
                                                elShape.moveTo(p.x, p.y);
477

    
478
                                                if (j < (numParts - 1)) {
479
                                                        j++;
480
                                                }
481
                                        } else {
482
                                                elShape.lineTo(p.x, p.y);
483
                                        }
484
                                }
485

    
486
                                double[] boxZ = new double[2];
487
                                boxZ[0] = bb.getDouble();
488
                                boxZ[1] = bb.getDouble();
489

    
490
                                double[] pZ = new double[numPoints];
491

    
492
                                for (i = 0; i < numPoints; i++) {
493
                                        pZ[i] = bb.getDouble();
494
                                }
495

    
496
                                return ShapeFactory.createPolyline3D(elShape, pZ);
497
                        case (SHP.POLYGON3D):
498
                        bb.position(bb.position() + 32);
499
                        numParts = bb.getInt();
500
                        numPoints = bb.getInt();
501
                        elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, numPoints);
502
                        tempParts = new int[numParts];
503

    
504
                        for (i = 0; i < numParts; i++) {
505
                                tempParts[i] = bb.getInt();
506
                        }
507

    
508
                        j = 0;
509

    
510
                        for (i = 0; i < numPoints; i++) {
511
                                p = readPoint(bb);
512

    
513
                                if (i == tempParts[j]) {
514
                                        elShape.moveTo(p.x, p.y);
515

    
516
                                        if (j < (numParts - 1)) {
517
                                                j++;
518
                                        }
519
                                } else {
520
                                        elShape.lineTo(p.x, p.y);
521
                                }
522
                        }
523

    
524
                        double[] boxpoZ = new double[2];
525
                        boxpoZ[0] = bb.getDouble();
526
                        boxpoZ[1] = bb.getDouble();
527

    
528
                        double[] poZ = new double[numPoints];
529

    
530
                        for (i = 0; i < numPoints; i++) {
531
                                poZ[i] = bb.getDouble();
532
                        }
533

    
534
                        return ShapeFactory.createPolygon3D(elShape, poZ);
535
                        
536
                        case (SHP.MULTIPOINT2D):
537
                                bb.position(bb.position() + 32);
538
                                numPoints = bb.getInt();
539

    
540
                                double[] tempX = new double[numPoints];
541
                                double[] tempY = new double[numPoints];
542

    
543
                                for (i = 0; i < numPoints; i++) {
544
                                        tempX[i] = bb.getDouble();
545
                                        tempY[i] = bb.getDouble();
546
                                }
547

    
548
                                return ShapeFactory.createMultipoint2D(tempX, tempY);
549

    
550
                        case (SHP.MULTIPOINT3D):
551
                                bb.position(bb.position() + 32);
552
                                numPoints = bb.getInt();
553

    
554
                                double[] temX = new double[numPoints];
555
                                double[] temY = new double[numPoints];
556
                                double[] temZ = new double[numPoints];
557

    
558
                                for (i = 0; i < numPoints; i++) {
559
                                        temX[i] = bb.getDouble();
560
                                        temY[i] = bb.getDouble();
561
                                        //temZ[i] = bb.getDouble();
562
                                }
563
                                
564
                                for (i = 0; i < numPoints; i++) {
565
                                        temZ[i] = bb.getDouble();
566
                                }
567
                                return ShapeFactory.createMultipoint3D(temX, temY, temZ);
568
                }
569

    
570
                return null;
571
        }
572

    
573
        /**
574
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShapeCount()
575
         */
576
        public int getShapeCount() {
577
                return numReg;
578
        }
579

    
580
        /**
581
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
582
         */
583
        public int getShapeType() {
584
                int auxType = 0;
585

    
586
                switch (type) {
587
                        case (SHP.POINT2D):
588
                        case (SHP.POINT3D):
589
                                auxType = auxType | FShape.POINT;
590

    
591
                                break;
592

    
593
                        case (SHP.POLYLINE2D):
594
                        case (SHP.POLYLINE3D):
595
                                auxType = auxType | FShape.LINE;
596

    
597
                                break;
598

    
599
                        case (SHP.POLYGON2D):
600
                        case (SHP.POLYGON3D):
601
                                auxType = auxType | FShape.POLYGON;
602

    
603
                                break;
604
                        case (SHP.MULTIPOINT2D):
605
                        case (SHP.MULTIPOINT3D):
606
                                auxType = auxType | FShape.MULTIPOINT;
607

    
608
                                break;
609
                }
610

    
611
                return auxType;
612
        }
613

    
614

    
615
        /**
616
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
617
         */
618
        public void initialize() throws IOException {
619
                // create a new header.
620
                ShapeFileHeader2 myHeader = new ShapeFileHeader2();
621

    
622
                // read the header
623
                myHeader.readHeader(bb);
624

    
625
                extent = new Rectangle2D.Double(myHeader.myXmin, myHeader.myYmin,
626
                                myHeader.myXmax - myHeader.myXmin,
627
                                myHeader.myYmax - myHeader.myYmin);
628

    
629
                type = myHeader.myShapeType;
630

    
631
                double x = myHeader.myXmin;
632
                double y = myHeader.myYmin;
633
                double w = myHeader.myXmax - myHeader.myXmin;
634
                double h = myHeader.myYmax - myHeader.myYmin;
635

    
636
                if (w == 0) {
637
                        x -= 0.1;
638
                        w = 0.2;
639
                }
640

    
641
                if (h == 0) {
642
                        y -= 0.1;
643
                        h = 0.2;
644
                }
645

    
646
                // String strFichDbf = m_Path.toLowerCase().replaceAll("\\.shp", ".dbf");
647
                String strFichDbf = fileShp.getAbsolutePath().replaceAll("\\.shp", ".dbf");
648
                strFichDbf = strFichDbf.replaceAll("\\.SHP", ".DBF");
649

    
650
                DbaseFileNIO m_FichDbf = new DbaseFileNIO();
651

    
652
                m_FichDbf.open(new File(strFichDbf));
653
                numReg = m_FichDbf.getRecordCount();
654
                
655
                // shpWriter.initialize(file);
656
                // m_posShapes = new long[numReg];
657

    
658
                // read the records.
659
                /* int tempCurrentLength = myHeader.getHeaderLength();
660
                int numReg = 0;
661

662
                long pos1;
663

664
                while (tempCurrentLength < myHeader.myFileLength) {
665
                        // read the record header
666
                        // ShapeFileRecord tempRecord = new ShapeFileRecord();
667
                        // Bytes 0 to 4 represent the record number in the file, these may be out of order.
668
                        bb.order(ByteOrder.BIG_ENDIAN);
669

670
                        // tempRecord.setIndex(in.readInt());
671
                        bb.getInt();
672

673
                        // read the content length of this record in 16 bit words, excluding the index.
674
                        // in.setLittleEndianMode(false);
675
                        int tempContentLength = bb.getInt();
676

677
                        pos1 = bb.position();
678

679
                        m_posShapes[numReg] = bb.position();
680

681
                        // Posicionamos
682
                        bb.position((pos1 + (2 * tempContentLength)));
683
                        numReg = numReg + 1;
684

685
                        // update the current length the 4 is for the index, and content length.
686
                        tempCurrentLength = tempCurrentLength + 4 + tempContentLength;
687
                } */
688
        }
689

    
690
        /**
691
         * Reads the Point from the shape file.
692
         *
693
         * @param in ByteBuffer.
694
         *
695
         * @return Point2D.
696
         */
697
        private synchronized Point2D.Double readPoint(BigByteBuffer2 in) {
698
                // create a new point
699
                Point2D.Double tempPoint = new Point2D.Double();
700

    
701
                // bytes 1 to 4 are the type and have already been read.
702
                // bytes 4 to 12 are the X coordinate
703
                in.order(ByteOrder.LITTLE_ENDIAN);
704
                tempPoint.setLocation(in.getDouble(), in.getDouble());
705

    
706
                return tempPoint;
707
        }
708

    
709
        /**
710
         * Lee un rect?ngulo del fichero.
711
         *
712
         * @param in ByteBuffer.
713
         *
714
         * @return Rect?ngulo.
715
         *
716
         * @throws IOException
717
         */
718
        private synchronized Rectangle2D.Double readRectangle(BigByteBuffer2 in)
719
                throws IOException {
720
                Rectangle2D.Double tempRect = new Rectangle2D.Double();
721
                in.order(ByteOrder.LITTLE_ENDIAN);
722
                tempRect.x = in.getDouble();
723
                tempRect.y = in.getDouble();
724

    
725
                tempRect.width = in.getDouble() - tempRect.x;
726

    
727
                if (tempRect.width == 0) {
728
                        tempRect.width = 0.2;
729
                        tempRect.x -= 0.1;
730
                }
731

    
732
                tempRect.height = in.getDouble() - tempRect.y;
733

    
734
                if (tempRect.height == 0) {
735
                        tempRect.height = 0.2;
736
                        tempRect.y -= 0.1;
737
                }
738

    
739
                return tempRect;
740
        }
741

    
742
        /**
743
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFullExtent()
744
         */
745
        public Rectangle2D getFullExtent() throws IOException {
746
                return extent;
747
        }
748

    
749
        /**
750
         * Obtiene el extent del shape a partir de un ?ndice.
751
         *
752
         * @param index ?ndice.
753
         *
754
         * @return Rect?ngulo.
755
         *
756
         * @throws IOException
757
         *
758
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds()
759
         */
760
        public synchronized Rectangle2D getShapeBounds(int index) throws IOException {
761
                Point2D p = new Point2D.Double();
762
                Rectangle2D BoundingBox = new Rectangle2D.Double();
763
                bb.position(getPositionForRecord(index));
764
                bb.order(ByteOrder.LITTLE_ENDIAN);
765

    
766
                int tipoShape = bb.getInt();
767

    
768
                if (tipoShape != SHP.NULL) {
769
                        type = tipoShape;
770
                }
771

    
772
                // retrieve that shape.
773
                // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
774
                switch (tipoShape) {
775
                        case (SHP.POINT2D):
776
                        case (SHP.POINT3D):
777
                                p = readPoint(bb);
778
                                BoundingBox = new Rectangle2D.Double(p.getX() - 0.1,
779
                                                p.getY() - 0.1, 0.2, 0.2);
780

    
781
                                break;
782

    
783
                        case (SHP.POLYLINE2D):
784
                        case (SHP.POLYGON2D):
785
                        case (SHP.MULTIPOINT2D):
786
                        case (SHP.POLYLINE3D):
787
                        case (SHP.POLYGON3D):
788
                        case (SHP.MULTIPOINT3D):
789

    
790
                                // BoundingBox
791
                                BoundingBox = readRectangle(bb);
792

    
793
                                break;
794
                }
795

    
796
                return BoundingBox;
797
        }
798

    
799
        /**
800
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
801
         */
802
        public boolean accept(File f) {
803
                return (f.getName().toUpperCase().endsWith("SHP"));
804
        }
805

    
806
        /**
807
         * @see com.hardcode.driverManager.Driver#getType()
808
         */
809
        public String getName() {
810
                // Para que se use este driver en lugar del viejo DemoShpDriver.
811
                
812
                // return "gvSIG indexed shp driver";
813
                return "gvSIG shp driver";
814
        }
815

    
816
        /**
817
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getDataDriverName()
818
         */
819
        public String getDataDriverName() {
820
                return "gdbms dbf driver";
821
        }
822

    
823
        /**
824
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getDataFile(java.io.File)
825
         */
826
        public File getDataFile(File f) {
827
                String str = f.getAbsolutePath();
828

    
829
                return new File(str.substring(0, str.length() - 3) + "dbf");
830
        }
831

    
832
    public File getShxFile(File f) {
833
        String str = f.getAbsolutePath();
834

    
835
        return new File(str.substring(0, str.length() - 3) + "shx");
836
    }
837
    
838
    
839
        /* (non-Javadoc)
840
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
841
         */
842
        public int getShapeType(int index) {
843
                // Por ahora todos los fichero .shp contienen
844
                // entidades del mismo tipo. Si trabajamos con
845
                // alguno mixto, tendremos que cambiar esta funci?n.
846
                return getShapeType();
847
        }
848

    
849
    /* (non-Javadoc)
850
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
851
     */
852
    public DriverAttributes getDriverAttributes() {
853
        return null;
854
    }
855
    
856
    private synchronized long getPositionForRecord(int numRec)
857
    {
858
        // shx file has a 100 bytes header, then, records
859
        // 8 bytes length, one for each entity.
860
        // first 4 bytes are the offset
861
        // next 4 bytes are length
862
        
863
        int posIndex = 100 + (numRec * 8);
864
        // bbShx.position(posIndex);
865
        long pos = 8 + 2*bbShx.getInt(posIndex);
866
        
867
        return pos;
868
    }
869

    
870
        public File getFile() {
871
                return fileShp;
872
        }
873
        
874
        public void reLoad() throws IOException {
875
                initialize();
876
        }
877

    
878
        public boolean canWriteGeometry(int gvSIGgeometryType) {                
879
                return shpWriter.canWriteGeometry(gvSIGgeometryType);
880
        }
881

    
882
        /* (non-Javadoc)
883
         * @see com.iver.cit.gvsig.fmap.edition.IWriter#preProcess()
884
         */
885
        public void preProcess() throws EditionException {
886
                // Necesitamos crear un fichero temporal porque a la vez que esic
887
                // La cabecera ya existe, as? que no la sobreescribimos.                
888
                // shpWriter.setWriteHeaders(false);
889
                shpWriter.preProcess();
890
                
891
        }
892

    
893
        public void process(IRowEdited row) throws EditionException {
894
                shpWriter.process(row);
895
                
896
        }
897

    
898
        public void postProcess() throws EditionException {
899
                shpWriter.postProcess();                
900
        
901
                try {
902
                        
903
                        // close();
904
                        
905
                        // Shp
906
                        FileChannel fcinShp = new FileInputStream(fTemp).getChannel();
907
                        FileChannel fcoutShp = new FileOutputStream(fileShp).getChannel();
908
                        DriverUtilities.copy(fcinShp, fcoutShp);
909

    
910
                        // Shx
911
                        File shxFile = getShxFile(fTemp);
912
                        FileChannel fcinShx = new FileInputStream(shxFile).getChannel();
913
                        FileChannel fcoutShx = new FileOutputStream(getShxFile(fileShp)).getChannel();
914
                        DriverUtilities.copy(fcinShx, fcoutShx);
915

    
916
                        // Dbf
917
                        File dbfFile = getDataFile(fTemp);
918
                        FileChannel fcinDbf = new FileInputStream(dbfFile).getChannel();
919
                        FileChannel fcoutDbf = new FileOutputStream(getDataFile(fileShp)).getChannel();
920
                        DriverUtilities.copy(fcinDbf, fcoutDbf);
921
                        
922
                        // Borramos los temporales
923
                        fTemp.delete();
924
                        shxFile.delete();
925
                        dbfFile.delete();
926
                        reLoad();
927
                        
928
                        
929
                        
930
                } catch (FileNotFoundException e) {
931
                        e.printStackTrace();
932
                        throw new EditionException(e);
933
                } catch (IOException e) {
934
                        e.printStackTrace();
935
                        throw new EditionException(e);
936
                }
937
                
938
                
939
        }
940

    
941
        public String getCapability(String capability) {
942
                return shpWriter.getCapability(capability);
943
        }
944

    
945
        public void setCapabilities(Properties capabilities) {
946
                shpWriter.setCapabilities(capabilities);
947
                
948
        }
949

    
950
        public boolean canWriteAttribute(int sqlType) {
951
                return shpWriter.canWriteAttribute(sqlType);
952
        }
953

    
954
        public ShpWriter getShpWriter() {
955
                return shpWriter;
956
        }
957

    
958
        public void setShpWriter(ShpWriter shpWriter) {
959
                this.shpWriter = shpWriter;
960
        }
961

    
962
        public void initialize(FLayer layer) throws EditionException {
963
                int aux = (int)(Math.random() * 1000);
964
                fTemp = new File(tempDirectoryPath + "/tmpShp" + aux + ".shp");
965
                shpWriter.setFile(fTemp);
966
                shpWriter.initialize(layer);
967
                
968
        }
969
        
970
}