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