4
* PostGIS extension for PostgreSQL JDBC driver - geometry model
6
* (C) 2004 Paul Ramsey, pramsey@refractions.net
8
* (C) 2005 Markus Schaber, markus@schabi.de
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.
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
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
24
* $Id: Point.java,v 1.7 2005/03/30 15:24:40 mschaber Exp $
29
import org.postgresql.util.PGtokenizer;
31
import java.sql.SQLException;
33
public class Point extends Geometry {
34
/* JDK 1.5 Serialization */
35
private static final long serialVersionUID = 0x100;
37
public static final boolean CUTINTS = true;
39
public int hashCode() {
40
return super.hashCode() ^ hashCode(x) ^ hashCode(y) ^ hashCode(z) ^ hashCode(m);
43
public static int hashCode(double value) {
44
long v = Double.doubleToLongBits(value);
45
return (int) (v ^ (v >>> 32));
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;
57
private boolean yequals(Point other) {
61
public Point getPoint(int index) {
65
throw new ArrayIndexOutOfBoundsException("Point only has a single Point! " + index);
69
/** Optimized versions for this special case */
70
public Point getFirstPoint() {
74
/** Optimized versions for this special case */
75
public Point getLastPoint() {
79
public int numPoints() {
84
* The X coordinate of the point.
89
* The Y coordinate of the point.
94
* The Z coordinate of the point.
99
* The measure of the point.
101
public double m = 0.0;
107
public Point(double x, double y, double z) {
115
public Point(double x, double y) {
124
* Construct a Point from EWKT.
126
* (3D and measures are legal, but SRID is not allowed).
128
public Point(String value) throws SQLException {
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))".
144
protected Point(String value, boolean haveM) throws SQLException {
146
value = value.trim();
147
if (value.indexOf("POINTM") == 0) {
149
value = value.substring(6).trim();
150
} else if (value.indexOf("POINT") == 0) {
151
value = value.substring(5).trim();
153
PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(value), ' ');
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();
165
m = Double.valueOf(t.getToken(dimension)).doubleValue();
167
} catch (NumberFormatException e) {
168
throw new SQLException("Error parsing Point: " + e.toString());
173
public void innerWKT(StringBuffer sb) {
181
if (dimension == 3) {
195
private static void cutint(StringBuffer sb) {
196
int l = sb.length() - 2;
197
if ((sb.charAt(l + 1) == '0') && (sb.charAt(l) == '.')) {
202
public double getX() {
206
public double getY() {
210
public double getZ() {
214
public double getM() {
218
public void setX(double x) {
222
public void setY(double y) {
226
public void setZ(double z) {
230
public void setM(double m) {
235
public void setX(int x) {
239
public void setY(int y) {
243
public void setZ(int z) {
247
public double distance(Point other) {
249
if (this.dimension != other.dimension) {
250
throw new IllegalArgumentException("Points have different dimensions!");
252
tx = this.x - other.x;
253
switch (this.dimension) {
255
return Math.sqrt(tx * tx);
257
ty = this.y - other.y;
258
return Math.sqrt(tx * tx + ty * ty);
260
ty = this.y - other.y;
261
tz = this.z - other.z;
262
return Math.sqrt(tx * tx + ty * ty + tz * tz);
264
throw new IllegalArgumentException("Illegal dimension of Point" + this.dimension);
b'\\ No newline at end of file'