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 @ 47648
History | View | Annotate | Download (35.6 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 com.vividsolutions.jts.algorithm.CGAlgorithms; |
26 |
import com.vividsolutions.jts.geom.Coordinate; |
27 |
import com.vividsolutions.jts.io.geojson.GeoJsonWriter; |
28 |
import com.vividsolutions.jts.operation.buffer.BufferParameters; |
29 |
import com.vividsolutions.jts.operation.distance.DistanceOp; |
30 |
import com.vividsolutions.jts.operation.overlay.snap.GeometrySnapper; |
31 |
import com.vividsolutions.jts.operation.valid.IsValidOp; |
32 |
import com.vividsolutions.jts.operation.valid.TopologyValidationError; |
33 |
import java.awt.Rectangle; |
34 |
import java.awt.Shape; |
35 |
import java.awt.geom.AffineTransform; |
36 |
import java.awt.geom.Rectangle2D; |
37 |
import org.apache.commons.codec.binary.Hex; |
38 |
import org.apache.commons.lang3.StringUtils; |
39 |
import org.cresques.cts.IProjection; |
40 |
import org.gvsig.fmap.crs.CRSFactory; |
41 |
import org.gvsig.fmap.geom.Geometry; |
42 |
import org.gvsig.fmap.geom.GeometryLocator; |
43 |
import org.gvsig.fmap.geom.GeometryManager; |
44 |
import org.gvsig.fmap.geom.aggregate.Aggregate; |
45 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
46 |
import org.gvsig.fmap.geom.complex.Complex; |
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.Curve; |
60 |
import org.gvsig.fmap.geom.primitive.Envelope; |
61 |
import org.gvsig.fmap.geom.primitive.OrientableCurve; |
62 |
import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
63 |
import org.gvsig.fmap.geom.primitive.OrientableSurface; |
64 |
import org.gvsig.fmap.geom.primitive.Point; |
65 |
import org.gvsig.fmap.geom.type.GeometryType; |
66 |
import org.gvsig.tools.util.IsEmpty; |
67 |
import org.slf4j.Logger; |
68 |
import org.slf4j.LoggerFactory; |
69 |
|
70 |
/**
|
71 |
* @author gvSIG Team
|
72 |
*
|
73 |
*/
|
74 |
@SuppressWarnings("UseSpecificCatch") |
75 |
public abstract class AbstractGeometry implements GeometryJTS { |
76 |
|
77 |
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractGeometry.class); |
78 |
|
79 |
/**
|
80 |
*
|
81 |
*/
|
82 |
private static final long serialVersionUID = 4999326772576222293L; |
83 |
|
84 |
private GeometryType geometryType;
|
85 |
|
86 |
private IProjection projection;
|
87 |
|
88 |
/**
|
89 |
*
|
90 |
* @param type
|
91 |
* @param subtype
|
92 |
*/
|
93 |
public AbstractGeometry(int type, int subtype) { |
94 |
try {
|
95 |
this.geometryType = GeometryLocator.getGeometryManager().getGeometryType(type, subtype);
|
96 |
} catch (Exception e) { |
97 |
// TODO: Ver de crear una excepcion para esto o si hay alguna en el API.
|
98 |
throw new RuntimeException(e); |
99 |
} |
100 |
} |
101 |
|
102 |
public void setGeometryType(GeometryType geometryType) { |
103 |
this.geometryType = geometryType;
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public GeometryType getGeometryType() {
|
108 |
return geometryType;
|
109 |
} |
110 |
|
111 |
protected GeometryManager getManager() {
|
112 |
return GeometryLocator.getGeometryManager();
|
113 |
} |
114 |
|
115 |
/*
|
116 |
* (non-Javadoc)
|
117 |
*
|
118 |
* @see org.gvsig.fmap.geom.Geometry#contains(org.gvsig.fmap.geom.Geometry)
|
119 |
*/
|
120 |
@Override
|
121 |
public boolean contains(Geometry geometry) throws GeometryOperationNotSupportedException, |
122 |
GeometryOperationException { |
123 |
if (!(geometry instanceof GeometryJTS)) { |
124 |
return false; |
125 |
} |
126 |
com.vividsolutions.jts.geom.Geometry theJTS = this.getJTS();
|
127 |
if(this instanceof Aggregate){ |
128 |
theJTS = theJTS.union(); |
129 |
} |
130 |
|
131 |
com.vividsolutions.jts.geom.Geometry otherJTS = ((GeometryJTS)geometry).getJTS(); |
132 |
if(geometry instanceof Aggregate){ |
133 |
otherJTS = otherJTS.union(); |
134 |
} |
135 |
return theJTS.contains(otherJTS);
|
136 |
} |
137 |
|
138 |
/*
|
139 |
* (non-Javadoc)
|
140 |
*
|
141 |
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
142 |
*/
|
143 |
@Override
|
144 |
public int compareTo(Object o) { |
145 |
return getJTS().compareTo(((GeometryJTS) o).getJTS());
|
146 |
} |
147 |
|
148 |
/*
|
149 |
* (non-Javadoc)
|
150 |
*
|
151 |
* @see java.awt.Shape#contains(double, double)
|
152 |
*/
|
153 |
@Override
|
154 |
public boolean contains(double x, double y) { |
155 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
156 |
Shape shp = getShape();
|
157 |
return shp.contains(x, y);
|
158 |
} |
159 |
|
160 |
/*
|
161 |
* (non-Javadoc)
|
162 |
*
|
163 |
* @see java.awt.Shape#intersects(double, double, double, double)
|
164 |
*/
|
165 |
@Override
|
166 |
public boolean intersects(double x, double y, double w, double h) { |
167 |
|
168 |
notifyDeprecated("Calling deprecated method of geometry intersects from shape interface");
|
169 |
Shape shp = this.getShape(); |
170 |
return shp.intersects(x, y, w, h);
|
171 |
} |
172 |
|
173 |
/*
|
174 |
* (non-Javadoc)
|
175 |
*
|
176 |
* @see java.awt.Shape#contains(double, double, double, double)
|
177 |
*/
|
178 |
@Override
|
179 |
public boolean contains(double x, double y, double w, double h) { |
180 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
181 |
Shape shp = this.getShape(); |
182 |
return shp.contains(x, y, w, h);
|
183 |
} |
184 |
|
185 |
/*
|
186 |
* (non-Javadoc)
|
187 |
*
|
188 |
* @see java.awt.Shape#contains(java.awt.geom.Point2D)
|
189 |
*/
|
190 |
@Override
|
191 |
public boolean contains(java.awt.geom.Point2D p) { |
192 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
193 |
Shape shp = this.getShape(); |
194 |
return shp.contains(p);
|
195 |
} |
196 |
|
197 |
/*
|
198 |
* (non-Javadoc)
|
199 |
*
|
200 |
* @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
|
201 |
*/
|
202 |
@Override
|
203 |
public boolean contains(Rectangle2D r) { |
204 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
|
205 |
Shape shp = this.getShape(); |
206 |
return shp.contains(r);
|
207 |
} |
208 |
|
209 |
/*
|
210 |
* (non-Javadoc)
|
211 |
*
|
212 |
* @see org.gvsig.fmap.geom.Geometry#distance(org.gvsig.fmap.geom.Geometry)
|
213 |
*/
|
214 |
@Override
|
215 |
public double distance(org.gvsig.fmap.geom.Geometry other) throws GeometryOperationNotSupportedException, |
216 |
GeometryOperationException { |
217 |
return getJTS().distance(((GeometryJTS) other).getJTS());
|
218 |
} |
219 |
|
220 |
/*
|
221 |
* (non-Javadoc)
|
222 |
*
|
223 |
* @see
|
224 |
* org.gvsig.fmap.geom.Geometry#isWithinDistance(org.gvsig.fmap.geom.Geometry
|
225 |
* , double)
|
226 |
*/
|
227 |
@Override
|
228 |
public boolean isWithinDistance(org.gvsig.fmap.geom.Geometry other, double distance) |
229 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
230 |
return com.vividsolutions.jts.operation.distance.DistanceOp.isWithinDistance(this.getJTS(), |
231 |
((GeometryJTS) other).getJTS(), distance); |
232 |
} |
233 |
|
234 |
/*
|
235 |
* (non-Javadoc)
|
236 |
*
|
237 |
* @see org.gvsig.fmap.geom.Geometry#overlaps(org.gvsig.fmap.geom.Geometry)
|
238 |
*/
|
239 |
@Override
|
240 |
public boolean overlaps(org.gvsig.fmap.geom.Geometry geometry) throws GeometryOperationNotSupportedException, |
241 |
GeometryOperationException { |
242 |
// TODO: this method can be implemented throw invokeOperation
|
243 |
return getJTS().overlaps(((GeometryJTS) geometry).getJTS());
|
244 |
} |
245 |
|
246 |
@Override
|
247 |
public boolean coveredBy(Geometry geometry) throws GeometryOperationNotSupportedException, |
248 |
GeometryOperationException { |
249 |
|
250 |
return getJTS().coveredBy(((GeometryJTS) geometry).getJTS());
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public boolean covers(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
255 |
// TODO: this method can be implemented throw invokeOperation
|
256 |
return getJTS().covers(((GeometryJTS) geometry).getJTS());
|
257 |
|
258 |
} |
259 |
|
260 |
@Override
|
261 |
public boolean crosses(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
262 |
// TODO: this method can be implemented throw invokeOperation
|
263 |
return getJTS().crosses(((GeometryJTS) geometry).getJTS());
|
264 |
} |
265 |
|
266 |
@Override
|
267 |
public boolean intersects(Geometry geometry) throws GeometryOperationNotSupportedException, |
268 |
GeometryOperationException { |
269 |
return getJTS().intersects(((GeometryJTS) geometry).getJTS());
|
270 |
} |
271 |
|
272 |
@Override
|
273 |
public boolean touches(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
274 |
// TODO: this method can be implemented throw invokeOperation
|
275 |
return getJTS().touches(((GeometryJTS) geometry).getJTS());
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public boolean disjoint(Geometry geometry) throws GeometryOperationNotSupportedException, |
280 |
GeometryOperationException { |
281 |
// TODO: this method can be implemented throw invokeOperation
|
282 |
return getJTS().disjoint(((GeometryJTS) geometry).getJTS());
|
283 |
} |
284 |
|
285 |
@Override
|
286 |
public boolean within(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
287 |
// TODO: this method can be implemented throw invokeOperation
|
288 |
return getJTS().within(((GeometryJTS) geometry).getJTS());
|
289 |
} |
290 |
|
291 |
@Override
|
292 |
public double area() throws GeometryOperationNotSupportedException, GeometryOperationException { |
293 |
return getJTS().getArea();
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
public double perimeter() throws GeometryOperationNotSupportedException, GeometryOperationException { |
298 |
return getJTS().getLength();
|
299 |
} |
300 |
|
301 |
/*
|
302 |
* (non-Javadoc)
|
303 |
*
|
304 |
* @see org.gvsig.fmap.geom.Geometry#intersects(java.awt.geom.Rectangle2D)
|
305 |
*/
|
306 |
@Override
|
307 |
public boolean intersects(Rectangle2D r) { |
308 |
double x = r.getMinX();
|
309 |
double y = r.getMinY();
|
310 |
double w = r.getWidth();
|
311 |
double h = r.getHeight();
|
312 |
|
313 |
return fastIntersects(x, y, w, h);
|
314 |
} |
315 |
|
316 |
/*
|
317 |
* (non-Javadoc)
|
318 |
*
|
319 |
* @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double,
|
320 |
* double)
|
321 |
*/
|
322 |
@Override
|
323 |
public boolean fastIntersects(double x, double y, double w, double h) { |
324 |
com.vividsolutions.jts.geom.Geometry rect; |
325 |
com.vividsolutions.jts.geom.Coordinate[] coord = new com.vividsolutions.jts.geom.Coordinate[5]; |
326 |
coord[0] = new com.vividsolutions.jts.geom.Coordinate(x, y); |
327 |
coord[1] = new com.vividsolutions.jts.geom.Coordinate(x + w, y); |
328 |
coord[2] = new com.vividsolutions.jts.geom.Coordinate(x + w, y + w); |
329 |
coord[3] = new com.vividsolutions.jts.geom.Coordinate(x, y + w); |
330 |
coord[4] = new com.vividsolutions.jts.geom.Coordinate(x, y); |
331 |
rect = JTSUtils.createJTSPolygon(coord); |
332 |
|
333 |
return getJTS().intersects(rect);
|
334 |
} |
335 |
|
336 |
/*
|
337 |
* (non-Javadoc)
|
338 |
*
|
339 |
* @see org.gvsig.fmap.geom.Geometry#getEnvelope()
|
340 |
*/
|
341 |
@Override
|
342 |
public Envelope getEnvelope() {
|
343 |
if (is3D()) {
|
344 |
if(this.isEmpty()){ |
345 |
return new Envelope3D(getProjection()); |
346 |
} |
347 |
Coordinate[] coordinates = getJTS().getCoordinates();
|
348 |
|
349 |
double minx = Double.POSITIVE_INFINITY; |
350 |
double miny = Double.POSITIVE_INFINITY; |
351 |
double minz = Double.POSITIVE_INFINITY; |
352 |
|
353 |
double maxx = Double.NEGATIVE_INFINITY; |
354 |
double maxy = Double.NEGATIVE_INFINITY; |
355 |
double maxz = Double.NEGATIVE_INFINITY; |
356 |
|
357 |
double x;
|
358 |
double y;
|
359 |
double z;
|
360 |
|
361 |
for (Coordinate coordinate : coordinates) {
|
362 |
x = coordinate.x; |
363 |
y = coordinate.y; |
364 |
z = coordinate.z; |
365 |
minx = Math.min(x, minx);
|
366 |
miny = Math.min(y, miny);
|
367 |
minz = Math.min(z, minz);
|
368 |
maxx = Math.max(x, maxx);
|
369 |
maxy = Math.max(y, maxy);
|
370 |
maxz = Math.max(z, maxz);
|
371 |
} |
372 |
|
373 |
if (minx <= maxx && miny <= maxy && (minz <= maxz || (Double.isNaN(minz) && Double.isNaN(maxz)) ) ) { |
374 |
Point min = new Point3D(minx, miny, minz); |
375 |
Point max = new Point3D(maxx, maxy, maxz); |
376 |
return new Envelope3D(min, max, this.getProjection()); |
377 |
} |
378 |
return new Envelope3D(this.getProjection()); |
379 |
} else {
|
380 |
if(this.isEmpty()){ |
381 |
return new Envelope2D(getProjection()); |
382 |
} |
383 |
com.vividsolutions.jts.geom.Envelope envelope = getJTS().getEnvelopeInternal(); |
384 |
return new Envelope2D(envelope, this.getProjection()); |
385 |
} |
386 |
} |
387 |
|
388 |
/*
|
389 |
* (non-Javadoc)
|
390 |
*
|
391 |
* @see org.gvsig.fmap.geom.Geometry#isSimple()
|
392 |
*/
|
393 |
@Override
|
394 |
public boolean isSimple() { |
395 |
return this.getJTS().isSimple(); |
396 |
} |
397 |
|
398 |
/*
|
399 |
* (non-Javadoc)
|
400 |
*
|
401 |
* @see org.gvsig.fmap.geom.Geometry#isCCW()
|
402 |
*/
|
403 |
@Override
|
404 |
public boolean isCCW() throws GeometryOperationNotSupportedException, GeometryOperationException { |
405 |
return CGAlgorithms.isCCW(this.getJTS().getCoordinates()); |
406 |
} |
407 |
|
408 |
/*
|
409 |
* (non-Javadoc)
|
410 |
*
|
411 |
* @see org.gvsig.fmap.geom.Geometry#invokeOperation(int,
|
412 |
* org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
413 |
*/
|
414 |
@Override
|
415 |
public Object invokeOperation(int index, GeometryOperationContext ctx) |
416 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
417 |
return getManager().invokeOperation(index, this, ctx); |
418 |
|
419 |
} |
420 |
|
421 |
/*
|
422 |
* (non-Javadoc)
|
423 |
*
|
424 |
* @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String,
|
425 |
* org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
426 |
*/
|
427 |
@Override
|
428 |
public Object invokeOperation(String opName, GeometryOperationContext ctx) |
429 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
430 |
return getManager().invokeOperation(opName, this, ctx); |
431 |
|
432 |
} |
433 |
|
434 |
/*
|
435 |
* (non-Javadoc)
|
436 |
*
|
437 |
* @see org.gvsig.fmap.geom.Geometry#getType()
|
438 |
*/
|
439 |
@Override
|
440 |
public int getType() { |
441 |
return this.getGeometryType().getType(); |
442 |
} |
443 |
|
444 |
@Override
|
445 |
public Object convertTo(String format) throws GeometryOperationNotSupportedException, GeometryOperationException { |
446 |
if( StringUtils.isEmpty(format) ) {
|
447 |
throw new IllegalArgumentException("Can't accept null as format name."); |
448 |
} |
449 |
format = format.trim().toLowerCase(); |
450 |
switch(format) {
|
451 |
case "jts": |
452 |
return this.getJTS(); |
453 |
case "wkb": |
454 |
return this.convertToWKB(); |
455 |
case "hexwkb": |
456 |
return this.convertToHexWKB(); |
457 |
case "ewkb": |
458 |
return this.convertToEWKB(); |
459 |
case "wkt": |
460 |
return this.convertToWKT(); |
461 |
case "gml": |
462 |
return GMLUtils.geometry2GML(this); |
463 |
case "json": |
464 |
case "geojson": |
465 |
return this.convertToGeoJson(); |
466 |
default:
|
467 |
throw new IllegalArgumentException("Format '"+format+"' not supported"); |
468 |
} |
469 |
|
470 |
} |
471 |
|
472 |
@Override
|
473 |
public byte[] convertToWKB() throws GeometryOperationNotSupportedException, GeometryOperationException { |
474 |
try {
|
475 |
return new OGCWKBEncoder().encode(this); |
476 |
} catch (Exception e) { |
477 |
throw new GeometryOperationException(e); |
478 |
} |
479 |
} |
480 |
|
481 |
@Override
|
482 |
public String convertToHexWKB() throws GeometryOperationNotSupportedException, GeometryOperationException { |
483 |
try {
|
484 |
byte[] bytes = new OGCWKBEncoder().encode(this); |
485 |
return Hex.encodeHexString(bytes);
|
486 |
} catch (Exception e) { |
487 |
throw new GeometryOperationException(e); |
488 |
} |
489 |
} |
490 |
|
491 |
public String convertToGeoJson() throws GeometryOperationNotSupportedException, GeometryOperationException { |
492 |
try {
|
493 |
GeoJsonWriter writer = new GeoJsonWriter();
|
494 |
writer.setEncodeCRS(true);
|
495 |
return writer.write(this.getJTS()); |
496 |
} catch (Exception e) { |
497 |
throw new GeometryOperationException(e); |
498 |
} |
499 |
} |
500 |
|
501 |
@Override
|
502 |
public String convertToHexEWKB() throws GeometryOperationNotSupportedException, GeometryOperationException { |
503 |
try {
|
504 |
byte[] bytes = new PostGISEWKBEncoder().encode(this); |
505 |
return Hex.encodeHexString(bytes);
|
506 |
} catch (Exception e) { |
507 |
throw new GeometryOperationException(e); |
508 |
} |
509 |
} |
510 |
|
511 |
@Override
|
512 |
public byte[] convertToWKBQuietly() { |
513 |
try {
|
514 |
return new OGCWKBEncoder().encode(this); |
515 |
} catch (Exception e) { |
516 |
return null; |
517 |
} |
518 |
} |
519 |
|
520 |
@Override
|
521 |
public String convertToHexWKBQuietly() { |
522 |
try {
|
523 |
byte[] bytes = new OGCWKBEncoder().encode(this); |
524 |
return Hex.encodeHexString(bytes);
|
525 |
} catch (Exception e) { |
526 |
return null; |
527 |
} |
528 |
} |
529 |
|
530 |
@Override
|
531 |
public String convertToHexEWKBQuietly() { |
532 |
try {
|
533 |
byte[] bytes = new PostGISEWKBEncoder().encode(this); |
534 |
return Hex.encodeHexString(bytes);
|
535 |
} catch (Exception e) { |
536 |
return null; |
537 |
} |
538 |
} |
539 |
|
540 |
@Override
|
541 |
public byte[] convertToWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException { |
542 |
/*
|
543 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
544 |
*/
|
545 |
|
546 |
try {
|
547 |
return new OGCWKBEncoder().encode(this); |
548 |
} catch (Exception e) { |
549 |
throw new GeometryOperationException(e); |
550 |
} |
551 |
} |
552 |
|
553 |
@Override
|
554 |
public byte[] convertToWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException, |
555 |
GeometryOperationException { |
556 |
/*
|
557 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
558 |
*/
|
559 |
Geometry geom = this;
|
560 |
if (this.getType() != type) { |
561 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
562 |
jts = JTSUtils.convertTypes(jts, this.getType(), type);
|
563 |
|
564 |
geom = JTSUtils.createGeometry(this.getProjection(), jts);
|
565 |
} |
566 |
try {
|
567 |
return new OGCWKBEncoder().encode(geom); |
568 |
} catch (Exception e) { |
569 |
throw new GeometryOperationException(e); |
570 |
} |
571 |
|
572 |
} |
573 |
|
574 |
@Override
|
575 |
public byte[] convertToEWKB() throws GeometryOperationNotSupportedException, GeometryOperationException { |
576 |
try {
|
577 |
return new PostGISEWKBEncoder().encode(this); |
578 |
} catch (Exception e) { |
579 |
throw new GeometryOperationException(e); |
580 |
} |
581 |
|
582 |
} |
583 |
|
584 |
@Override
|
585 |
public byte[] convertToEWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException { |
586 |
/*
|
587 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
588 |
*/
|
589 |
|
590 |
try {
|
591 |
return new PostGISEWKBEncoder().encode(this); |
592 |
} catch (Exception e) { |
593 |
throw new GeometryOperationException(e); |
594 |
} |
595 |
} |
596 |
|
597 |
@Override
|
598 |
public byte[] convertToEWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException, |
599 |
GeometryOperationException { |
600 |
/*
|
601 |
* No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
|
602 |
*/
|
603 |
Geometry geom = this;
|
604 |
if (this.getType() != type) { |
605 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
606 |
jts = JTSUtils.convertTypes(jts, this.getType(), type);
|
607 |
|
608 |
geom = JTSUtils.createGeometry(this.getProjection(), jts);
|
609 |
} |
610 |
try {
|
611 |
return new PostGISEWKBEncoder().encode(geom); |
612 |
} catch (Exception e) { |
613 |
throw new GeometryOperationException(e); |
614 |
} |
615 |
|
616 |
} |
617 |
|
618 |
@Override
|
619 |
public String convertToWKT() throws GeometryOperationNotSupportedException, GeometryOperationException { |
620 |
int subType = getGeometryType().getSubType();
|
621 |
|
622 |
EWKTWriter writer; // = null;
|
623 |
|
624 |
switch (subType) {
|
625 |
case Geometry.SUBTYPES.GEOM3D:
|
626 |
writer = new EWKTWriter(3, false); |
627 |
break;
|
628 |
case Geometry.SUBTYPES.GEOM2DM:
|
629 |
writer = new EWKTWriter(3, true); |
630 |
break;
|
631 |
case Geometry.SUBTYPES.GEOM3DM:
|
632 |
writer = new EWKTWriter(4, true); |
633 |
break;
|
634 |
|
635 |
default:
|
636 |
writer = new EWKTWriter(2, false); |
637 |
break;
|
638 |
} |
639 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
640 |
return writer.write(jts);
|
641 |
} |
642 |
|
643 |
@Override
|
644 |
public String convertToWKTQuietly() { |
645 |
try {
|
646 |
return this.convertToWKT(); |
647 |
} catch(Exception ex) { |
648 |
return null; |
649 |
} |
650 |
} |
651 |
|
652 |
@Override
|
653 |
public org.gvsig.fmap.geom.Geometry buffer(double distance) throws GeometryOperationNotSupportedException, |
654 |
GeometryOperationException { |
655 |
if( distance==0 ) { |
656 |
return this; |
657 |
} |
658 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance)); |
659 |
} |
660 |
|
661 |
|
662 |
@Override
|
663 |
public org.gvsig.fmap.geom.Geometry buffer(double distance, int joinStyle, boolean capButt) throws GeometryOperationNotSupportedException, |
664 |
GeometryOperationException { |
665 |
if( distance==0 ) { |
666 |
return this; |
667 |
} |
668 |
|
669 |
int quadrantSegments = JTSUtils.calculateQuadrantSegments(joinStyle);
|
670 |
|
671 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer( |
672 |
distance, |
673 |
quadrantSegments, |
674 |
capButt ? BufferParameters.CAP_FLAT : BufferParameters.CAP_ROUND |
675 |
)); |
676 |
} |
677 |
|
678 |
/*
|
679 |
* (non-Javadoc)
|
680 |
*
|
681 |
* @see org.gvsig.fmap.geom.Geometry#snapTo(org.gvsig.fmap.geom.Geometry,
|
682 |
* double)
|
683 |
*/
|
684 |
@Override
|
685 |
public org.gvsig.fmap.geom.Geometry snapTo(org.gvsig.fmap.geom.Geometry other, double snapTolerance) |
686 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
687 |
Geometry result; // = null;
|
688 |
GeometrySnapper snapper = new GeometrySnapper(getJTS());
|
689 |
com.vividsolutions.jts.geom.Geometry jts_result = snapper.snapTo(((GeometryJTS) other).getJTS(), snapTolerance); |
690 |
result = JTSUtils.createGeometry(this.getProjection(), jts_result);
|
691 |
return result;
|
692 |
} |
693 |
|
694 |
/*
|
695 |
* (non-Javadoc)
|
696 |
*
|
697 |
* @see org.gvsig.fmap.geom.Geometry#getInteriorPoint()
|
698 |
*/
|
699 |
@Override
|
700 |
public Point getInteriorPoint() throws GeometryOperationNotSupportedException, GeometryOperationException { |
701 |
|
702 |
com.vividsolutions.jts.geom.Geometry geometry = getJTS(); |
703 |
com.vividsolutions.jts.geom.Point point = geometry.getInteriorPoint(); |
704 |
Geometry result = JTSUtils.createGeometry(this.getProjection(), point);
|
705 |
return (Point) result; |
706 |
} |
707 |
|
708 |
/*
|
709 |
* (non-Javadoc)
|
710 |
*
|
711 |
* @see org.gvsig.fmap.geom.Geometry#isValid()
|
712 |
*/
|
713 |
@Override
|
714 |
public boolean isValid() { |
715 |
return getJTS().isValid();
|
716 |
} |
717 |
|
718 |
@Override
|
719 |
public ValidationStatus getValidationStatus() {
|
720 |
DefaultValidationStatus status = new DefaultValidationStatus(ValidationStatus.VALID, null); |
721 |
com.vividsolutions.jts.geom.Geometry jtsgeom; // = null;
|
722 |
try {
|
723 |
jtsgeom = this.getJTS();
|
724 |
IsValidOp validOp = new IsValidOp(jtsgeom);
|
725 |
if (!validOp.isValid() ) {
|
726 |
status.setValidationError(validOp.getValidationError()); |
727 |
} |
728 |
} catch (Throwable ex) { |
729 |
status.setStatusCode(ValidationStatus.CURRUPTED); |
730 |
status.setMesage("The geometry is corrupted.");
|
731 |
if (this instanceof OrientableSurface) { |
732 |
int vertices = ((OrientableSurface) this).getNumVertices(); |
733 |
if (vertices < 3) { |
734 |
status.setStatusCode(ValidationStatus.TOO_FEW_POINTS); |
735 |
status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]); |
736 |
} |
737 |
} else if (this instanceof OrientableCurve) { |
738 |
int vertices = ((OrientableCurve) this).getNumVertices(); |
739 |
if (vertices < 2) { |
740 |
status.setStatusCode(ValidationStatus.TOO_FEW_POINTS); |
741 |
status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]); |
742 |
} |
743 |
} |
744 |
} |
745 |
return status;
|
746 |
} |
747 |
|
748 |
/*
|
749 |
* (non-Javadoc)
|
750 |
*
|
751 |
* @see org.gvsig.fmap.geom.Geometry#makeValid()
|
752 |
*/
|
753 |
@Override
|
754 |
public org.gvsig.fmap.geom.Geometry makeValid() {
|
755 |
try {
|
756 |
ValidationStatus vs = this.getValidationStatus();
|
757 |
if (vs.isValid()) {
|
758 |
return this; |
759 |
} |
760 |
Geometry g; // = null;
|
761 |
switch (vs.getStatusCode()) {
|
762 |
case Geometry.ValidationStatus.RING_SELF_INTERSECTION:
|
763 |
case Geometry.ValidationStatus.SELF_INTERSECTION:
|
764 |
g = this.buffer(0); |
765 |
if (g.isValid()) {
|
766 |
return g;
|
767 |
} |
768 |
break;
|
769 |
|
770 |
case Geometry.ValidationStatus.TOO_FEW_POINTS:
|
771 |
if (this instanceof OrientableCurve) { |
772 |
int vertices = ((OrientableCurve) this).getNumVertices(); |
773 |
if (vertices < 2) { |
774 |
return null; // new |
775 |
// DefaultNullGeometry(this.getGeometryType());
|
776 |
} |
777 |
} |
778 |
if (this instanceof OrientableSurface) { |
779 |
int vertices = ((OrientableSurface) this).getNumVertices(); |
780 |
if (vertices < 3) { |
781 |
return null; // new |
782 |
// DefaultNullGeometry(this.getGeometryType());
|
783 |
} |
784 |
} |
785 |
} |
786 |
} catch (Exception ex) { |
787 |
return null; |
788 |
} |
789 |
return null; |
790 |
} |
791 |
|
792 |
/*
|
793 |
* (non-Javadoc)
|
794 |
*
|
795 |
* @see org.gvsig.fmap.geom.Geometry#getBounds2D()
|
796 |
*/
|
797 |
@Override
|
798 |
public Rectangle2D getBounds2D() { |
799 |
com.vividsolutions.jts.geom.Envelope envInternal = getJTS().getEnvelopeInternal(); |
800 |
return new Rectangle2D.Double(envInternal.getMinX(), envInternal.getMinY(), envInternal.getWidth(), |
801 |
envInternal.getHeight()); |
802 |
} |
803 |
|
804 |
/*
|
805 |
* (non-Javadoc)
|
806 |
*
|
807 |
* @see java.awt.Shape#getBounds()
|
808 |
*/
|
809 |
@Override
|
810 |
public Rectangle getBounds() { |
811 |
return this.getShape().getBounds(); |
812 |
} |
813 |
|
814 |
/*
|
815 |
* (non-Javadoc)
|
816 |
*
|
817 |
* @see org.gvsig.fmap.geom.Geometry#getInternalShape()
|
818 |
*/
|
819 |
@Override
|
820 |
public Shape getInternalShape() { |
821 |
return getShape();
|
822 |
} |
823 |
|
824 |
@Override
|
825 |
public void rotate(double radAngle, double basex, double basey) { |
826 |
|
827 |
AffineTransform at = new AffineTransform(); |
828 |
at.rotate(radAngle, basex, basey); |
829 |
this.transform(at);
|
830 |
} |
831 |
|
832 |
@Override
|
833 |
public void move(double dx, double dy) { |
834 |
|
835 |
AffineTransform at = new AffineTransform(); |
836 |
at.translate(dx, dy); |
837 |
this.transform(at);
|
838 |
} |
839 |
|
840 |
@Override
|
841 |
public void scale(Point basePoint, double sx, double sy) { |
842 |
|
843 |
AffineTransform at = new AffineTransform(); |
844 |
at.setToTranslation(basePoint.getX(), basePoint.getY()); |
845 |
at.scale(sx, sy); |
846 |
at.translate(-basePoint.getX(), -basePoint.getY()); |
847 |
this.transform(at);
|
848 |
} |
849 |
|
850 |
@Override
|
851 |
public Geometry[] closestPoints(Geometry other) throws GeometryOperationNotSupportedException, |
852 |
GeometryOperationException { |
853 |
Point[] points; |
854 |
|
855 |
Coordinate[] jts_points = DistanceOp.nearestPoints(getJTS(), ((GeometryJTS) other).getJTS());
|
856 |
points = new Point[jts_points.length]; |
857 |
for (int i = 0; i < jts_points.length; i++) { |
858 |
try {
|
859 |
points[i] = JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), jts_points[i]); |
860 |
} catch (CreateGeometryException e) {
|
861 |
throw new GeometryOperationException(e); |
862 |
} |
863 |
} |
864 |
|
865 |
return (Geometry[]) points; |
866 |
} |
867 |
|
868 |
@Override
|
869 |
public Geometry convexHull() throws GeometryOperationNotSupportedException, GeometryOperationException { |
870 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().convexHull(), null); |
871 |
} |
872 |
|
873 |
@Override
|
874 |
public Geometry difference(Geometry other) throws GeometryOperationNotSupportedException, |
875 |
GeometryOperationException { |
876 |
com.vividsolutions.jts.geom.Geometry otherJTS = ((GeometryJTS) other).getJTS(); |
877 |
if(other instanceof Aggregate){ |
878 |
otherJTS = otherJTS.union(); |
879 |
} |
880 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().difference(otherJTS), null); |
881 |
} |
882 |
|
883 |
@Override
|
884 |
public Geometry intersection(Geometry other) throws GeometryOperationNotSupportedException, |
885 |
GeometryOperationException { |
886 |
com.vividsolutions.jts.geom.Geometry otherJTS = ((GeometryJTS) other).getJTS(); |
887 |
if(other instanceof Aggregate){ |
888 |
otherJTS = otherJTS.union(); |
889 |
} |
890 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().intersection(otherJTS), null); |
891 |
} |
892 |
|
893 |
@Override
|
894 |
public Geometry union(Geometry other) throws GeometryOperationNotSupportedException, GeometryOperationException { |
895 |
try {
|
896 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
897 |
com.vividsolutions.jts.geom.Geometry otherJts = ((GeometryJTS)other).getJTS(); |
898 |
|
899 |
if(jts.isValid() && otherJts.isValid()){
|
900 |
return JTSUtils.createGeometry(this.getProjection(), jts.union(otherJts), this.getGeometryType()); |
901 |
} |
902 |
MultiPrimitive geom = this.getManager().createMultiPrimitive(geometryType);
|
903 |
geom.addPrimitives(this);
|
904 |
geom.addPrimitives(other); |
905 |
return geom;
|
906 |
} catch (CreateGeometryException ex) {
|
907 |
throw new GeometryOperationException( |
908 |
this.getGeometryType().getType(),
|
909 |
this.getManager().getGeometryOperationCode(Geometry.OPERATIONS.UNION),
|
910 |
ex); |
911 |
} |
912 |
} |
913 |
|
914 |
@Override
|
915 |
public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException, |
916 |
GeometryOperationException { |
917 |
try {
|
918 |
return JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), getJTS().getCentroid().getCoordinate()); |
919 |
} catch (CreateGeometryException e) {
|
920 |
throw new GeometryOperationException(e); |
921 |
} |
922 |
} |
923 |
|
924 |
protected void notifyDeprecated(String message) { |
925 |
LOGGER.info(message); |
926 |
|
927 |
} |
928 |
|
929 |
|
930 |
@Override
|
931 |
public boolean ensureOrientation(boolean ccw) throws GeometryOperationNotSupportedException, GeometryOperationException { |
932 |
if (ccw != isCCW()) {
|
933 |
flip(); |
934 |
return true; |
935 |
} |
936 |
return false; |
937 |
} |
938 |
|
939 |
/* (non-Javadoc)
|
940 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#out(org.gvsig.fmap.geom.Geometry)
|
941 |
*/
|
942 |
@Override
|
943 |
public boolean out(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
944 |
GeometryJTS otherJtsGeom = (GeometryJTS) geometry; |
945 |
return (!contains(otherJtsGeom) && !intersects(otherJtsGeom));
|
946 |
} |
947 |
|
948 |
@Override
|
949 |
public boolean equals(Object obj) { |
950 |
if (obj instanceof GeometryJTS) { |
951 |
return this.getJTS().equals(((GeometryJTS) obj).getJTS()); |
952 |
} |
953 |
return false; |
954 |
} |
955 |
|
956 |
@Override
|
957 |
public String toString() { |
958 |
return this.getGeometryType().getFullName(); |
959 |
} |
960 |
|
961 |
@Override
|
962 |
public IProjection getProjection() {
|
963 |
return this.projection; |
964 |
} |
965 |
|
966 |
@Override
|
967 |
public void setProjectionIffNull(IProjection projection) { |
968 |
if (this.projection == null) { |
969 |
this.projection = projection;
|
970 |
} |
971 |
} |
972 |
|
973 |
@Override
|
974 |
public void setProjection(IProjection projection) { |
975 |
this.projection = projection;
|
976 |
} |
977 |
|
978 |
@Override
|
979 |
public void setProjection(String projection) { |
980 |
IProjection proj = CRSFactory.getCRS("EPSG:4326");
|
981 |
this.setProjection(proj);
|
982 |
} |
983 |
|
984 |
@Override
|
985 |
@SuppressWarnings("CloneDoesntCallSuperClone") |
986 |
public Geometry clone() throws CloneNotSupportedException { |
987 |
return this.cloneGeometry(); |
988 |
} |
989 |
|
990 |
@Override
|
991 |
public Geometry boundary() {
|
992 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().getBoundary(), null); |
993 |
} |
994 |
|
995 |
@Override
|
996 |
public Geometry fix() {
|
997 |
try {
|
998 |
ValidationStatus status = this.getValidationStatus();
|
999 |
if(status.isValid()) {
|
1000 |
return this.cloneGeometry(); |
1001 |
} |
1002 |
int statusCode = status.getStatusCode();
|
1003 |
Geometry fixed; |
1004 |
switch (statusCode) {
|
1005 |
case ValidationStatus.VALID:
|
1006 |
return this.cloneGeometry(); |
1007 |
case ValidationStatus.SELF_INTERSECTION:
|
1008 |
case ValidationStatus.RING_SELF_INTERSECTION:
|
1009 |
fixed = this.buffer(Double.MIN_VALUE); |
1010 |
break;
|
1011 |
case ValidationStatus.CURRUPTED:
|
1012 |
case ValidationStatus.UNKNOW:
|
1013 |
case ValidationStatus.DISCONNECTED_INTERIOR:
|
1014 |
case ValidationStatus.DUPLICATE_RINGS:
|
1015 |
case ValidationStatus.HOLE_OUTSIDE_SHELL:
|
1016 |
case ValidationStatus.INVALID_COORDINATE:
|
1017 |
case ValidationStatus.NESTED_HOLES:
|
1018 |
case ValidationStatus.NESTED_SHELLS:
|
1019 |
case ValidationStatus.RING_NOT_CLOSED:
|
1020 |
case ValidationStatus.TOO_FEW_POINTS:
|
1021 |
default:
|
1022 |
return null; |
1023 |
} |
1024 |
if(!fixed.isValid()){
|
1025 |
return null; |
1026 |
} |
1027 |
if(this.getGeometryType().getType() != fixed.getGeometryType().getType()){ |
1028 |
return null; |
1029 |
} |
1030 |
return fixed;
|
1031 |
} catch (Exception ex) { |
1032 |
return null; |
1033 |
} |
1034 |
} |
1035 |
|
1036 |
@Override
|
1037 |
public Geometry forceSubtype(int subtype) throws GeometryOperationNotSupportedException, GeometryOperationException { |
1038 |
switch(subtype){
|
1039 |
case Geometry.SUBTYPES.GEOM2D:
|
1040 |
return force2D();
|
1041 |
case Geometry.SUBTYPES.GEOM2DM:
|
1042 |
return force2DM();
|
1043 |
case Geometry.SUBTYPES.GEOM3D:
|
1044 |
return force3D();
|
1045 |
case Geometry.SUBTYPES.GEOM3DM:
|
1046 |
return force3DM();
|
1047 |
} |
1048 |
return this; |
1049 |
} |
1050 |
|
1051 |
protected abstract Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException; |
1052 |
|
1053 |
protected abstract Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException; |
1054 |
|
1055 |
protected abstract Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException; |
1056 |
|
1057 |
@Override
|
1058 |
public boolean isEmpty() { |
1059 |
if(this instanceof Aggregate) { |
1060 |
if(((Aggregate)this).getPrimitivesNumber()<1){ |
1061 |
return true; |
1062 |
} |
1063 |
} else if(this instanceof Complex) { |
1064 |
if(((Complex)this).getPrimitivesNumber()<1){ |
1065 |
return true; |
1066 |
} |
1067 |
} else if(this instanceof OrientablePrimitive) { |
1068 |
return ((OrientablePrimitive)this).isEmpty(); |
1069 |
} |
1070 |
return false; |
1071 |
} |
1072 |
|
1073 |
|
1074 |
|
1075 |
} |