Statistics
| Revision:

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 @ 44795

History | View | Annotate | Download (27.4 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.jts;
24

    
25
import java.awt.Rectangle;
26
import java.awt.Shape;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.Rectangle2D;
29

    
30
import com.vividsolutions.jts.algorithm.CGAlgorithms;
31
import com.vividsolutions.jts.geom.Coordinate;
32
import com.vividsolutions.jts.io.WKTWriter;
33
import com.vividsolutions.jts.operation.distance.DistanceOp;
34
import com.vividsolutions.jts.operation.overlay.snap.GeometrySnapper;
35
import com.vividsolutions.jts.operation.valid.IsValidOp;
36
import com.vividsolutions.jts.operation.valid.TopologyValidationError;
37
import org.codehaus.plexus.util.StringUtils;
38
import org.cresques.cts.IProjection;
39
import org.gvsig.fmap.crs.CRSFactory;
40

    
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.GeometryLocator;
46
import org.gvsig.fmap.geom.GeometryManager;
47
import org.gvsig.fmap.geom.exception.CreateGeometryException;
48
import org.gvsig.fmap.geom.jts.operation.towkb.OGCWKBEncoder;
49
import org.gvsig.fmap.geom.jts.operation.towkb.PostGISEWKBEncoder;
50
import org.gvsig.fmap.geom.jts.operation.towkt.EWKTWriter;
51
import org.gvsig.fmap.geom.jts.primitive.Envelope2D;
52
import org.gvsig.fmap.geom.jts.primitive.Envelope3D;
53
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
54
import org.gvsig.fmap.geom.jts.util.GMLUtils;
55
import org.gvsig.fmap.geom.jts.util.JTSUtils;
56
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
57
import org.gvsig.fmap.geom.operation.GeometryOperationException;
58
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
59
import org.gvsig.fmap.geom.primitive.Envelope;
60
import org.gvsig.fmap.geom.primitive.OrientableCurve;
61
import org.gvsig.fmap.geom.primitive.OrientableSurface;
62
import org.gvsig.fmap.geom.primitive.Point;
63
import org.gvsig.fmap.geom.type.GeometryType;
64

    
65
/**
66
 * @author fdiaz
67
 *
68
 */
69
public abstract class AbstractGeometry implements GeometryJTS {
70

    
71
    protected static final Logger logger = LoggerFactory.getLogger(AbstractGeometry.class);
72

    
73
    /**
74
     *
75
     */
76
    private static final long serialVersionUID = 4999326772576222293L;
77

    
78
    private final GeometryType geometryType;
79

    
80
    private IProjection projection;
81

    
82
    /**
83
     *
84
     */
85
    public AbstractGeometry(int type, int subtype) {
86
        try {
87
            this.geometryType = GeometryLocator.getGeometryManager().getGeometryType(type, subtype);
88
        } catch (Exception e) {
89
            // TODO: Ver de crear una excepcion para esto o si hay alguna en el API.
90
            throw new RuntimeException(e);
91
        }
92
    }
93

    
94
    public GeometryType getGeometryType() {
95
        return geometryType;
96
    }
97

    
98
    protected GeometryManager getManager() {
99
        return GeometryLocator.getGeometryManager();
100
    }
101

    
102
    /*
103
     * (non-Javadoc)
104
     *
105
     * @see org.gvsig.fmap.geom.Geometry#contains(org.gvsig.fmap.geom.Geometry)
106
     */
107
    public boolean contains(Geometry geometry) throws GeometryOperationNotSupportedException,
108
            GeometryOperationException {
109
        if (!(geometry instanceof GeometryJTS)) {
110
            return false;
111
        }
112
        return getJTS().contains(((GeometryJTS) geometry).getJTS());
113
    }
114

    
115
    /*
116
     * (non-Javadoc)
117
     *
118
     * @see java.lang.Comparable#compareTo(java.lang.Object)
119
     */
120
    public int compareTo(Object o) {
121
        return getJTS().compareTo(((GeometryJTS) o).getJTS());
122
    }
123

    
124
    /*
125
     * (non-Javadoc)
126
     *
127
     * @see java.awt.Shape#contains(double, double)
128
     */
129
    public boolean contains(double x, double y) {
130
        notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
131
        Shape shp = getShape();
132
        return shp.contains(x, y);
133
    }
134

    
135
    /*
136
     * (non-Javadoc)
137
     *
138
     * @see java.awt.Shape#intersects(double, double, double, double)
139
     */
140
    public boolean intersects(double x, double y, double w, double h) {
141

    
142
        notifyDeprecated("Calling deprecated method of geometry intersects from shape interface");
143
        Shape shp = this.getShape();
144
        return shp.intersects(x, y, w, h);
145
    }
146

    
147
    /*
148
     * (non-Javadoc)
149
     *
150
     * @see java.awt.Shape#contains(double, double, double, double)
151
     */
152
    public boolean contains(double x, double y, double w, double h) {
153
        notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
154
        Shape shp = this.getShape();
155
        return shp.contains(x, y, w, h);
156
    }
157

    
158
    /*
159
     * (non-Javadoc)
160
     *
161
     * @see java.awt.Shape#contains(java.awt.geom.Point2D)
162
     */
163
    public boolean contains(java.awt.geom.Point2D p) {
164
        notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
165
        Shape shp = this.getShape();
166
        return shp.contains(p);
167
    }
168

    
169
    /*
170
     * (non-Javadoc)
171
     *
172
     * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
173
     */
174
    public boolean contains(Rectangle2D r) {
175
        notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
176
        Shape shp = this.getShape();
177
        return shp.contains(r);
178
    }
179

    
180
    /*
181
     * (non-Javadoc)
182
     *
183
     * @see org.gvsig.fmap.geom.Geometry#distance(org.gvsig.fmap.geom.Geometry)
184
     */
185
    public double distance(org.gvsig.fmap.geom.Geometry other) throws GeometryOperationNotSupportedException,
186
            GeometryOperationException {
187
        return getJTS().distance(((GeometryJTS) other).getJTS());
188
    }
189

    
190
    /*
191
     * (non-Javadoc)
192
     *
193
     * @see
194
     * org.gvsig.fmap.geom.Geometry#isWithinDistance(org.gvsig.fmap.geom.Geometry
195
     * , double)
196
     */
197
    public boolean isWithinDistance(org.gvsig.fmap.geom.Geometry other, double distance)
198
            throws GeometryOperationNotSupportedException, GeometryOperationException {
199
        return com.vividsolutions.jts.operation.distance.DistanceOp.isWithinDistance(this.getJTS(),
200
                ((GeometryJTS) other).getJTS(), distance);
201
    }
202

    
203
    /*
204
     * (non-Javadoc)
205
     *
206
     * @see org.gvsig.fmap.geom.Geometry#overlaps(org.gvsig.fmap.geom.Geometry)
207
     */
208
    public boolean overlaps(org.gvsig.fmap.geom.Geometry geometry) throws GeometryOperationNotSupportedException,
209
            GeometryOperationException {
210
        // TODO: this method can be implemented throw invokeOperation
211
        return getJTS().overlaps(((GeometryJTS) geometry).getJTS());
212
    }
213

    
214
    public boolean coveredBy(Geometry geometry) throws GeometryOperationNotSupportedException,
215
            GeometryOperationException {
216

    
217
        return getJTS().coveredBy(((GeometryJTS) geometry).getJTS());
218
    }
219

    
220
    public boolean covers(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
221
        // TODO: this method can be implemented throw invokeOperation
222
        return getJTS().covers(((GeometryJTS) geometry).getJTS());
223

    
224
    }
225

    
226
    public boolean crosses(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
227
        // TODO: this method can be implemented throw invokeOperation
228
        return getJTS().crosses(((GeometryJTS) geometry).getJTS());
229
    }
230

    
231
    public boolean intersects(Geometry geometry) throws GeometryOperationNotSupportedException,
232
            GeometryOperationException {
233
        return getJTS().intersects(((GeometryJTS) geometry).getJTS());
234
    }
235

    
236
    public boolean touches(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
237
        // TODO: this method can be implemented throw invokeOperation
238
        return getJTS().touches(((GeometryJTS) geometry).getJTS());
239
    }
240

    
241
    public boolean disjoint(Geometry geometry) throws GeometryOperationNotSupportedException,
242
            GeometryOperationException {
243
        // TODO: this method can be implemented throw invokeOperation
244
        return getJTS().disjoint(((GeometryJTS) geometry).getJTS());
245
    }
246

    
247
    public boolean within(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
248
        // TODO: this method can be implemented throw invokeOperation
249
        return getJTS().within(((GeometryJTS) geometry).getJTS());
250
    }
251

    
252
    public double area() throws GeometryOperationNotSupportedException, GeometryOperationException {
253
        return getJTS().getArea();
254
    }
255

    
256
    public double perimeter() throws GeometryOperationNotSupportedException, GeometryOperationException {
257
        return getJTS().getLength();
258
    }
259

    
260
    /*
261
     * (non-Javadoc)
262
     *
263
     * @see org.gvsig.fmap.geom.Geometry#intersects(java.awt.geom.Rectangle2D)
264
     */
265
    public boolean intersects(Rectangle2D r) {
266
        double x = r.getMinX();
267
        double y = r.getMinY();
268
        double w = r.getWidth();
269
        double h = r.getHeight();
270

    
271
        return fastIntersects(x, y, w, h);
272
    }
273

    
274
    /*
275
     * (non-Javadoc)
276
     *
277
     * @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double,
278
     * double)
279
     */
280
    public boolean fastIntersects(double x, double y, double w, double h) {
281
        com.vividsolutions.jts.geom.Geometry rect = null;
282
        com.vividsolutions.jts.geom.Coordinate[] coord = new com.vividsolutions.jts.geom.Coordinate[5];
283
        coord[0] = new com.vividsolutions.jts.geom.Coordinate(x, y);
284
        coord[1] = new com.vividsolutions.jts.geom.Coordinate(x + w, y);
285
        coord[2] = new com.vividsolutions.jts.geom.Coordinate(x + w, y + w);
286
        coord[3] = new com.vividsolutions.jts.geom.Coordinate(x, y + w);
287
        coord[4] = new com.vividsolutions.jts.geom.Coordinate(x, y);
288
        rect = JTSUtils.createJTSPolygon(coord);
289

    
290
        return getJTS().intersects(rect);
291
    }
292

    
293
    /*
294
     * (non-Javadoc)
295
     *
296
     * @see org.gvsig.fmap.geom.Geometry#getEnvelope()
297
     */
298
    public Envelope getEnvelope() {
299
        if (is3D()) {
300
            Coordinate[] coordinates = getJTS().getCoordinates();
301

    
302
            double minx = Double.POSITIVE_INFINITY;
303
            double miny = Double.POSITIVE_INFINITY;
304
            double minz = Double.POSITIVE_INFINITY;
305

    
306
            double maxx = Double.NEGATIVE_INFINITY;
307
            double maxy = Double.NEGATIVE_INFINITY;
308
            double maxz = Double.NEGATIVE_INFINITY;
309

    
310
            double x;
311
            double y;
312
            double z;
313

    
314
            for (int i = 0; i < coordinates.length; i++) {
315
                x = coordinates[i].x;
316
                y = coordinates[i].y;
317
                z = coordinates[i].z;
318
                minx = Math.min(x, minx);
319
                miny = Math.min(y, miny);
320
                minz = Math.min(z, minz);
321
                maxx = Math.max(x, maxx);
322
                maxy = Math.max(y, maxy);
323
                maxz = Math.max(z, maxz);
324
            }
325

    
326
            if (minx <= maxx && miny <= maxy && minz <= maxz) {
327
                Point min = new Point3D(minx, miny, minz);
328
                Point max = new Point3D(maxx, maxy, maxz);
329
                return new Envelope3D(min, max);
330
            }
331
            return new Envelope3D();
332
        } else {
333
            com.vividsolutions.jts.geom.Envelope envelope = getJTS().getEnvelopeInternal();
334
            return new Envelope2D(envelope);
335
        }
336
    }
337

    
338
    /*
339
     * (non-Javadoc)
340
     *
341
     * @see org.gvsig.fmap.geom.Geometry#isSimple()
342
     */
343
    public boolean isSimple() {
344
        return this.getJTS().isSimple();
345
    }
346

    
347
    /*
348
     * (non-Javadoc)
349
     *
350
     * @see org.gvsig.fmap.geom.Geometry#isCCW()
351
     */
352
    public boolean isCCW() throws GeometryOperationNotSupportedException, GeometryOperationException {
353
        return CGAlgorithms.isCCW(this.getJTS().getCoordinates());
354
    }
355

    
356
    /*
357
     * (non-Javadoc)
358
     *
359
     * @see org.gvsig.fmap.geom.Geometry#invokeOperation(int,
360
     * org.gvsig.fmap.geom.operation.GeometryOperationContext)
361
     */
362
    public Object invokeOperation(int index, GeometryOperationContext ctx)
363
            throws GeometryOperationNotSupportedException, GeometryOperationException {
364
        return getManager().invokeOperation(index, this, ctx);
365

    
366
    }
367

    
368
    /*
369
     * (non-Javadoc)
370
     *
371
     * @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String,
372
     * org.gvsig.fmap.geom.operation.GeometryOperationContext)
373
     */
374
    public Object invokeOperation(String opName, GeometryOperationContext ctx)
375
            throws GeometryOperationNotSupportedException, GeometryOperationException {
376
        return getManager().invokeOperation(opName, this, ctx);
377

    
378
    }
379

    
380
    /*
381
     * (non-Javadoc)
382
     *
383
     * @see org.gvsig.fmap.geom.Geometry#getType()
384
     */
385
    public int getType() {
386
        return this.getGeometryType().getType();
387
    }
388

    
389
    @Override
390
    public Object convertTo(String format) throws GeometryOperationNotSupportedException, GeometryOperationException {
391
        if( StringUtils.isEmpty(format) ) {
392
            throw new IllegalArgumentException("Can't accept null as format name.");
393
        }
394
        format = format.trim().toLowerCase();
395
        switch(format) {
396
            case "jts":
397
                return this.getJTS();
398
            case "wkb":
399
                return this.convertToWKB();
400
            case "ewkb":
401
                return this.convertToEWKB();
402
            case "wkt":
403
                return this.convertToWKT();
404
            case "gml":
405
                return GMLUtils.geometry2GML(this);
406
            case "json":
407
            case "geojson":
408
            default:
409
                throw new IllegalArgumentException("Format '"+format+"' not supported");
410
        }
411
        
412
    }
413
    
414
    public byte[] convertToWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
415
        try {
416
            return new OGCWKBEncoder().encode(this);
417
        } catch (Exception e) {
418
            throw new GeometryOperationException(e);
419
        }
420

    
421
    }
422

    
423
    public byte[] convertToWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException {
424
        /*
425
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
426
         */
427

    
428
        try {
429
            return new OGCWKBEncoder().encode(this);
430
        } catch (Exception e) {
431
            throw new GeometryOperationException(e);
432
        }
433
    }
434

    
435
    @Override
436
    public byte[] convertToWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException,
437
            GeometryOperationException {
438
        /*
439
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
440
         */
441
        Geometry geom = this;
442
        if (this.getType() != type) {
443
            com.vividsolutions.jts.geom.Geometry jts = getJTS();
444
            jts = JTSUtils.convertTypes(jts, this.getType(), type);
445

    
446
            geom = JTSUtils.createGeometry(this.getProjection(), jts);
447
        }
448
        try {
449
            return new OGCWKBEncoder().encode(geom);
450
        } catch (Exception e) {
451
            throw new GeometryOperationException(e);
452
        }
453

    
454
    }
455

    
456
    public byte[] convertToEWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
457
        try {
458
            return new PostGISEWKBEncoder().encode(this);
459
        } catch (Exception e) {
460
            throw new GeometryOperationException(e);
461
        }
462

    
463
    }
464

    
465
    public byte[] convertToEWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException {
466
        /*
467
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
468
         */
469

    
470
        try {
471
            return new PostGISEWKBEncoder().encode(this);
472
        } catch (Exception e) {
473
            throw new GeometryOperationException(e);
474
        }
475
    }
476

    
477
    public byte[] convertToEWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException,
478
            GeometryOperationException {
479
        /*
480
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
481
         */
482
        Geometry geom = this;
483
        if (this.getType() != type) {
484
            com.vividsolutions.jts.geom.Geometry jts = getJTS();
485
            jts = JTSUtils.convertTypes(jts, this.getType(), type);
486

    
487
            geom = JTSUtils.createGeometry(this.getProjection(), jts);
488
        }
489
        try {
490
            return new PostGISEWKBEncoder().encode(geom);
491
        } catch (Exception e) {
492
            throw new GeometryOperationException(e);
493
        }
494

    
495
    }
496

    
497
    /*
498
     * (non-Javadoc)
499
     *
500
     * @see org.gvsig.fmap.geom.Geometry#convertToWKT()
501
     */
502
    public String convertToWKT() throws GeometryOperationNotSupportedException, GeometryOperationException {
503
        int subType = getGeometryType().getSubType();
504

    
505
        EWKTWriter writer = null;
506

    
507
        switch (subType) {
508
            case Geometry.SUBTYPES.GEOM3D:
509
                writer = new EWKTWriter(3, false);
510
                break;
511
            case Geometry.SUBTYPES.GEOM2DM:
512
                writer = new EWKTWriter(3, true);
513
                break;
514
            case Geometry.SUBTYPES.GEOM3DM:
515
                writer = new EWKTWriter(4, true);
516
                break;
517

    
518
            default:
519
                writer = new EWKTWriter(2, false);
520
                break;
521
        }
522
        com.vividsolutions.jts.geom.Geometry jts = getJTS();
523
        return writer.write(jts);
524
    }
525

    
526
    /*
527
     * (non-Javadoc)
528
     *
529
     * @see org.gvsig.fmap.geom.Geometry#buffer(double)
530
     */
531
    public org.gvsig.fmap.geom.Geometry buffer(double distance) throws GeometryOperationNotSupportedException,
532
            GeometryOperationException {
533
            if( distance==0 ) {
534
              return this;
535
            }
536
        return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance));
537
    }
538

    
539
    /*
540
     * (non-Javadoc)
541
     *
542
     * @see org.gvsig.fmap.geom.Geometry#snapTo(org.gvsig.fmap.geom.Geometry,
543
     * double)
544
     */
545
    public org.gvsig.fmap.geom.Geometry snapTo(org.gvsig.fmap.geom.Geometry other, double snapTolerance)
546
            throws GeometryOperationNotSupportedException, GeometryOperationException {
547
        Geometry result = null;
548
        GeometrySnapper snapper = new GeometrySnapper(getJTS());
549
        com.vividsolutions.jts.geom.Geometry jts_result = snapper.snapTo(((GeometryJTS) other).getJTS(), snapTolerance);
550
        result = JTSUtils.createGeometry(this.getProjection(), jts_result);
551
        return result;
552
    }
553

    
554
    /*
555
     * (non-Javadoc)
556
     *
557
     * @see org.gvsig.fmap.geom.Geometry#getInteriorPoint()
558
     */
559
    public Point getInteriorPoint() throws GeometryOperationNotSupportedException, GeometryOperationException {
560

    
561
        com.vividsolutions.jts.geom.Geometry geometry = getJTS();
562
        com.vividsolutions.jts.geom.Point point = geometry.getInteriorPoint();
563
        Geometry result = JTSUtils.createGeometry(this.getProjection(), point);
564
        return (Point) result;
565
    }
566

    
567
    /*
568
     * (non-Javadoc)
569
     *
570
     * @see org.gvsig.fmap.geom.Geometry#isValid()
571
     */
572
    public boolean isValid() {
573
        return getJTS().isValid();
574
    }
575

    
576
    @Override
577
    public ValidationStatus getValidationStatus() {
578
        DefaultValidationStatus status = new DefaultValidationStatus(ValidationStatus.VALID, null);
579
        com.vividsolutions.jts.geom.Geometry jtsgeom = null;
580
        try {
581
            jtsgeom = this.getJTS();
582
            IsValidOp validOp = new IsValidOp(jtsgeom);
583
            if (!validOp.isValid() ) {
584
                status.setValidationError(validOp.getValidationError());
585
            }
586
        } catch (Exception ex) {
587
            status.setStatusCode(ValidationStatus.CURRUPTED);
588
            status.setMesage("The geometry is corrupted.");
589
            if (this instanceof OrientableSurface) {
590
                int vertices = ((OrientableSurface) this).getNumVertices();
591
                if (vertices < 3) {
592
                    status.setStatusCode(ValidationStatus.TOO_FEW_POINTS);
593
                    status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]);
594
                }
595
            } else if (this instanceof OrientableCurve) {
596
                int vertices = ((OrientableCurve) this).getNumVertices();
597
                if (vertices < 2) {
598
                    status.setStatusCode(ValidationStatus.TOO_FEW_POINTS);
599
                    status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]);
600
                }
601
            }
602
        }
603
        return status;
604
    }
605

    
606
    /*
607
     * (non-Javadoc)
608
     *
609
     * @see org.gvsig.fmap.geom.Geometry#makeValid()
610
     */
611
    public org.gvsig.fmap.geom.Geometry makeValid() {
612
        try {
613
            ValidationStatus vs = this.getValidationStatus();
614
            if (vs.isValid()) {
615
                return this;
616
            }
617
            Geometry g = null;
618
            switch (vs.getStatusCode()) {
619
                case Geometry.ValidationStatus.RING_SELF_INTERSECTION:
620
                case Geometry.ValidationStatus.SELF_INTERSECTION:
621
                    g = this.buffer(0);
622
                    if (g.isValid()) {
623
                        return g;
624
                    }
625
                    break;
626

    
627
                case Geometry.ValidationStatus.TOO_FEW_POINTS:
628
                    if (this instanceof OrientableCurve) {
629
                        int vertices = ((OrientableCurve) this).getNumVertices();
630
                        if (vertices < 2) {
631
                            return null; // new
632
                            // DefaultNullGeometry(this.getGeometryType());
633
                        }
634
                    }
635
                    if (this instanceof OrientableSurface) {
636
                        int vertices = ((OrientableSurface) this).getNumVertices();
637
                        if (vertices < 3) {
638
                            return null; // new
639
                            // DefaultNullGeometry(this.getGeometryType());
640
                        }
641
                    }
642
            }
643
        } catch (Exception ex) {
644
            return null;
645
        }
646
        return null;
647
    }
648

    
649
    /*
650
     * (non-Javadoc)
651
     *
652
     * @see org.gvsig.fmap.geom.Geometry#getBounds2D()
653
     */
654
    public Rectangle2D getBounds2D() {
655
        com.vividsolutions.jts.geom.Envelope envInternal = getJTS().getEnvelopeInternal();
656
        return new Rectangle2D.Double(envInternal.getMinX(), envInternal.getMinY(), envInternal.getWidth(),
657
                envInternal.getHeight());
658
    }
659

    
660
    /*
661
     * (non-Javadoc)
662
     *
663
     * @see java.awt.Shape#getBounds()
664
     */
665
    public Rectangle getBounds() {
666
        return this.getShape().getBounds();
667
    }
668

    
669
    /*
670
     * (non-Javadoc)
671
     *
672
     * @see org.gvsig.fmap.geom.Geometry#getInternalShape()
673
     */
674
    public Shape getInternalShape() {
675
        return getShape();
676
    }
677

    
678
    public void rotate(double radAngle, double basex, double basey) {
679

    
680
        AffineTransform at = new AffineTransform();
681
        at.rotate(radAngle, basex, basey);
682
        this.transform(at);
683
    }
684

    
685
    public void move(double dx, double dy) {
686

    
687
        AffineTransform at = new AffineTransform();
688
        at.translate(dx, dy);
689
        this.transform(at);
690
    }
691

    
692
    public void scale(Point basePoint, double sx, double sy) {
693

    
694
        AffineTransform at = new AffineTransform();
695
        at.setToTranslation(basePoint.getX(), basePoint.getY());
696
        at.scale(sx, sy);
697
        at.translate(-basePoint.getX(), -basePoint.getY());
698
        this.transform(at);
699
    }
700

    
701
    public Geometry[] closestPoints(Geometry other) throws GeometryOperationNotSupportedException,
702
            GeometryOperationException {
703
        Point[] points = null;
704

    
705
        Coordinate[] jts_points = DistanceOp.nearestPoints(getJTS(), ((GeometryJTS) other).getJTS());
706
        points = new Point[jts_points.length];
707
        for (int i = 0; i < jts_points.length; i++) {
708
            try {
709
                points[i] = JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), jts_points[i]);
710
            } catch (CreateGeometryException e) {
711
                throw new GeometryOperationException(e);
712
            }
713
        }
714

    
715
        return (Geometry[]) points;
716
    }
717

    
718
    public Geometry convexHull() throws GeometryOperationNotSupportedException, GeometryOperationException {
719

    
720
        return JTSUtils.createGeometry(this.getProjection(), getJTS().convexHull());
721
    }
722

    
723
    public Geometry difference(Geometry other) throws GeometryOperationNotSupportedException,
724
            GeometryOperationException {
725
        return JTSUtils.createGeometry(this.getProjection(), getJTS().difference(((GeometryJTS) other).getJTS()));
726
    }
727

    
728
    public Geometry intersection(Geometry other) throws GeometryOperationNotSupportedException,
729
            GeometryOperationException {
730
        return JTSUtils.createGeometry(this.getProjection(), getJTS().intersection(((GeometryJTS) other).getJTS()));
731
    }
732

    
733
    public Geometry union(Geometry other) throws GeometryOperationNotSupportedException, GeometryOperationException {
734
        return JTSUtils.createGeometry(this.getProjection(), getJTS().union(((GeometryJTS) other).getJTS()));
735
    }
736

    
737
    public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException,
738
            GeometryOperationException {
739
        try {
740
            return JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), getJTS().getCentroid().getCoordinate());
741
        } catch (CreateGeometryException e) {
742
            throw new GeometryOperationException(e);
743
        }
744
    }
745

    
746
    protected void notifyDeprecated(String message) {
747
        logger.info(message);
748

    
749
    }
750

    
751

    
752
    /* (non-Javadoc)
753
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#ensureOrientation(boolean)
754
     */
755
    public boolean ensureOrientation(boolean ccw) throws GeometryOperationNotSupportedException, GeometryOperationException {
756
        if (ccw != isCCW()) {
757
            flip();
758
            return true;
759
        }
760
        return false;
761
    }
762

    
763
    /* (non-Javadoc)
764
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#out(org.gvsig.fmap.geom.Geometry)
765
     */
766
    public boolean out(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
767
        GeometryJTS otherJtsGeom = (GeometryJTS) geometry;
768
        return (!contains(otherJtsGeom) && !intersects(otherJtsGeom));
769
    }
770

    
771
    /* (non-Javadoc)
772
     * @see java.lang.Object#equals(java.lang.Object)
773
     */
774
    @Override
775
    public boolean equals(Object obj) {
776
        if (obj instanceof GeometryJTS) {
777
            return this.getJTS().equals(((GeometryJTS) obj).getJTS());
778
        }
779
        return false;
780
    }
781

    
782
    public String toString() {
783
        return this.getGeometryType().getFullName();
784
    }
785

    
786
    @Override
787
    public IProjection getProjection() {
788
        return this.projection;
789
    }
790

    
791
    @Override
792
    public void setProjectionIffNull(IProjection projection) {
793
        if (this.projection == null) {
794
            this.projection = projection;
795
        }
796
    }
797

    
798
    @Override
799
    public void setProjection(IProjection projection) {
800
        this.projection = projection;
801
    }
802

    
803
    @Override
804
    public void setProjection(String projection) {
805
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
806
        this.setProjection(proj);
807
    }
808

    
809
    @Override
810
    @SuppressWarnings("CloneDoesntCallSuperClone")
811
    public Geometry clone() throws CloneNotSupportedException {
812
        return this.cloneGeometry();
813
    }
814

    
815
}