~ubuntu-branches/ubuntu/feisty/postgis/feisty

« back to all changes in this revision

Viewing changes to jdbc2/src/org/postgis/Point.java

  • Committer: Bazaar Package Importer
  • Author(s): Alex Bodnaru
  • Date: 2005-05-05 10:02:45 UTC
  • Revision ID: james.westby@ubuntu.com-20050505100245-3005l6jn1jwvpsrw
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Test.java
 
3
 * 
 
4
 * PostGIS extension for PostgreSQL JDBC driver - geometry model
 
5
 * 
 
6
 * (C) 2004 Paul Ramsey, pramsey@refractions.net
 
7
 * 
 
8
 * (C) 2005 Markus Schaber, markus@schabi.de
 
9
 * 
 
10
 * This library is free software; you can redistribute it and/or modify it under
 
11
 * the terms of the GNU Lesser General Public License as published by the Free
 
12
 * Software Foundation, either version 2.1 of the License.
 
13
 * 
 
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
17
 * details.
 
18
 * 
 
19
 * You should have received a copy of the GNU Lesser General Public License
 
20
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 
21
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit the web at
 
22
 * http://www.gnu.org.
 
23
 * 
 
24
 * $Id: Point.java,v 1.7 2005/03/30 15:24:40 mschaber Exp $
 
25
 */
 
26
 
 
27
package org.postgis;
 
28
 
 
29
import org.postgresql.util.PGtokenizer;
 
30
 
 
31
import java.sql.SQLException;
 
32
 
 
33
public class Point extends Geometry {
 
34
    /* JDK 1.5 Serialization */
 
35
    private static final long serialVersionUID = 0x100;
 
36
 
 
37
    public static final boolean CUTINTS = true;
 
38
 
 
39
    public int hashCode() {
 
40
        return super.hashCode() ^ hashCode(x) ^ hashCode(y) ^ hashCode(z) ^ hashCode(m);
 
41
    }
 
42
 
 
43
    public static int hashCode(double value) {
 
44
        long v = Double.doubleToLongBits(value);
 
45
        return (int) (v ^ (v >>> 32));
 
46
    }
 
47
 
 
48
    protected boolean equalsintern(Geometry otherg) {
 
49
        Point other = (Point) otherg;
 
50
        boolean xequals = x == other.x;
 
51
        boolean zequals = ((dimension == 2) || (z == other.z));
 
52
        boolean mequals = ((haveMeasure == false) || (m == other.m));
 
53
        boolean result = xequals && yequals(other) && zequals && mequals;
 
54
        return result;
 
55
    }
 
56
 
 
57
    private boolean yequals(Point other) {
 
58
        return y == other.y;
 
59
    }
 
60
 
 
61
    public Point getPoint(int index) {
 
62
        if (index == 0) {
 
63
            return this;
 
64
        } else {
 
65
            throw new ArrayIndexOutOfBoundsException("Point only has a single Point! " + index);
 
66
        }
 
67
    }
 
68
 
 
69
    /** Optimized versions for this special case */
 
70
    public Point getFirstPoint() {
 
71
        return this;
 
72
    }
 
73
 
 
74
    /** Optimized versions for this special case */
 
75
    public Point getLastPoint() {
 
76
        return this;
 
77
    }
 
78
 
 
79
    public int numPoints() {
 
80
        return 1;
 
81
    }
 
82
 
 
83
    /**
 
84
     * The X coordinate of the point.
 
85
     */
 
86
    public double x;
 
87
 
 
88
    /**
 
89
     * The Y coordinate of the point.
 
90
     */
 
91
    public double y;
 
92
 
 
93
    /**
 
94
     * The Z coordinate of the point.
 
95
     */
 
96
    public double z;
 
97
 
 
98
    /**
 
99
     * The measure of the point.
 
100
     */
 
101
    public double m = 0.0;
 
102
 
 
103
    public Point() {
 
104
        super(POINT);
 
105
    }
 
106
 
 
107
    public Point(double x, double y, double z) {
 
108
        this();
 
109
        this.x = x;
 
110
        this.y = y;
 
111
        this.z = z;
 
112
        dimension = 3;
 
113
    }
 
114
 
 
115
    public Point(double x, double y) {
 
116
        this();
 
117
        this.x = x;
 
118
        this.y = y;
 
119
        this.z = 0.0;
 
120
        dimension = 2;
 
121
    }
 
122
 
 
123
    /**
 
124
     * Construct a Point from EWKT.
 
125
     * 
 
126
     * (3D and measures are legal, but SRID is not allowed).
 
127
     */
 
128
    public Point(String value) throws SQLException {
 
129
        this(value, false);
 
130
    }
 
131
 
 
132
    /**
 
133
     * Construct a Point
 
134
     * 
 
135
     * @param value The text representation of this point
 
136
     * @param haveM Hint whether we have a measure. This is used by other
 
137
     *            geometries parsing inner points where we only get "1 2 3 4"
 
138
     *            like strings without the "POINT(" and ")" stuff. If there
 
139
     *            acutally is a POINTM prefix, this overrides the given value.
 
140
     *            However, POINT does not set it to false, as they can be
 
141
     *            contained in measured collections, as in
 
142
     *            "GEOMETRYCOLLECTIONM(POINT(0 0 0))".
 
143
     */
 
144
    protected Point(String value, boolean haveM) throws SQLException {
 
145
        this();
 
146
        value = value.trim();
 
147
        if (value.indexOf("POINTM") == 0) {
 
148
            haveM = true;
 
149
            value = value.substring(6).trim();
 
150
        } else if (value.indexOf("POINT") == 0) {
 
151
            value = value.substring(5).trim();
 
152
        }
 
153
        PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(value), ' ');
 
154
        try {
 
155
            x = Double.valueOf(t.getToken(0)).doubleValue();
 
156
            y = Double.valueOf(t.getToken(1)).doubleValue();
 
157
            haveM |= t.getSize() == 4;
 
158
            if ((t.getSize() == 3 && !haveM) || (t.getSize() == 4)) {
 
159
                z = Double.valueOf(t.getToken(2)).doubleValue();
 
160
                dimension = 3;
 
161
            } else {
 
162
                dimension = 2;
 
163
            }
 
164
            if (haveM) {
 
165
                m = Double.valueOf(t.getToken(dimension)).doubleValue();
 
166
            }
 
167
        } catch (NumberFormatException e) {
 
168
            throw new SQLException("Error parsing Point: " + e.toString());
 
169
        }
 
170
        haveMeasure = haveM;
 
171
    }
 
172
 
 
173
    public void innerWKT(StringBuffer sb) {
 
174
        sb.append(x);
 
175
        if (CUTINTS)
 
176
            cutint(sb);
 
177
        sb.append(' ');
 
178
        sb.append(y);
 
179
        if (CUTINTS)
 
180
            cutint(sb);
 
181
        if (dimension == 3) {
 
182
            sb.append(' ');
 
183
            sb.append(z);
 
184
            if (CUTINTS)
 
185
                cutint(sb);
 
186
        }
 
187
        if (haveMeasure) {
 
188
            sb.append(' ');
 
189
            sb.append(m);
 
190
            if (CUTINTS)
 
191
                cutint(sb);
 
192
        }
 
193
    }
 
194
 
 
195
    private static void cutint(StringBuffer sb) {
 
196
        int l = sb.length() - 2;
 
197
        if ((sb.charAt(l + 1) == '0') && (sb.charAt(l) == '.')) {
 
198
            sb.setLength(l);
 
199
        }
 
200
    }
 
201
 
 
202
    public double getX() {
 
203
        return x;
 
204
    }
 
205
 
 
206
    public double getY() {
 
207
        return y;
 
208
    }
 
209
 
 
210
    public double getZ() {
 
211
        return z;
 
212
    }
 
213
 
 
214
    public double getM() {
 
215
        return m;
 
216
    }
 
217
 
 
218
    public void setX(double x) {
 
219
        this.x = x;
 
220
    }
 
221
 
 
222
    public void setY(double y) {
 
223
        this.y = y;
 
224
    }
 
225
 
 
226
    public void setZ(double z) {
 
227
        this.z = z;
 
228
    }
 
229
 
 
230
    public void setM(double m) {
 
231
        haveMeasure = true;
 
232
        this.m = m;
 
233
    }
 
234
 
 
235
    public void setX(int x) {
 
236
        this.x = x;
 
237
    }
 
238
 
 
239
    public void setY(int y) {
 
240
        this.y = y;
 
241
    }
 
242
 
 
243
    public void setZ(int z) {
 
244
        this.z = z;
 
245
    }
 
246
 
 
247
    public double distance(Point other) {
 
248
        double tx, ty, tz;
 
249
        if (this.dimension != other.dimension) {
 
250
            throw new IllegalArgumentException("Points have different dimensions!");
 
251
        }
 
252
        tx = this.x - other.x;
 
253
        switch (this.dimension) {
 
254
        case 1 :
 
255
            return Math.sqrt(tx * tx);
 
256
        case 2 :
 
257
            ty = this.y - other.y;
 
258
            return Math.sqrt(tx * tx + ty * ty);
 
259
        case 3 :
 
260
            ty = this.y - other.y;
 
261
            tz = this.z - other.z;
 
262
            return Math.sqrt(tx * tx + ty * ty + tz * tz);
 
263
        default :
 
264
            throw new IllegalArgumentException("Illegal dimension of Point" + this.dimension);
 
265
        }
 
266
    }
 
267
}
 
 
b'\\ No newline at end of file'