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