svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / shp / utils / SHPPolygon.java @ 28790
History | View | Annotate | Download (7.58 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.dal.store.shp.utils; |
42 |
|
43 |
import java.awt.geom.Point2D; |
44 |
import java.awt.geom.Rectangle2D; |
45 |
import java.nio.ByteBuffer; |
46 |
import java.nio.MappedByteBuffer; |
47 |
|
48 |
import org.gvsig.fmap.geom.Geometry; |
49 |
import org.gvsig.fmap.geom.GeometryLocator; |
50 |
import org.gvsig.fmap.geom.GeometryManager; |
51 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
52 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
53 |
import org.gvsig.fmap.geom.primitive.Envelope; |
54 |
import org.gvsig.fmap.geom.primitive.Point; |
55 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
* Elemento shape de tipo Pol?gono.
|
61 |
*
|
62 |
* @author Vicente Caballero Navarro
|
63 |
*/
|
64 |
public class SHPPolygon extends SHPMultiLine { |
65 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
66 |
private static final Logger logger = LoggerFactory.getLogger(SHPPolygon.class); |
67 |
|
68 |
/**
|
69 |
* Crea un nuevo SHPPolygon.
|
70 |
*/
|
71 |
public SHPPolygon() {
|
72 |
m_type = SHP.POLYGON2D; |
73 |
} |
74 |
|
75 |
/**
|
76 |
* Crea un nuevo SHPPolygon.
|
77 |
*
|
78 |
* @param type Tipo de shape.
|
79 |
*
|
80 |
* @throws ShapefileException
|
81 |
*/
|
82 |
public SHPPolygon(int type) { |
83 |
if ((type != SHP.POLYGON2D) &&
|
84 |
(type != SHP.POLYGONM) && |
85 |
(type != SHP.POLYGON3D)) { |
86 |
// throw new ShapefileException("No es de tipo 5, 15, o 25");
|
87 |
} |
88 |
|
89 |
m_type = type; |
90 |
} |
91 |
|
92 |
/**
|
93 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
|
94 |
*/
|
95 |
public int getShapeType() { |
96 |
return m_type;
|
97 |
} |
98 |
|
99 |
/**
|
100 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
|
101 |
*/
|
102 |
public synchronized Geometry read(MappedByteBuffer buffer, int type) { |
103 |
double minX = buffer.getDouble();
|
104 |
double minY = buffer.getDouble();
|
105 |
double maxX = buffer.getDouble();
|
106 |
double maxY = buffer.getDouble();
|
107 |
Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX, |
108 |
maxY - maxY); |
109 |
int numParts = buffer.getInt();
|
110 |
int numPoints = buffer.getInt();
|
111 |
|
112 |
int[] partOffsets = new int[numParts]; |
113 |
|
114 |
for (int i = 0; i < numParts; i++) { |
115 |
partOffsets[i] = buffer.getInt(); |
116 |
} |
117 |
|
118 |
Point[] points = readPoints(buffer, numPoints); |
119 |
|
120 |
/* if (m_type == FConstant.SHAPE_TYPE_POLYGONZ) {
|
121 |
//z
|
122 |
buffer.position(buffer.position() + (2 * 8));
|
123 |
for (int t = 0; t < numPoints; t++) {
|
124 |
points[t].z = buffer.getDouble();
|
125 |
}
|
126 |
}
|
127 |
*/
|
128 |
int offset = 0; |
129 |
int start;
|
130 |
int finish;
|
131 |
int length;
|
132 |
|
133 |
for (int part = 0; part < numParts; part++) { |
134 |
start = partOffsets[part]; |
135 |
|
136 |
if (part == (numParts - 1)) { |
137 |
finish = numPoints; |
138 |
} else {
|
139 |
finish = partOffsets[part + 1];
|
140 |
} |
141 |
|
142 |
length = finish - start; |
143 |
|
144 |
Point[] pointsPart = new Point[length]; |
145 |
|
146 |
for (int i = 0; i < length; i++) { |
147 |
pointsPart[i] = points[offset++]; |
148 |
} |
149 |
} |
150 |
|
151 |
try {
|
152 |
return geomManager.createSurface(getGeneralPathX(points, partOffsets), SUBTYPES.GEOM2D);
|
153 |
} catch (CreateGeometryException e) {
|
154 |
logger.error("Error creating a surface", e);
|
155 |
return null; |
156 |
} |
157 |
} |
158 |
|
159 |
/**
|
160 |
* Lee los puntos del buffer.
|
161 |
*
|
162 |
* @param buffer
|
163 |
* @param numPoints N?mero de puntos.
|
164 |
*
|
165 |
* @return Vector de Puntos.
|
166 |
*/
|
167 |
private synchronized Point[] readPoints(final MappedByteBuffer buffer, |
168 |
final int numPoints) { |
169 |
Point[] points = new Point[numPoints]; |
170 |
|
171 |
for (int t = 0; t < numPoints; t++) { |
172 |
try {
|
173 |
points[t] = geomManager.createPoint(buffer.getDouble(), buffer.getDouble(), SUBTYPES.GEOM2D); |
174 |
} catch (CreateGeometryException e) {
|
175 |
logger.error("Error creating a point", e);
|
176 |
} |
177 |
} |
178 |
|
179 |
return points;
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
|
184 |
*/
|
185 |
public synchronized void write(ByteBuffer buffer, Geometry geometry) { |
186 |
//FPolygon2D polyLine;
|
187 |
//polyLine = (FPolygon2D) geometry.getShape();
|
188 |
Envelope env = geometry.getEnvelope(); |
189 |
|
190 |
buffer.putDouble(env.getMinimum(0));
|
191 |
buffer.putDouble(env.getMinimum(1));
|
192 |
buffer.putDouble(env.getMaximum(0));
|
193 |
buffer.putDouble(env.getMaximum(1));
|
194 |
//////
|
195 |
///obtainsPoints(geometry.getGeneralPathXIterator());
|
196 |
|
197 |
//int[] parts=polyLine.getParts();
|
198 |
//FPoint2D[] points=polyLine.getPoints();
|
199 |
int nparts = parts.length;
|
200 |
int npoints = points.length;
|
201 |
|
202 |
//////
|
203 |
///int npoints = polyLine.getNumPoints();
|
204 |
///int nparts = polyLine.getNumParts();
|
205 |
buffer.putInt(nparts); |
206 |
buffer.putInt(npoints); |
207 |
|
208 |
// int count = 0;
|
209 |
|
210 |
for (int t = 0; t < nparts; t++) { |
211 |
///buffer.putInt(polyLine.getPart(t));
|
212 |
buffer.putInt(parts[t]); |
213 |
} |
214 |
|
215 |
///FPoint[] points = polyLine.getPoints();
|
216 |
for (int t = 0; t < points.length; t++) { |
217 |
///buffer.putDouble(points[t].x);
|
218 |
///buffer.putDouble(points[t].y);
|
219 |
buffer.putDouble(points[t].getX()); |
220 |
buffer.putDouble(points[t].getY()); |
221 |
} |
222 |
|
223 |
if (m_type == SHP.POLYGON3D) {
|
224 |
double[] zExtreame = SHP.getZMinMax(zs); |
225 |
if (Double.isNaN(zExtreame[0])) { |
226 |
buffer.putDouble(0.0);
|
227 |
buffer.putDouble(0.0);
|
228 |
} else {
|
229 |
buffer.putDouble(zExtreame[0]);
|
230 |
buffer.putDouble(zExtreame[1]);
|
231 |
} |
232 |
for (int t = 0; t < npoints; t++) { |
233 |
double z = zs[t];
|
234 |
if (Double.isNaN(z)) { |
235 |
buffer.putDouble(0.0);
|
236 |
} else {
|
237 |
buffer.putDouble(z); |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
if ((m_type == SHP.POLYGONM) ||
|
243 |
(m_type == SHP.POLYGON3D)) { |
244 |
buffer.putDouble(-10E40);
|
245 |
buffer.putDouble(-10E40);
|
246 |
|
247 |
for (int t = 0; t < npoints; t++) { |
248 |
buffer.putDouble(-10E40);
|
249 |
} |
250 |
} |
251 |
} |
252 |
|
253 |
/**
|
254 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
|
255 |
*/
|
256 |
public synchronized int getLength(Geometry fgeometry) { |
257 |
// FPolygon2D multi;
|
258 |
//multi = (FPolygon2D) fgeometry.getShape();
|
259 |
///int nrings = 0;
|
260 |
///obtainsPoints(fgeometry.getGeneralPathXIterator());
|
261 |
|
262 |
//int[] parts=multi.getParts();
|
263 |
//FPoint2D[] points;
|
264 |
/////////
|
265 |
//points = multi.getPoints();
|
266 |
int npoints = points.length;
|
267 |
|
268 |
///////////
|
269 |
///nrings = multi.getNumParts();
|
270 |
///int npoints = multi.getNumPoints();
|
271 |
int length;
|
272 |
|
273 |
if (m_type == SHP.POLYGON3D) {
|
274 |
length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) + |
275 |
16;
|
276 |
} else if (m_type == SHP.POLYGONM) { |
277 |
length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) + |
278 |
16;
|
279 |
} else if (m_type == SHP.POLYGON2D) { |
280 |
length = 44 + (4 * parts.length) + (16 * npoints); |
281 |
} else {
|
282 |
throw new IllegalStateException( |
283 |
"Expected ShapeType of Polygon, got " + m_type);
|
284 |
} |
285 |
|
286 |
return length;
|
287 |
} |
288 |
} |