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

History | View | Annotate | Download (25.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.cresques.cts.IProjection;
38

    
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.geom.GeometryLocator;
44
import org.gvsig.fmap.geom.GeometryManager;
45
import org.gvsig.fmap.geom.exception.CreateGeometryException;
46
import org.gvsig.fmap.geom.jts.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.JTSUtils;
53
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
54
import org.gvsig.fmap.geom.operation.GeometryOperationException;
55
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
56
import org.gvsig.fmap.geom.primitive.Envelope;
57
import org.gvsig.fmap.geom.primitive.OrientableCurve;
58
import org.gvsig.fmap.geom.primitive.OrientableSurface;
59
import org.gvsig.fmap.geom.primitive.Point;
60
import org.gvsig.fmap.geom.type.GeometryType;
61

    
62
/**
63
 * @author fdiaz
64
 *
65
 */
66
public abstract class AbstractGeometry implements GeometryJTS {
67

    
68
    protected static final Logger logger = LoggerFactory.getLogger(AbstractGeometry.class);
69

    
70
    /**
71
     *
72
     */
73
    private static final long serialVersionUID = 4999326772576222293L;
74

    
75
    private final GeometryType geometryType;
76

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

    
91
    public GeometryType getGeometryType(){
92
        return geometryType;
93
    }
94

    
95
    protected GeometryManager getManager() {
96
        return GeometryLocator.getGeometryManager();
97
    }
98

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

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

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

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

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

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

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

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

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

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

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

    
211
    public boolean coveredBy(Geometry geometry) throws GeometryOperationNotSupportedException,
212
        GeometryOperationException {
213

    
214
        return getJTS().coveredBy(((GeometryJTS) geometry).getJTS());
215
    }
216

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

    
221
    }
222

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

    
228
    public boolean intersects(Geometry geometry) throws GeometryOperationNotSupportedException,
229
        GeometryOperationException {
230
        return getJTS().intersects(((GeometryJTS) geometry).getJTS());
231
    }
232

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

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

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

    
249
    public double area() throws GeometryOperationNotSupportedException, GeometryOperationException {
250
        return getJTS().getArea();
251
    }
252

    
253
    public double perimeter() throws GeometryOperationNotSupportedException, GeometryOperationException {
254
        return getJTS().getLength();
255
    }
256

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

    
268
        return fastIntersects(x, y, w, h);
269
    }
270

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

    
287
        return getJTS().intersects(rect);
288
    }
289

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

    
299
            double minx = Double.POSITIVE_INFINITY;
300
            double miny = Double.POSITIVE_INFINITY;
301
            double minz = Double.POSITIVE_INFINITY;
302

    
303
            double maxx = Double.NEGATIVE_INFINITY;
304
            double maxy = Double.NEGATIVE_INFINITY;
305
            double maxz = Double.NEGATIVE_INFINITY;
306

    
307
            double x;
308
            double y;
309
            double z;
310

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

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

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

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

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

    
363
    }
364

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

    
375
    }
376

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

    
386
    public byte[] convertToWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
387
            try {
388
                        return new OGCWKBEncoder().encode(this);
389
                } catch (Exception e) {
390
                        throw new GeometryOperationException(e);
391
                }
392

    
393
    }
394

    
395
    public byte[] convertToWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException {
396
            /*
397
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
398
             */
399

    
400
            try {
401
                        return new OGCWKBEncoder().encode(this);
402
                } catch (Exception e) {
403
                        throw new GeometryOperationException(e);
404
                }
405
    }
406

    
407
    public byte[] convertToWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException,
408
        GeometryOperationException {
409
            /*
410
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
411
             */
412
            Geometry geom = this;
413
        if(this.getType() != type){
414
            com.vividsolutions.jts.geom.Geometry jts = getJTS();
415
            jts = JTSUtils.convertTypes(jts, this.getType(), type);
416

    
417
            geom = JTSUtils.createGeometry(jts);
418
        }
419
            try {
420
                        return new OGCWKBEncoder().encode(geom);
421
                } catch (Exception e) {
422
                        throw new GeometryOperationException(e);
423
                }
424

    
425
    }
426

    
427

    
428

    
429
    public byte[] convertToEWKB() throws GeometryOperationNotSupportedException, GeometryOperationException {
430
            try {
431
                        return new PostGISEWKBEncoder().encode(this);
432
                } catch (Exception e) {
433
                        throw new GeometryOperationException(e);
434
                }
435

    
436
    }
437

    
438
    public byte[] convertToEWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException {
439
            /*
440
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
441
             */
442

    
443
            try {
444
                        return new PostGISEWKBEncoder().encode(this);
445
                } catch (Exception e) {
446
                        throw new GeometryOperationException(e);
447
                }
448
    }
449

    
450
    public byte[] convertToEWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException,
451
        GeometryOperationException {
452
            /*
453
             * No se sabe si la especificaci?n de OGC soporta SRS. OGCWKBEncoder no.
454
             */
455
            Geometry geom = this;
456
        if(this.getType() != type){
457
            com.vividsolutions.jts.geom.Geometry jts = getJTS();
458
            jts = JTSUtils.convertTypes(jts, this.getType(), type);
459

    
460
            geom = JTSUtils.createGeometry(jts);
461
        }
462
            try {
463
                        return new PostGISEWKBEncoder().encode(geom);
464
                } catch (Exception e) {
465
                        throw new GeometryOperationException(e);
466
                }
467

    
468
    }
469

    
470
    /*
471
     * (non-Javadoc)
472
     *
473
     * @see org.gvsig.fmap.geom.Geometry#convertToWKT()
474
     */
475
    public String convertToWKT() throws GeometryOperationNotSupportedException, GeometryOperationException {
476
        int subType = getGeometryType().getSubType();
477

    
478
        EWKTWriter writer = null;
479

    
480
        switch (subType) {
481
        case Geometry.SUBTYPES.GEOM3D:
482
            writer = new EWKTWriter(3, false);
483
            break;
484
        case Geometry.SUBTYPES.GEOM2DM:
485
            writer = new EWKTWriter(3, true);
486
            break;
487
        case Geometry.SUBTYPES.GEOM3DM:
488
            writer = new EWKTWriter(4, true);
489
            break;
490

    
491
        default:
492
            writer = new EWKTWriter(2, false);
493
            break;
494
        }
495
        com.vividsolutions.jts.geom.Geometry jts = getJTS();
496
        return writer.write(jts);
497
    }
498

    
499
    /*
500
     * (non-Javadoc)
501
     *
502
     * @see org.gvsig.fmap.geom.Geometry#buffer(double)
503
     */
504
    public org.gvsig.fmap.geom.Geometry buffer(double distance) throws GeometryOperationNotSupportedException,
505
        GeometryOperationException {
506
        return JTSUtils.createGeometry(getJTS().buffer(distance));
507
    }
508

    
509
    /*
510
     * (non-Javadoc)
511
     *
512
     * @see org.gvsig.fmap.geom.Geometry#snapTo(org.gvsig.fmap.geom.Geometry,
513
     * double)
514
     */
515
    public org.gvsig.fmap.geom.Geometry snapTo(org.gvsig.fmap.geom.Geometry other, double snapTolerance)
516
        throws GeometryOperationNotSupportedException, GeometryOperationException {
517
        Geometry result = null;
518
        GeometrySnapper snapper = new GeometrySnapper(getJTS());
519
        com.vividsolutions.jts.geom.Geometry jts_result = snapper.snapTo(((GeometryJTS) other).getJTS(), snapTolerance);
520
        result = JTSUtils.createGeometry(jts_result);
521
        return result;
522
    }
523

    
524
    /*
525
     * (non-Javadoc)
526
     *
527
     * @see org.gvsig.fmap.geom.Geometry#getInteriorPoint()
528
     */
529
    public Point getInteriorPoint() throws GeometryOperationNotSupportedException, GeometryOperationException {
530

    
531
        com.vividsolutions.jts.geom.Geometry geometry = getJTS();
532
        com.vividsolutions.jts.geom.Point point = geometry.getInteriorPoint();
533
        Geometry result = JTSUtils.createGeometry(point);
534
        return (Point) result;
535
    }
536

    
537
    /*
538
     * (non-Javadoc)
539
     *
540
     * @see org.gvsig.fmap.geom.Geometry#isValid()
541
     */
542
    public boolean isValid() {
543
        return getJTS().isValid();
544
    }
545

    
546
    /*
547
     * (non-Javadoc)
548
     *
549
     * @see org.gvsig.fmap.geom.Geometry#getValidationStatus()
550
     */
551
    public ValidationStatus getValidationStatus() {
552
        DefaultValidationStatus status = new DefaultValidationStatus(ValidationStatus.VALID, null);
553
        com.vividsolutions.jts.geom.Geometry jtsgeom = null;
554
        try {
555
            jtsgeom = this.getJTS();
556
            IsValidOp validOp = new IsValidOp(jtsgeom);
557
            if (validOp != null) {
558
                status.setValidationError(validOp.getValidationError());
559
            }
560
        } catch (Exception ex) {
561
            status.setStatusCode(ValidationStatus.CURRUPTED);
562
            status.setMesage("The geometry is corrupted.");
563
            if (this instanceof OrientableSurface) {
564
                int vertices = ((OrientableSurface) this).getNumVertices();
565
                if (vertices < 3) {
566
                    status.setStatusCode(ValidationStatus.TOO_FEW_POINTS);
567
                    status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]);
568
                }
569
            } else if (this instanceof OrientableCurve) {
570
                int vertices = ((OrientableCurve) this).getNumVertices();
571
                if (vertices < 2) {
572
                    status.setStatusCode(ValidationStatus.TOO_FEW_POINTS);
573
                    status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]);
574
                }
575
            }
576
        }
577
        return status;
578
    }
579

    
580
    /*
581
     * (non-Javadoc)
582
     *
583
     * @see org.gvsig.fmap.geom.Geometry#makeValid()
584
     */
585
    public org.gvsig.fmap.geom.Geometry makeValid() {
586
        try {
587
            ValidationStatus vs = this.getValidationStatus();
588
            if (vs.isValid()) {
589
                return this;
590
            }
591
            Geometry g = null;
592
            switch (vs.getStatusCode()) {
593
            case Geometry.ValidationStatus.RING_SELF_INTERSECTION:
594
            case Geometry.ValidationStatus.SELF_INTERSECTION:
595
                g = this.buffer(0);
596
                if (g.isValid()) {
597
                    return g;
598
                }
599
                break;
600

    
601
            case Geometry.ValidationStatus.TOO_FEW_POINTS:
602
                if (this instanceof OrientableCurve) {
603
                    int vertices = ((OrientableCurve) this).getNumVertices();
604
                    if (vertices < 2) {
605
                        return null; // new
606
                                     // DefaultNullGeometry(this.getGeometryType());
607
                    }
608
                }
609
                if (this instanceof OrientableSurface) {
610
                    int vertices = ((OrientableSurface) this).getNumVertices();
611
                    if (vertices < 3) {
612
                        return null; // new
613
                                     // DefaultNullGeometry(this.getGeometryType());
614
                    }
615
                }
616
            }
617
        } catch (Exception ex) {
618
            return null;
619
        }
620
        return null;
621
    }
622

    
623
    /*
624
     * (non-Javadoc)
625
     *
626
     * @see org.gvsig.fmap.geom.Geometry#getBounds2D()
627
     */
628
    public Rectangle2D getBounds2D() {
629
        com.vividsolutions.jts.geom.Envelope envInternal = getJTS().getEnvelopeInternal();
630
        return new Rectangle2D.Double(envInternal.getMinX(), envInternal.getMinY(), envInternal.getWidth(),
631
            envInternal.getHeight());
632
    }
633

    
634
    /*
635
     * (non-Javadoc)
636
     *
637
     * @see java.awt.Shape#getBounds()
638
     */
639
    public Rectangle getBounds() {
640
        return this.getShape().getBounds();
641
    }
642

    
643
    /*
644
     * (non-Javadoc)
645
     *
646
     * @see org.gvsig.fmap.geom.Geometry#getInternalShape()
647
     */
648
    public Shape getInternalShape() {
649
        return getShape();
650
    }
651

    
652
    public void rotate(double radAngle, double basex, double basey) {
653

    
654
        AffineTransform at = new AffineTransform();
655
        at.rotate(radAngle, basex, basey);
656
        this.transform(at);
657
    }
658

    
659
    public void move(double dx, double dy) {
660

    
661
        AffineTransform at = new AffineTransform();
662
        at.translate(dx, dy);
663
        this.transform(at);
664
    }
665

    
666
    public void scale(Point basePoint, double sx, double sy) {
667

    
668
        AffineTransform at = new AffineTransform();
669
        at.setToTranslation(basePoint.getX(), basePoint.getY());
670
        at.scale(sx, sy);
671
        at.translate(-basePoint.getX(), -basePoint.getY());
672
        this.transform(at);
673
    }
674

    
675
    public Geometry[] closestPoints(Geometry other) throws GeometryOperationNotSupportedException,
676
        GeometryOperationException {
677
        Point[] points = null;
678

    
679
        Coordinate[] jts_points = DistanceOp.nearestPoints(getJTS(), ((GeometryJTS) other).getJTS());
680
        points = new Point[jts_points.length];
681
        for (int i = 0; i < jts_points.length; i++) {
682
            try {
683
                points[i] = JTSUtils.createPoint(this.getGeometryType(), jts_points[i]);
684
            } catch (CreateGeometryException e) {
685
                throw new GeometryOperationException(e);
686
            }
687
        }
688

    
689
        return (Geometry[]) points;
690
    }
691

    
692
    public Geometry convexHull() throws GeometryOperationNotSupportedException, GeometryOperationException {
693

    
694
        return JTSUtils.createGeometry(getJTS().convexHull());
695
    }
696

    
697
    public Geometry difference(Geometry other) throws GeometryOperationNotSupportedException,
698
        GeometryOperationException {
699
        return JTSUtils.createGeometry(getJTS().difference(((GeometryJTS) other).getJTS()));
700
    }
701

    
702
    public Geometry intersection(Geometry other) throws GeometryOperationNotSupportedException,
703
        GeometryOperationException {
704
        return JTSUtils.createGeometry(getJTS().intersection(((GeometryJTS) other).getJTS()));
705
    }
706

    
707
    public Geometry union(Geometry other) throws GeometryOperationNotSupportedException, GeometryOperationException {
708
        return JTSUtils.createGeometry(getJTS().union(((GeometryJTS) other).getJTS()));
709
    }
710

    
711
    public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException,
712
        GeometryOperationException {
713
        try {
714
            return JTSUtils.createPoint(this.getGeometryType(), getJTS().getCentroid().getCoordinate());
715
        } catch (CreateGeometryException e) {
716
            throw new GeometryOperationException(e);
717
        }
718
    }
719

    
720
    protected void notifyDeprecated(String message) {
721
        logger.info(message);
722

    
723
    }
724

    
725

    
726
    /* (non-Javadoc)
727
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#ensureOrientation(boolean)
728
     */
729
    public boolean ensureOrientation(boolean ccw) throws GeometryOperationNotSupportedException, GeometryOperationException {
730
        if(ccw!=isCCW()){
731
            flip();
732
            return true;
733
        }
734
        return false;
735
    }
736

    
737
    /* (non-Javadoc)
738
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#out(org.gvsig.fmap.geom.Geometry)
739
     */
740
    public boolean out(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException {
741
        GeometryJTS otherJtsGeom = (GeometryJTS)geometry;
742
        return (!contains(otherJtsGeom) && !intersects(otherJtsGeom));
743
    }
744

    
745
    /* (non-Javadoc)
746
     * @see java.lang.Object#equals(java.lang.Object)
747
     */
748
    @Override
749
    public boolean equals(Object obj) {
750
        if (obj instanceof GeometryJTS) {
751
            return this.getJTS().equals(((GeometryJTS) obj).getJTS());
752
        }
753
        return false;
754
    }
755

    
756
    public String toString(){
757
        return this.getGeometryType().getFullName();
758
    }
759

    
760
    @Override
761
    public IProjection getProjection() {
762
        return this.projection;
763
    }
764

    
765
    @Override
766
    public void setProjectionIffNull(IProjection projection) {
767
        if( this.projection==null ) {
768
            this.projection = projection;
769
        }
770
    }
771

    
772
    @Override
773
    public void setProjection(IProjection projection) {
774
        this.projection = projection;
775
    }
776

    
777
}