svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / surface / polygon / Polygon3D.java @ 45762
History | View | Annotate | Download (11.4 KB)
1 | 42260 | fdiaz | /* 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.geom.jts.primitive.surface.polygon; |
||
24 | |||
25 | 42275 | fdiaz | import java.awt.geom.AffineTransform; |
26 | import java.awt.geom.PathIterator; |
||
27 | 42267 | fdiaz | import java.util.ArrayList; |
28 | 42281 | fdiaz | import java.util.Iterator; |
29 | 42267 | fdiaz | import java.util.List; |
30 | |||
31 | 42260 | fdiaz | import com.vividsolutions.jts.geom.Coordinate; |
32 | 42441 | fdiaz | import com.vividsolutions.jts.geom.GeometryFactory; |
33 | import com.vividsolutions.jts.geomgraph.Position; |
||
34 | import com.vividsolutions.jts.operation.buffer.BufferParameters; |
||
35 | import com.vividsolutions.jts.operation.buffer.OffsetCurveBuilder; |
||
36 | 43244 | jjdelcerro | import java.awt.Shape; |
37 | import java.awt.geom.GeneralPath; |
||
38 | 42260 | fdiaz | |
39 | 42464 | fdiaz | import org.cresques.cts.ICoordTrans; |
40 | |||
41 | 42260 | fdiaz | import org.gvsig.fmap.geom.Geometry; |
42 | 42269 | fdiaz | import org.gvsig.fmap.geom.GeometryException; |
43 | import org.gvsig.fmap.geom.aggregate.MultiLine; |
||
44 | import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
||
45 | 42281 | fdiaz | import org.gvsig.fmap.geom.jts.GeometryJTS; |
46 | 42269 | fdiaz | import org.gvsig.fmap.geom.jts.aggregate.MultiLine3D; |
47 | import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon3D; |
||
48 | 43244 | jjdelcerro | import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX; |
49 | 42270 | fdiaz | import org.gvsig.fmap.geom.jts.primitive.curve.line.BaseLine3D; |
50 | 42267 | fdiaz | import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3D; |
51 | 42283 | fdiaz | import org.gvsig.fmap.geom.jts.primitive.point.Point3D; |
52 | import org.gvsig.fmap.geom.jts.primitive.ring.Ring3D; |
||
53 | 44612 | jjdelcerro | import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
54 | 42274 | fdiaz | import org.gvsig.fmap.geom.jts.util.JTSUtils; |
55 | 42281 | fdiaz | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
56 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
57 | 42267 | fdiaz | import org.gvsig.fmap.geom.primitive.Line; |
58 | import org.gvsig.fmap.geom.primitive.Polygon; |
||
59 | import org.gvsig.fmap.geom.primitive.Ring; |
||
60 | import org.gvsig.fmap.geom.primitive.SurfaceAppearance; |
||
61 | 42260 | fdiaz | |
62 | |||
63 | |||
64 | /**
|
||
65 | * @author fdiaz
|
||
66 | *
|
||
67 | */
|
||
68 | 42270 | fdiaz | public class Polygon3D extends BaseLine3D implements Polygon { |
69 | 42260 | fdiaz | |
70 | /**
|
||
71 | *
|
||
72 | */
|
||
73 | private static final long serialVersionUID = 5748137753517288500L; |
||
74 | |||
75 | 42267 | fdiaz | List<Ring> interiorRings = new ArrayList<Ring>(); |
76 | |||
77 | 42260 | fdiaz | /**
|
78 | * @param subtype
|
||
79 | */
|
||
80 | 42271 | fdiaz | public Polygon3D() {
|
81 | 42283 | fdiaz | super(Geometry.TYPES.POLYGON);
|
82 | 42260 | fdiaz | } |
83 | |||
84 | /**
|
||
85 | *
|
||
86 | */
|
||
87 | public Polygon3D(Coordinate[] coordinates) { |
||
88 | 42304 | fdiaz | super(Geometry.TYPES.POLYGON, coordinates);
|
89 | 42283 | fdiaz | closePrimitive(); |
90 | 42260 | fdiaz | } |
91 | |||
92 | 42267 | fdiaz | /* (non-Javadoc)
|
93 | * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
|
||
94 | */
|
||
95 | 44040 | jjdelcerro | public Polygon cloneGeometry() { |
96 | 42304 | fdiaz | Polygon3D cloned = new Polygon3D(cloneCoordinates().toCoordinateArray());
|
97 | for (int i = 0; i < getNumInteriorRings(); i++){ |
||
98 | cloned.addInteriorRing((Ring)getInteriorRing(i).cloneGeometry()); |
||
99 | } |
||
100 | return cloned;
|
||
101 | 42267 | fdiaz | } |
102 | 44612 | jjdelcerro | |
103 | @Override
|
||
104 | public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
105 | ArrayListCoordinateSequence coordinates2D = new ArrayListCoordinateSequence(coordinates.size());
|
||
106 | for (Coordinate coordinate : this.coordinates) { |
||
107 | coordinates2D.add(new Coordinate(coordinate.x, coordinate.y));
|
||
108 | } |
||
109 | Polygon2D poligon2D = new Polygon2D(coordinates2D);
|
||
110 | 45157 | jolivas | poligon2D.setProjection(this.getProjection());
|
111 | 44612 | jjdelcerro | for (int i = 0; i < getNumInteriorRings(); i++){ |
112 | poligon2D.addInteriorRing((Ring)getInteriorRing(i).force2D()); |
||
113 | } |
||
114 | return poligon2D;
|
||
115 | } |
||
116 | 44040 | jjdelcerro | |
117 | @Override
|
||
118 | @SuppressWarnings("CloneDoesntCallSuperClone") |
||
119 | public Polygon clone() throws CloneNotSupportedException { |
||
120 | return this.cloneGeometry(); |
||
121 | } |
||
122 | 42260 | fdiaz | |
123 | /* (non-Javadoc)
|
||
124 | 42267 | fdiaz | * @see org.gvsig.fmap.geom.primitive.Surface#setSurfaceAppearance(org.gvsig.fmap.geom.primitive.SurfaceAppearance)
|
125 | 42260 | fdiaz | */
|
126 | 42267 | fdiaz | public void setSurfaceAppearance(SurfaceAppearance app) { |
127 | // TODO Auto-generated method stub
|
||
128 | |||
129 | 42260 | fdiaz | } |
130 | |||
131 | /* (non-Javadoc)
|
||
132 | 42267 | fdiaz | * @see org.gvsig.fmap.geom.primitive.Surface#getSurfaceAppearance()
|
133 | 42260 | fdiaz | */
|
134 | 42267 | fdiaz | public SurfaceAppearance getSurfaceAppearance() {
|
135 | // TODO Auto-generated method stub
|
||
136 | return null; |
||
137 | 42260 | fdiaz | } |
138 | |||
139 | /* (non-Javadoc)
|
||
140 | 42267 | fdiaz | * @see org.gvsig.fmap.geom.primitive.Surface#getNumInteriorRings()
|
141 | 42260 | fdiaz | */
|
142 | 42267 | fdiaz | public int getNumInteriorRings() { |
143 | return this.interiorRings.size(); |
||
144 | 42260 | fdiaz | } |
145 | |||
146 | /* (non-Javadoc)
|
||
147 | 42267 | fdiaz | * @see org.gvsig.fmap.geom.primitive.Surface#getInteriorRing(int)
|
148 | 42260 | fdiaz | */
|
149 | 42267 | fdiaz | public Ring getInteriorRing(int index) { |
150 | return interiorRings.get(index);
|
||
151 | 42260 | fdiaz | } |
152 | 42267 | fdiaz | |
153 | /* (non-Javadoc)
|
||
154 | * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Ring)
|
||
155 | */
|
||
156 | public void addInteriorRing(Ring ring) { |
||
157 | interiorRings.add(ring); |
||
158 | } |
||
159 | |||
160 | /* (non-Javadoc)
|
||
161 | * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Line)
|
||
162 | */
|
||
163 | 42283 | fdiaz | public void addInteriorRing(Line line) { |
164 | Ring3D ring = new Ring3D();
|
||
165 | ring.ensureCapacity(line.getNumVertices()); |
||
166 | for(int i=0; i<line.getNumVertices(); i++){ |
||
167 | Point3D vertex = (Point3D)line.getVertex(i); |
||
168 | ring.addVertex(new Point3D(vertex.getX(), vertex.getY(), vertex.getZ()));
|
||
169 | } |
||
170 | ring.closePrimitive(); |
||
171 | interiorRings.add(ring); |
||
172 | 42267 | fdiaz | } |
173 | |||
174 | 42283 | fdiaz | /*
|
175 | * (non-Javadoc)
|
||
176 | *
|
||
177 | * @see
|
||
178 | * org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.
|
||
179 | * geom.primitive.Polygon)
|
||
180 | */
|
||
181 | public void addInteriorRing(Polygon polygon) { |
||
182 | Ring3D ring = new Ring3D();
|
||
183 | ring.ensureCapacity(polygon.getNumVertices()); |
||
184 | for(int i=0; i<polygon.getNumVertices(); i++){ |
||
185 | Point3D vertex = (Point3D)polygon.getVertex(i); |
||
186 | ring.addVertex(new Point3D(vertex.getX(), vertex.getY(), vertex.getZ()));
|
||
187 | } |
||
188 | ring.closePrimitive(); |
||
189 | interiorRings.add(ring); |
||
190 | } |
||
191 | |||
192 | 42267 | fdiaz | /* (non-Javadoc)
|
193 | * @see org.gvsig.fmap.geom.primitive.Surface#removeInteriorRing(int)
|
||
194 | */
|
||
195 | public void removeInteriorRing(int index) { |
||
196 | interiorRings.remove(index); |
||
197 | } |
||
198 | 42269 | fdiaz | |
199 | /* (non-Javadoc)
|
||
200 | * @see org.gvsig.fmap.geom.primitive.Line#toLines()
|
||
201 | */
|
||
202 | public MultiLine toLines() throws GeometryException { |
||
203 | MultiLine multiLine = new MultiLine3D();
|
||
204 | multiLine.addPrimitive(new Line3D(coordinates.toCoordinateArray()));
|
||
205 | return multiLine;
|
||
206 | } |
||
207 | |||
208 | /* (non-Javadoc)
|
||
209 | * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
|
||
210 | */
|
||
211 | public MultiPolygon toPolygons() throws GeometryException { |
||
212 | MultiPolygon multiPolygon = new MultiPolygon3D();
|
||
213 | multiPolygon.addPrimitive(this);
|
||
214 | return multiPolygon;
|
||
215 | } |
||
216 | |||
217 | 42274 | fdiaz | /* (non-Javadoc)
|
218 | * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
|
||
219 | */
|
||
220 | public com.vividsolutions.jts.geom.Geometry getJTS() {
|
||
221 | return JTSUtils.createJTSPolygon(coordinates, interiorRings);
|
||
222 | } |
||
223 | 42275 | fdiaz | |
224 | /*
|
||
225 | * (non-Javadoc)
|
||
226 | *
|
||
227 | * @see
|
||
228 | * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
|
||
229 | * )
|
||
230 | */
|
||
231 | public PathIterator getPathIterator(AffineTransform at) { |
||
232 | PolygonIterator pi = new PolygonIterator(this, at); |
||
233 | return pi;
|
||
234 | } |
||
235 | |||
236 | 42281 | fdiaz | /* (non-Javadoc)
|
237 | * @see org.gvsig.fmap.geom.jts.GeometryJTS#ensureOrientation(boolean)
|
||
238 | */
|
||
239 | public boolean ensureOrientation(boolean ccw) throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
240 | boolean result = super.ensureOrientation(ccw); |
||
241 | for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
||
242 | GeometryJTS ring = (GeometryJTS) iterator.next(); |
||
243 | ring.ensureOrientation(!ccw); |
||
244 | } |
||
245 | return result;
|
||
246 | } |
||
247 | |||
248 | /* (non-Javadoc)
|
||
249 | * @see org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#flip()
|
||
250 | */
|
||
251 | @Override
|
||
252 | public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
253 | super.flip();
|
||
254 | for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
||
255 | GeometryJTS ring = (GeometryJTS) iterator.next(); |
||
256 | ring.flip(); |
||
257 | } |
||
258 | } |
||
259 | 42304 | fdiaz | |
260 | /* (non-Javadoc)
|
||
261 | * @see org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#transform(java.awt.geom.AffineTransform)
|
||
262 | */
|
||
263 | @Override
|
||
264 | public void transform(AffineTransform at) { |
||
265 | super.transform(at);
|
||
266 | for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
||
267 | GeometryJTS ring = (GeometryJTS) iterator.next(); |
||
268 | ring.transform(at); |
||
269 | } |
||
270 | } |
||
271 | 42441 | fdiaz | |
272 | |||
273 | /* (non-Javadoc)
|
||
274 | * @see org.gvsig.fmap.geom.Geometry#offset(double)
|
||
275 | */
|
||
276 | public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
277 | 44099 | jjdelcerro | return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance)); |
278 | 42441 | fdiaz | } |
279 | 42464 | fdiaz | |
280 | 45762 | fdiaz | @Override
|
281 | public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
282 | return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance, JTSUtils.calculateQuadrantSegments(joinStyle))); |
||
283 | } |
||
284 | |||
285 | 42464 | fdiaz | /* (non-Javadoc)
|
286 | * @see org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#reProject(org.cresques.cts.ICoordTrans)
|
||
287 | */
|
||
288 | @Override
|
||
289 | public void reProject(ICoordTrans ct) { |
||
290 | super.reProject(ct);
|
||
291 | for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
||
292 | GeometryJTS ring = (GeometryJTS) iterator.next(); |
||
293 | ring.reProject(ct); |
||
294 | } |
||
295 | if (coordinates.size()>=2 && !isClosed()) { |
||
296 | closePrimitive(); |
||
297 | } |
||
298 | } |
||
299 | 42875 | fdiaz | |
300 | @Override
|
||
301 | public boolean equals(Object obj) { |
||
302 | boolean res = super.equals(obj); |
||
303 | if(res && obj instanceof Polygon3D){ |
||
304 | Polygon3D other = (Polygon3D)obj; |
||
305 | if(this.getNumVertices() != other.getNumVertices()){ |
||
306 | return false; |
||
307 | } |
||
308 | for (int i = 0; i < this.getNumVertices(); i++) { |
||
309 | Coordinate coordinate = this.coordinates.get(i);
|
||
310 | Coordinate otherCoordinate = other.coordinates.get(i); |
||
311 | if (otherCoordinate.getOrdinate(2) != coordinate.getOrdinate(2)) { |
||
312 | return false; |
||
313 | } |
||
314 | } |
||
315 | if(this.getNumInteriorRings() != other.getNumInteriorRings()){ |
||
316 | return false; |
||
317 | } |
||
318 | for(int i=0; i < this.getNumInteriorRings(); i++){ |
||
319 | if(!other.getInteriorRing(i).equals(this.getInteriorRing(i))){ |
||
320 | return false; |
||
321 | } |
||
322 | } |
||
323 | return true; |
||
324 | } else {
|
||
325 | return false; |
||
326 | } |
||
327 | } |
||
328 | 43244 | jjdelcerro | |
329 | @Override
|
||
330 | public Shape getShape(AffineTransform affineTransform) { |
||
331 | return PolygonHelper.getShape(this, affineTransform); |
||
332 | } |
||
333 | 42260 | fdiaz | } |