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 / SHPReader2DM.java @ 42811

History | View | Annotate | Download (8.15 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 SHPReader2DM extends AbstractSHPReader {
50

    
51
    /**
52
     *
53
     */
54
    public SHPReader2DM(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 Geometry 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 X coordinate
65
        GeometryManager gManager = GeometryLocator.getGeometryManager();
66
        bb.order(ByteOrder.LITTLE_ENDIAN);
67
        double x = bb.getDouble();
68
        double y = bb.getDouble();
69
        double m = bb.getDouble();
70
        Point p = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM);
71
        p.setX(x);
72
        p.setY(y);
73
        p.setCoordinateAt(p.getDimension()-1, m);
74
        return p;
75
    }
76

    
77
    /* (non-Javadoc)
78
     * @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoLyline(org.gvsig.utils.bigfile.BigByteBuffer2)
79
     */
80
    public Geometry readPoLyline(BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
81

    
82
        GeometryManager gManager = GeometryLocator.getGeometryManager();
83

    
84
        Point p = null;
85
        int numParts;
86
        int numPoints;
87
        int i;
88
        int j;
89

    
90
        bb.position(bb.position() + 32);
91
        numParts = bb.getInt();
92
        numPoints = bb.getInt();
93

    
94
        int[] tempParts = new int[numParts];
95

    
96
        MultiLine multiLine = null;
97
        Line line = null;
98
        if (numParts > 1) {
99
            multiLine = (MultiLine) gManager.create(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM2DM);
100
        }
101

    
102
        for (i = 0; i < numParts; i++) {
103
            tempParts[i] = bb.getInt();
104
        }
105

    
106
        j = 0;
107

    
108
        for (i = 0; i < numPoints; i++) {
109
            p = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM);
110
            fillXY(p, bb);
111

    
112
            if (i == tempParts[j]) {
113
                if(multiLine!=null && line != null){
114
                    multiLine.addCurve(line);
115
                }
116
                line = (Line) gManager.create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2DM);
117
                if (j < (numParts - 1)) {
118
                    j++;
119
                }
120
            }
121
            line.addVertex(p);
122
        }
123
        if (multiLine != null) {
124
            multiLine.addCurve(line);
125
            fillM(multiLine, bb);
126
            return multiLine;
127
        } else {
128
            fillM(line, bb);
129
            return line;
130
        }
131
    }
132

    
133

    
134
    /* (non-Javadoc)
135
     * @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoLygon(org.gvsig.utils.bigfile.BigByteBuffer2)
136
     */
137
    public Geometry readPoLygon(BigByteBuffer2 bb) throws CreateGeometryException, ReadException, GeometryOperationNotSupportedException, GeometryOperationException {
138

    
139
        GeometryManager gManager = GeometryLocator.getGeometryManager();
140

    
141
        Point p = null;
142
        int numParts;
143
        int numPoints;
144
        int i;
145

    
146
        bb.position(bb.position() + 32);
147
        numParts = bb.getInt();
148
        numPoints = bb.getInt();
149

    
150
        int[] tempParts = new int[numParts];
151

    
152
        for (i = 0; i < numParts; i++) {
153
            tempParts[i] = bb.getInt();
154
        }
155

    
156
        MultiPolygon multipolygon = null;
157
        Polygon polygon = null;
158
        Ring ring = null;
159

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

    
204
        if (multipolygon != null) {
205
            multipolygon.addPrimitive(polygon);
206
            fillM(multipolygon, bb);
207
            return multipolygon;
208
        } else {
209
            fillM(polygon, bb);
210
            return polygon;
211
        }
212
    }
213

    
214
    /* (non-Javadoc)
215
     * @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readMultiPoint(org.gvsig.utils.bigfile.BigByteBuffer2)
216
     */
217
    public Geometry readMultiPoint(BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
218

    
219
        GeometryManager gManager = GeometryLocator.getGeometryManager();
220

    
221
        int numPoints;
222
        bb.position(bb.position() + 32);
223
        numPoints = bb.getInt();
224

    
225
        MultiPoint multipoint = gManager.createMultiPoint(Geometry.SUBTYPES.GEOM2DM);
226

    
227
        for (int i = 0; i < numPoints; i++) {
228
            Point point = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM);
229
            multipoint.addPoint(point);
230
        }
231

    
232
        fillM(multipoint, bb);
233
        return multipoint;
234
    }
235

    
236
}