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

History | View | Annotate | Download (30.2 KB)

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

    
25
import com.vividsolutions.jts.algorithm.CGAlgorithms;
26
import com.vividsolutions.jts.geom.Coordinate;
27
import com.vividsolutions.jts.operation.buffer.BufferParameters;
28
import com.vividsolutions.jts.operation.distance.DistanceOp;
29
import com.vividsolutions.jts.operation.overlay.snap.GeometrySnapper;
30
import com.vividsolutions.jts.operation.valid.IsValidOp;
31
import com.vividsolutions.jts.operation.valid.TopologyValidationError;
32
import java.awt.Rectangle;
33
import java.awt.Shape;
34
import java.awt.geom.AffineTransform;
35
import java.awt.geom.Rectangle2D;
36
import org.apache.commons.codec.binary.Hex;
37
import org.apache.commons.lang3.StringUtils;
38
import org.cresques.cts.IProjection;
39
import org.gvsig.fmap.crs.CRSFactory;
40
import org.gvsig.fmap.geom.Geometry;
41
import org.gvsig.fmap.geom.GeometryLocator;
42
import org.gvsig.fmap.geom.GeometryManager;
43
import org.gvsig.fmap.geom.exception.CreateGeometryException;
44
import org.gvsig.fmap.geom.jts.operation.towkb.OGCWKBEncoder;
45
import org.gvsig.fmap.geom.jts.operation.towkb.PostGISEWKBEncoder;
46
import org.gvsig.fmap.geom.jts.operation.towkt.EWKTWriter;
47
import org.gvsig.fmap.geom.jts.primitive.Envelope2D;
48
import org.gvsig.fmap.geom.jts.primitive.Envelope3D;
49
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
50
import org.gvsig.fmap.geom.jts.util.GMLUtils;
51
import org.gvsig.fmap.geom.jts.util.JTSUtils;
52
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
53
import org.gvsig.fmap.geom.operation.GeometryOperationException;
54
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
55
import org.gvsig.fmap.geom.primitive.Envelope;
56
import org.gvsig.fmap.geom.primitive.OrientableCurve;
57
import org.gvsig.fmap.geom.primitive.OrientableSurface;
58
import org.gvsig.fmap.geom.primitive.Point;
59
import org.gvsig.fmap.geom.type.GeometryType;
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

    
63
/**
64
 * @author gvSIG Team
65
 *
66
 */
67
@SuppressWarnings("UseSpecificCatch")
68
public abstract class AbstractGeometry implements GeometryJTS {
69

    
70
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractGeometry.class);
71

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

    
77
    private GeometryType geometryType;
78

    
79
    private IProjection projection;
80

    
81
    /**
82
     *
83
     * @param type
84
     * @param subtype
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
    
95
    public void setGeometryType(GeometryType geometryType) {
96
        this.geometryType = geometryType;
97
    }
98

    
99
    @Override
100
    public GeometryType getGeometryType() {
101
        return geometryType;
102
    }
103

    
104
    protected GeometryManager getManager() {
105
        return GeometryLocator.getGeometryManager();
106
    }
107

    
108
    /*
109
     * (non-Javadoc)
110
     *
111
     * @see org.gvsig.fmap.geom.Geometry#contains(org.gvsig.fmap.geom.Geometry)
112
     */
113
    @Override
114
    public boolean contains(Geometry geometry) throws GeometryOperationNotSupportedException,
115
            GeometryOperationException {
116
        if (!(geometry instanceof GeometryJTS)) {
117
            return false;
118
        }
119
        return getJTS().contains(((GeometryJTS) geometry).getJTS());
120
    }
121

    
122
    /*
123
     * (non-Javadoc)
124
     *
125
     * @see java.lang.Comparable#compareTo(java.lang.Object)
126
     */
127
    @Override
128
    public int compareTo(Object o) {
129
        return getJTS().compareTo(((GeometryJTS) o).getJTS());
130
    }
131

    
132
    /*
133
     * (non-Javadoc)
134
     *
135
     * @see java.awt.Shape#contains(double, double)
136
     */
137
    @Override
138
    public boolean contains(double x, double y) {
139
        notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
140
        Shape shp = getShape();
141
        return shp.contains(x, y);
142
    }
143

    
144
    /*
145
     * (non-Javadoc)
146
     *
147
     * @see java.awt.Shape#intersects(double, double, double, double)
148
     */
149
    @Override
150
    public boolean intersects(double x, double y, double w, double h) {
151

    
152
        notifyDeprecated("Calling deprecated method of geometry intersects from shape interface");
153
        Shape shp = this.getShape();
154
        return shp.intersects(x, y, w, h);
155
    }
156

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

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

    
181
    /*
182
     * (non-Javadoc)
183
     *
184
     * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
185
     */
186
    @Override
187
    public boolean contains(Rectangle2D r) {
188
        notifyDeprecated("Calling deprecated method of geometry contains from shape interface");
189
        Shape shp = this.getShape();
190
        return shp.contains(r);
191
    }
192

    
193
    /*
194
     * (non-Javadoc)
195
     *
196
     * @see org.gvsig.fmap.geom.Geometry#distance(org.gvsig.fmap.geom.Geometry)
197
     */
198
    @Override
199
    public double distance(org.gvsig.fmap.geom.Geometry other) throws GeometryOperationNotSupportedException,
200
            GeometryOperationException {
201
        return getJTS().distance(((GeometryJTS) other).getJTS());
202
    }
203

    
204
    /*
205
     * (non-Javadoc)
206
     *
207
     * @see
208
     * org.gvsig.fmap.geom.Geometry#isWithinDistance(org.gvsig.fmap.geom.Geometry
209
     * , double)
210
     */
211
    @Override
212
    public boolean isWithinDistance(org.gvsig.fmap.geom.Geometry other, double distance)
213
            throws GeometryOperationNotSupportedException, GeometryOperationException {
214
        return com.vividsolutions.jts.operation.distance.DistanceOp.isWithinDistance(this.getJTS(),
215
                ((GeometryJTS) other).getJTS(), distance);
216
    }
217

    
218
    /*
219
     * (non-Javadoc)
220
     *
221
     * @see org.gvsig.fmap.geom.Geometry#overlaps(org.gvsig.fmap.geom.Geometry)
222
     */
223
    @Override
224
    public boolean overlaps(org.gvsig.fmap.geom.Geometry geometry) throws GeometryOperationNotSupportedException,
225
            GeometryOperationException {
226
        // TODO: this method can be implemented throw invokeOperation
227
        return getJTS().overlaps(((GeometryJTS) geometry).getJTS());
228
    }
229

    
230
    @Override
231
    public boolean coveredBy(Geometry geometry) throws GeometryOperationNotSupportedException,
232
            GeometryOperationException {
233

    
234
        return getJTS().coveredBy(((GeometryJTS) geometry).getJTS());
235
    }
236

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

    
242
    }
243

    
244
    @Override
245
    public boolean crosses(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
246
        // TODO: this method can be implemented throw invokeOperation
247
        return getJTS().crosses(((GeometryJTS) geometry).getJTS());
248
    }
249

    
250
    @Override
251
    public boolean intersects(Geometry geometry) throws GeometryOperationNotSupportedException,
252
            GeometryOperationException {
253
        return getJTS().intersects(((GeometryJTS) geometry).getJTS());
254
    }
255

    
256
    @Override
257
    public boolean touches(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
258
        // TODO: this method can be implemented throw invokeOperation
259
        return getJTS().touches(((GeometryJTS) geometry).getJTS());
260
    }
261

    
262
    @Override
263
    public boolean disjoint(Geometry geometry) throws GeometryOperationNotSupportedException,
264
            GeometryOperationException {
265
        // TODO: this method can be implemented throw invokeOperation
266
        return getJTS().disjoint(((GeometryJTS) geometry).getJTS());
267
    }
268

    
269
    @Override
270
    public boolean within(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
271
        // TODO: this method can be implemented throw invokeOperation
272
        return getJTS().within(((GeometryJTS) geometry).getJTS());
273
    }
274

    
275
    @Override
276
    public double area() throws GeometryOperationNotSupportedException, GeometryOperationException {
277
        return getJTS().getArea();
278
    }
279

    
280
    @Override
281
    public double perimeter() throws GeometryOperationNotSupportedException, GeometryOperationException {
282
        return getJTS().getLength();
283
    }
284

    
285
    /*
286
     * (non-Javadoc)
287
     *
288
     * @see org.gvsig.fmap.geom.Geometry#intersects(java.awt.geom.Rectangle2D)
289
     */
290
    @Override
291
    public boolean intersects(Rectangle2D r) {
292
        double x = r.getMinX();
293
        double y = r.getMinY();
294
        double w = r.getWidth();
295
        double h = r.getHeight();
296

    
297
        return fastIntersects(x, y, w, h);
298
    }
299

    
300
    /*
301
     * (non-Javadoc)
302
     *
303
     * @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double,
304
     * double)
305
     */
306
    @Override
307
    public boolean fastIntersects(double x, double y, double w, double h) {
308
        com.vividsolutions.jts.geom.Geometry rect; 
309
        com.vividsolutions.jts.geom.Coordinate[] coord = new com.vividsolutions.jts.geom.Coordinate[5];
310
        coord[0] = new com.vividsolutions.jts.geom.Coordinate(x, y);
311
        coord[1] = new com.vividsolutions.jts.geom.Coordinate(x + w, y);
312
        coord[2] = new com.vividsolutions.jts.geom.Coordinate(x + w, y + w);
313
        coord[3] = new com.vividsolutions.jts.geom.Coordinate(x, y + w);
314
        coord[4] = new com.vividsolutions.jts.geom.Coordinate(x, y);
315
        rect = JTSUtils.createJTSPolygon(coord);
316

    
317
        return getJTS().intersects(rect);
318
    }
319

    
320
    /*
321
     * (non-Javadoc)
322
     *
323
     * @see org.gvsig.fmap.geom.Geometry#getEnvelope()
324
     */
325
    @Override
326
    public Envelope getEnvelope() {
327
        if (is3D()) {
328
            Coordinate[] coordinates = getJTS().getCoordinates();
329

    
330
            double minx = Double.POSITIVE_INFINITY;
331
            double miny = Double.POSITIVE_INFINITY;
332
            double minz = Double.POSITIVE_INFINITY;
333

    
334
            double maxx = Double.NEGATIVE_INFINITY;
335
            double maxy = Double.NEGATIVE_INFINITY;
336
            double maxz = Double.NEGATIVE_INFINITY;
337

    
338
            double x;
339
            double y;
340
            double z;
341

    
342
            for (Coordinate coordinate : coordinates) {
343
                x = coordinate.x;
344
                y = coordinate.y;
345
                z = coordinate.z;
346
                minx = Math.min(x, minx);
347
                miny = Math.min(y, miny);
348
                minz = Math.min(z, minz);
349
                maxx = Math.max(x, maxx);
350
                maxy = Math.max(y, maxy);
351
                maxz = Math.max(z, maxz);
352
            }
353

    
354
            if (minx <= maxx && miny <= maxy && minz <= maxz) {
355
                Point min = new Point3D(minx, miny, minz);
356
                Point max = new Point3D(maxx, maxy, maxz);
357
                return new Envelope3D(min, max);
358
            }
359
            return new Envelope3D();
360
        } else {
361
            com.vividsolutions.jts.geom.Envelope envelope = getJTS().getEnvelopeInternal();
362
            return new Envelope2D(envelope);
363
        }
364
    }
365

    
366
    /*
367
     * (non-Javadoc)
368
     *
369
     * @see org.gvsig.fmap.geom.Geometry#isSimple()
370
     */
371
    @Override
372
    public boolean isSimple() {
373
        return this.getJTS().isSimple();
374
    }
375

    
376
    /*
377
     * (non-Javadoc)
378
     *
379
     * @see org.gvsig.fmap.geom.Geometry#isCCW()
380
     */
381
    @Override
382
    public boolean isCCW() throws GeometryOperationNotSupportedException, GeometryOperationException {
383
        return CGAlgorithms.isCCW(this.getJTS().getCoordinates());
384
    }
385

    
386
    /*
387
     * (non-Javadoc)
388
     *
389
     * @see org.gvsig.fmap.geom.Geometry#invokeOperation(int,
390
     * org.gvsig.fmap.geom.operation.GeometryOperationContext)
391
     */
392
    @Override
393
    public Object invokeOperation(int index, GeometryOperationContext ctx)
394
            throws GeometryOperationNotSupportedException, GeometryOperationException {
395
        return getManager().invokeOperation(index, this, ctx);
396

    
397
    }
398

    
399
    /*
400
     * (non-Javadoc)
401
     *
402
     * @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String,
403
     * org.gvsig.fmap.geom.operation.GeometryOperationContext)
404
     */
405
    @Override
406
    public Object invokeOperation(String opName, GeometryOperationContext ctx)
407
            throws GeometryOperationNotSupportedException, GeometryOperationException {
408
        return getManager().invokeOperation(opName, this, ctx);
409

    
410
    }
411

    
412
    /*
413
     * (non-Javadoc)
414
     *
415
     * @see org.gvsig.fmap.geom.Geometry#getType()
416
     */
417
    @Override
418
    public int getType() {
419
        return this.getGeometryType().getType();
420
    }
421

    
422
    @Override
423
    public Object convertTo(String format) throws GeometryOperationNotSupportedException, GeometryOperationException {
424
        if( StringUtils.isEmpty(format) ) {
425
            throw new IllegalArgumentException("Can't accept null as format name.");
426
        }
427
        format = format.trim().toLowerCase();
428
        switch(format) {
429
            case "jts":
430
                return this.getJTS();
431
            case "wkb":
432
                return this.convertToWKB();
433
            case "hexwkb":
434
                return this.convertToHexWKB();
435
            case "ewkb":
436
                return this.convertToEWKB();
437
            case "wkt":
438
                return this.convertToWKT();
439
            case "gml":
440
                return GMLUtils.geometry2GML(this);
441
            case "json":
442
            case "geojson":
443
            default:
444
                throw new IllegalArgumentException("Format '"+format+"' not supported");
445
        }
446
        
447
    }
448
    
449
    @Override
450
    public byte[] convertToWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
451
        try {
452
            return new OGCWKBEncoder().encode(this);
453
        } catch (Exception e) {
454
            throw new GeometryOperationException(e);
455
        }
456
    }
457

    
458
    @Override
459
    public String convertToHexWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
460
        try {
461
            byte[] bytes = new OGCWKBEncoder().encode(this);
462
            return Hex.encodeHexString(bytes);
463
        } catch (Exception e) {
464
            throw new GeometryOperationException(e);
465
        }
466
    }
467
    
468
    @Override
469
    public String convertToHexEWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
470
        try {
471
            byte[] bytes = new PostGISEWKBEncoder().encode(this);
472
            return Hex.encodeHexString(bytes);
473
        } catch (Exception e) {
474
            throw new GeometryOperationException(e);
475
        }
476
    }
477

    
478
    @Override
479
    public byte[] convertToWKBQuietly() {
480
        try {
481
            return new OGCWKBEncoder().encode(this);
482
        } catch (Exception e) {
483
            return null;
484
        }
485
    }
486
    
487
    @Override
488
    public String convertToHexWKBQuietly() {
489
        try {
490
            byte[] bytes = new OGCWKBEncoder().encode(this);
491
            return Hex.encodeHexString(bytes);
492
        } catch (Exception e) {
493
            return null;
494
        }
495
    }
496
    
497
    @Override
498
    public byte[] convertToWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException {
499
        /*
500
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
501
         */
502

    
503
        try {
504
            return new OGCWKBEncoder().encode(this);
505
        } catch (Exception e) {
506
            throw new GeometryOperationException(e);
507
        }
508
    }
509

    
510
    @Override
511
    public byte[] convertToWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException,
512
            GeometryOperationException {
513
        /*
514
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
515
         */
516
        Geometry geom = this;
517
        if (this.getType() != type) {
518
            com.vividsolutions.jts.geom.Geometry jts = getJTS();
519
            jts = JTSUtils.convertTypes(jts, this.getType(), type);
520

    
521
            geom = JTSUtils.createGeometry(this.getProjection(), jts);
522
        }
523
        try {
524
            return new OGCWKBEncoder().encode(geom);
525
        } catch (Exception e) {
526
            throw new GeometryOperationException(e);
527
        }
528

    
529
    }
530

    
531
    @Override
532
    public byte[] convertToEWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
533
        try {
534
            return new PostGISEWKBEncoder().encode(this);
535
        } catch (Exception e) {
536
            throw new GeometryOperationException(e);
537
        }
538

    
539
    }
540

    
541
    @Override
542
    public byte[] convertToEWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException {
543
        /*
544
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
545
         */
546

    
547
        try {
548
            return new PostGISEWKBEncoder().encode(this);
549
        } catch (Exception e) {
550
            throw new GeometryOperationException(e);
551
        }
552
    }
553

    
554
    @Override
555
    public byte[] convertToEWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException,
556
            GeometryOperationException {
557
        /*
558
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
559
         */
560
        Geometry geom = this;
561
        if (this.getType() != type) {
562
            com.vividsolutions.jts.geom.Geometry jts = getJTS();
563
            jts = JTSUtils.convertTypes(jts, this.getType(), type);
564

    
565
            geom = JTSUtils.createGeometry(this.getProjection(), jts);
566
        }
567
        try {
568
            return new PostGISEWKBEncoder().encode(geom);
569
        } catch (Exception e) {
570
            throw new GeometryOperationException(e);
571
        }
572

    
573
    }
574

    
575
    @Override
576
    public String convertToWKT() throws GeometryOperationNotSupportedException, GeometryOperationException {
577
        int subType = getGeometryType().getSubType();
578

    
579
        EWKTWriter writer; // = null;
580

    
581
        switch (subType) {
582
            case Geometry.SUBTYPES.GEOM3D:
583
                writer = new EWKTWriter(3, false);
584
                break;
585
            case Geometry.SUBTYPES.GEOM2DM:
586
                writer = new EWKTWriter(3, true);
587
                break;
588
            case Geometry.SUBTYPES.GEOM3DM:
589
                writer = new EWKTWriter(4, true);
590
                break;
591

    
592
            default:
593
                writer = new EWKTWriter(2, false);
594
                break;
595
        }
596
        com.vividsolutions.jts.geom.Geometry jts = getJTS();
597
        return writer.write(jts);
598
    }
599

    
600
    @Override
601
    public String convertToWKTQuietly() {
602
        try {
603
            return this.convertToWKT();
604
        } catch(Exception ex) {
605
            return null;
606
        }
607
    }
608

    
609
    @Override
610
    public org.gvsig.fmap.geom.Geometry buffer(double distance) throws GeometryOperationNotSupportedException,
611
        GeometryOperationException {
612
        if( distance==0 ) {
613
          return this;
614
        }
615
        return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance));
616
    }
617
    
618
    
619
    @Override
620
    public org.gvsig.fmap.geom.Geometry buffer(double distance, int joinStyle, boolean capButt) throws GeometryOperationNotSupportedException,
621
        GeometryOperationException {
622
        if( distance==0 ) {
623
          return this;
624
        }
625
        
626
        int quadrantSegments = JTSUtils.calculateQuadrantSegments(joinStyle);
627
        
628
        return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(
629
                distance, 
630
                quadrantSegments, 
631
                capButt ? BufferParameters.CAP_FLAT : BufferParameters.CAP_ROUND
632
        ));
633
    }
634

    
635
    /*
636
     * (non-Javadoc)
637
     *
638
     * @see org.gvsig.fmap.geom.Geometry#snapTo(org.gvsig.fmap.geom.Geometry,
639
     * double)
640
     */
641
    @Override
642
    public org.gvsig.fmap.geom.Geometry snapTo(org.gvsig.fmap.geom.Geometry other, double snapTolerance)
643
            throws GeometryOperationNotSupportedException, GeometryOperationException {
644
        Geometry result; // = null;
645
        GeometrySnapper snapper = new GeometrySnapper(getJTS());
646
        com.vividsolutions.jts.geom.Geometry jts_result = snapper.snapTo(((GeometryJTS) other).getJTS(), snapTolerance);
647
        result = JTSUtils.createGeometry(this.getProjection(), jts_result);
648
        return result;
649
    }
650

    
651
    /*
652
     * (non-Javadoc)
653
     *
654
     * @see org.gvsig.fmap.geom.Geometry#getInteriorPoint()
655
     */
656
    @Override
657
    public Point getInteriorPoint() throws GeometryOperationNotSupportedException, GeometryOperationException {
658

    
659
        com.vividsolutions.jts.geom.Geometry geometry = getJTS();
660
        com.vividsolutions.jts.geom.Point point = geometry.getInteriorPoint();
661
        Geometry result = JTSUtils.createGeometry(this.getProjection(), point);
662
        return (Point) result;
663
    }
664

    
665
    /*
666
     * (non-Javadoc)
667
     *
668
     * @see org.gvsig.fmap.geom.Geometry#isValid()
669
     */
670
    @Override
671
    public boolean isValid() {
672
        return getJTS().isValid();
673
    }
674

    
675
    @Override
676
    public ValidationStatus getValidationStatus() {
677
        DefaultValidationStatus status = new DefaultValidationStatus(ValidationStatus.VALID, null);
678
        com.vividsolutions.jts.geom.Geometry jtsgeom; // = null;
679
        try {
680
            jtsgeom = this.getJTS();
681
            IsValidOp validOp = new IsValidOp(jtsgeom);
682
            if (!validOp.isValid() ) {
683
                status.setValidationError(validOp.getValidationError());
684
            }
685
        } catch (Exception ex) {
686
            status.setStatusCode(ValidationStatus.CURRUPTED);
687
            status.setMesage("The geometry is corrupted.");
688
            if (this instanceof OrientableSurface) {
689
                int vertices = ((OrientableSurface) this).getNumVertices();
690
                if (vertices < 3) {
691
                    status.setStatusCode(ValidationStatus.TOO_FEW_POINTS);
692
                    status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]);
693
                }
694
            } else if (this instanceof OrientableCurve) {
695
                int vertices = ((OrientableCurve) this).getNumVertices();
696
                if (vertices < 2) {
697
                    status.setStatusCode(ValidationStatus.TOO_FEW_POINTS);
698
                    status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]);
699
                }
700
            }
701
        }
702
        return status;
703
    }
704

    
705
    /*
706
     * (non-Javadoc)
707
     *
708
     * @see org.gvsig.fmap.geom.Geometry#makeValid()
709
     */
710
    @Override
711
    public org.gvsig.fmap.geom.Geometry makeValid() {
712
        try {
713
            ValidationStatus vs = this.getValidationStatus();
714
            if (vs.isValid()) {
715
                return this;
716
            }
717
            Geometry g; // = null;
718
            switch (vs.getStatusCode()) {
719
                case Geometry.ValidationStatus.RING_SELF_INTERSECTION:
720
                case Geometry.ValidationStatus.SELF_INTERSECTION:
721
                    g = this.buffer(0);
722
                    if (g.isValid()) {
723
                        return g;
724
                    }
725
                    break;
726

    
727
                case Geometry.ValidationStatus.TOO_FEW_POINTS:
728
                    if (this instanceof OrientableCurve) {
729
                        int vertices = ((OrientableCurve) this).getNumVertices();
730
                        if (vertices < 2) {
731
                            return null; // new
732
                            // DefaultNullGeometry(this.getGeometryType());
733
                        }
734
                    }
735
                    if (this instanceof OrientableSurface) {
736
                        int vertices = ((OrientableSurface) this).getNumVertices();
737
                        if (vertices < 3) {
738
                            return null; // new
739
                            // DefaultNullGeometry(this.getGeometryType());
740
                        }
741
                    }
742
            }
743
        } catch (Exception ex) {
744
            return null;
745
        }
746
        return null;
747
    }
748

    
749
    /*
750
     * (non-Javadoc)
751
     *
752
     * @see org.gvsig.fmap.geom.Geometry#getBounds2D()
753
     */
754
    @Override
755
    public Rectangle2D getBounds2D() {
756
        com.vividsolutions.jts.geom.Envelope envInternal = getJTS().getEnvelopeInternal();
757
        return new Rectangle2D.Double(envInternal.getMinX(), envInternal.getMinY(), envInternal.getWidth(),
758
                envInternal.getHeight());
759
    }
760

    
761
    /*
762
     * (non-Javadoc)
763
     *
764
     * @see java.awt.Shape#getBounds()
765
     */
766
    @Override
767
    public Rectangle getBounds() {
768
        return this.getShape().getBounds();
769
    }
770

    
771
    /*
772
     * (non-Javadoc)
773
     *
774
     * @see org.gvsig.fmap.geom.Geometry#getInternalShape()
775
     */
776
    @Override
777
    public Shape getInternalShape() {
778
        return getShape();
779
    }
780

    
781
    @Override
782
    public void rotate(double radAngle, double basex, double basey) {
783

    
784
        AffineTransform at = new AffineTransform();
785
        at.rotate(radAngle, basex, basey);
786
        this.transform(at);
787
    }
788

    
789
    @Override
790
    public void move(double dx, double dy) {
791

    
792
        AffineTransform at = new AffineTransform();
793
        at.translate(dx, dy);
794
        this.transform(at);
795
    }
796

    
797
    @Override
798
    public void scale(Point basePoint, double sx, double sy) {
799

    
800
        AffineTransform at = new AffineTransform();
801
        at.setToTranslation(basePoint.getX(), basePoint.getY());
802
        at.scale(sx, sy);
803
        at.translate(-basePoint.getX(), -basePoint.getY());
804
        this.transform(at);
805
    }
806

    
807
    @Override
808
    public Geometry[] closestPoints(Geometry other) throws GeometryOperationNotSupportedException,
809
            GeometryOperationException {
810
        Point[] points;
811

    
812
        Coordinate[] jts_points = DistanceOp.nearestPoints(getJTS(), ((GeometryJTS) other).getJTS());
813
        points = new Point[jts_points.length];
814
        for (int i = 0; i < jts_points.length; i++) {
815
            try {
816
                points[i] = JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), jts_points[i]);
817
            } catch (CreateGeometryException e) {
818
                throw new GeometryOperationException(e);
819
            }
820
        }
821

    
822
        return (Geometry[]) points;
823
    }
824

    
825
    @Override
826
    public Geometry convexHull() throws GeometryOperationNotSupportedException, GeometryOperationException {
827

    
828
        return JTSUtils.createGeometry(this.getProjection(), getJTS().convexHull());
829
    }
830

    
831
    @Override
832
    public Geometry difference(Geometry other) throws GeometryOperationNotSupportedException,
833
            GeometryOperationException {
834
        return JTSUtils.createGeometry(this.getProjection(), getJTS().difference(((GeometryJTS) other).getJTS()));
835
    }
836

    
837
    @Override
838
    public Geometry intersection(Geometry other) throws GeometryOperationNotSupportedException,
839
            GeometryOperationException {
840
        return JTSUtils.createGeometry(this.getProjection(), getJTS().intersection(((GeometryJTS) other).getJTS()));
841
    }
842

    
843
    @Override
844
    public Geometry union(Geometry other) throws GeometryOperationNotSupportedException, GeometryOperationException {
845
        return JTSUtils.createGeometry(this.getProjection(), getJTS().union(((GeometryJTS) other).getJTS()));
846
    }
847

    
848
    @Override
849
    public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException,
850
            GeometryOperationException {
851
        try {
852
            return JTSUtils.createPoint(this.getGeometryType(), this.getProjection(), getJTS().getCentroid().getCoordinate());
853
        } catch (CreateGeometryException e) {
854
            throw new GeometryOperationException(e);
855
        }
856
    }
857

    
858
    protected void notifyDeprecated(String message) {
859
        LOGGER.info(message);
860

    
861
    }
862

    
863

    
864
    @Override
865
    public boolean ensureOrientation(boolean ccw) throws GeometryOperationNotSupportedException, GeometryOperationException {
866
        if (ccw != isCCW()) {
867
            flip();
868
            return true;
869
        }
870
        return false;
871
    }
872

    
873
    /* (non-Javadoc)
874
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#out(org.gvsig.fmap.geom.Geometry)
875
     */
876
    @Override
877
    public boolean out(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
878
        GeometryJTS otherJtsGeom = (GeometryJTS) geometry;
879
        return (!contains(otherJtsGeom) && !intersects(otherJtsGeom));
880
    }
881

    
882
    /* (non-Javadoc)
883
     * @see java.lang.Object#equals(java.lang.Object)
884
     */
885
    @Override
886
    public boolean equals(Object obj) {
887
        if (obj instanceof GeometryJTS) {
888
            return this.getJTS().equals(((GeometryJTS) obj).getJTS());
889
        }
890
        return false;
891
    }
892

    
893
    @Override
894
    public String toString() {
895
        return this.getGeometryType().getFullName();
896
    }
897

    
898
    @Override
899
    public IProjection getProjection() {
900
        return this.projection;
901
    }
902

    
903
    @Override
904
    public void setProjectionIffNull(IProjection projection) {
905
        if (this.projection == null) {
906
            this.projection = projection;
907
        }
908
    }
909

    
910
    @Override
911
    public void setProjection(IProjection projection) {
912
        this.projection = projection;
913
    }
914

    
915
    @Override
916
    public void setProjection(String projection) {
917
        IProjection proj = CRSFactory.getCRS("EPSG:4326");
918
        this.setProjection(proj);
919
    }
920

    
921
    @Override
922
    @SuppressWarnings("CloneDoesntCallSuperClone")
923
    public Geometry clone() throws CloneNotSupportedException {
924
        return this.cloneGeometry();
925
    }
926

    
927
    @Override
928
    public Geometry boundary() {
929
        return JTSUtils.createGeometry(this.getProjection(), getJTS().getBoundary());
930
    }
931

    
932
    
933
}