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