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