Revision 41556

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/Launcher.java
3723 3723
				+ "\n");
3724 3724
		writer.write("    http.proxyUserName : "
3725 3725
				+ props.get("http.proxyUserName") + "\n");
3726
		writer.write("    http.proxyPassword : "
3727
				+ props.get("http.proxyPassword") + "\n");
3726
                
3727
                String proxyPass = (String) props.get("http.proxyPassword");
3728
                if( proxyPass!=null ) {
3729
                    proxyPass = "**********";
3730
                }
3731
		writer.write("    http.proxyPassword : " + proxyPass + "\n");
3728 3732

  
3729 3733
		String skinName = "(unknow)";
3730 3734
		try {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.shp/src/main/java/org/gvsig/fmap/dal/store/shp/SHPStoreProvider.java
48 48
import org.gvsig.fmap.dal.feature.FeatureSet;
49 49
import org.gvsig.fmap.dal.feature.FeatureStore;
50 50
import org.gvsig.fmap.dal.feature.FeatureType;
51
import org.gvsig.fmap.dal.feature.exception.AttributeNameException;
52 51
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
53 52
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
54 53
import org.gvsig.fmap.dal.resource.ResourceAction;
......
63 62
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
64 63
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
65 64
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider;
66
import org.gvsig.fmap.dal.store.shp.utils.SHPFile;
65
import org.gvsig.fmap.dal.store.shp.utils.ISHPFile;
66
import org.gvsig.fmap.dal.store.shp.utils.SHPFile2;
67 67
import org.gvsig.fmap.geom.Geometry;
68 68
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
69 69
import org.gvsig.fmap.geom.GeometryLocator;
......
80 80
	private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class);
81 81
	public static String NAME = "Shape";
82 82
	public static String DESCRIPTION = "Shape file";
83
	private SHPFile shpFile;
83
	private ISHPFile shpFile;
84 84

  
85 85
	private MultiResource resource;
86 86

  
......
103 103
	protected void init(DBFStoreParameters params,
104 104
			DataStoreProviderServices storeServices) throws InitializeException {
105 105

  
106
		this.shpFile = new SHPFile((SHPStoreParameters) params);
106
		this.shpFile = new SHPFile2((SHPStoreParameters) params);
107 107
		super.init(params, storeServices);
108 108
                this.shpFile.setUseNullGeometry(this.getShpParameters().getUseNullGeometry());
109 109
	}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.shp/src/main/java/org/gvsig/fmap/dal/store/shp/utils/SHPFile.java
62 62
 * @author jmvivo
63 63
 * 
64 64
 */
65
public class SHPFile {
65
public class SHPFile implements ISHPFile {
66 66

  
67 67
    private static final Logger logger = LoggerFactory.getLogger(SHPFile.class);
68 68
    private Envelope extent;
......
114 114
        }
115 115
    }
116 116

  
117
    @Override
117 118
    public void setUseNullGeometry(boolean useNullGeometry) {
118 119
        this.useNullGeometry = useNullGeometry;
119 120
    }
......
123 124
     * 
124 125
     * @see org.gvsig.fmap.dal.Resource#doClose()
125 126
     */
127
    @Override
126 128
    public void close() throws CloseException {
127 129
        CloseException ret = null;
128 130

  
......
153 155
        srsParameters = null;
154 156
    }
155 157

  
158
    @Override
156 159
    public boolean isOpen() {
157 160
        return this.fin != null;
158 161
    }
159 162

  
163
    @Override
160 164
    public synchronized void open() throws DataException {
161 165
        try {
162 166
            fin = new FileInputStream(this.params.getSHPFile());
......
273 277
        }
274 278
    }
275 279

  
280
    @Override
276 281
    public Envelope getFullExtent() throws ReadException {
277 282
        return this.extent;
278 283
    }
279 284

  
285
    @Override
280 286
    public boolean isEditable() {
281 287
        return this.params.getDBFFile().canWrite()
282 288
            && this.params.getSHPFile().canWrite()
283 289
            && this.params.getSHXFile().canWrite();
284 290
    }
285 291

  
292
    @Override
286 293
    public int getGeometryType() throws ReadException {
287 294
        int auxType = 0;
288 295

  
......
314 321
        return auxType;
315 322
    }
316 323

  
324
    @Override
317 325
    public int getGeometrySubType() throws ReadException {
318 326
        switch (type) {
319 327
        case (SHP.POINT2D):
......
352 360
     * @throws ReadException
353 361
     * @throws CreateGeometryException
354 362
     */
363
    @Override
355 364
    public synchronized Geometry getGeometry(long position)
356 365
        throws ReadException, CreateGeometryException {
357 366

  
......
533 542
            }
534 543
        }
535 544
    }
536

  
545
    
537 546
    private void fillSurface(Surface surface)
538 547
        throws CreateGeometryException, ReadException {
539 548
        Point p = null;
......
663 672
     * @throws CreateEnvelopeException
664 673
     * @throws CreateGeometryException
665 674
     */
675
    @Override
666 676
    public synchronized Envelope getBoundingBox(long featureIndex)
667 677
        throws ReadException, CreateEnvelopeException, CreateGeometryException {
668 678
        Point p = null;
......
718 728
    /**
719 729
     * @return
720 730
     */
731
    @Override
721 732
    public String getSRSParameters() {
722 733
        return this.srsParameters;
723 734
    }
......
751 762
        }
752 763
    }
753 764

  
765
    @Override
754 766
    public boolean canWriteGeometry(int gvSIGgeometryType) {
755 767
        for (int i = 0; i < supportedGeometryTypes.length; i++) {
756 768
            if (gvSIGgeometryType == supportedGeometryTypes[i]) {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.shp/src/main/java/org/gvsig/fmap/dal/store/shp/utils/SHPFile2.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.store.shp.utils;
24

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

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

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

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

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

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

  
79
    private SHPStoreParameters params;
80

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

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

  
91
    private boolean useNullGeometry = false;
92

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

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

  
116
    public void setUseNullGeometry(boolean useNullGeometry) {
117
        this.useNullGeometry = useNullGeometry;
118
    }
119

  
120
    /*
121
     * (non-Javadoc)
122
     * 
123
     * @see org.gvsig.fmap.dal.Resource#doClose()
124
     */
125
    public void close() throws CloseException {
126
        CloseException ret = null;
127

  
128
        // FIXME: Arreglar esto para que se acumulen los errores
129
        try {
130
            channel.close();
131
            channelShx.close();
132
        } catch (IOException e) {
133
            ret = new CloseException("SHPFile.close", e);
134
        } finally {
135
            try {
136
                fin.close();
137
                finShx.close();
138
            } catch (IOException e1) {
139
                ret = new CloseException("SHPFile.close", e1);
140
            }
141
        }
142

  
143
        if (ret != null) {
144
            throw ret;
145
        }
146
        bb = null;
147
        bbShx = null;
148
        fin = null;
149
        finShx = null;
150
        channel = null;
151
        channelShx = null;
152
        srsParameters = null;
153
    }
154

  
155
    public boolean isOpen() {
156
        return this.fin != null;
157
    }
158

  
159
    public synchronized void open() throws DataException {
160
        try {
161
            fin = new FileInputStream(this.params.getSHPFile());
162
        } catch (java.io.FileNotFoundException e) {
163
            throw new FileNotFoundException(this.params.getSHPFileName());
164
        }
165

  
166
        // Open the file and then get a channel from the stream
167
        channel = fin.getChannel();
168

  
169
        // long size = channel.size();
170
        // Get the file's size and then map it into memory
171
        // bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
172
        try {
173
            bb = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY);
174
        } catch (IOException e) {
175
            throw new ReadException(this.params.getSHPFileName(), e);
176

  
177
        }
178
        try {
179
            finShx = new FileInputStream(this.params.getSHXFile());
180
        } catch (java.io.FileNotFoundException e) {
181
            throw new FileNotFoundException(this.params.getSHXFileName());
182
        }
183

  
184
        // Open the file and then get a channel from the stream
185
        channelShx = finShx.getChannel();
186

  
187
        // long sizeShx = channelShx.size();
188
        // Get the file's size and then map it into memory
189
        // bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
190
        // bbShx = channelShx.map(FileChannel.MapMode.READ_ONLY, 0, sizeShx);
191
        try {
192
            bbShx
193
                    = new BigByteBuffer2(channelShx, FileChannel.MapMode.READ_ONLY);
194
        } catch (IOException e) {
195
            throw new ReadException(this.params.getSHXFileName(), e);
196

  
197
        }
198
        bbShx.order(ByteOrder.BIG_ENDIAN);
199

  
200
        // create a new header.
201
        ShapeFileHeader2 myHeader = new ShapeFileHeader2();
202

  
203
        bb.position(0);
204

  
205
        // read the header
206
        myHeader.readHeader(bb);
207

  
208
        double[] min = new double[2];
209
        min[0] = myHeader.myXmin;
210
        min[1] = myHeader.myYmin;
211
        double[] max = new double[2];
212
        max[0] = myHeader.myXmax;
213
        max[1] = myHeader.myYmax;
214

  
215
        try {
216
            extent
217
                    = gManager.createEnvelope(min[0], min[1], max[0], max[1],
218
                            SUBTYPES.GEOM2D);
219
        } catch (CreateEnvelopeException e1) {
220
            logger.error("Error creating the envelope", e1);
221
        }
222
        // extent = new Rectangle2D.Double(myHeader.myXmin, myHeader.myYmin,
223
        // myHeader.myXmax - myHeader.myXmin,
224
        // myHeader.myYmax - myHeader.myYmin);
225

  
226
        type = myHeader.myShapeType;
227
        subType = getGeometrySubType();
228

  
229
        this.initSupportedGeometryTypes();
230

  
231
        double x = myHeader.myXmin;
232
        double y = myHeader.myYmin;
233
        double w = myHeader.myXmax - myHeader.myXmin;
234
        double h = myHeader.myYmax - myHeader.myYmin;
235

  
236
        if (w == 0) {
237
            x -= 0.1;
238
            w = 0.2;
239
        }
240

  
241
        if (h == 0) {
242
            y -= 0.1;
243
            h = 0.2;
244
        }
245

  
246
        // TODO: SRS
247
        File prjFile = SHP.getPrjFile(this.params.getSHPFile());
248
        if (prjFile.exists()) {
249
            BufferedReader input = null;
250
            try {
251
                input = new BufferedReader(new FileReader(prjFile));
252
            } catch (java.io.FileNotFoundException e) {
253
                throw new FileNotFoundException(prjFile.getAbsolutePath());
254
            }
255

  
256
            try {
257
                this.srsParameters = input.readLine();
258
            } catch (IOException e) {
259
                throw new ReadException("SHPFile.open prj", e);
260
            } finally {
261
                try {
262
                    input.close();
263
                } catch (IOException e) {
264
                    // TODO ???
265
                }
266
            }
267

  
268
        } else {
269
            this.srsParameters = null;
270
        }
271
    }
272

  
273
    public Envelope getFullExtent() throws ReadException {
274
        return this.extent;
275
    }
276

  
277
    public boolean isEditable() {
278
        return this.params.getDBFFile().canWrite()
279
                && this.params.getSHPFile().canWrite()
280
                && this.params.getSHXFile().canWrite();
281
    }
282

  
283
    public int getGeometryType() throws ReadException {
284
        int auxType = 0;
285

  
286
        switch (type) {
287
            case (SHP.POINT2D):
288
            case (SHP.POINT3D):
289
                auxType = auxType | Geometry.TYPES.POINT;
290

  
291
                break;
292

  
293
            case (SHP.POLYLINE2D):
294
            case (SHP.POLYLINE3D):
295
                auxType = auxType | Geometry.TYPES.MULTICURVE;
296

  
297
                break;
298

  
299
            case (SHP.POLYGON2D):
300
            case (SHP.POLYGON3D):
301
                auxType = auxType | Geometry.TYPES.MULTISURFACE;
302

  
303
                break;
304
            case (SHP.MULTIPOINT2D):
305
            case (SHP.MULTIPOINT3D):
306
                auxType = auxType | Geometry.TYPES.MULTIPOINT;
307

  
308
                break;
309
        }
310

  
311
        return auxType;
312
    }
313

  
314
    public int getGeometrySubType() throws ReadException {
315
        switch (type) {
316
            case (SHP.POINT2D):
317
            case (SHP.POLYLINE2D):
318
            case (SHP.POLYGON2D):
319
            case (SHP.MULTIPOINT2D):
320
                return SUBTYPES.GEOM2D;
321
            case (SHP.POINT3D):
322
            case (SHP.POLYLINE3D):
323
            case (SHP.POLYGON3D):
324
            case (SHP.MULTIPOINT3D):
325
                return SUBTYPES.GEOM3D;
326
            case (SHP.POINTM):
327
            case (SHP.POLYLINEM):
328
            case (SHP.POLYGONM):
329
            case (SHP.MULTIPOINTM):
330
                return SUBTYPES.GEOM2DM;
331
        }
332

  
333
        return SUBTYPES.UNKNOWN;
334
    }
335

  
336
    private Geometry getNullGeometry() throws CreateGeometryException {
337
        if (this.useNullGeometry) {
338
            return gtypeNull.create();
339
        }
340
        return null;
341
    }
342

  
343
    /**
344
     * Gets the geometry with the index provided. Set to synchronized to prevent
345
     * concurrent threads issue (?)
346
     *
347
     * @param position
348
     * @return
349
     * @throws ReadException
350
     * @throws CreateGeometryException
351
     */
352
    public synchronized Geometry getGeometry(long position)
353
            throws ReadException, CreateGeometryException {
354

  
355
        int shapeType;
356
        bb.position(getPositionForRecord(position));
357
        bb.order(ByteOrder.LITTLE_ENDIAN);
358
        shapeType = bb.getInt();
359

  
360
        if (shapeType == SHP.NULL) {
361
            return getNullGeometry();
362
        }
363

  
364
        /*
365
         * Inconsistency: this particular shape is not
366
         * of the expected type. This can be because the SHP file
367
         * is corrupt and it can cause "out of memory error"
368
         * because the code will possibly try to instantiate a
369
         * huge (absurd) array, so it's safer to return a null geometry
370
         */
371
        if (shapeType != type) {
372
            return getNullGeometry();
373
        }
374

  
375
        Geometry geometry;
376
        Point point;
377
        switch (type) {
378
            case (SHP.POINT2D):
379
                point = readPoint(bb);
380
                return point;
381

  
382
            case (SHP.POINT3D):
383
                point = readPoint(bb);
384
                point.setCoordinateAt(
385
                        Geometry.DIMENSIONS.Z,
386
                        bb.getDouble()
387
                );
388
                return point;
389

  
390
            case (SHP.POINTM):
391
                point = readPoint(bb);
392
                point.setCoordinateAt(2, bb.getDouble());
393
                return point;
394

  
395
            case (SHP.POLYLINE2D):
396
                geometry = readGeometry(
397
                        Geometry.TYPES.CURVE,
398
                        Geometry.TYPES.MULTICURVE,
399
                        Geometry.SUBTYPES.GEOM2D);
400
                if (geometry == null) {
401
                    return getNullGeometry();
402
                }
403
                return geometry;
404

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

  
415
            case (SHP.POLYLINE3D):
416
                geometry = readGeometry(
417
                        Geometry.TYPES.CURVE,
418
                        Geometry.TYPES.MULTICURVE,
419
                        Geometry.SUBTYPES.GEOM3D);
420
                if (geometry == null) {
421
                    return getNullGeometry();
422
                }
423
                return geometry;
424

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

  
435
            case (SHP.MULTIPOINT2D):
436
                geometry = readMultiPoint(SUBTYPES.GEOM2D);
437
                if (geometry == null) {
438
                    return getNullGeometry();
439
                }
440
                return geometry;
441

  
442
            case (SHP.MULTIPOINT3D):
443
                geometry = readMultiPoint(SUBTYPES.GEOM3D);
444
                if (geometry == null) {
445
                    return getNullGeometry();
446
                }
447
                return geometry;
448
        }
449

  
450
        return null;
451
    }
452

  
453
    private Geometry readMultiPoint(int subtype) throws CreateGeometryException, ReadException {
454
        Point point;
455

  
456
        bb.position(bb.position() + 32);
457
        int numPoints = bb.getInt();
458

  
459
        Point[] points = new Point[numPoints];
460

  
461
        MultiPrimitive multi = (MultiPrimitive) gManager.create(Geometry.TYPES.MULTIPOINT, subtype);
462

  
463
        for (int i = 0; i < numPoints; i++) {
464
            point = (Point) gManager.create(TYPES.POINT, subtype);
465
            point.setX(bb.getDouble());
466
            point.setY(bb.getDouble());
467
            multi.addPrimitive(point);
468
        }
469

  
470
        if (subtype == Geometry.SUBTYPES.GEOM3D) {
471
            for (int i = 0; i < numPoints; i++) {
472
                double z = bb.getDouble();
473
                point = (Point) multi.getPrimitiveAt(i);
474
                point.setCoordinateAt(Geometry.DIMENSIONS.Z, z);
475
            }
476
        }
477
        if (multi.getPrimitivesNumber() == 0) {
478
            return null;
479
        }
480
        return multi;
481
    }
482

  
483
    private Geometry readGeometry(int type, int typemulti, int subtype)
484
            throws CreateGeometryException, ReadException {
485

  
486
        bb.position(bb.position() + 32);
487

  
488
        int numberOfSurfaces = bb.getInt();
489
        int numberOfPoints = bb.getInt();
490

  
491
        int[] indexOfPointsOfSurfaces = new int[numberOfSurfaces+1];
492
        for (int i = 0; i < numberOfSurfaces; i++) {
493
            indexOfPointsOfSurfaces[i] = bb.getInt();
494
        }
495
        indexOfPointsOfSurfaces[numberOfSurfaces] = -1;
496
                
497
        MultiPrimitive multi = null;
498
        if (numberOfSurfaces > 1) {
499
            multi = (MultiPrimitive) gManager.create(typemulti, subtype);
500
        }
501
        Surface surface = null;
502

  
503
        int nextSurface = 0;
504

  
505
        for (int i = 0; i < numberOfPoints; i++) {
506
            Point p = readPoint(subtype, bb);
507
            if (i == indexOfPointsOfSurfaces[nextSurface]) {
508
                // Cambiamos de poligono
509
                if( surface != null ) {
510
                    if( multi != null ) {
511
                        multi.addPrimitive(surface);
512
                    }
513
                }
514
                surface = (Surface) gManager.create(type, subtype);
515
                surface.addVertex(p);
516
                nextSurface++;
517
            } else {
518
                surface.addVertex(p);
519
            }
520
        }
521
        if (multi != null) {
522
            multi.addPrimitive(surface);
523
            if (subtype == Geometry.SUBTYPES.GEOM3D) {
524
                fillZ(multi);
525
            }
526
            return multi;
527
        }
528
        if (surface.getNumVertices() < 1) {
529
            return null;
530
        }
531
        if (subtype == Geometry.SUBTYPES.GEOM3D) {
532
            fillZ(surface);
533
        }
534
        return surface;
535
    }
536

  
537
    private void fillZ(Geometry geometry)
538
            throws CreateGeometryException, ReadException {
539
        double[] boxZ = new double[2];
540
        boxZ[0] = bb.getDouble();
541
        boxZ[1] = bb.getDouble();
542

  
543
        if (geometry == null) {
544
            return;
545
        }
546
        if (geometry instanceof Aggregate) {
547
            Aggregate multi = (Aggregate) geometry;
548
            for (int i = 0; i < multi.getPrimitivesNumber(); i++) {
549
                OrientablePrimitive primitive = (OrientablePrimitive) geometry;
550
                for (int p = 0; p < primitive.getNumVertices(); p++) {
551
                    primitive.setCoordinateAt(p, 2, bb.getDouble());
552
                }
553
            }
554
        } else if (geometry instanceof OrientablePrimitive) {
555
            OrientablePrimitive primitive = (OrientablePrimitive) geometry;
556
            for (int p = 0; p < primitive.getNumVertices(); p++) {
557
                primitive.setCoordinateAt(p, 2, bb.getDouble());
558
            }
559
        } else {
560
            logger.warn("Geoemtry type '" + geometry.getClass().getName() + "'unexpected ");
561
        }
562
    }
563

  
564
    private long getPositionForRecord(long numRec) {
565
        // shx file has a 100 bytes header, then, records
566
        // 8 bytes length, one for each entity.
567
        // first 4 bytes are the offset
568
        // next 4 bytes are length
569

  
570
        int posIndex = 100 + ((int) numRec * 8);
571
        // bbShx.position(posIndex);
572
        long pos = 8 + 2 * bbShx.getInt(posIndex);
573

  
574
        return pos;
575
    }
576

  
577
    /**
578
     * Reads the Point from the shape file.
579
     *
580
     * @param in ByteBuffer.
581
     *
582
     * @return Point2D.
583
     * @throws ReadException
584
     * @throws CreateGeometryException
585
     */
586
    private Point readPoint(BigByteBuffer2 in)
587
            throws CreateGeometryException, ReadException {
588
        return readPoint(subType, in);
589
    }
590

  
591
    private Point readPoint(int subtype, BigByteBuffer2 in)
592
            throws CreateGeometryException, ReadException {
593
        // bytes 1 to 4 are the type and have already been read.
594
        // bytes 4 to 12 are the X coordinate
595
        in.order(ByteOrder.LITTLE_ENDIAN);
596
        double x = in.getDouble();
597
        double y = in.getDouble();
598
        Point p = gManager.createPoint(x, y, subtype);
599
        return p;
600
    }
601

  
602
    /**
603
     * Lee un rect?ngulo del fichero.
604
     *
605
     * @param in ByteBuffer.
606
     *
607
     * @return Rect?ngulo.
608
     * @throws CreateEnvelopeException
609
     *
610
     * @throws IOException
611
     */
612
    private Envelope readRectangle(BigByteBuffer2 in)
613
            throws CreateEnvelopeException {
614
        in.order(ByteOrder.LITTLE_ENDIAN);
615
        double x = in.getDouble();
616
        double y = in.getDouble();
617

  
618
        double x2 = in.getDouble();
619

  
620
        if (x2 - x == 0) {
621
            x2 += 0.2;
622
            x -= 0.1;
623
        }
624

  
625
        double y2 = in.getDouble();
626

  
627
        if (y2 - y == 0) {
628
            y2 += 0.2;
629
            y -= 0.1;
630
        }
631
        Envelope tempEnvelope
632
                = gManager.createEnvelope(x, y, x2, y2, SUBTYPES.GEOM2D);
633
        return tempEnvelope;
634
    }
635

  
636
    /**
637
     * Gets the geometry bbox with the index provided. Set to synchronized to
638
     * prevent concurrent threads issue (?)
639
     *
640
     * @param featureIndex
641
     * @return
642
     * @throws ReadException
643
     * @throws CreateEnvelopeException
644
     * @throws CreateGeometryException
645
     */
646
    public synchronized Envelope getBoundingBox(long featureIndex)
647
            throws ReadException, CreateEnvelopeException, CreateGeometryException {
648
        Point p;
649
        Envelope BoundingBox = null;
650
        try {
651
            bb.position(getPositionForRecord(featureIndex));
652
        } catch (Exception e) {
653
            throw new ReadException("getBondingBox (" + featureIndex + ")", e);
654
            // logger.error(" Shapefile is corrupted. Drawing aborted. ="+e+
655
            // "  "+"index = "+index);
656
        }
657
        bb.order(ByteOrder.LITTLE_ENDIAN);
658

  
659
        int tipoShape = bb.getInt();
660

  
661
        // AZABALA: si tipoShape viene con valores erroneos deja de funcionar
662
        // el metodo getShape(i)
663
        // if (tipoShape != SHP.NULL) {
664
        // type = tipoShape;
665
        // }
666
        // retrieve that shape.
667
        // tempRecord.setShape(readShape(tempShapeType, tempContentLength, in));
668
        switch (tipoShape) {
669
            case (SHP.POINT2D):
670
            case (SHP.POINT3D):
671
                p = readPoint(bb);
672
                BoundingBox
673
                        = gManager.createEnvelope(p.getX() - 0.1, p.getY() - 0.1,
674
                                p.getX() + 0.2, p.getY() + 0.2, SUBTYPES.GEOM2D);
675
                // new Rectangle2D.Double(p.getX() - 0.1,
676
                // p.getY() - 0.1, 0.2, 0.2);
677

  
678
                break;
679

  
680
            case (SHP.POLYLINE2D):
681
            case (SHP.POLYGON2D):
682
            case (SHP.MULTIPOINT2D):
683
            case (SHP.POLYLINE3D):
684
            case (SHP.POLYGON3D):
685
            case (SHP.MULTIPOINT3D):
686

  
687
                // BoundingBox
688
                BoundingBox = readRectangle(bb);
689

  
690
                break;
691
        }
692

  
693
        return BoundingBox;
694
    }
695

  
696
    /**
697
     * @return
698
     */
699
    public String getSRSParameters() {
700
        return this.srsParameters;
701
    }
702

  
703
    private void initSupportedGeometryTypes() throws ReadException {
704
        switch (this.getGeometryType()) {
705
            case Geometry.TYPES.POINT:
706
                supportedGeometryTypes
707
                        = new int[]{Geometry.TYPES.POINT, Geometry.TYPES.NULL};
708
                break;
709
            case Geometry.TYPES.MULTIPOINT:
710
                supportedGeometryTypes
711
                        = new int[]{Geometry.TYPES.MULTIPOINT, Geometry.TYPES.NULL};
712
                break;
713
            case Geometry.TYPES.MULTICURVE:
714
                supportedGeometryTypes
715
                        = new int[]{Geometry.TYPES.CURVE, Geometry.TYPES.ELLIPSE,
716
                            Geometry.TYPES.ARC, Geometry.TYPES.CIRCLE,
717
                            Geometry.TYPES.SURFACE, Geometry.TYPES.NULL,
718
                            Geometry.TYPES.MULTICURVE};
719
                break;
720
            case Geometry.TYPES.MULTISURFACE:
721
                supportedGeometryTypes
722
                        = new int[]{Geometry.TYPES.ELLIPSE, Geometry.TYPES.CIRCLE,
723
                            Geometry.TYPES.SURFACE, Geometry.TYPES.NULL,
724
                            Geometry.TYPES.MULTISURFACE};
725
                break;
726

  
727
            default:
728
                supportedGeometryTypes = new int[]{};
729
        }
730
    }
731

  
732
    public boolean canWriteGeometry(int gvSIGgeometryType) {
733
        for (int i = 0; i < supportedGeometryTypes.length; i++) {
734
            if (gvSIGgeometryType == supportedGeometryTypes[i]) {
735
                return true;
736
            }
737
        }
738
        return false;
739
    }
740
}
0 741

  

Also available in: Unified diff