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 / SHPPolygon.java @ 40559
History | View | Annotate | Download (6.26 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.shp.utils; |
25 |
|
26 |
import java.awt.geom.Rectangle2D; |
27 |
import java.nio.ByteBuffer; |
28 |
import java.nio.MappedByteBuffer; |
29 |
|
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.GeometryLocator; |
32 |
import org.gvsig.fmap.geom.GeometryManager; |
33 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
34 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
35 |
import org.gvsig.fmap.geom.primitive.Envelope; |
36 |
import org.gvsig.fmap.geom.primitive.Point; |
37 |
import org.slf4j.Logger; |
38 |
import org.slf4j.LoggerFactory; |
39 |
|
40 |
/**
|
41 |
* Elemento shape de tipo Pol?gono.
|
42 |
*
|
43 |
* @author Vicente Caballero Navarro
|
44 |
*/
|
45 |
public class SHPPolygon extends SHPMultiLine { |
46 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
47 |
private static final Logger logger = LoggerFactory.getLogger(SHPPolygon.class); |
48 |
|
49 |
/**
|
50 |
* Crea un nuevo SHPPolygon.
|
51 |
*/
|
52 |
public SHPPolygon() {
|
53 |
m_type = SHP.POLYGON2D; |
54 |
} |
55 |
|
56 |
/**
|
57 |
* Crea un nuevo SHPPolygon.
|
58 |
*
|
59 |
* @param type Tipo de shape.
|
60 |
*
|
61 |
* @throws ShapefileException
|
62 |
*/
|
63 |
public SHPPolygon(int type) { |
64 |
if ((type != SHP.POLYGON2D) &&
|
65 |
(type != SHP.POLYGONM) && |
66 |
(type != SHP.POLYGON3D)) { |
67 |
// throw new ShapefileException("No es de tipo 5, 15, o 25");
|
68 |
} |
69 |
|
70 |
m_type = type; |
71 |
} |
72 |
|
73 |
/**
|
74 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
|
75 |
*/
|
76 |
public int getShapeType() { |
77 |
return m_type;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
|
82 |
*/
|
83 |
public synchronized Geometry read(MappedByteBuffer buffer, int type) { |
84 |
double minX = buffer.getDouble();
|
85 |
double minY = buffer.getDouble();
|
86 |
double maxX = buffer.getDouble();
|
87 |
double maxY = buffer.getDouble();
|
88 |
Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX, |
89 |
maxY - maxY); |
90 |
int numParts = buffer.getInt();
|
91 |
int numPoints = buffer.getInt();
|
92 |
|
93 |
int[] partOffsets = new int[numParts]; |
94 |
|
95 |
for (int i = 0; i < numParts; i++) { |
96 |
partOffsets[i] = buffer.getInt(); |
97 |
} |
98 |
|
99 |
Point[] points = readPoints(buffer, numPoints); |
100 |
|
101 |
/* if (m_type == FConstant.SHAPE_TYPE_POLYGONZ) {
|
102 |
//z
|
103 |
buffer.position(buffer.position() + (2 * 8));
|
104 |
for (int t = 0; t < numPoints; t++) {
|
105 |
points[t].z = buffer.getDouble();
|
106 |
}
|
107 |
}
|
108 |
*/
|
109 |
int offset = 0; |
110 |
int start;
|
111 |
int finish;
|
112 |
int length;
|
113 |
|
114 |
for (int part = 0; part < numParts; part++) { |
115 |
start = partOffsets[part]; |
116 |
|
117 |
if (part == (numParts - 1)) { |
118 |
finish = numPoints; |
119 |
} else {
|
120 |
finish = partOffsets[part + 1];
|
121 |
} |
122 |
|
123 |
length = finish - start; |
124 |
|
125 |
Point[] pointsPart = new Point[length]; |
126 |
|
127 |
for (int i = 0; i < length; i++) { |
128 |
pointsPart[i] = points[offset++]; |
129 |
} |
130 |
} |
131 |
|
132 |
try {
|
133 |
return geomManager.createSurface(getGeneralPathX(points, partOffsets), SUBTYPES.GEOM2D);
|
134 |
} catch (CreateGeometryException e) {
|
135 |
logger.error("Error creating a surface", e);
|
136 |
return null; |
137 |
} |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Lee los puntos del buffer.
|
142 |
*
|
143 |
* @param buffer
|
144 |
* @param numPoints N?mero de puntos.
|
145 |
*
|
146 |
* @return Vector de Puntos.
|
147 |
*/
|
148 |
private synchronized Point[] readPoints(final MappedByteBuffer buffer, |
149 |
final int numPoints) { |
150 |
Point[] points = new Point[numPoints]; |
151 |
|
152 |
for (int t = 0; t < numPoints; t++) { |
153 |
try {
|
154 |
points[t] = geomManager.createPoint(buffer.getDouble(), buffer.getDouble(), SUBTYPES.GEOM2D); |
155 |
} catch (CreateGeometryException e) {
|
156 |
logger.error("Error creating a point", e);
|
157 |
} |
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 |
Envelope env = geometry.getEnvelope(); |
168 |
|
169 |
buffer.putDouble(env.getMinimum(0));
|
170 |
buffer.putDouble(env.getMinimum(1));
|
171 |
buffer.putDouble(env.getMaximum(0));
|
172 |
buffer.putDouble(env.getMaximum(1));
|
173 |
|
174 |
int nparts = parts.length;
|
175 |
int npoints = points.length;
|
176 |
|
177 |
buffer.putInt(nparts); |
178 |
buffer.putInt(npoints); |
179 |
|
180 |
for (int t = 0; t < nparts; t++) { |
181 |
buffer.putInt(parts[t]); |
182 |
} |
183 |
|
184 |
for (int t = 0; t < points.length; t++) { |
185 |
buffer.putDouble(points[t].getX()); |
186 |
buffer.putDouble(points[t].getY()); |
187 |
} |
188 |
|
189 |
if (m_type == SHP.POLYGON3D) {
|
190 |
double[] zExtreame = SHP.getZMinMax(zs); |
191 |
if (Double.isNaN(zExtreame[0])) { |
192 |
buffer.putDouble(0.0);
|
193 |
buffer.putDouble(0.0);
|
194 |
} else {
|
195 |
buffer.putDouble(zExtreame[0]);
|
196 |
buffer.putDouble(zExtreame[1]);
|
197 |
} |
198 |
for (int t = 0; t < npoints; t++) { |
199 |
double z = zs[t];
|
200 |
if (Double.isNaN(z)) { |
201 |
buffer.putDouble(0.0);
|
202 |
} else {
|
203 |
buffer.putDouble(z); |
204 |
} |
205 |
} |
206 |
} |
207 |
|
208 |
if ((m_type == SHP.POLYGONM)) {
|
209 |
buffer.putDouble(-10E40);
|
210 |
buffer.putDouble(-10E40);
|
211 |
|
212 |
for (int t = 0; t < npoints; t++) { |
213 |
buffer.putDouble(-10E40);
|
214 |
} |
215 |
} |
216 |
} |
217 |
|
218 |
/**
|
219 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
|
220 |
*/
|
221 |
public synchronized int getLength(Geometry fgeometry) { |
222 |
int npoints = points.length;
|
223 |
|
224 |
int length;
|
225 |
|
226 |
if (m_type == SHP.POLYGON3D || m_type == SHP.POLYGONM) {
|
227 |
length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) + 16; |
228 |
} else if (m_type == SHP.POLYGON2D) { |
229 |
length = 44 + (4 * parts.length) + (16 * npoints); |
230 |
} else {
|
231 |
throw new IllegalStateException("Expected ShapeType of Polygon, got " + m_type); |
232 |
} |
233 |
|
234 |
return length;
|
235 |
} |
236 |
} |