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 @ 42811

History | View | Annotate | Download (8.59 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' 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
    public void checkNumVerticesOfRing(Ring ring ) throws InvalidNumberOfPointsInLinearRingException {
70
            if( ring.getNumVertices()<4 ) {
71
                if( this.fixLinearRings() ) {
72
                    Point p = ring.getVertex(ring.getNumVertices()-1);
73
                    while( ring.getNumVertices()<4 ) {
74
                        p = (Point) p.cloneGeometry();
75
                        ring.addVertex(p);
76
                    }
77
                } else {
78
                    throw new InvalidNumberOfPointsInLinearRingException(ring.getNumVertices());
79
                }
80
            }        
81
    }
82
    
83
    public boolean fixLinearRings() {
84
        return params.getFixLinearRings();
85
    }
86
    
87
    /**
88
     * @param p
89
     * @param bb
90
     */
91
    protected void fillXY(Point p, BigByteBuffer2 bb) {
92
        bb.order(ByteOrder.LITTLE_ENDIAN);
93
        double x = bb.getDouble();
94
        double y = bb.getDouble();
95
        p.setX(x);
96
        p.setY(y);
97
    }
98

    
99
    protected void fillM(Geometry geometry, BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
100
        double[] boxM = new double[2];
101
        boxM[0] = bb.getDouble();
102
        boxM[1] = bb.getDouble();
103
        if (geometry instanceof MultiPrimitive) {
104
            for (int primitivesNumber = 0; primitivesNumber < ((MultiPrimitive) geometry).getPrimitivesNumber(); primitivesNumber++) {
105
                Primitive primitive = ((MultiPrimitive) geometry).getPrimitiveAt(primitivesNumber);
106
                if (primitive instanceof Point) {
107
                    Point point = (Point) primitive;
108
                    point.setCoordinateAt(point.getDimension()-1, bb.getDouble());
109
                }
110
                if (primitive instanceof Line) {
111
                    Line line = (Line) primitive;
112
                    for (int i = 0; i < line.getNumVertices(); i++) {
113
                        line.setCoordinateAt(i, line.getDimension()-1, bb.getDouble());
114
                    }
115
                }
116
                if (primitive instanceof Polygon) {
117
                    Polygon polygon = (Polygon) primitive;
118
                    for (int i = 0; i < polygon.getNumVertices(); i++) {
119
                        polygon.setCoordinateAt(i, polygon.getDimension()-1, bb.getDouble());
120
                    }
121
                    int rings = polygon.getNumInteriorRings();
122
                    for (int i = 0; i < rings; i++) {
123
                        Ring ring = polygon.getInteriorRing(i);
124
                        for (int j = 0; j < ring.getNumVertices(); j++) {
125
                            ring.setCoordinateAt(j, ring.getDimension()-1, bb.getDouble());
126
                        }
127
                    }
128
                }
129
            }
130
        } else if (geometry instanceof Line) {
131
            Line line = (Line) geometry;
132
            for (int i = 0; i < line.getNumVertices(); i++) {
133
                line.setCoordinateAt(i, line.getDimension()-1, bb.getDouble());
134
            }
135
        } else if (geometry instanceof Polygon) {
136
            Polygon polygon = (Polygon) geometry;
137
            for (int i = 0; i < polygon.getNumVertices(); i++) {
138
                polygon.setCoordinateAt(i, polygon.getDimension()-1, bb.getDouble());
139
            }
140
            int rings = polygon.getNumInteriorRings();
141
            for (int i = 0; i < rings; i++) {
142
                Ring ring = polygon.getInteriorRing(i);
143
                for (int j = 0; j < ring.getNumVertices(); j++) {
144
                    ring.setCoordinateAt(j, ring.getDimension()-1, bb.getDouble());
145
                }
146
            }
147
        }
148
    }
149

    
150
    protected void fillZ(Geometry geometry, BigByteBuffer2 bb) throws CreateGeometryException, ReadException {
151
        double[] boxM = new double[2];
152
        boxM[0] = bb.getDouble();
153
        boxM[1] = bb.getDouble();
154
        if (geometry instanceof MultiPrimitive) {
155
            for (int primitivesNumber = 0; primitivesNumber < ((MultiPrimitive) geometry).getPrimitivesNumber(); primitivesNumber++) {
156
                Primitive primitive = ((MultiPrimitive) geometry).getPrimitiveAt(primitivesNumber);
157
                if (primitive instanceof Point) {
158
                    Point point = (Point) primitive;
159
                    point.setCoordinateAt(Geometry.DIMENSIONS.Z, bb.getDouble());
160
                }
161
                if (primitive instanceof Line) {
162
                    Line line = (Line) primitive;
163
                    for (int i = 0; i < line.getNumVertices(); i++) {
164
                        line.setCoordinateAt(i, Geometry.DIMENSIONS.Z, bb.getDouble());
165
                    }
166
                }
167
                if (primitive instanceof Polygon) {
168
                    Polygon polygon = (Polygon) primitive;
169
                    for (int i = 0; i < polygon.getNumVertices(); i++) {
170
                        polygon.setCoordinateAt(i, Geometry.DIMENSIONS.Z, 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, Geometry.DIMENSIONS.Z, bb.getDouble());
177
                        }
178
                    }
179
                }
180
            }
181
        } else if (geometry instanceof Line) {
182
            Line line = (Line) geometry;
183
            for (int i = 0; i < line.getNumVertices(); i++) {
184
                line.setCoordinateAt(i, Geometry.DIMENSIONS.Z, bb.getDouble());
185
            }
186
        } else if (geometry instanceof Polygon) {
187
            Polygon polygon = (Polygon) geometry;
188
            for (int i = 0; i < polygon.getNumVertices(); i++) {
189
                polygon.setCoordinateAt(i, Geometry.DIMENSIONS.Z, bb.getDouble());
190
            }
191
            int rings = polygon.getNumInteriorRings();
192
            for (int i = 0; i < rings; i++) {
193
                Ring ring = polygon.getInteriorRing(i);
194
                for (int j = 0; j < ring.getNumVertices(); j++) {
195
                    ring.setCoordinateAt(j, Geometry.DIMENSIONS.Z, bb.getDouble());
196
                }
197
            }
198
        }
199
    }
200

    
201

    
202
}