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 / SHPPolygon2DWriter.java @ 42324
History | View | Annotate | Download (8.16 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.nio.ByteBuffer; |
27 |
import java.util.ArrayList; |
28 |
|
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
import org.gvsig.fmap.dal.exception.WriteException; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.geom.GeometryException; |
35 |
import org.gvsig.fmap.geom.GeometryLocator; |
36 |
import org.gvsig.fmap.geom.GeometryManager; |
37 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
38 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
39 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
40 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
41 |
import org.gvsig.fmap.geom.primitive.Envelope; |
42 |
import org.gvsig.fmap.geom.primitive.Point; |
43 |
import org.gvsig.fmap.geom.primitive.Polygon; |
44 |
import org.gvsig.fmap.geom.primitive.Primitive; |
45 |
import org.gvsig.fmap.geom.primitive.Ring; |
46 |
import org.gvsig.fmap.geom.primitive.Surface; |
47 |
import org.gvsig.tools.exception.BaseException; |
48 |
|
49 |
/**
|
50 |
*
|
51 |
*
|
52 |
*/
|
53 |
public class SHPPolygon2DWriter implements SHPShapeWriter { |
54 |
private Geometry geometry;
|
55 |
private int m_type; |
56 |
private int[] parts; |
57 |
private Point[] points; |
58 |
|
59 |
private static final Logger logger = LoggerFactory.getLogger(SHPPolygon2DWriter.class); |
60 |
|
61 |
|
62 |
/**
|
63 |
* Crea un nuevo SHPPolygon.
|
64 |
*/
|
65 |
public SHPPolygon2DWriter() {
|
66 |
m_type = SHP.POLYGON2D; |
67 |
} |
68 |
|
69 |
/**
|
70 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
|
71 |
*/
|
72 |
public int getShapeType() { |
73 |
return m_type;
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* @throws WriteException
|
78 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
|
79 |
*/
|
80 |
public synchronized void write(ByteBuffer buffer) throws WriteException { |
81 |
Envelope env = geometry.getEnvelope(); |
82 |
|
83 |
buffer.putDouble(env.getMinimum(0));
|
84 |
buffer.putDouble(env.getMinimum(1));
|
85 |
buffer.putDouble(env.getMaximum(0));
|
86 |
buffer.putDouble(env.getMaximum(1));
|
87 |
|
88 |
|
89 |
try {
|
90 |
initialize(geometry); |
91 |
} catch (BaseException e) {
|
92 |
throw new WriteException("SHPPolygon2DWriter", e); |
93 |
} |
94 |
|
95 |
int numParts = parts.length;
|
96 |
int npoints = points.length;
|
97 |
|
98 |
buffer.putInt(numParts); |
99 |
buffer.putInt(npoints); |
100 |
|
101 |
for (int i = 0; i < numParts; i++) { |
102 |
buffer.putInt(parts[i]); |
103 |
} |
104 |
|
105 |
for (int t = 0; t < npoints; t++) { |
106 |
Point point = points[t];
|
107 |
buffer.putDouble(point.getX()); |
108 |
buffer.putDouble(point.getY()); |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* @throws GeometryException
|
115 |
* @throws GeometryOperationException
|
116 |
* @throws GeometryOperationNotSupportedException
|
117 |
*
|
118 |
*/
|
119 |
public void initialize(Geometry g) throws GeometryException, GeometryOperationNotSupportedException, GeometryOperationException { |
120 |
|
121 |
geometry = g; |
122 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
123 |
|
124 |
ArrayList<Point> arrayPoints = new ArrayList<Point>(); |
125 |
ArrayList<Integer> arrayParts = new ArrayList<Integer>(); |
126 |
|
127 |
if(geometry instanceof Polygon){ |
128 |
Polygon polygon = (Polygon)geometry; |
129 |
polygon.ensureOrientation(false);
|
130 |
int index = 0; |
131 |
arrayParts.add(index); |
132 |
for (int i = 0; i < polygon.getNumVertices(); i++) { |
133 |
arrayPoints.add(polygon.getVertex(i)); |
134 |
} |
135 |
if(polygon.getNumInteriorRings()!=0){ |
136 |
index += polygon.getNumVertices(); |
137 |
arrayParts.add(index); |
138 |
} |
139 |
for (int r=0; r<polygon.getNumInteriorRings(); r++){ |
140 |
Ring ring = polygon.getInteriorRing(r); |
141 |
ring.ensureOrientation(true);
|
142 |
for (int i = 0; i < ring.getNumVertices(); i++) { |
143 |
arrayPoints.add(ring.getVertex(i)); |
144 |
} |
145 |
if(r<polygon.getNumInteriorRings()-1){ |
146 |
index += ring.getNumVertices(); |
147 |
arrayParts.add(index); |
148 |
} |
149 |
} |
150 |
} else {
|
151 |
|
152 |
MultiPolygon multiPolygon = null;
|
153 |
if (geometry instanceof MultiPolygon) { |
154 |
multiPolygon = (MultiPolygon) geometry; |
155 |
} else if (geometry instanceof MultiSurface) { |
156 |
multiPolygon = geomManager.createMultiPolygon(Geometry.SUBTYPES.GEOM2D); |
157 |
MultiSurface multiSurface = (MultiSurface) geometry; |
158 |
for (int i = 0; i < multiSurface.getPrimitivesNumber(); i++) { |
159 |
Surface surface = (Surface) multiSurface.getPrimitiveAt(i); |
160 |
if (surface instanceof Polygon) { |
161 |
Polygon polygon = (Polygon)surface; |
162 |
polygon.ensureOrientation(false);
|
163 |
multiPolygon.addPrimitive(polygon); |
164 |
} else {
|
165 |
MultiPolygon polygons = surface.toPolygons(); |
166 |
for (int j = 0; j < polygons.getPrimitivesNumber(); j++) { |
167 |
Primitive polygon = polygons.getPrimitiveAt(j); |
168 |
polygon.ensureOrientation(false);
|
169 |
multiPolygon.addPrimitive(polygon); |
170 |
} |
171 |
} |
172 |
} |
173 |
} else {
|
174 |
multiPolygon = geometry.toPolygons(); |
175 |
} |
176 |
|
177 |
arrayParts.add(0);
|
178 |
int index = 0; |
179 |
for (int i = 0; i < multiPolygon.getPrimitivesNumber(); i++) { |
180 |
Polygon polygon = (Polygon) multiPolygon.getPrimitiveAt(i); |
181 |
|
182 |
polygon.ensureOrientation(false);
|
183 |
for (int j = 0; j < polygon.getNumVertices(); j++) { |
184 |
arrayPoints.add(polygon.getVertex(j)); |
185 |
} |
186 |
if(polygon.getNumInteriorRings()!=0 || i<multiPolygon.getPrimitivesNumber()-1){ |
187 |
index += polygon.getNumVertices(); |
188 |
arrayParts.add(index); |
189 |
} |
190 |
for (int r=0; r<polygon.getNumInteriorRings(); r++){ |
191 |
Ring ring = polygon.getInteriorRing(r); |
192 |
ring.ensureOrientation(true);
|
193 |
for (int j = 0; j < ring.getNumVertices(); j++) { |
194 |
arrayPoints.add(ring.getVertex(j)); |
195 |
} |
196 |
if((i<multiPolygon.getPrimitivesNumber()-1) || (r<polygon.getNumInteriorRings()-1)){ |
197 |
index += ring.getNumVertices(); |
198 |
arrayParts.add(index); |
199 |
} |
200 |
} |
201 |
} |
202 |
} |
203 |
points = arrayPoints.toArray(new Point[0]); |
204 |
parts = new int[arrayParts.size()]; |
205 |
for (int i = 0; i < parts.length; i++) { |
206 |
parts[i] = arrayParts.get(i); |
207 |
} |
208 |
} |
209 |
|
210 |
|
211 |
/**
|
212 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
|
213 |
*/
|
214 |
public synchronized int getLength() { |
215 |
int npoints = points.length;
|
216 |
|
217 |
int length;
|
218 |
|
219 |
if (m_type == SHP.POLYGON3D || m_type == SHP.POLYGONM) {
|
220 |
length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) + 16; |
221 |
} else if (m_type == SHP.POLYGON2D) { |
222 |
length = 44 + (4 * parts.length) + (16 * npoints); |
223 |
} else {
|
224 |
throw new IllegalStateException("Expected ShapeType of Polygon, got " + m_type); |
225 |
} |
226 |
|
227 |
return length;
|
228 |
} |
229 |
} |