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

History | View | Annotate | Download (10.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 java.awt.Shape;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.PathIterator;
28
import java.util.Arrays;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.cts.IProjection;
32

    
33
import org.gvsig.fmap.geom.DirectPosition;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryException;
36
import org.gvsig.fmap.geom.aggregate.MultiLine;
37
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
38
import org.gvsig.fmap.geom.exception.CreateGeometryException;
39
import org.gvsig.fmap.geom.exception.ReprojectionRuntimeException;
40
import org.gvsig.fmap.geom.handler.AbstractHandler;
41
import org.gvsig.fmap.geom.handler.FinalHandler;
42
import org.gvsig.fmap.geom.handler.Handler;
43
import org.gvsig.fmap.geom.jts.MCoordinate;
44
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
45
import org.gvsig.fmap.geom.jts.primitive.AbstractPrimitive;
46
import org.gvsig.fmap.geom.jts.primitive.Envelope2D;
47
import org.gvsig.fmap.geom.jts.util.JTSUtils;
48
import org.gvsig.fmap.geom.operation.GeometryOperationException;
49
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
50
import org.gvsig.fmap.geom.primitive.Envelope;
51
import org.gvsig.fmap.geom.primitive.Point;
52

    
53
/**
54
 * @author fdiaz
55
 *
56
 */
57
public abstract class AbstractPoint extends AbstractPrimitive implements PointJTS {
58

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

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

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

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

    
89
    class PointDirectPosition implements DirectPosition {
90

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

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

    
109
    }
110

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

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

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

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

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

    
158
    /*
159
     * (non-Javadoc)
160
     *
161
     * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
162
     */
163
    public double[] getCoordinates() {
164
        double[] coords = new double[this.getDimension()];
165
        for (int i = 0; i < this.getDimension(); i++) {
166
            coords[i] = this.coordinate.getOrdinate(i);
167
        }
168
        return coords;
169
    }
170

    
171
    /*
172
     * (non-Javadoc)
173
     *
174
     * @see org.gvsig.fmap.geom.primitive.Point#getX()
175
     */
176
    public double getX() {
177
        return this.coordinate.x;
178
    }
179

    
180
    /*
181
     * (non-Javadoc)
182
     *
183
     * @see org.gvsig.fmap.geom.primitive.Point#getY()
184
     */
185
    public double getY() {
186
        return this.coordinate.y;
187
    }
188

    
189
    /*
190
     * (non-Javadoc)
191
     *
192
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
193
     */
194
    public com.vividsolutions.jts.geom.Geometry getJTS() {
195
        return JTSUtils.createJTSPoint(this.coordinate);
196
    }
197

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

    
226
    /*
227
     * (non-Javadoc)
228
     *
229
     * @see
230
     * org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
231
     */
232
    @Override
233
    public void transform(AffineTransform at) {
234
        if (at == null) {
235
            return;
236
        }
237

    
238
        java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(this.getX(), this.getY());
239
        at.transform(p, p);
240
        setX(p.getX());
241
        setY(p.getY());
242
    }
243

    
244
    /*
245
     * (non-Javadoc)
246
     *
247
     * @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#getJTSCoordinates()
248
     */
249
    public com.vividsolutions.jts.geom.Coordinate getJTSCoordinate() {
250
        return this.coordinate;
251
    }
252

    
253
    /*
254
     * (non-Javadoc)
255
     *
256
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
257
     */
258
    public Shape getShape(AffineTransform affineTransform) {
259
        return new DefaultGeneralPathX(getPathIterator(affineTransform), false, 0);
260
    }
261

    
262
    /*
263
     * (non-Javadoc)
264
     *
265
     * @see org.gvsig.fmap.geom.Geometry#getShape()
266
     */
267
    public Shape getShape() {
268
        return new DefaultGeneralPathX(getPathIterator(null), false, 0);
269
    }
270

    
271
    /*
272
     * (non-Javadoc)
273
     *
274
     * @see
275
     * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
276
     * , double)
277
     */
278
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
279
        return getPathIterator(at);
280
    }
281

    
282
    /*
283
     * (non-Javadoc)
284
     *
285
     * @see org.gvsig.fmap.geom.Geometry#getHandlers(int)
286
     */
287
    public Handler[] getHandlers(int type) {
288
        return new Handler[] { new PointHandler() };
289
    }
290

    
291
    class PointHandler extends AbstractHandler implements FinalHandler {
292

    
293
        public PointHandler() {
294
            point = new java.awt.geom.Point2D.Double(AbstractPoint.this.getX(), AbstractPoint.this.getY());
295
            index = 0;
296
        }
297

    
298
        public void move(double movex, double movey) {
299
            AbstractPoint.this.setX(AbstractPoint.this.getX() + movex);
300
            AbstractPoint.this.setY(AbstractPoint.this.getY() + movey);
301
        }
302

    
303
        public void set(double setx, double sety) {
304
            AbstractPoint.this.setX(setx);
305
            AbstractPoint.this.setY(sety);
306
        }
307
    }
308

    
309
    public Envelope getEnvelope() {
310
        return new Envelope2D(this.getX(), this.getY(), this.getX(), this.getY());
311
    }
312

    
313

    
314
    /* (non-Javadoc)
315
     * @see org.gvsig.fmap.geom.Geometry#toLines()
316
     */
317
    public MultiLine toLines() throws GeometryException {
318
        String message = "Can't get lines from a point";
319
        notifyDeprecated(message);
320
        throw new UnsupportedOperationException(message);
321
    }
322

    
323
    /* (non-Javadoc)
324
     * @see org.gvsig.fmap.geom.Geometry#toPolygons()
325
     */
326
    public MultiPolygon toPolygons() throws GeometryException {
327
        String message = "Can't get polygons from a point";
328
        notifyDeprecated(message);
329
        throw new UnsupportedOperationException(message);
330
    }
331

    
332

    
333
    /* (non-Javadoc)
334
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
335
     */
336
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
337
        //Do nothing
338
    }
339

    
340
    public abstract String toString();
341

    
342
    /* (non-Javadoc)
343
     * @see org.gvsig.fmap.geom.Geometry#canBeTransformed(java.awt.geom.AffineTransform)
344
     */
345
    @Override
346
    public boolean canBeTransformed(AffineTransform at) {
347
        return true;
348
    }
349

    
350
    /* (non-Javadoc)
351
     * @see org.gvsig.fmap.geom.Geometry#canBeReprojected(org.cresques.cts.ICoordTrans)
352
     */
353
    @Override
354
    public boolean canBeReprojected(ICoordTrans ct) {
355
        return true;
356
    }
357

    
358
    @Override
359
    public int hashCode() {
360
        double v[];
361
        if( this.coordinate instanceof MCoordinate ) {
362
            v = new double[] {
363
                coordinate.x,
364
                coordinate.y,
365
                coordinate.z,
366
                ((MCoordinate)coordinate).m
367
            };
368
        } else {
369
            v = new double[] {
370
                coordinate.x,
371
                coordinate.y,
372
                coordinate.z,
373
                Double.NaN
374
            };
375
        }
376
        return Arrays.hashCode(v);
377
    }
378

    
379

    
380
    @Override
381
    public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException,
382
        GeometryOperationException {
383
        return (Point) this.cloneGeometry();
384
    }    
385

    
386
    @Override
387
    @SuppressWarnings("CloneDoesntCallSuperClone")
388
    public Point clone() throws CloneNotSupportedException {
389
        return this.cloneGeometry();
390
    }
391

    
392
    @Override
393
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
394
        Point2D p = new Point2D(this.getProjection(), this.coordinate.x, this.coordinate.y);
395
        return p;
396
    }
397

    
398
}