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 / SHPReader2D.java @ 42823
History | View | Annotate | Download (7.55 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 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
27 |
|
28 |
import org.gvsig.fmap.geom.Geometry; |
29 |
import org.gvsig.fmap.geom.GeometryLocator; |
30 |
import org.gvsig.fmap.geom.GeometryManager; |
31 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
32 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
33 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
34 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
35 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
36 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
37 |
import org.gvsig.fmap.geom.primitive.Line; |
38 |
import org.gvsig.fmap.geom.primitive.Point; |
39 |
import org.gvsig.fmap.geom.primitive.Polygon; |
40 |
import org.gvsig.fmap.geom.primitive.Ring; |
41 |
import org.gvsig.utils.bigfile.BigByteBuffer2; |
42 |
|
43 |
|
44 |
/**
|
45 |
* @author fdiaz
|
46 |
*
|
47 |
*/
|
48 |
public class SHPReader2D extends AbstractSHPReader { |
49 |
|
50 |
/**
|
51 |
*
|
52 |
*/
|
53 |
public SHPReader2D(SHPStoreParameters params) {
|
54 |
super(params);
|
55 |
} |
56 |
|
57 |
/* (non-Javadoc)
|
58 |
* @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoint(org.gvsig.utils.bigfile.BigByteBuffer2)
|
59 |
*/
|
60 |
public Geometry readPoint(BigByteBuffer2 bb) throws CreateGeometryException{ |
61 |
// bytes 1 to 4 are the type and have already been read.
|
62 |
// bytes 4 to 12 are the X coordinate
|
63 |
GeometryManager gManager = GeometryLocator.getGeometryManager(); |
64 |
bb.order(ByteOrder.LITTLE_ENDIAN);
|
65 |
double x = bb.getDouble();
|
66 |
double y = bb.getDouble();
|
67 |
Point p = (Point) gManager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D); |
68 |
p.setX(x); |
69 |
p.setY(y); |
70 |
return p;
|
71 |
} |
72 |
|
73 |
/* (non-Javadoc)
|
74 |
* @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoLyline(org.gvsig.utils.bigfile.BigByteBuffer2)
|
75 |
*/
|
76 |
public Geometry readPoLyline(BigByteBuffer2 bb) throws CreateGeometryException { |
77 |
|
78 |
GeometryManager gManager = GeometryLocator.getGeometryManager(); |
79 |
|
80 |
Point p = null; |
81 |
int numParts;
|
82 |
int numPoints;
|
83 |
int i;
|
84 |
int j;
|
85 |
|
86 |
bb.position(bb.position() + 32);
|
87 |
numParts = bb.getInt(); |
88 |
numPoints = bb.getInt(); |
89 |
|
90 |
int[] tempParts = new int[numParts]; |
91 |
|
92 |
MultiLine multiLine = null;
|
93 |
Line line = null; |
94 |
if (numParts > 1) { |
95 |
multiLine = (MultiLine) gManager.create(Geometry.TYPES.MULTILINE, Geometry.SUBTYPES.GEOM2D); |
96 |
} |
97 |
|
98 |
for (i = 0; i < numParts; i++) { |
99 |
tempParts[i] = bb.getInt(); |
100 |
} |
101 |
|
102 |
j = 0;
|
103 |
|
104 |
for (i = 0; i < numPoints; i++) { |
105 |
p = (Point) readPoint(bb);
|
106 |
|
107 |
if (i == tempParts[j]) {
|
108 |
if(multiLine!=null && line != null){ |
109 |
multiLine.addCurve(line); |
110 |
} |
111 |
line = (Line) gManager.create(Geometry.TYPES.LINE, Geometry.SUBTYPES.GEOM2D);
|
112 |
if (j < (numParts - 1)) { |
113 |
j++; |
114 |
} |
115 |
} |
116 |
line.addVertex(p); |
117 |
} |
118 |
checkNumVerticesOfLine(line); |
119 |
|
120 |
if (multiLine != null) { |
121 |
multiLine.addCurve(line); |
122 |
return multiLine;
|
123 |
} else {
|
124 |
return line;
|
125 |
} |
126 |
} |
127 |
|
128 |
/* (non-Javadoc)
|
129 |
* @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoLygon(org.gvsig.utils.bigfile.BigByteBuffer2)
|
130 |
*/
|
131 |
public Geometry readPoLygon(BigByteBuffer2 bb) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException { |
132 |
|
133 |
GeometryManager gManager = GeometryLocator.getGeometryManager(); |
134 |
|
135 |
Point p = null; |
136 |
int numParts;
|
137 |
int numPoints;
|
138 |
int i;
|
139 |
|
140 |
bb.position(bb.position() + 32);
|
141 |
numParts = bb.getInt(); |
142 |
numPoints = bb.getInt(); |
143 |
|
144 |
int[] tempParts = new int[numParts]; |
145 |
|
146 |
for (i = 0; i < numParts; i++) { |
147 |
tempParts[i] = bb.getInt(); |
148 |
} |
149 |
|
150 |
MultiPolygon multipolygon = null;
|
151 |
Polygon polygon = null; |
152 |
Ring ring = null;
|
153 |
|
154 |
int pointsCounter = 0; |
155 |
for(int part=0; part<numParts; part++){ |
156 |
ring = (Ring) gManager.create(Geometry.TYPES.RING, Geometry.SUBTYPES.GEOM2D); |
157 |
int lastPoint = numPoints;
|
158 |
if(part<numParts-1){ |
159 |
lastPoint = tempParts[part+1];
|
160 |
} |
161 |
while(pointsCounter<lastPoint){
|
162 |
p = (Point) readPoint(bb);
|
163 |
ring.addVertex(p); |
164 |
pointsCounter++; |
165 |
} |
166 |
ring.closePrimitive(); |
167 |
checkNumVerticesOfRing(ring); |
168 |
if (ring.isCCW() && polygon!=null) { |
169 |
// Los anillos interiores deben ser CCW pero si encontramos un
|
170 |
// anillo interior que no est? envuelto en un pol?gono
|
171 |
// consideramos que es un pol?gono en s? y nos aseguramos de
|
172 |
// darle la vuelta (en el else)
|
173 |
//FIXME: Comprobar que este es el comportamiento deseado
|
174 |
polygon.addInteriorRing(ring); |
175 |
} else {
|
176 |
if(polygon!=null){ |
177 |
if(multipolygon==null){ |
178 |
multipolygon = (MultiPolygon) gManager.create(Geometry.TYPES.MULTIPOLYGON, Geometry.SUBTYPES.GEOM2D); |
179 |
} |
180 |
multipolygon.addPrimitive(polygon); |
181 |
} |
182 |
polygon = (Polygon) gManager.create(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM2D);
|
183 |
polygon.ensureCapacity(ring.getNumVertices()); |
184 |
if(ring.isCCW()){
|
185 |
//To ensure CW orientation for polygons
|
186 |
for (int v = ring.getNumVertices()-1; v >=0; v--) { |
187 |
polygon.addVertex(ring.getVertex(v)); |
188 |
} |
189 |
} else {
|
190 |
for (int v = 0; v < ring.getNumVertices(); v++) { |
191 |
polygon.addVertex(ring.getVertex(v)); |
192 |
} |
193 |
} |
194 |
} |
195 |
} |
196 |
|
197 |
if (multipolygon != null) { |
198 |
multipolygon.addPrimitive(polygon); |
199 |
return multipolygon;
|
200 |
} else {
|
201 |
return polygon;
|
202 |
} |
203 |
} |
204 |
|
205 |
/* (non-Javadoc)
|
206 |
* @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readMultiPoint(org.gvsig.utils.bigfile.BigByteBuffer2)
|
207 |
*/
|
208 |
public Geometry readMultiPoint(BigByteBuffer2 bb) throws CreateGeometryException { |
209 |
|
210 |
GeometryManager gManager = GeometryLocator.getGeometryManager(); |
211 |
|
212 |
int numPoints;
|
213 |
bb.position(bb.position() + 32);
|
214 |
numPoints = bb.getInt(); |
215 |
|
216 |
MultiPoint multipoint = gManager.createMultiPoint(Geometry.SUBTYPES.GEOM2D); |
217 |
|
218 |
for (int i = 0; i < numPoints; i++) { |
219 |
multipoint.addPoint((Point)readPoint(bb));
|
220 |
} |
221 |
return multipoint;
|
222 |
} |
223 |
|
224 |
} |