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 @ 42811
History | View | Annotate | Download (7.52 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 |
if (multiLine != null) { |
119 |
multiLine.addCurve(line); |
120 |
return multiLine;
|
121 |
} else {
|
122 |
return line;
|
123 |
} |
124 |
} |
125 |
|
126 |
/* (non-Javadoc)
|
127 |
* @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readPoLygon(org.gvsig.utils.bigfile.BigByteBuffer2)
|
128 |
*/
|
129 |
public Geometry readPoLygon(BigByteBuffer2 bb) throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException { |
130 |
|
131 |
GeometryManager gManager = GeometryLocator.getGeometryManager(); |
132 |
|
133 |
Point p = null; |
134 |
int numParts;
|
135 |
int numPoints;
|
136 |
int i;
|
137 |
|
138 |
bb.position(bb.position() + 32);
|
139 |
numParts = bb.getInt(); |
140 |
numPoints = bb.getInt(); |
141 |
|
142 |
int[] tempParts = new int[numParts]; |
143 |
|
144 |
for (i = 0; i < numParts; i++) { |
145 |
tempParts[i] = bb.getInt(); |
146 |
} |
147 |
|
148 |
MultiPolygon multipolygon = null;
|
149 |
Polygon polygon = null; |
150 |
Ring ring = null;
|
151 |
|
152 |
int pointsCounter = 0; |
153 |
for(int part=0; part<numParts; part++){ |
154 |
ring = (Ring) gManager.create(Geometry.TYPES.RING, Geometry.SUBTYPES.GEOM2D); |
155 |
int lastPoint = numPoints;
|
156 |
if(part<numParts-1){ |
157 |
lastPoint = tempParts[part+1];
|
158 |
} |
159 |
while(pointsCounter<lastPoint){
|
160 |
p = (Point) readPoint(bb);
|
161 |
ring.addVertex(p); |
162 |
pointsCounter++; |
163 |
} |
164 |
ring.closePrimitive(); |
165 |
checkNumVerticesOfRing(ring); |
166 |
if (ring.isCCW() && polygon!=null) { |
167 |
// Los anillos interiores deben ser CCW pero si encontramos un
|
168 |
// anillo interior que no est? envuelto en un pol?gono
|
169 |
// consideramos que es un pol?gono en s? y nos aseguramos de
|
170 |
// darle la vuelta (en el else)
|
171 |
//FIXME: Comprobar que este es el comportamiento deseado
|
172 |
polygon.addInteriorRing(ring); |
173 |
} else {
|
174 |
if(polygon!=null){ |
175 |
if(multipolygon==null){ |
176 |
multipolygon = (MultiPolygon) gManager.create(Geometry.TYPES.MULTIPOLYGON, Geometry.SUBTYPES.GEOM2D); |
177 |
} |
178 |
multipolygon.addPrimitive(polygon); |
179 |
} |
180 |
polygon = (Polygon) gManager.create(Geometry.TYPES.POLYGON, Geometry.SUBTYPES.GEOM2D);
|
181 |
polygon.ensureCapacity(ring.getNumVertices()); |
182 |
if(ring.isCCW()){
|
183 |
//To ensure CW orientation for polygons
|
184 |
for (int v = ring.getNumVertices()-1; v >=0; v--) { |
185 |
polygon.addVertex(ring.getVertex(v)); |
186 |
} |
187 |
} else {
|
188 |
for (int v = 0; v < ring.getNumVertices(); v++) { |
189 |
polygon.addVertex(ring.getVertex(v)); |
190 |
} |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
if (multipolygon != null) { |
196 |
multipolygon.addPrimitive(polygon); |
197 |
return multipolygon;
|
198 |
} else {
|
199 |
return polygon;
|
200 |
} |
201 |
} |
202 |
|
203 |
/* (non-Javadoc)
|
204 |
* @see org.gvsig.fmap.dal.store.shp.utils.SHPReader#readMultiPoint(org.gvsig.utils.bigfile.BigByteBuffer2)
|
205 |
*/
|
206 |
public Geometry readMultiPoint(BigByteBuffer2 bb) throws CreateGeometryException { |
207 |
|
208 |
GeometryManager gManager = GeometryLocator.getGeometryManager(); |
209 |
|
210 |
int numPoints;
|
211 |
bb.position(bb.position() + 32);
|
212 |
numPoints = bb.getInt(); |
213 |
|
214 |
MultiPoint multipoint = gManager.createMultiPoint(Geometry.SUBTYPES.GEOM2D); |
215 |
|
216 |
for (int i = 0; i < numPoints; i++) { |
217 |
multipoint.addPoint((Point)readPoint(bb));
|
218 |
} |
219 |
return multipoint;
|
220 |
} |
221 |
|
222 |
} |