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 / primitive / point / AbstractPoint.java @ 47432

History | View | Annotate | Download (11.9 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.primitive.point;
24

    
25
import com.vividsolutions.jts.geom.CoordinateSequence;
26
import java.awt.Shape;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.PathIterator;
29
import java.util.Arrays;
30
import org.cresques.cts.ICoordTrans;
31
import org.gvsig.fmap.geom.DirectPosition;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.GeometryException;
34
import org.gvsig.fmap.geom.aggregate.MultiLine;
35
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
36
import org.gvsig.fmap.geom.handler.AbstractHandler;
37
import org.gvsig.fmap.geom.handler.FinalHandler;
38
import org.gvsig.fmap.geom.handler.Handler;
39
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
40
import org.gvsig.fmap.geom.jts.mgeom.MCoordinate;
41
import org.gvsig.fmap.geom.jts.primitive.AbstractPrimitive;
42
import org.gvsig.fmap.geom.jts.primitive.Envelope2D;
43
import org.gvsig.fmap.geom.jts.util.JTSUtils;
44
import org.gvsig.fmap.geom.operation.GeometryOperationException;
45
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
46
import org.gvsig.fmap.geom.primitive.Envelope;
47
import org.gvsig.fmap.geom.primitive.Point;
48
import org.gvsig.tools.util.GetItem;
49
import org.gvsig.tools.util.IsEmpty;
50
import org.gvsig.tools.util.Size;
51

    
52
/**
53
 * @author fdiaz
54
 *
55
 */
56
public abstract class AbstractPoint extends AbstractPrimitive implements PointJTS, IsEmpty, Size, GetItem<Double> {
57

    
58
    /**
59
     *
60
     */
61
    private static final long serialVersionUID = -8378193151810866724L;
62
    protected com.vividsolutions.jts.geom.Coordinate coordinate;
63

    
64
    /**
65
    *
66
    */
67
    protected AbstractPoint(int subtype) {
68
        super(Geometry.TYPES.POINT, subtype);
69
    }
70

    
71
    /**
72
     *
73
     */
74
    public AbstractPoint(int subtype, com.vividsolutions.jts.geom.Coordinate coordinate) {
75
        this(subtype);
76
        this.coordinate = coordinate;
77
    }
78

    
79
    /*
80
     * (non-Javadoc)
81
     *
82
     * @see org.gvsig.fmap.geom.primitive.Point#getDirectPosition()
83
     */
84
    public DirectPosition getDirectPosition() {
85
        return new PointDirectPosition();
86
    }
87

    
88
    class PointDirectPosition implements DirectPosition {
89

    
90
        /*
91
         * (non-Javadoc)
92
         *
93
         * @see org.gvsig.fmap.geom.DirectPosition#getDimension()
94
         */
95
        public int getDimension() {
96
            return AbstractPoint.this.getDimension();
97
        }
98

    
99
        /*
100
         * (non-Javadoc)
101
         *
102
         * @see org.gvsig.fmap.geom.DirectPosition#getOrdinate(int)
103
         */
104
        public double getOrdinate(int dimension) {
105
            return AbstractPoint.this.getCoordinateAt(dimension);
106
        }
107

    
108
    }
109

    
110
    /*
111
     * (non-Javadoc)
112
     *
113
     * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
114
     */
115
    public void setCoordinateAt(int dimension, double value) {
116
        this.coordinate.setOrdinate(dimension, value);
117
    }
118

    
119
    /*
120
     * (non-Javadoc)
121
     *
122
     * @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
123
     */
124
    public void setCoordinates(double[] values) {
125
        for (int i = 0; i < values.length; i++) {
126
            this.coordinate.setOrdinate(i, values[i]);
127
        }
128
    }
129

    
130
  /*
131
     * (non-Javadoc)
132
     *
133
     * @see org.gvsig.fmap.geom.primitive.Point#setX(double)
134
     */
135
    public void setX(double x) {
136
        this.coordinate.x = x;
137
    }
138

    
139
    /*
140
     * (non-Javadoc)
141
     *
142
     * @see org.gvsig.fmap.geom.primitive.Point#setY(double)
143
     */
144
    public void setY(double y) {
145
        this.coordinate.y = y;
146
    }
147

    
148
    /*
149
     * (non-Javadoc)
150
     *
151
     * @see org.gvsig.fmap.geom.primitive.Point#getCoordinateAt(int)
152
     */
153
    public double getCoordinateAt(int dimension) {
154
        return this.coordinate.getOrdinate(dimension);
155
    }
156

    
157
    /*
158
     * (non-Javadoc)
159
     *
160
     * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
161
     */
162
    public double[] getCoordinates() {
163
        double[] coords = new double[this.getDimension()];
164
        coords[0] = getX();
165
        coords[1] = getY();
166
        if(this.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM2DM){
167
            coords[2] = this.getCoordinateAt(CoordinateSequence.M);
168
        } else if(this.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM3D){
169
            coords[2] = this.getCoordinateAt(CoordinateSequence.Z);
170
        } else if(this.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM3DM){
171
            coords[2] = this.getCoordinateAt(CoordinateSequence.Z);
172
            coords[3] = this.getCoordinateAt(CoordinateSequence.M);
173
        }
174
        return coords;
175
    }
176

    
177
    /*
178
     * (non-Javadoc)
179
     *
180
     * @see org.gvsig.fmap.geom.primitive.Point#getX()
181
     */
182
    public double getX() {
183
        return this.coordinate.x;
184
    }
185

    
186
    /*
187
     * (non-Javadoc)
188
     *
189
     * @see org.gvsig.fmap.geom.primitive.Point#getY()
190
     */
191
    public double getY() {
192
        return this.coordinate.y;
193
    }
194

    
195
    /*
196
     * (non-Javadoc)
197
     *
198
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
199
     */
200
    public com.vividsolutions.jts.geom.Geometry getJTS() {
201
        return JTSUtils.createJTSPoint(this.coordinate);
202
    }
203

    
204
    /*
205
     * (non-Javadoc)
206
     *
207
     * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
208
     */
209
    @Override
210
    public void reProject(ICoordTrans ct) {
211
        if (ct == null) {
212
            return;
213
        }
214
        java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(this.getX(), this.getY());
215
        try {
216
            p = ct.convert(p, p);
217
            this.setX(p.getX());
218
            this.setY(p.getY());
219
            this.setProjection(ct.getPDest());
220
        } catch (Exception exc) {
221
            /*
222
             * This can happen when the reprojection lib is unable
223
             * to reproject (for example the source point
224
             * is out of the valid range and some computing
225
             * problem happens)
226
             */
227
            this.setX(0);
228
            this.setY(0);
229
        }
230
    }
231

    
232
    /*
233
     * (non-Javadoc)
234
     *
235
     * @see
236
     * org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
237
     */
238
    @Override
239
    public void transform(AffineTransform at) {
240
        if (at == null) {
241
            return;
242
        }
243

    
244
        java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(this.getX(), this.getY());
245
        at.transform(p, p);
246
        setX(p.getX());
247
        setY(p.getY());
248
    }
249

    
250
    /*
251
     * (non-Javadoc)
252
     *
253
     * @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#getJTSCoordinates()
254
     */
255
    public com.vividsolutions.jts.geom.Coordinate getJTSCoordinate() {
256
        return this.coordinate;
257
    }
258

    
259
    /*
260
     * (non-Javadoc)
261
     *
262
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
263
     */
264
    public Shape getShape(AffineTransform affineTransform) {
265
        return new DefaultGeneralPathX(getPathIterator(affineTransform), false, 0);
266
    }
267

    
268
    /*
269
     * (non-Javadoc)
270
     *
271
     * @see org.gvsig.fmap.geom.Geometry#getShape()
272
     */
273
    public Shape getShape() {
274
        return new DefaultGeneralPathX(getPathIterator(null), false, 0);
275
    }
276

    
277
    /*
278
     * (non-Javadoc)
279
     *
280
     * @see
281
     * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
282
     * , double)
283
     */
284
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
285
        return getPathIterator(at);
286
    }
287

    
288
    /*
289
     * (non-Javadoc)
290
     *
291
     * @see org.gvsig.fmap.geom.Geometry#getHandlers(int)
292
     */
293
    public Handler[] getHandlers(int type) {
294
        return new Handler[] { new PointHandler() };
295
    }
296

    
297
    class PointHandler extends AbstractHandler implements FinalHandler {
298

    
299
        public PointHandler() {
300
            point = new java.awt.geom.Point2D.Double(AbstractPoint.this.getX(), AbstractPoint.this.getY());
301
            index = 0;
302
        }
303

    
304
        public void move(double movex, double movey) {
305
            AbstractPoint.this.setX(AbstractPoint.this.getX() + movex);
306
            AbstractPoint.this.setY(AbstractPoint.this.getY() + movey);
307
        }
308

    
309
        public void set(double setx, double sety) {
310
            AbstractPoint.this.setX(setx);
311
            AbstractPoint.this.setY(sety);
312
        }
313
    }
314

    
315
    public Envelope getEnvelope() {
316
        return new Envelope2D(this.getX(), this.getY(), this.getX(), this.getY(), this.getProjection());
317
    }
318

    
319

    
320
    /* (non-Javadoc)
321
     * @see org.gvsig.fmap.geom.Geometry#toLines()
322
     */
323
    public MultiLine toLines() throws GeometryException {
324
        String message = "Can't get lines from a point";
325
        notifyDeprecated(message);
326
        throw new UnsupportedOperationException(message);
327
    }
328

    
329
    /* (non-Javadoc)
330
     * @see org.gvsig.fmap.geom.Geometry#toPolygons()
331
     */
332
    public MultiPolygon toPolygons() throws GeometryException {
333
        String message = "Can't get polygons from a point";
334
        notifyDeprecated(message);
335
        throw new UnsupportedOperationException(message);
336
    }
337

    
338

    
339
    /* (non-Javadoc)
340
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
341
     */
342
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
343
        //Do nothing
344
    }
345

    
346
    public abstract String toString();
347

    
348
    /* (non-Javadoc)
349
     * @see org.gvsig.fmap.geom.Geometry#canBeTransformed(java.awt.geom.AffineTransform)
350
     */
351
    @Override
352
    public boolean canBeTransformed(AffineTransform at) {
353
        return true;
354
    }
355

    
356
    /* (non-Javadoc)
357
     * @see org.gvsig.fmap.geom.Geometry#canBeReprojected(org.cresques.cts.ICoordTrans)
358
     */
359
    @Override
360
    public boolean canBeReprojected(ICoordTrans ct) {
361
        return true;
362
    }
363

    
364
    @Override
365
    public int hashCode() {
366
        double v[];
367
        if( this.coordinate instanceof MCoordinate ) {
368
            v = new double[] {
369
                coordinate.x,
370
                coordinate.y,
371
                coordinate.z,
372
                ((MCoordinate)coordinate).m
373
            };
374
        } else {
375
            v = new double[] {
376
                coordinate.x,
377
                coordinate.y,
378
                coordinate.z,
379
                Double.NaN
380
            };
381
        }
382
        return Arrays.hashCode(v);
383
    }
384

    
385

    
386
    @Override
387
    public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException,
388
        GeometryOperationException {
389
        return (Point) this.cloneGeometry();
390
    }    
391

    
392
    @Override
393
    @SuppressWarnings("CloneDoesntCallSuperClone")
394
    public Point clone() throws CloneNotSupportedException {
395
        return this.cloneGeometry();
396
    }
397

    
398
    @Override
399
    public Point force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
400
        Point2D p = new Point2D(this.getProjection(), this.coordinate.x, this.coordinate.y);
401
        return p;
402
    }
403

    
404
    @Override
405
    public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
406
        return offset(distance);
407
    }
408

    
409

    
410
    @Override
411
    public boolean isEmpty() {
412
        return this.coordinate==null;
413
    }
414

    
415
    @Override
416
    public int size() {
417
        return this.getDimension();
418
    }
419

    
420
    @Override
421
    public Double get(int position) {
422
        return this.getCoordinateAt(position);
423
    }
424
}