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 / AbstractGeometry.java @ 44225
History | View | Annotate | Download (27.1 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; |
24 |
|
25 |
import java.awt.Rectangle; |
26 |
import java.awt.Shape; |
27 |
import java.awt.geom.AffineTransform; |
28 |
import java.awt.geom.Rectangle2D; |
29 |
|
30 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
31 |
import com.vividsolutions.jts.geom.Coordinate; |
32 |
import com.vividsolutions.jts.io.WKTWriter; |
33 |
import com.vividsolutions.jts.operation.distance.DistanceOp; |
34 |
import com.vividsolutions.jts.operation.overlay.snap.GeometrySnapper; |
35 |
import com.vividsolutions.jts.operation.valid.IsValidOp; |
36 |
import com.vividsolutions.jts.operation.valid.TopologyValidationError; |
37 |
import org.codehaus.plexus.util.StringUtils; |
38 |
import org.cresques.cts.IProjection; |
39 |
|
40 |
import org.slf4j.Logger; |
41 |
import org.slf4j.LoggerFactory; |
42 |
|
43 |
import org.gvsig.fmap.geom.Geometry; |
44 |
import org.gvsig.fmap.geom.GeometryLocator; |
45 |
import org.gvsig.fmap.geom.GeometryManager; |
46 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
47 |
import org.gvsig.fmap.geom.jts.operation.towkb.OGCWKBEncoder; |
48 |
import org.gvsig.fmap.geom.jts.operation.towkb.PostGISEWKBEncoder; |
49 |
import org.gvsig.fmap.geom.jts.operation.towkt.EWKTWriter; |
50 |
import org.gvsig.fmap.geom.jts.primitive.Envelope2D; |
51 |
import org.gvsig.fmap.geom.jts.primitive.Envelope3D; |
52 |
import org.gvsig.fmap.geom.jts.primitive.point.Point3D; |
53 |
import org.gvsig.fmap.geom.jts.util.GMLUtils; |
54 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
55 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
56 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
57 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
58 |
import org.gvsig.fmap.geom.primitive.Envelope; |
59 |
import org.gvsig.fmap.geom.primitive.OrientableCurve; |
60 |
import org.gvsig.fmap.geom.primitive.OrientableSurface; |
61 |
import org.gvsig.fmap.geom.primitive.Point; |
62 |
import org.gvsig.fmap.geom.type.GeometryType; |
63 |
|
64 |
/**
|
65 |
* @author fdiaz
|
66 |
*
|
67 |
*/
|
68 |
public abstract class AbstractGeometry implements GeometryJTS { |
69 |
|
70 |
protected static final Logger logger = LoggerFactory.getLogger(AbstractGeometry.class); |
71 |
|
72 |
/**
|
73 |
*
|
74 |
*/
|
75 |
private static final long serialVersionUID = 4999326772576222293L; |
76 |
|
77 |
private final GeometryType geometryType; |
78 |
|
79 |
private IProjection projection;
|
80 |
|
81 |
/**
|
82 |
*
|
83 |
*/
|
84 |
public AbstractGeometry(int type, int subtype) { |
85 |
try {
|
86 |
this.geometryType = GeometryLocator.getGeometryManager().getGeometryType(type, subtype);
|
87 |
} catch (Exception e) { |
88 |
// TODO: Ver de crear una excepcion para esto o si hay alguna en el API.
|
89 |
throw new RuntimeException(e); |
90 |
} |
91 |
} |
92 |
|
93 |
public GeometryType getGeometryType() {
|
94 |
return geometryType;
|
95 |
} |
96 |
|
97 |
protected GeometryManager getManager() {
|
98 |
return GeometryLocator.getGeometryManager();
|
99 |
} |
100 |
|
101 |
/*
|
102 |
* (non-Javadoc)
|
103 |
*
|
104 |
* @see org.gvsig.fmap.geom.Geometry#contains(org.gvsig.fmap.geom.Geometry)
|
105 |
*/
|
106 |
public boolean contains(Geometry geometry) throws GeometryOperationNotSupportedException, |
107 |
GeometryOperationException { |
108 |
if (!(geometry instanceof GeometryJTS)) { |
109 |
return false; |
110 |
} |
111 |
return getJTS().contains(((GeometryJTS) geometry).getJTS());
|
112 |
} |
113 |
|
114 |
/*
|
115 |
* (non-Javadoc)
|
116 |
*
|
117 |
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
118 |
*/
|
119 |
public int compareTo(Object o) { |
120 |
return getJTS().compareTo(((GeometryJTS) o).getJTS());
|
121 |
} |
122 |
|
123 |
/*
|
124 |
* (non-Javadoc)
|
125 |
*
|
126 |
* @see java.awt.Shape#contains(double, double)
|
127 |
*/
|
128 |
public boolean contains(double x, double y) { |
129 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
130 |
Shape shp = getShape();
|
131 |
return shp.contains(x, y);
|
132 |
} |
133 |
|
134 |
/*
|
135 |
* (non-Javadoc)
|
136 |
*
|
137 |
* @see java.awt.Shape#intersects(double, double, double, double)
|
138 |
*/
|
139 |
public boolean intersects(double x, double y, double w, double h) { |
140 |
|
141 |
notifyDeprecated("Calling deprecated method of geometry intersects from shape interface");
|
142 |
Shape shp = this.getShape(); |
143 |
return shp.intersects(x, y, w, h);
|
144 |
} |
145 |
|
146 |
/*
|
147 |
* (non-Javadoc)
|
148 |
*
|
149 |
* @see java.awt.Shape#contains(double, double, double, double)
|
150 |
*/
|
151 |
public boolean contains(double x, double y, double w, double h) { |
152 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
153 |
Shape shp = this.getShape(); |
154 |
return shp.contains(x, y, w, h);
|
155 |
} |
156 |
|
157 |
/*
|
158 |
* (non-Javadoc)
|
159 |
*
|
160 |
* @see java.awt.Shape#contains(java.awt.geom.Point2D)
|
161 |
*/
|
162 |
public boolean contains(java.awt.geom.Point2D p) { |
163 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
164 |
Shape shp = this.getShape(); |
165 |
return shp.contains(p);
|
166 |
} |
167 |
|
168 |
/*
|
169 |
* (non-Javadoc)
|
170 |
*
|
171 |
* @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
|
172 |
*/
|
173 |
public boolean contains(Rectangle2D r) { |
174 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
175 |
Shape shp = this.getShape(); |
176 |
return shp.contains(r);
|
177 |
} |
178 |
|
179 |
/*
|
180 |
* (non-Javadoc)
|
181 |
*
|
182 |
* @see org.gvsig.fmap.geom.Geometry#distance(org.gvsig.fmap.geom.Geometry)
|
183 |
*/
|
184 |
public double distance(org.gvsig.fmap.geom.Geometry other) throws GeometryOperationNotSupportedException, |
185 |
GeometryOperationException { |
186 |
return getJTS().distance(((GeometryJTS) other).getJTS());
|
187 |
} |
188 |
|
189 |
/*
|
190 |
* (non-Javadoc)
|
191 |
*
|
192 |
* @see
|
193 |
* org.gvsig.fmap.geom.Geometry#isWithinDistance(org.gvsig.fmap.geom.Geometry
|
194 |
* , double)
|
195 |
*/
|
196 |
public boolean isWithinDistance(org.gvsig.fmap.geom.Geometry other, double distance) |
197 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
198 |
return com.vividsolutions.jts.operation.distance.DistanceOp.isWithinDistance(this.getJTS(), |
199 |
((GeometryJTS) other).getJTS(), distance); |
200 |
} |
201 |
|
202 |
/*
|
203 |
* (non-Javadoc)
|
204 |
*
|
205 |
* @see org.gvsig.fmap.geom.Geometry#overlaps(org.gvsig.fmap.geom.Geometry)
|
206 |
*/
|
207 |
public boolean overlaps(org.gvsig.fmap.geom.Geometry geometry) throws GeometryOperationNotSupportedException, |
208 |
GeometryOperationException { |
209 |
// TODO: this method can be implemented throw invokeOperation
|
210 |
return getJTS().overlaps(((GeometryJTS) geometry).getJTS());
|
211 |
} |
212 |
|
213 |
public boolean coveredBy(Geometry geometry) throws GeometryOperationNotSupportedException, |
214 |
GeometryOperationException { |
215 |
|
216 |
return getJTS().coveredBy(((GeometryJTS) geometry).getJTS());
|
217 |
} |
218 |
|
219 |
public boolean covers(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
220 |
// TODO: this method can be implemented throw invokeOperation
|
221 |
return getJTS().covers(((GeometryJTS) geometry).getJTS());
|
222 |
|
223 |
} |
224 |
|
225 |
public boolean crosses(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
226 |
// TODO: this method can be implemented throw invokeOperation
|
227 |
return getJTS().crosses(((GeometryJTS) geometry).getJTS());
|
228 |
} |
229 |
|
230 |
public boolean intersects(Geometry geometry) throws GeometryOperationNotSupportedException, |
231 |
GeometryOperationException { |
232 |
return getJTS().intersects(((GeometryJTS) geometry).getJTS());
|
233 |
} |
234 |
|
235 |
public boolean touches(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
236 |
// TODO: this method can be implemented throw invokeOperation
|
237 |
return getJTS().touches(((GeometryJTS) geometry).getJTS());
|
238 |
} |
239 |
|
240 |
public boolean disjoint(Geometry geometry) throws GeometryOperationNotSupportedException, |
241 |
GeometryOperationException { |
242 |
// TODO: this method can be implemented throw invokeOperation
|
243 |
return getJTS().disjoint(((GeometryJTS) geometry).getJTS());
|
244 |
} |
245 |
|
246 |
public boolean within(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
247 |
// TODO: this method can be implemented throw invokeOperation
|
248 |
return getJTS().within(((GeometryJTS) geometry).getJTS());
|
249 |
} |
250 |
|
251 |
public double area() throws GeometryOperationNotSupportedException, GeometryOperationException { |
252 |
return getJTS().getArea();
|
253 |
} |
254 |
|
255 |
public double perimeter() throws GeometryOperationNotSupportedException, GeometryOperationException { |
256 |
return getJTS().getLength();
|
257 |
} |
258 |
|
259 |
/*
|
260 |
* (non-Javadoc)
|
261 |
*
|
262 |
* @see org.gvsig.fmap.geom.Geometry#intersects(java.awt.geom.Rectangle2D)
|
263 |
*/
|
264 |
public boolean intersects(Rectangle2D r) { |
265 |
double x = r.getMinX();
|
266 |
double y = r.getMinY();
|
267 |
double w = r.getWidth();
|
268 |
double h = r.getHeight();
|
269 |
|
270 |
return fastIntersects(x, y, w, h);
|
271 |
} |
272 |
|
273 |
/*
|
274 |
* (non-Javadoc)
|
275 |
*
|
276 |
* @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double,
|
277 |
* double)
|
278 |
*/
|
279 |
public boolean fastIntersects(double x, double y, double w, double h) { |
280 |
com.vividsolutions.jts.geom.Geometry rect = null;
|
281 |
com.vividsolutions.jts.geom.Coordinate[] coord = new com.vividsolutions.jts.geom.Coordinate[5]; |
282 |
coord[0] = new com.vividsolutions.jts.geom.Coordinate(x, y); |
283 |
coord[1] = new com.vividsolutions.jts.geom.Coordinate(x + w, y); |
284 |
coord[2] = new com.vividsolutions.jts.geom.Coordinate(x + w, y + w); |
285 |
coord[3] = new com.vividsolutions.jts.geom.Coordinate(x, y + w); |
286 |
coord[4] = new com.vividsolutions.jts.geom.Coordinate(x, y); |
287 |
rect = JTSUtils.createJTSPolygon(coord); |
288 |
|
289 |
return getJTS().intersects(rect);
|
290 |
} |
291 |
|
292 |
/*
|
293 |
* (non-Javadoc)
|
294 |
*
|
295 |
* @see org.gvsig.fmap.geom.Geometry#getEnvelope()
|
296 |
*/
|
297 |
public Envelope getEnvelope() {
|
298 |
if (is3D()) {
|
299 |
Coordinate[] coordinates = getJTS().getCoordinates();
|
300 |
|
301 |
double minx = Double.POSITIVE_INFINITY; |
302 |
double miny = Double.POSITIVE_INFINITY; |
303 |
double minz = Double.POSITIVE_INFINITY; |
304 |
|
305 |
double maxx = Double.NEGATIVE_INFINITY; |
306 |
double maxy = Double.NEGATIVE_INFINITY; |
307 |
double maxz = Double.NEGATIVE_INFINITY; |
308 |
|
309 |
double x;
|
310 |
double y;
|
311 |
double z;
|
312 |
|
313 |
for (int i = 0; i < coordinates.length; i++) { |
314 |
x = coordinates[i].x; |
315 |
y = coordinates[i].y; |
316 |
z = coordinates[i].z; |
317 |
minx = Math.min(x, minx);
|
318 |
miny = Math.min(y, miny);
|
319 |
minz = Math.min(z, minz);
|
320 |
maxx = Math.max(x, maxx);
|
321 |
maxy = Math.max(y, maxy);
|
322 |
maxz = Math.max(z, maxz);
|
323 |
} |
324 |
|
325 |
if (minx <= maxx && miny <= maxy && minz <= maxz) {
|
326 |
Point min = new Point3D(minx, miny, minz); |
327 |
Point max = new Point3D(maxx, maxy, maxz); |
328 |
return new Envelope3D(min, max); |
329 |
} |
330 |
return new Envelope3D(); |
331 |
} else {
|
332 |
com.vividsolutions.jts.geom.Envelope envelope = getJTS().getEnvelopeInternal(); |
333 |
return new Envelope2D(envelope); |
334 |
} |
335 |
} |
336 |
|
337 |
/*
|
338 |
* (non-Javadoc)
|
339 |
*
|
340 |
* @see org.gvsig.fmap.geom.Geometry#isSimple()
|
341 |
*/
|
342 |
public boolean isSimple() { |
343 |
return this.getJTS().isSimple(); |
344 |
} |
345 |
|
346 |
/*
|
347 |
* (non-Javadoc)
|
348 |
*
|
349 |
* @see org.gvsig.fmap.geom.Geometry#isCCW()
|
350 |
*/
|
351 |
public boolean isCCW() throws GeometryOperationNotSupportedException, GeometryOperationException { |
352 |
return CGAlgorithms.isCCW(this.getJTS().getCoordinates()); |
353 |
} |
354 |
|
355 |
/*
|
356 |
* (non-Javadoc)
|
357 |
*
|
358 |
* @see org.gvsig.fmap.geom.Geometry#invokeOperation(int,
|
359 |
* org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
360 |
*/
|
361 |
public Object invokeOperation(int index, GeometryOperationContext ctx) |
362 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
363 |
return getManager().invokeOperation(index, this, ctx); |
364 |
|
365 |
} |
366 |
|
367 |
/*
|
368 |
* (non-Javadoc)
|
369 |
*
|
370 |
* @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String,
|
371 |
* org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
372 |
*/
|
373 |
public Object invokeOperation(String opName, GeometryOperationContext ctx) |
374 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
375 |
return getManager().invokeOperation(opName, this, ctx); |
376 |
|
377 |
} |
378 |
|
379 |
/*
|
380 |
* (non-Javadoc)
|
381 |
*
|
382 |
* @see org.gvsig.fmap.geom.Geometry#getType()
|
383 |
*/
|
384 |
public int getType() { |
385 |
return this.getGeometryType().getType(); |
386 |
} |
387 |
|
388 |
@Override
|
389 |
public Object convertTo(String format) throws GeometryOperationNotSupportedException, GeometryOperationException { |
390 |
if( StringUtils.isEmpty(format) ) {
|
391 |
throw new IllegalArgumentException("Can't accept null as format name."); |
392 |
} |
393 |
format = format.trim().toLowerCase(); |
394 |
switch(format) {
|
395 |
case "jts": |
396 |
return this.getJTS(); |
397 |
case "wkb": |
398 |
return this.convertToWKB(); |
399 |
case "ewkb": |
400 |
return this.convertToEWKB(); |
401 |
case "wkt": |
402 |
return this.convertToWKT(); |
403 |
case "gml": |
404 |
return GMLUtils.geometry2GML(this); |
405 |
case "json": |
406 |
case "geojson": |
407 |
default:
|
408 |
throw new IllegalArgumentException("Format '"+format+"' not supported"); |
409 |
} |
410 |
|
411 |
} |
412 |
|
413 |
public byte[] convertToWKB() throws GeometryOperationNotSupportedException, GeometryOperationException { |
414 |
try {
|
415 |
return new OGCWKBEncoder().encode(this); |
416 |
} catch (Exception e) { |
417 |
throw new GeometryOperationException(e); |
418 |
} |
419 |
|
420 |
} |
421 |
|
422 |
public byte[] convertToWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException { |
423 |
/*
|
424 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
425 |
*/
|
426 |
|
427 |
try {
|
428 |
return new OGCWKBEncoder().encode(this); |
429 |
} catch (Exception e) { |
430 |
throw new GeometryOperationException(e); |
431 |
} |
432 |
} |
433 |
|
434 |
@Override
|
435 |
public byte[] convertToWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException, |
436 |
GeometryOperationException { |
437 |
/*
|
438 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
439 |
*/
|
440 |
Geometry geom = this;
|
441 |
if (this.getType() != type) { |
442 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
443 |
jts = JTSUtils.convertTypes(jts, this.getType(), type);
|
444 |
|
445 |
geom = JTSUtils.createGeometry(this.getProjection(), jts);
|
446 |
} |
447 |
try {
|
448 |
return new OGCWKBEncoder().encode(geom); |
449 |
} catch (Exception e) { |
450 |
throw new GeometryOperationException(e); |
451 |
} |
452 |
|
453 |
} |
454 |
|
455 |
public byte[] convertToEWKB() throws GeometryOperationNotSupportedException, GeometryOperationException { |
456 |
try {
|
457 |
return new PostGISEWKBEncoder().encode(this); |
458 |
} catch (Exception e) { |
459 |
throw new GeometryOperationException(e); |
460 |
} |
461 |
|
462 |
} |
463 |
|
464 |
public byte[] convertToEWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException { |
465 |
/*
|
466 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
467 |
*/
|
468 |
|
469 |
try {
|
470 |
return new PostGISEWKBEncoder().encode(this); |
471 |
} catch (Exception e) { |
472 |
throw new GeometryOperationException(e); |
473 |
} |
474 |
} |
475 |
|
476 |
public byte[] convertToEWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException, |
477 |
GeometryOperationException { |
478 |
/*
|
479 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
480 |
*/
|
481 |
Geometry geom = this;
|
482 |
if (this.getType() != type) { |
483 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
484 |
jts = JTSUtils.convertTypes(jts, this.getType(), type);
|
485 |
|
486 |
geom = JTSUtils.createGeometry(this.getProjection(), jts);
|
487 |
} |
488 |
try {
|
489 |
return new PostGISEWKBEncoder().encode(geom); |
490 |
} catch (Exception e) { |
491 |
throw new GeometryOperationException(e); |
492 |
} |
493 |
|
494 |
} |
495 |
|
496 |
/*
|
497 |
* (non-Javadoc)
|
498 |
*
|
499 |
* @see org.gvsig.fmap.geom.Geometry#convertToWKT()
|
500 |
*/
|
501 |
public String convertToWKT() throws GeometryOperationNotSupportedException, GeometryOperationException { |
502 |
int subType = getGeometryType().getSubType();
|
503 |
|
504 |
EWKTWriter writer = null;
|
505 |
|
506 |
switch (subType) {
|
507 |
case Geometry.SUBTYPES.GEOM3D:
|
508 |
writer = new EWKTWriter(3, false); |
509 |
break;
|
510 |
case Geometry.SUBTYPES.GEOM2DM:
|
511 |
writer = new EWKTWriter(3, true); |
512 |
break;
|
513 |
case Geometry.SUBTYPES.GEOM3DM:
|
514 |
writer = new EWKTWriter(4, true); |
515 |
break;
|
516 |
|
517 |
default:
|
518 |
writer = new EWKTWriter(2, false); |
519 |
break;
|
520 |
} |
521 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
522 |
return writer.write(jts);
|
523 |
} |
524 |
|
525 |
/*
|
526 |
* (non-Javadoc)
|
527 |
*
|
528 |
* @see org.gvsig.fmap.geom.Geometry#buffer(double)
|
529 |
*/
|
530 |
public org.gvsig.fmap.geom.Geometry buffer(double distance) throws GeometryOperationNotSupportedException, |
531 |
GeometryOperationException { |
532 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance)); |
533 |
} |
534 |
|
535 |
/*
|
536 |
* (non-Javadoc)
|
537 |
*
|
538 |
* @see org.gvsig.fmap.geom.Geometry#snapTo(org.gvsig.fmap.geom.Geometry,
|
539 |
* double)
|
540 |
*/
|
541 |
public org.gvsig.fmap.geom.Geometry snapTo(org.gvsig.fmap.geom.Geometry other, double snapTolerance) |
542 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
543 |
Geometry result = null;
|
544 |
GeometrySnapper snapper = new GeometrySnapper(getJTS());
|
545 |
com.vividsolutions.jts.geom.Geometry jts_result = snapper.snapTo(((GeometryJTS) other).getJTS(), snapTolerance); |
546 |
result = JTSUtils.createGeometry(this.getProjection(), jts_result);
|
547 |
return result;
|
548 |
} |
549 |
|
550 |
/*
|
551 |
* (non-Javadoc)
|
552 |
*
|
553 |
* @see org.gvsig.fmap.geom.Geometry#getInteriorPoint()
|
554 |
*/
|
555 |
public Point getInteriorPoint() throws GeometryOperationNotSupportedException, GeometryOperationException { |
556 |
|
557 |
com.vividsolutions.jts.geom.Geometry geometry = getJTS(); |
558 |
com.vividsolutions.jts.geom.Point point = geometry.getInteriorPoint(); |
559 |
Geometry result = JTSUtils.createGeometry(this.getProjection(), point);
|
560 |
return (Point) result; |
561 |
} |
562 |
|
563 |
/*
|
564 |
* (non-Javadoc)
|
565 |
*
|
566 |
* @see org.gvsig.fmap.geom.Geometry#isValid()
|
567 |
*/
|
568 |
public boolean isValid() { |
569 |
return getJTS().isValid();
|
570 |
} |
571 |
|
572 |
@Override
|
573 |
public ValidationStatus getValidationStatus() {
|
574 |
DefaultValidationStatus status = new DefaultValidationStatus(ValidationStatus.VALID, null); |
575 |
com.vividsolutions.jts.geom.Geometry jtsgeom = null;
|
576 |
try {
|
577 |
jtsgeom = this.getJTS();
|
578 |
IsValidOp validOp = new IsValidOp(jtsgeom);
|
579 |
if (!validOp.isValid() ) {
|
580 |
status.setValidationError(validOp.getValidationError()); |
581 |
} |
582 |
} catch (Exception ex) { |
583 |
status.setStatusCode(ValidationStatus.CURRUPTED); |
584 |
status.setMesage("The geometry is corrupted.");
|
585 |
if (this instanceof OrientableSurface) { |
586 |
int vertices = ((OrientableSurface) this).getNumVertices(); |
587 |
if (vertices < 3) { |
588 |
status.setStatusCode(ValidationStatus.TOO_FEW_POINTS); |
589 |
status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]); |
590 |
} |
591 |
} else if (this instanceof OrientableCurve) { |
592 |
int vertices = ((OrientableCurve) this).getNumVertices(); |
593 |
if (vertices < 2) { |
594 |
status.setStatusCode(ValidationStatus.TOO_FEW_POINTS); |
595 |
status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]); |
596 |
} |
597 |
} |
598 |
} |
599 |
return status;
|
600 |
} |
601 |
|
602 |
/*
|
603 |
* (non-Javadoc)
|
604 |
*
|
605 |
* @see org.gvsig.fmap.geom.Geometry#makeValid()
|
606 |
*/
|
607 |
public org.gvsig.fmap.geom.Geometry makeValid() {
|
608 |
try {
|
609 |
ValidationStatus vs = this.getValidationStatus();
|
610 |
if (vs.isValid()) {
|
611 |
return this; |
612 |
} |
613 |
Geometry g = null;
|
614 |
switch (vs.getStatusCode()) {
|
615 |
case Geometry.ValidationStatus.RING_SELF_INTERSECTION:
|
616 |
case Geometry.ValidationStatus.SELF_INTERSECTION:
|
617 |
g = this.buffer(0); |
618 |
if (g.isValid()) {
|
619 |
return g;
|
620 |
} |
621 |
break;
|
622 |
|
623 |
case Geometry.ValidationStatus.TOO_FEW_POINTS:
|
624 |
if (this instanceof OrientableCurve) { |
625 |
int vertices = ((OrientableCurve) this).getNumVertices(); |
626 |
if (vertices < 2) { |
627 |
return null; // new |
628 |
// DefaultNullGeometry(this.getGeometryType());
|
629 |
} |
630 |
} |
631 |
if (this instanceof OrientableSurface) { |
632 |
int vertices = ((OrientableSurface) this).getNumVertices(); |
633 |
if (vertices < 3) { |
634 |
return null; // new |
635 |
// DefaultNullGeometry(this.getGeometryType());
|
636 |
} |
637 |
} |
638 |
} |
639 |
} catch (Exception ex) { |
640 |
return null; |
641 |
} |
642 |
return null; |
643 |
} |
644 |
|
645 |
/*
|
646 |
* (non-Javadoc)
|
647 |
*
|
648 |
* @see org.gvsig.fmap.geom.Geometry#getBounds2D()
|
649 |
*/
|
650 |
public Rectangle2D getBounds2D() { |
651 |
com.vividsolutions.jts.geom.Envelope envInternal = getJTS().getEnvelopeInternal(); |
652 |
return new Rectangle2D.Double(envInternal.getMinX(), envInternal.getMinY(), envInternal.getWidth(), |
653 |
envInternal.getHeight()); |
654 |
} |
655 |
|
656 |
/*
|
657 |
* (non-Javadoc)
|
658 |
*
|
659 |
* @see java.awt.Shape#getBounds()
|
660 |
*/
|
661 |
public Rectangle getBounds() { |
662 |
return this.getShape().getBounds(); |
663 |
} |
664 |
|
665 |
/*
|
666 |
* (non-Javadoc)
|
667 |
*
|
668 |
* @see org.gvsig.fmap.geom.Geometry#getInternalShape()
|
669 |
*/
|
670 |
public Shape getInternalShape() { |
671 |
return getShape();
|
672 |
} |
673 |
|
674 |
public void rotate(double radAngle, double basex, double basey) { |
675 |
|
676 |
AffineTransform at = new AffineTransform(); |
677 |
at.rotate(radAngle, basex, basey); |
678 |
this.transform(at);
|
679 |
} |
680 |
|
681 |
public void move(double dx, double dy) { |
682 |
|
683 |
AffineTransform at = new AffineTransform(); |
684 |
at.translate(dx, dy); |
685 |
this.transform(at);
|
686 |
} |
687 |
|
688 |
public void scale(Point basePoint, double sx, double sy) { |
689 |
|
690 |
AffineTransform at = new AffineTransform(); |
691 |
at.setToTranslation(basePoint.getX(), basePoint.getY()); |
692 |
at.scale(sx, sy); |
693 |
at.translate(-basePoint.getX(), -basePoint.getY()); |
694 |
this.transform(at);
|
695 |
} |
696 |
|
697 |
public Geometry[] closestPoints(Geometry other) throws GeometryOperationNotSupportedException, |
698 |
GeometryOperationException { |
699 |
Point[] points = null; |
700 |
|
701 |
Coordinate[] jts_points = DistanceOp.nearestPoints(getJTS(), ((GeometryJTS) other).getJTS());
|
702 |
points = new Point[jts_points.length]; |
703 |
for (int i = 0; i < jts_points.length; i++) { |
704 |
try {
|
705 |
points[i] = JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), jts_points[i]); |
706 |
} catch (CreateGeometryException e) {
|
707 |
throw new GeometryOperationException(e); |
708 |
} |
709 |
} |
710 |
|
711 |
return (Geometry[]) points; |
712 |
} |
713 |
|
714 |
public Geometry convexHull() throws GeometryOperationNotSupportedException, GeometryOperationException { |
715 |
|
716 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().convexHull()); |
717 |
} |
718 |
|
719 |
public Geometry difference(Geometry other) throws GeometryOperationNotSupportedException, |
720 |
GeometryOperationException { |
721 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().difference(((GeometryJTS) other).getJTS())); |
722 |
} |
723 |
|
724 |
public Geometry intersection(Geometry other) throws GeometryOperationNotSupportedException, |
725 |
GeometryOperationException { |
726 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().intersection(((GeometryJTS) other).getJTS())); |
727 |
} |
728 |
|
729 |
public Geometry union(Geometry other) throws GeometryOperationNotSupportedException, GeometryOperationException { |
730 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().union(((GeometryJTS) other).getJTS())); |
731 |
} |
732 |
|
733 |
public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException, |
734 |
GeometryOperationException { |
735 |
try {
|
736 |
return JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), getJTS().getCentroid().getCoordinate()); |
737 |
} catch (CreateGeometryException e) {
|
738 |
throw new GeometryOperationException(e); |
739 |
} |
740 |
} |
741 |
|
742 |
protected void notifyDeprecated(String message) { |
743 |
logger.info(message); |
744 |
|
745 |
} |
746 |
|
747 |
|
748 |
/* (non-Javadoc)
|
749 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#ensureOrientation(boolean)
|
750 |
*/
|
751 |
public boolean ensureOrientation(boolean ccw) throws GeometryOperationNotSupportedException, GeometryOperationException { |
752 |
if (ccw != isCCW()) {
|
753 |
flip(); |
754 |
return true; |
755 |
} |
756 |
return false; |
757 |
} |
758 |
|
759 |
/* (non-Javadoc)
|
760 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#out(org.gvsig.fmap.geom.Geometry)
|
761 |
*/
|
762 |
public boolean out(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
763 |
GeometryJTS otherJtsGeom = (GeometryJTS) geometry; |
764 |
return (!contains(otherJtsGeom) && !intersects(otherJtsGeom));
|
765 |
} |
766 |
|
767 |
/* (non-Javadoc)
|
768 |
* @see java.lang.Object#equals(java.lang.Object)
|
769 |
*/
|
770 |
@Override
|
771 |
public boolean equals(Object obj) { |
772 |
if (obj instanceof GeometryJTS) { |
773 |
return this.getJTS().equals(((GeometryJTS) obj).getJTS()); |
774 |
} |
775 |
return false; |
776 |
} |
777 |
|
778 |
public String toString() { |
779 |
return this.getGeometryType().getFullName(); |
780 |
} |
781 |
|
782 |
@Override
|
783 |
public IProjection getProjection() {
|
784 |
return this.projection; |
785 |
} |
786 |
|
787 |
@Override
|
788 |
public void setProjectionIffNull(IProjection projection) { |
789 |
if (this.projection == null) { |
790 |
this.projection = projection;
|
791 |
} |
792 |
} |
793 |
|
794 |
@Override
|
795 |
public void setProjection(IProjection projection) { |
796 |
this.projection = projection;
|
797 |
} |
798 |
|
799 |
@Override
|
800 |
@SuppressWarnings("CloneDoesntCallSuperClone") |
801 |
public Geometry clone() throws CloneNotSupportedException { |
802 |
return this.cloneGeometry(); |
803 |
} |
804 |
|
805 |
} |