Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / utils / SHPReader3DM.java @ 42876

History | View | Annotate | Download (8.6 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.store.shp.utils;
24

    
25
import java.nio.ByteOrder;
26

    
27
import org.gvsig.fmap.dal.exception.ReadException;
28
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters;
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryLocator;
31
import org.gvsig.fmap.geom.GeometryManager;
32
import org.gvsig.fmap.geom.aggregate.MultiLine;
33
import org.gvsig.fmap.geom.aggregate.MultiPoint;
34
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
35
import org.gvsig.fmap.geom.exception.CreateGeometryException;
36
import org.gvsig.fmap.geom.operation.GeometryOperationException;
37
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
38
import org.gvsig.fmap.geom.primitive.Line;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.geom.primitive.Polygon;
41
import org.gvsig.fmap.geom.primitive.Ring;
42
import org.gvsig.utils.bigfile.BigByteBuffer2;
43

    
44

    
45
/**
46
 * @author fdiaz
47
 *
48
 */
49
public class SHPReader3DM extends AbstractSHPReader {
50

    
51
    /**
52
     *
53
     */
54
    public SHPReader3DM(SHPStoreParameters params) {
55
        super(params);
56
    }
57

    
58
    /* (non-Javadoc)
59
     * @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoint(org.gvsig.utils.bigfile.BigByteBuffer2)
60
     */
61
    public Point readPoint(BigByteBuffer2 bb) throws CreateGeometryException {
62
        // bytes 1 to 4 are the type and have already been read.
63
        // bytes 4 to 12 are the X coordinate
64
        // bytes 13 to 20 are the Y coordinate
65
        // bytes 21 to 28 are the Z coordinate
66
        // bytes 29 to 36 are the M coordinate
67
        GeometryManager gManager = GeometryLocator.getGeometryManager();
68
        bb.order(ByteOrder.LITTLE_ENDIAN);
69
        double x = bb.getDouble();
70
        double y = bb.getDouble();
71
        double z = bb.getDouble();
72
        double m = bb.getDouble();
73
        Point p = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM3DM);
74
        p.setX(x);
75
        p.setY(y);
76
        p.setCoordinateAt(Geometry.DIMENSIONS.Z, z);
77
        p.setCoordinateAt(p.getDimension()-1, m);
78
        return p;
79
    }
80

    
81
    /* (non-Javadoc)
82
     * @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoLyline(org.gvsig.utils.bigfile.BigByteBuffer2)
83
     */
84
    public Geometry readPoLyline(BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
85

    
86
        GeometryManager gManager = GeometryLocator.getGeometryManager();
87

    
88
        Point p = null;
89
        int numParts;
90
        int numPoints;
91
        int i;
92
        int j;
93

    
94
        bb.position(bb.position() + 32);
95
        numParts = bb.getInt();
96
        numPoints = bb.getInt();
97

    
98
        int[] tempParts = new int[numParts];
99

    
100
        MultiLine multiLine = null;
101
        Line line = null;
102
        if (numParts > 1) {
103
            multiLine = (MultiLine) gManager.create(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM3DM);
104
        }
105

    
106
        for (i = 0; i < numParts; i++) {
107
            tempParts[i] = bb.getInt();
108
        }
109

    
110
        j = 0;
111

    
112
        for (i = 0; i < numPoints; i++) {
113
            p = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM3DM);
114
            fillXY(p, bb);
115

    
116
            if (i == tempParts[j]) {
117
                if(multiLine!=null && line != null){
118
                    multiLine.addCurve(line);
119
                }
120
                line = (Line) gManager.create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM3DM);
121
                if (j < (numParts - 1)) {
122
                    j++;
123
                }
124
            }
125
            line.addVertex(p);
126
        }
127
        checkNumVerticesOfLine(line);
128

    
129
        if (multiLine != null) {
130
            multiLine.addCurve(line);
131
            fillZ(multiLine, bb);
132
            fillM(multiLine, bb);
133
            return multiLine;
134
        } else {
135
            fillZ(line, bb);
136
            fillM(line, bb);
137
            return line;
138
        }
139
    }
140

    
141
    /* (non-Javadoc)
142
     * @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoLygon(org.gvsig.utils.bigfile.BigByteBuffer2)
143
     */
144
    public Geometry readPoLygon(BigByteBuffer2 bb) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException, ReadException {
145

    
146
        GeometryManager gManager = GeometryLocator.getGeometryManager();
147

    
148
        Point p = null;
149
        int numParts;
150
        int numPoints;
151
        int i;
152

    
153
        bb.position(bb.position() + 32);
154
        numParts = bb.getInt();
155
        numPoints = bb.getInt();
156

    
157
        int[] tempParts = new int[numParts];
158

    
159
        for (i = 0; i < numParts; i++) {
160
            tempParts[i] = bb.getInt();
161
        }
162

    
163
        MultiPolygon multipolygon = null;
164
        Polygon polygon = null;
165
        Ring ring = null;
166

    
167
        int pointsCounter = 0;
168
        for(int part=0; part<numParts; part++){
169
            ring = (Ring) gManager.create(Geometry.TYPES.RING, Geometry.SUBTYPES.GEOM3DM);
170
            int lastPoint = numPoints;
171
            if(part<numParts-1){
172
                lastPoint = tempParts[part+1];
173
            }
174
            while(pointsCounter<lastPoint){
175
                p = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM3DM);
176
                fillXY(p, bb);
177
                ring.addVertex(p);
178
                pointsCounter++;
179
            }
180
            ring.closePrimitive();
181
            checkNumVerticesOfRing(ring);
182
            if (ring.isCCW() && polygon!=null) {
183
                // Los anillos interiores deben ser CCW pero si encontramos un
184
                // anillo interior que no est? envuelto en un pol?gono
185
                // consideramos que es un pol?gono en s? y nos aseguramos de
186
                // darle la vuelta (en el else)
187
                //FIXME: Comprobar que este es el comportamiento deseado
188
                polygon.addInteriorRing(ring);
189
            } else {
190
                if(polygon!=null){
191
                    if(multipolygon==null){
192
                        multipolygon = (MultiPolygon) gManager.create(Geometry.TYPES.MULTIPOLYGON, Geometry.SUBTYPES.GEOM3DM);
193
                    }
194
                    multipolygon.addPrimitive(polygon);
195
                }
196
                polygon = (Polygon) gManager.create(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM3DM);
197
                polygon.ensureCapacity(ring.getNumVertices());
198
                if(ring.isCCW()){
199
                    //To ensure CW orientation for polygons
200
                    for (int v = ring.getNumVertices()-1; v >=0; v--) {
201
                        polygon.addVertex(ring.getVertex(v));
202
                    }
203
                } else {
204
                    for (int v = 0; v < ring.getNumVertices(); v++) {
205
                        polygon.addVertex(ring.getVertex(v));
206
                    }
207
                }
208
            }
209
        }
210

    
211
        if (multipolygon != null) {
212
            multipolygon.addPrimitive(polygon);
213
            fillZ(multipolygon, bb);
214
            fillM(multipolygon, bb);
215
            return multipolygon;
216
        } else {
217
            fillZ(polygon, bb);
218
            fillM(polygon, bb);
219
            return polygon;
220
        }
221
    }
222

    
223
    /* (non-Javadoc)
224
     * @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readMultiPoint(org.gvsig.utils.bigfile.BigByteBuffer2)
225
     */
226
    public Geometry readMultiPoint(BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
227

    
228
        GeometryManager gManager = GeometryLocator.getGeometryManager();
229

    
230
        int numPoints;
231
        bb.position(bb.position() + 32);
232
        numPoints = bb.getInt();
233

    
234
        MultiPoint multipoint = gManager.createMultiPoint(Geometry.SUBTYPES.GEOM3DM);
235

    
236
        for (int i = 0; i < numPoints; i++) {
237
            Point point = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM3DM);
238
            point.setX(bb.getDouble());
239
            point.setY(bb.getDouble());
240
            multipoint.addPoint(point);
241
        }
242

    
243
        fillZ(multipoint, bb);
244
        fillM(multipoint, bb);
245
        return multipoint;
246
    }
247

    
248
}