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

History | View | Annotate | Download (10.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.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

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

    
52
/**
53
 * @author fdiaz
54
 *
55
 */
56
public abstract class AbstractPoint extends AbstractPrimitive implements PointJTS {
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
        for (int i = 0; i < this.getDimension(); i++) {
165
            coords[i] = this.coordinate.getOrdinate(i);
166
        }
167
        return coords;
168
    }
169

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

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

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

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

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

    
234
        java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(this.getX(), this.getY());
235
        at.transform(p, p);
236
        setX(p.getX());
237
        setY(p.getY());
238
    }
239

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

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

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

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

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

    
287
    class PointHandler extends AbstractHandler implements FinalHandler {
288

    
289
        public PointHandler() {
290
            point = new java.awt.geom.Point2D.Double(AbstractPoint.this.getX(), AbstractPoint.this.getY());
291
            index = 0;
292
        }
293

    
294
        public void move(double movex, double movey) {
295
            AbstractPoint.this.setX(AbstractPoint.this.getX() + movex);
296
            AbstractPoint.this.setY(AbstractPoint.this.getY() + movey);
297
        }
298

    
299
        public void set(double setx, double sety) {
300
            AbstractPoint.this.setX(setx);
301
            AbstractPoint.this.setY(sety);
302
        }
303
    }
304

    
305
    public Envelope getEnvelope() {
306
        return new Envelope2D(this.getX(), this.getY(), this.getX(), this.getY());
307
    }
308

    
309

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

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

    
328

    
329
    /* (non-Javadoc)
330
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
331
     */
332
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
333
        //Do nothing
334
    }
335

    
336
    public abstract String toString();
337

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

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

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

    
375

    
376
    @Override
377
    public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException,
378
        GeometryOperationException {
379
        return (Point) this.cloneGeometry();
380
    }    
381
}