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 @ 47755
History | View | Annotate | Download (12.8 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 java.awt.geom.AffineTransform; |
26 |
import java.awt.geom.PathIterator; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
|
31 |
import com.vividsolutions.jts.geom.Coordinate; |
32 |
import java.awt.Shape; |
33 |
import java.awt.geom.GeneralPath; |
34 |
|
35 |
import org.cresques.cts.ICoordTrans; |
36 |
|
37 |
import org.gvsig.fmap.geom.Geometry; |
38 |
import org.gvsig.fmap.geom.GeometryException; |
39 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
40 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
41 |
import org.gvsig.fmap.geom.jts.GeometryJTS; |
42 |
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D; |
43 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D; |
44 |
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX; |
45 |
import org.gvsig.fmap.geom.jts.mgeom.MCoordinate; |
46 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.BaseLine2D; |
47 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D; |
48 |
import org.gvsig.fmap.geom.jts.primitive.point.Point2D; |
49 |
import org.gvsig.fmap.geom.jts.primitive.ring.Ring2D; |
50 |
import org.gvsig.fmap.geom.jts.primitive.surface.split.SurfaceSplitOperation; |
51 |
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
52 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
53 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
54 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
55 |
import org.gvsig.fmap.geom.primitive.Line; |
56 |
import org.gvsig.fmap.geom.primitive.Point; |
57 |
import org.gvsig.fmap.geom.primitive.Polygon; |
58 |
import org.gvsig.fmap.geom.primitive.Ring; |
59 |
import org.gvsig.fmap.geom.primitive.SurfaceAppearance; |
60 |
|
61 |
/**
|
62 |
* @author fdiaz
|
63 |
*
|
64 |
*/
|
65 |
public class Polygon2D extends BaseLine2D implements Polygon { |
66 |
|
67 |
/**
|
68 |
*
|
69 |
*/
|
70 |
private static final long serialVersionUID = 4927309852756014929L; |
71 |
|
72 |
List<Ring> interiorRings = new ArrayList<Ring>(); |
73 |
|
74 |
/**
|
75 |
* @param subtype
|
76 |
*/
|
77 |
public Polygon2D() {
|
78 |
super(Geometry.TYPES.POLYGON);
|
79 |
} |
80 |
|
81 |
/**
|
82 |
*
|
83 |
*/
|
84 |
public Polygon2D(Coordinate[] coordinates) { |
85 |
super(Geometry.TYPES.POLYGON, coordinates);
|
86 |
closePrimitive(); |
87 |
} |
88 |
|
89 |
public Polygon2D(ArrayListCoordinateSequence coordinates) {
|
90 |
super(Geometry.TYPES.POLYGON, coordinates);
|
91 |
closePrimitive(); |
92 |
} |
93 |
|
94 |
/*
|
95 |
* (non-Javadoc)
|
96 |
*
|
97 |
* @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
|
98 |
*/
|
99 |
@Override
|
100 |
public Polygon cloneGeometry() { |
101 |
Polygon2D cloned = new Polygon2D(cloneCoordinates().toCoordinateArray());
|
102 |
for (int i = 0; i < getNumInteriorRings(); i++){ |
103 |
cloned.addInteriorRing((Ring)getInteriorRing(i).cloneGeometry()); |
104 |
} |
105 |
cloned.setProjection(this.getProjection());
|
106 |
return cloned;
|
107 |
} |
108 |
|
109 |
@Override
|
110 |
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 |
poligon2D.setProjection(this.getProjection());
|
117 |
for (int i = 0; i < getNumInteriorRings(); i++){ |
118 |
poligon2D.addInteriorRing((Ring)getInteriorRing(i).force2D()); |
119 |
} |
120 |
return poligon2D;
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
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 |
@SuppressWarnings("CloneDoesntCallSuperClone") |
167 |
public Polygon clone() throws CloneNotSupportedException { |
168 |
return this.cloneGeometry(); |
169 |
} |
170 |
|
171 |
/*
|
172 |
* (non-Javadoc)
|
173 |
*
|
174 |
* @see
|
175 |
* org.gvsig.fmap.geom.primitive.Surface#setSurfaceAppearance(org.gvsig.
|
176 |
* fmap.geom.primitive.SurfaceAppearance)
|
177 |
*/
|
178 |
public void setSurfaceAppearance(SurfaceAppearance app) { |
179 |
// TODO Auto-generated method stub
|
180 |
|
181 |
} |
182 |
|
183 |
/*
|
184 |
* (non-Javadoc)
|
185 |
*
|
186 |
* @see org.gvsig.fmap.geom.primitive.Surface#getSurfaceAppearance()
|
187 |
*/
|
188 |
public SurfaceAppearance getSurfaceAppearance() {
|
189 |
// TODO Auto-generated method stub
|
190 |
return null; |
191 |
} |
192 |
|
193 |
/*
|
194 |
* (non-Javadoc)
|
195 |
*
|
196 |
* @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 |
* @see
|
215 |
* org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.
|
216 |
* geom.primitive.Ring)
|
217 |
*/
|
218 |
public void addInteriorRing(Ring ring) { |
219 |
interiorRings.add(ring); |
220 |
} |
221 |
|
222 |
/*
|
223 |
* (non-Javadoc)
|
224 |
*
|
225 |
* @see
|
226 |
* org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.
|
227 |
* geom.primitive.Line)
|
228 |
*/
|
229 |
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 |
} |
239 |
|
240 |
/*
|
241 |
* (non-Javadoc)
|
242 |
*
|
243 |
* @see
|
244 |
* org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.
|
245 |
* geom.primitive.Polygon)
|
246 |
*/
|
247 |
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 |
} |
257 |
|
258 |
/*
|
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 |
/* (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 |
/* (non-Javadoc)
|
286 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
|
287 |
*/
|
288 |
public com.vividsolutions.jts.geom.Geometry getJTS() {
|
289 |
return JTSUtils.createJTSPolygon(getProjection(), coordinates, interiorRings);
|
290 |
} |
291 |
|
292 |
/*
|
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 |
|
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 |
|
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 |
|
340 |
/* (non-Javadoc)
|
341 |
* @see org.gvsig.fmap.geom.Geometry#offset(double)
|
342 |
*/
|
343 |
public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
344 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance)); |
345 |
} |
346 |
|
347 |
@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 |
/* (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 |
|
369 |
@Override
|
370 |
public Shape getShape(AffineTransform affineTransform) { |
371 |
return PolygonHelper.getShape(this, affineTransform); |
372 |
} |
373 |
|
374 |
@Override
|
375 |
public Geometry split(Geometry splitter) {
|
376 |
SurfaceSplitOperation op = new SurfaceSplitOperation();
|
377 |
return op.split(this, splitter); |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public Ring createRing() {
|
382 |
return new Ring2D(); |
383 |
} |
384 |
} |