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 / AbstractSHPReader.java @ 43464

History | View | Annotate | Download (9.98 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.exception.ReadRuntimeException;
29
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
32
import org.gvsig.fmap.geom.exception.CreateGeometryException;
33
import org.gvsig.fmap.geom.primitive.Line;
34
import org.gvsig.fmap.geom.primitive.Point;
35
import org.gvsig.fmap.geom.primitive.Polygon;
36
import org.gvsig.fmap.geom.primitive.Primitive;
37
import org.gvsig.fmap.geom.primitive.Ring;
38
import org.gvsig.utils.bigfile.BigByteBuffer2;
39

    
40

    
41
/**
42
 * @author fdiaz
43
 *
44
 */
45
public abstract class AbstractSHPReader implements SHPReader {
46
    private final SHPStoreParameters params;
47

    
48
    /**
49
     *
50
     * @param params
51
     */
52
    public AbstractSHPReader(SHPStoreParameters params) {
53
        this.params = params;
54
    }
55

    
56
    protected class InvalidNumberOfPointsInLinearRingException extends ReadRuntimeException {
57

    
58
        private final static String MESSAGE_FORMAT = "Invalid number of points in LinearRing (found %(NumPoints) - must be 0 or >= 4).\nCheck 'Fix LinearRings and LineStrings' in the shape's properties of the add layer dialog to try fix it.";
59
        private final static String MESSAGE_KEY = "_InvalidNumberOfPointsInLinearRingException";
60
        private static final long serialVersionUID = -8265770463632826027L;
61

    
62
        public InvalidNumberOfPointsInLinearRingException(int numPoints) {
63
            super(MESSAGE_FORMAT, null, MESSAGE_KEY, serialVersionUID);
64
            setValue("NumPoints", numPoints);
65
            this.getMessage();
66
        }
67
    }
68

    
69
    protected class InvalidNumberOfPointsInLineStringException extends ReadRuntimeException {
70

    
71
        /**
72
         *
73
         */
74
        private static final long serialVersionUID = -6458824187605578665L;
75
        private final static String MESSAGE_FORMAT = "Invalid number of points in LineString (found %(NumPoints) - must be 0 or >= 2).\nCheck 'Fix LinearRings and LineStrings' in the shape's properties of the add layer dialog to try fix it.";
76
        private final static String MESSAGE_KEY = "_InvalidNumberOfPointsInLineStringException";
77

    
78
        public InvalidNumberOfPointsInLineStringException(int numPoints) {
79
            super(MESSAGE_FORMAT, null, MESSAGE_KEY, serialVersionUID);
80
            setValue("NumPoints", numPoints);
81
            this.getMessage();
82
        }
83
    }
84
    public void checkNumVerticesOfRing(Ring ring ) throws InvalidNumberOfPointsInLinearRingException {
85
            if( ring.getNumVertices()<4 ) {
86
                if( this.fixLinearRings() ) {
87
                    Point p = ring.getVertex(ring.getNumVertices()-1);
88
                    while( ring.getNumVertices()<4 ) {
89
                        p = (Point) p.cloneGeometry();
90
                        ring.addVertex(p);
91
                    }
92
                } else {
93
                    throw new InvalidNumberOfPointsInLinearRingException(ring.getNumVertices());
94
                }
95
            }
96
    }
97

    
98
    public void checkNumVerticesOfLine(Line line) throws InvalidNumberOfPointsInLinearRingException {
99
        if( line == null ) {
100
            return;
101
        }
102
        if (line.getNumVertices() > 0 && line.getNumVertices() < 2) {
103
            if (this.fixLinearRings()) {
104
                Point p = line.getVertex(line.getNumVertices() - 1);
105
                while (line.getNumVertices() < 2) {
106
                    p = (Point) p.cloneGeometry();
107
                    line.addVertex(p);
108
                }
109
            } else {
110
                throw new InvalidNumberOfPointsInLineStringException(line.getNumVertices());
111
            }
112
        }
113
    }
114

    
115
    public boolean fixLinearRings() {
116
        return params.getFixLinearRings();
117
    }
118

    
119
    /**
120
     * @param p
121
     * @param bb
122
     */
123
    protected void fillXY(Point p, BigByteBuffer2 bb) {
124
        bb.order(ByteOrder.LITTLE_ENDIAN);
125
        double x = bb.getDouble();
126
        double y = bb.getDouble();
127
        p.setX(x);
128
        p.setY(y);
129
    }
130

    
131
    protected void fillM(Geometry geometry, BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
132
        double[] boxM = new double[2];
133
        boxM[0] = bb.getDouble();
134
        boxM[1] = bb.getDouble();
135
        if (geometry instanceof MultiPrimitive) {
136
            for (int primitivesNumber = 0; primitivesNumber < ((MultiPrimitive) geometry).getPrimitivesNumber(); primitivesNumber++) {
137
                Primitive primitive = ((MultiPrimitive) geometry).getPrimitiveAt(primitivesNumber);
138
                if (primitive instanceof Point) {
139
                    Point point = (Point) primitive;
140
                    point.setCoordinateAt(point.getDimension()-1, bb.getDouble());
141
                }
142
                if (primitive instanceof Line) {
143
                    Line line = (Line) primitive;
144
                    for (int i = 0; i < line.getNumVertices(); i++) {
145
                        line.setCoordinateAt(i, line.getDimension()-1, bb.getDouble());
146
                    }
147
                }
148
                if (primitive instanceof Polygon) {
149
                    Polygon polygon = (Polygon) primitive;
150
                    for (int i = 0; i < polygon.getNumVertices(); i++) {
151
                        polygon.setCoordinateAt(i, polygon.getDimension()-1, bb.getDouble());
152
                    }
153
                    int rings = polygon.getNumInteriorRings();
154
                    for (int i = 0; i < rings; i++) {
155
                        Ring ring = polygon.getInteriorRing(i);
156
                        for (int j = 0; j < ring.getNumVertices(); j++) {
157
                            ring.setCoordinateAt(j, ring.getDimension()-1, bb.getDouble());
158
                        }
159
                    }
160
                }
161
            }
162
        } else if (geometry instanceof Line) {
163
            Line line = (Line) geometry;
164
            for (int i = 0; i < line.getNumVertices(); i++) {
165
                line.setCoordinateAt(i, line.getDimension()-1, bb.getDouble());
166
            }
167
        } else if (geometry instanceof Polygon) {
168
            Polygon polygon = (Polygon) geometry;
169
            for (int i = 0; i < polygon.getNumVertices(); i++) {
170
                polygon.setCoordinateAt(i, polygon.getDimension()-1, bb.getDouble());
171
            }
172
            int rings = polygon.getNumInteriorRings();
173
            for (int i = 0; i < rings; i++) {
174
                Ring ring = polygon.getInteriorRing(i);
175
                for (int j = 0; j < ring.getNumVertices(); j++) {
176
                    ring.setCoordinateAt(j, ring.getDimension()-1, bb.getDouble());
177
                }
178
            }
179
        }
180
    }
181

    
182
    protected void fillZ(Geometry geometry, BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
183
        double[] boxM = new double[2];
184
        boxM[0] = bb.getDouble();
185
        boxM[1] = bb.getDouble();
186
        if (geometry instanceof MultiPrimitive) {
187
            for (int primitivesNumber = 0; primitivesNumber < ((MultiPrimitive) geometry).getPrimitivesNumber(); primitivesNumber++) {
188
                Primitive primitive = ((MultiPrimitive) geometry).getPrimitiveAt(primitivesNumber);
189
                if (primitive instanceof Point) {
190
                    Point point = (Point) primitive;
191
                    point.setCoordinateAt(Geometry.DIMENSIONS.Z, bb.getDouble());
192
                }
193
                if (primitive instanceof Line) {
194
                    Line line = (Line) primitive;
195
                    for (int i = 0; i < line.getNumVertices(); i++) {
196
                        line.setCoordinateAt(i, Geometry.DIMENSIONS.Z, bb.getDouble());
197
                    }
198
                }
199
                if (primitive instanceof Polygon) {
200
                    Polygon polygon = (Polygon) primitive;
201
                    for (int i = 0; i < polygon.getNumVertices(); i++) {
202
                        polygon.setCoordinateAt(i, Geometry.DIMENSIONS.Z, bb.getDouble());
203
                    }
204
                    int rings = polygon.getNumInteriorRings();
205
                    for (int i = 0; i < rings; i++) {
206
                        Ring ring = polygon.getInteriorRing(i);
207
                        for (int j = 0; j < ring.getNumVertices(); j++) {
208
                            ring.setCoordinateAt(j, Geometry.DIMENSIONS.Z, bb.getDouble());
209
                        }
210
                    }
211
                }
212
            }
213
        } else if (geometry instanceof Line) {
214
            Line line = (Line) geometry;
215
            for (int i = 0; i < line.getNumVertices(); i++) {
216
                line.setCoordinateAt(i, Geometry.DIMENSIONS.Z, bb.getDouble());
217
            }
218
        } else if (geometry instanceof Polygon) {
219
            Polygon polygon = (Polygon) geometry;
220
            for (int i = 0; i < polygon.getNumVertices(); i++) {
221
                polygon.setCoordinateAt(i, Geometry.DIMENSIONS.Z, bb.getDouble());
222
            }
223
            int rings = polygon.getNumInteriorRings();
224
            for (int i = 0; i < rings; i++) {
225
                Ring ring = polygon.getInteriorRing(i);
226
                for (int j = 0; j < ring.getNumVertices(); j++) {
227
                    ring.setCoordinateAt(j, Geometry.DIMENSIONS.Z, bb.getDouble());
228
                }
229
            }
230
        }
231
    }
232

    
233

    
234
}