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