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