svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / shp / utils / SHPPolygon.java @ 22373
History | View | Annotate | Download (6.92 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.fmap.data.feature.file.shp.utils; |
42 |
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.nio.ByteBuffer; |
45 |
import java.nio.MappedByteBuffer; |
46 |
|
47 |
import org.gvsig.fmap.geom.Geometry; |
48 |
import org.gvsig.fmap.geom.primitive.Point2D; |
49 |
import org.gvsig.fmap.geom.primitive.Surface2D; |
50 |
|
51 |
/**
|
52 |
* Elemento shape de tipo Pol?gono.
|
53 |
*
|
54 |
* @author Vicente Caballero Navarro
|
55 |
*/
|
56 |
public class SHPPolygon extends SHPMultiLine { |
57 |
|
58 |
/**
|
59 |
* Crea un nuevo SHPPolygon.
|
60 |
*/
|
61 |
public SHPPolygon() {
|
62 |
m_type = SHP.POLYGON2D; |
63 |
} |
64 |
|
65 |
/**
|
66 |
* Crea un nuevo SHPPolygon.
|
67 |
*
|
68 |
* @param type Tipo de shape.
|
69 |
*
|
70 |
* @throws ShapefileException
|
71 |
*/
|
72 |
public SHPPolygon(int type) { |
73 |
if ((type != SHP.POLYGON2D) &&
|
74 |
(type != SHP.POLYGONM) && |
75 |
(type != SHP.POLYGON3D)) { |
76 |
// throw new ShapefileException("No es de tipo 5, 15, o 25");
|
77 |
} |
78 |
|
79 |
m_type = type; |
80 |
} |
81 |
|
82 |
/**
|
83 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
|
84 |
*/
|
85 |
public int getShapeType() { |
86 |
return m_type;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
|
91 |
*/
|
92 |
public synchronized Geometry read(MappedByteBuffer buffer, int type) { |
93 |
double minX = buffer.getDouble();
|
94 |
double minY = buffer.getDouble();
|
95 |
double maxX = buffer.getDouble();
|
96 |
double maxY = buffer.getDouble();
|
97 |
Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX, |
98 |
maxY - maxY); |
99 |
int numParts = buffer.getInt();
|
100 |
int numPoints = buffer.getInt();
|
101 |
|
102 |
int[] partOffsets = new int[numParts]; |
103 |
|
104 |
for (int i = 0; i < numParts; i++) { |
105 |
partOffsets[i] = buffer.getInt(); |
106 |
} |
107 |
|
108 |
Point2D[] points = readPoints(buffer, numPoints); |
109 |
|
110 |
/* if (m_type == FConstant.SHAPE_TYPE_POLYGONZ) {
|
111 |
//z
|
112 |
buffer.position(buffer.position() + (2 * 8));
|
113 |
for (int t = 0; t < numPoints; t++) {
|
114 |
points[t].z = buffer.getDouble();
|
115 |
}
|
116 |
}
|
117 |
*/
|
118 |
int offset = 0; |
119 |
int start;
|
120 |
int finish;
|
121 |
int length;
|
122 |
|
123 |
for (int part = 0; part < numParts; part++) { |
124 |
start = partOffsets[part]; |
125 |
|
126 |
if (part == (numParts - 1)) { |
127 |
finish = numPoints; |
128 |
} else {
|
129 |
finish = partOffsets[part + 1];
|
130 |
} |
131 |
|
132 |
length = finish - start; |
133 |
|
134 |
Point2D[] pointsPart = new Point2D[length]; |
135 |
|
136 |
for (int i = 0; i < length; i++) { |
137 |
pointsPart[i] = points[offset++]; |
138 |
} |
139 |
} |
140 |
|
141 |
return (Geometry) new Surface2D(getGeneralPathX(points, partOffsets)); |
142 |
} |
143 |
|
144 |
/**
|
145 |
* Lee los puntos del buffer.
|
146 |
*
|
147 |
* @param buffer
|
148 |
* @param numPoints N?mero de puntos.
|
149 |
*
|
150 |
* @return Vector de Puntos.
|
151 |
*/
|
152 |
private synchronized Point2D[] readPoints(final MappedByteBuffer buffer, |
153 |
final int numPoints) { |
154 |
Point2D[] points = new Point2D[numPoints]; |
155 |
|
156 |
for (int t = 0; t < numPoints; t++) { |
157 |
points[t] = new Point2D(buffer.getDouble(), buffer.getDouble()); |
158 |
} |
159 |
|
160 |
return points;
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
|
165 |
*/
|
166 |
public synchronized void write(ByteBuffer buffer, Geometry geometry) { |
167 |
//FPolygon2D polyLine;
|
168 |
//polyLine = (FPolygon2D) geometry.getShape();
|
169 |
Rectangle2D rec = geometry.getBounds2D();
|
170 |
buffer.putDouble(rec.getMinX()); |
171 |
buffer.putDouble(rec.getMinY()); |
172 |
buffer.putDouble(rec.getMaxX()); |
173 |
buffer.putDouble(rec.getMaxY()); |
174 |
|
175 |
//////
|
176 |
///obtainsPoints(geometry.getGeneralPathXIterator());
|
177 |
|
178 |
//int[] parts=polyLine.getParts();
|
179 |
//FPoint2D[] points=polyLine.getPoints();
|
180 |
int nparts = parts.length;
|
181 |
int npoints = points.length;
|
182 |
|
183 |
//////
|
184 |
///int npoints = polyLine.getNumPoints();
|
185 |
///int nparts = polyLine.getNumParts();
|
186 |
buffer.putInt(nparts); |
187 |
buffer.putInt(npoints); |
188 |
|
189 |
int count = 0; |
190 |
|
191 |
for (int t = 0; t < nparts; t++) { |
192 |
///buffer.putInt(polyLine.getPart(t));
|
193 |
buffer.putInt(parts[t]); |
194 |
} |
195 |
|
196 |
///FPoint[] points = polyLine.getPoints();
|
197 |
for (int t = 0; t < points.length; t++) { |
198 |
///buffer.putDouble(points[t].x);
|
199 |
///buffer.putDouble(points[t].y);
|
200 |
buffer.putDouble(points[t].getX()); |
201 |
buffer.putDouble(points[t].getY()); |
202 |
} |
203 |
|
204 |
if (m_type == SHP.POLYGON3D) {
|
205 |
double[] zExtreame = SHP.getZMinMax(zs); |
206 |
if (Double.isNaN(zExtreame[0])) { |
207 |
buffer.putDouble(0.0);
|
208 |
buffer.putDouble(0.0);
|
209 |
} else {
|
210 |
buffer.putDouble(zExtreame[0]);
|
211 |
buffer.putDouble(zExtreame[1]);
|
212 |
} |
213 |
for (int t = 0; t < npoints; t++) { |
214 |
double z = zs[t];
|
215 |
if (Double.isNaN(z)) { |
216 |
buffer.putDouble(0.0);
|
217 |
} else {
|
218 |
buffer.putDouble(z); |
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
if ((m_type == SHP.POLYGONM) ||
|
224 |
(m_type == SHP.POLYGON3D)) { |
225 |
buffer.putDouble(-10E40);
|
226 |
buffer.putDouble(-10E40);
|
227 |
|
228 |
for (int t = 0; t < npoints; t++) { |
229 |
buffer.putDouble(-10E40);
|
230 |
} |
231 |
} |
232 |
} |
233 |
|
234 |
/**
|
235 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
|
236 |
*/
|
237 |
public synchronized int getLength(Geometry fgeometry) { |
238 |
// FPolygon2D multi;
|
239 |
//multi = (FPolygon2D) fgeometry.getShape();
|
240 |
///int nrings = 0;
|
241 |
///obtainsPoints(fgeometry.getGeneralPathXIterator());
|
242 |
|
243 |
//int[] parts=multi.getParts();
|
244 |
//FPoint2D[] points;
|
245 |
/////////
|
246 |
//points = multi.getPoints();
|
247 |
int npoints = points.length;
|
248 |
|
249 |
///////////
|
250 |
///nrings = multi.getNumParts();
|
251 |
///int npoints = multi.getNumPoints();
|
252 |
int length;
|
253 |
|
254 |
if (m_type == SHP.POLYGON3D) {
|
255 |
length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) + |
256 |
16 + (8 * npoints) + 16; |
257 |
} else if (m_type == SHP.POLYGONM) { |
258 |
length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) + |
259 |
16;
|
260 |
} else if (m_type == SHP.POLYGON2D) { |
261 |
length = 44 + (4 * parts.length) + (16 * npoints); |
262 |
} else {
|
263 |
throw new IllegalStateException( |
264 |
"Expected ShapeType of Polygon, got " + m_type);
|
265 |
} |
266 |
|
267 |
return length;
|
268 |
} |
269 |
} |