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 / Point2D.java @ 44612

History | View | Annotate | Download (7.48 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.geom.AffineTransform;
26
import java.awt.geom.PathIterator;
27

    
28
import com.vividsolutions.jts.geom.Coordinate;
29
import org.cresques.cts.IProjection;
30

    
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.GeometryException;
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.aggregate.MultiPoint;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D;
36
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
37
import org.gvsig.fmap.geom.jts.gputils.GeneralPathXIterator;
38
import org.gvsig.fmap.geom.jts.primitive.curve.circumference.Circumference2D;
39
import org.gvsig.fmap.geom.operation.GeometryOperationException;
40
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
41
import org.gvsig.fmap.geom.primitive.GeneralPathX;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.geom.type.GeometryType;
44

    
45
/**
46
 * @author fdiaz
47
 *
48
 */
49
public class Point2D extends AbstractPoint {
50

    
51
    /**
52
     *
53
     */
54
    private static final long serialVersionUID = -5486980182751169991L;
55

    
56
    /**
57
     *
58
     */
59
    public Point2D(IProjection proj, Coordinate coordinates) {
60
        super(Geometry.SUBTYPES.GEOM2D, coordinates);
61
        this.setProjection(proj);
62
    }
63

    
64
    /**
65
     *
66
     */
67
    public Point2D() {
68
        this(null, new Coordinate(0, 0));
69
    }
70

    
71
    /**
72
    *
73
    */
74
    public Point2D(double x, double y) {
75
        this(null, new Coordinate(x, y));
76
    }
77

    
78
    public Point2D(IProjection proj, double x, double y) {
79
        this(proj, new Coordinate(x, y));
80
    }
81

    
82
    /*
83
     * (non-Javadoc)
84
     *
85
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
86
     */
87
    public Point cloneGeometry() {
88
        return new Point2D(this.getProjection(), (Coordinate) this.coordinate.clone());
89
    }
90

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

    
100
    /*
101
     * (non-Javadoc)
102
     *
103
     * @see org.gvsig.fmap.geom.Geometry#getGeometryType()
104
     */
105
    public GeometryType getGeometryType() {
106
        try {
107
            return GeometryLocator.getGeometryManager().getGeometryType(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
108
        } catch (Exception e) {
109
            return null;
110
        }
111
    }
112

    
113
    /*
114
     * (non-Javadoc)
115
     *
116
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
117
     */
118
    public boolean is3D() {
119
        return false;
120
    }
121

    
122
    /* (non-Javadoc)
123
     * @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#setJTSCoordinate(com.vividsolutions.jts.geom.Coordinate)
124
     */
125
    public void setJTSCoordinate(Coordinate coordinate) {
126
        this.coordinate = coordinate;
127
    }
128

    
129
    /*
130
     * (non-Javadoc)
131
     *
132
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
133
     */
134
    public GeneralPathX getGeneralPath() {
135
        return new DefaultGeneralPathX(new PointIterator(null),false,0);
136
    }
137

    
138
    /*
139
     * (non-Javadoc)
140
     *
141
     * @see
142
     * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
143
     * )
144
     */
145
    public PathIterator getPathIterator(AffineTransform at) {
146
        PointIterator pi = new PointIterator(at);
147
        return pi;
148
    }
149

    
150

    
151
    public class PointIterator extends GeneralPathXIterator {
152
        /** Transform applied on the coordinates during iteration */
153
        private AffineTransform at;
154

    
155
        /** True when the point has been read once */
156
        private boolean done;
157

    
158
        /**
159
         * Creates a new PointIterator object.
160
         *
161
         * @param p The polygon
162
         * @param at The affine transform applied to coordinates during iteration
163
         */
164
        public PointIterator(AffineTransform at) {
165
            super(new GeneralPathX());
166
            if (at == null) {
167
                at = new AffineTransform();
168
            }
169

    
170
            this.at = at;
171
            done = false;
172
        }
173

    
174
        /**
175
         * Return the winding rule for determining the interior of the path.
176
         *
177
         * @return <code>WIND_EVEN_ODD</code> by default.
178
         */
179
        public int getWindingRule() {
180
            return PathIterator.WIND_EVEN_ODD;
181
        }
182

    
183
        /**
184
         * @see java.awt.geom.PathIterator#next()
185
         */
186
        public void next() {
187
            done = true;
188
        }
189

    
190
        /**
191
         * @see java.awt.geom.PathIterator#isDone()
192
         */
193
        public boolean isDone() {
194
            return done;
195
        }
196

    
197
        /**
198
         * @see java.awt.geom.PathIterator#currentSegment(double[])
199
         */
200
        public int currentSegment(double[] coords) {
201
            coords[0] = getX();
202
            coords[1] = getY();
203
            at.transform(coords, 0, coords, 0, 1);
204

    
205
            return PathIterator.SEG_MOVETO;
206
        }
207

    
208
        /* (non-Javadoc)
209
         * @see java.awt.geom.PathIterator#currentSegment(float[])
210
         */
211
        public int currentSegment(float[] coords) {
212
            coords[0] = (float) getX();
213
            coords[1] = (float) getY();
214
            at.transform(coords, 0, coords, 0, 1);
215

    
216
            return PathIterator.SEG_MOVETO;
217
        }
218
    }
219

    
220
    /* (non-Javadoc)
221
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
222
     */
223
    public MultiPoint toPoints() throws GeometryException {
224
        MultiPoint multiPoint = new MultiPoint2D();
225
        multiPoint.addPoint(this);
226
        return multiPoint;
227
    }
228

    
229
    /* (non-Javadoc)
230
     * @see org.gvsig.fmap.geom.Geometry#offset(double)
231
     */
232
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
233
        return new Circumference2D((Point)this.cloneGeometry(), distance);
234
    }
235

    
236
    public String toString() {
237
        StringBuilder builder = new StringBuilder();
238
        builder.append("POINT (");
239
        for( int i=0; i<this.getDimension()-1; i++) {
240
            builder.append(this.getCoordinateAt(i));
241
            builder.append(" ");
242
        }
243
        builder.append(this.getCoordinateAt(this.getDimension()-1));
244
        builder.append(")");
245
        return builder.toString();
246
    }
247

    
248
    @Override
249
    public boolean equals(Object obj) {
250
        if( obj == null ) {
251
            return false;
252
        }
253
        Point other;
254
        try {
255
            other = (Point) obj;
256
        } catch(ClassCastException e) {
257
            return false;
258
        }
259
        if( this.getGeometryType().getSubType() != other.getGeometryType().getSubType() ) {
260
            return false;
261
        }
262
        if( this.coordinate.x != other.getX() ) {
263
            return false;
264
        }
265
        if( this.coordinate.y != other.getY() ) {
266
            return false;
267
        }
268
        return true;
269
    }
270

    
271
}