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