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

History | View | Annotate | Download (30.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 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.aggregate.Aggregate;
44
import org.gvsig.fmap.geom.complex.Complex;
45
import org.gvsig.fmap.geom.exception.CreateGeometryException;
46
import org.gvsig.fmap.geom.jts.operation.towkb.OGCWKBEncoder;
47
import org.gvsig.fmap.geom.jts.operation.towkb.PostGISEWKBEncoder;
48
import org.gvsig.fmap.geom.jts.operation.towkt.EWKTWriter;
49
import org.gvsig.fmap.geom.jts.primitive.Envelope2D;
50
import org.gvsig.fmap.geom.jts.primitive.Envelope3D;
51
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
52
import org.gvsig.fmap.geom.jts.util.GMLUtils;
53
import org.gvsig.fmap.geom.jts.util.JTSUtils;
54
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
55
import org.gvsig.fmap.geom.operation.GeometryOperationException;
56
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
57
import org.gvsig.fmap.geom.primitive.Envelope;
58
import org.gvsig.fmap.geom.primitive.OrientableCurve;
59
import org.gvsig.fmap.geom.primitive.OrientableSurface;
60
import org.gvsig.fmap.geom.primitive.Point;
61
import org.gvsig.fmap.geom.type.GeometryType;
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

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

    
72
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractGeometry.class);
73

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

    
79
    private GeometryType geometryType;
80

    
81
    private IProjection projection;
82

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

    
101
    @Override
102
    public GeometryType getGeometryType() {
103
        return geometryType;
104
    }
105

    
106
    protected GeometryManager getManager() {
107
        return GeometryLocator.getGeometryManager();
108
    }
109

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

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

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

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

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

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

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

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

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

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

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

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

    
236
        return getJTS().coveredBy(((GeometryJTS) geometry).getJTS());
237
    }
238

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

    
244
    }
245

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

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

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

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

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

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

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

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

    
299
        return fastIntersects(x, y, w, h);
300
    }
301

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

    
319
        return getJTS().intersects(rect);
320
    }
321

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

    
332
            double minx = Double.POSITIVE_INFINITY;
333
            double miny = Double.POSITIVE_INFINITY;
334
            double minz = Double.POSITIVE_INFINITY;
335

    
336
            double maxx = Double.NEGATIVE_INFINITY;
337
            double maxy = Double.NEGATIVE_INFINITY;
338
            double maxz = Double.NEGATIVE_INFINITY;
339

    
340
            double x;
341
            double y;
342
            double z;
343

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

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

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

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

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

    
399
    }
400

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

    
412
    }
413

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

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

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

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

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

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

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

    
531
    }
532

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

    
541
    }
542

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

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

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

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

    
575
    }
576

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

    
581
        EWKTWriter writer; // = null;
582

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
783
    @Override
784
    public void rotate(double radAngle, double basex, double basey) {
785

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

    
791
    @Override
792
    public void move(double dx, double dy) {
793

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

    
799
    @Override
800
    public void scale(Point basePoint, double sx, double sy) {
801

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

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

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

    
824
        return (Geometry[]) points;
825
    }
826

    
827
    @Override
828
    public Geometry convexHull() throws GeometryOperationNotSupportedException, GeometryOperationException {
829
        return JTSUtils.createGeometry(this.getProjection(), getJTS().convexHull(), null);
830
    }
831

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

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

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

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

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

    
862
    }
863

    
864

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

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

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

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

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

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

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

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

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

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

    
933
    
934
}