Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / primitive / impl / Point2D.java @ 40435

History | View | Annotate | Download (9.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.fmap.geom.primitive.impl;
23

    
24
import java.awt.Rectangle;
25
import java.awt.Shape;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.PathIterator;
28
import java.awt.geom.Rectangle2D;
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.exception.ReprojectionRuntimeException;
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.primitive.Envelope;
40
import org.gvsig.fmap.geom.primitive.FShape;
41
import org.gvsig.fmap.geom.primitive.GeneralPathX;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.geom.primitive.PointIterator;
44
import org.gvsig.fmap.geom.type.GeometryType;
45

    
46
/**
47
 * 2D point implementation.
48
 * 
49
 * @author Vicente Caballero Navarro
50
 * @author gvSIG team
51
 */
52
public class Point2D extends AbstractPrimitive implements Point, DirectPosition {
53
        
54
        private static final long serialVersionUID = 1836257305083881299L;
55

    
56
    protected static final String COORDINATES_FIELD = "coordinates";
57
    protected double x;
58
    protected double y;
59

    
60
    /**
61
     * The constructor with the GeometryType like and argument
62
     * is used by the {@link GeometryType}{@link #create()} to create the
63
     * geometry
64
     * 
65
     * @param type
66
     *            The geometry type
67
     */
68
    public Point2D(GeometryType geometryType) {
69
            super(geometryType, null, null);
70
        this.x = 0.0d;
71
        this.y = 0.0d;
72
    }
73

    
74
    /**
75
     * Constructor used in the {@link Geometry#cloneGeometry()} method
76
     */
77
    Point2D(GeometryType geometryType, String id, IProjection projection,
78
        double x, double y) {
79
        super(geometryType, id, projection);
80
        this.x = x;
81
        this.y = y;
82
    }
83

    
84
    public Point2D(double x, double y) {
85
        super(TYPES.POINT, SUBTYPES.GEOM2D);
86
        this.x = x;
87
        this.y = y;
88
    }
89

    
90
    public Point2D(GeometryType geometryType, double x, double y) {
91
        super(geometryType, null, null);
92
        this.x = x;
93
        this.y = y;
94
    }
95

    
96
    public Point2D(int type, int subtype) {
97
        super(type, subtype);
98
        this.x = 0.0d;
99
        this.y = 0.0d;
100
    }
101

    
102
    public Point2D(java.awt.geom.Point2D point) {
103
        super(TYPES.POINT, SUBTYPES.GEOM2D);
104
        this.x = point.getX();
105
        this.y = point.getY();
106
    }
107

    
108
    /**
109
     * Aplica la transformaci?nn de la matriz de transformaci?n que se pasa como
110
     * par?metro.
111
     * 
112
     * @param at
113
     *            Matriz de transformaci?n.
114
     */
115
    public void transform(AffineTransform at) {
116
        
117
        if (at == null) {
118
            return;
119
        }
120
        
121
        double[] coordinates = getCoordinates();
122
        at.transform(coordinates, 0, coordinates, 0, 1);
123
        setCoordinates(coordinates);
124
    }
125

    
126
    public boolean contains(double x, double y) {
127
        if ((this.x == x) || (this.y == y)) {
128
            return true;
129
        } else {
130
            return false;
131
        }
132
    }
133

    
134
    public boolean contains(double x, double y, double w, double h) {
135
        return false;
136
    }
137

    
138
    public boolean intersects(double x, double y, double w, double h) {
139
        Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h);
140

    
141
        return rAux.contains(this.x, this.y);
142
    }
143

    
144
    public Rectangle getBounds() {
145
        return new Rectangle((int) x, (int) y, 0, 0);
146
    }
147

    
148
    public double getX() {
149
        return x;
150
    }
151

    
152
    public double getY() {
153
        return y;
154
    }
155

    
156
    public boolean contains(java.awt.geom.Point2D p) {
157
        return false;
158
    }
159

    
160
    public Rectangle2D getBounds2D() {
161
        return new Rectangle2D.Double(x - 0.01, y - 0.01, 0.02, 0.02);
162
    }
163

    
164
    public boolean contains(Rectangle2D r) {
165
        return false;
166
    }
167

    
168
    public boolean intersects(Rectangle2D r) {
169
        return r.contains(x, y);
170
    }
171

    
172
    public Shape getShape(AffineTransform affineTransform) {
173
        return this;
174
    }
175

    
176
    public PathIterator getPathIterator(AffineTransform at) {
177
        return new PointIterator(getPoint2D(), at);
178
    }
179

    
180
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
181
        return new PointIterator(getPoint2D(), at);
182
    }
183

    
184
    private java.awt.geom.Point2D getPoint2D() {
185
        return new java.awt.geom.Point2D.Double(x, y);
186
    }
187

    
188
    public int getShapeType() {
189
        return TYPES.POINT;
190
    }
191

    
192
    public FShape cloneFShape() {
193
        return new Point2D(getGeometryType(), id, projection, x, y);
194
    }
195

    
196
    public void reProject(ICoordTrans ct) {
197
        java.awt.geom.Point2D p = getPoint2D();
198
        
199
        try {
200
            p = ct.convert(p, p);
201
            this.x = p.getX();
202
            this.y = p.getY();
203
        } catch (Exception exc) {
204
            /*
205
             * This can happen when the reprojection lib is unable
206
             * to reproject (for example the source point
207
             * is out of the valid range and some computing
208
             * problem happens)
209
             */
210
            throw new ReprojectionRuntimeException(
211
                ct.getPOrig(), ct.getPDest(), getPoint2D(), exc);
212
        }
213
    }
214

    
215
    public Handler[] getStretchingHandlers() {
216
        return new Handler[] { new PointHandler(0, x, y, this) };
217
    }
218

    
219
    public Handler[] getSelectHandlers() {
220
        return new Handler[] { new PointHandler(0, x, y, this) };
221
    }
222

    
223
    /**
224
     * 
225
     * @author Vicente Caballero Navarro
226
     * @author gvSIG team
227
     */
228
    class PointHandler extends AbstractHandler implements FinalHandler {
229

    
230
        private final Point gvSIGPoint;
231

    
232
        public PointHandler(int i, double x, double y, Point gvSIGPoint) {
233
            this.gvSIGPoint = gvSIGPoint;
234
            point = new java.awt.geom.Point2D.Double(x, y);
235
            index = i;
236
        }
237

    
238
        public void move(double movex, double movey) {
239
            gvSIGPoint.setX(gvSIGPoint.getX() + movex);
240
            gvSIGPoint.setY(gvSIGPoint.getY() + movey);
241
        }
242

    
243
        public void set(double setx, double sety) {
244
            gvSIGPoint.setX(setx);
245
            gvSIGPoint.setY(sety);
246
        }
247
    }
248

    
249
    public int getDimension() {
250
        return 2;
251
    }
252

    
253
    public Envelope getEnvelope() {
254
        return new Envelope2D(x - 0.01, y - 0.01, x + 0.02, y + 0.02);
255
    }
256

    
257
    public GeneralPathX getGeneralPath() {
258
        return null;
259
    }
260

    
261
    public DirectPosition getDirectPosition() {
262
        return this;
263
    }
264

    
265
    public double getOrdinate(int dim) {
266
        if (dim == Geometry.DIMENSIONS.X) {
267
            return getX();
268
        } else
269
            if (dim == Geometry.DIMENSIONS.Y) {
270
                return getY();
271
            } else {
272
                return 0;
273
            }
274
    }
275

    
276
    public boolean equals(Object other) {
277
        if (!super.equals(other)) {
278
            return false;
279
        }
280
        Point2D pother = (Point2D) other;
281
        if (Math.abs(this.getX() - pother.getX()) > 0.0000001) {
282
            return false;
283
        }
284
        if (Math.abs(this.getY() - pother.getY()) > 0.0000001) {
285
            return false;
286
        }
287
        return true;
288

    
289
    }
290

    
291
    public double[] getCoordinates() {
292
        return new double[] { x, y };
293
    }
294

    
295
    public double getCoordinateAt(int dimension) {
296
        switch (dimension) {
297
        case Geometry.DIMENSIONS.X:
298
            return x;
299
        case Geometry.DIMENSIONS.Y:
300
            return y;
301
        default:
302
            throw new ArrayIndexOutOfBoundsException(dimension);
303
        }
304
    }
305

    
306
    public void setCoordinateAt(int dimension, double value) {
307
        switch (dimension) {
308
        case Geometry.DIMENSIONS.X:
309
            this.x = value;
310
            break;
311
        case Geometry.DIMENSIONS.Y:
312
            this.y = value;
313
            break;
314
        default:
315
            throw new ArrayIndexOutOfBoundsException(dimension);
316
        }
317
    }
318

    
319
    public void setCoordinates(double[] values) {
320
        this.x = values[Geometry.DIMENSIONS.X];
321
        this.y = values[Geometry.DIMENSIONS.Y];
322
    }
323

    
324
    public void setX(double x) {
325
        this.x = x;
326
    }
327

    
328
    public void setY(double y) {
329
        this.y = y;
330
    }
331

    
332
    public String toString() {
333
        StringBuffer buffer = new StringBuffer();
334
        double[] coordinates = getCoordinates();
335
        buffer.append(getFullTypeName()).append("(").append(coordinates[0]);
336
        for (int i = 1; i < coordinates.length; i++) {
337
            buffer.append(",").append(coordinates[i]);
338
        }
339
        buffer.append(")");
340
        return buffer.toString();
341
    }
342

    
343
    protected String getFullTypeName() {
344
        return "Point2D";
345
    }
346
    
347
    public int getType() {
348
        return TYPES.POINT;
349
    }
350
}