kiwi-postgis  Version 0.1.0.0
Coordinate Class Reference

A lightweight class used to store coordinates on the 2-dimensional Cartesian plane. More...

Inheritance diagram for Coordinate:
Collaboration diagram for Coordinate:

Classes

class  DimensionalComparator
 Compares two Coordinates, allowing for either a 2-dimensional or 3-dimensional comparison, and handling NaN values correctly. More...
 

Public Member Functions

 Coordinate (double x, double y, double z)
 Constructs a Coordinate at (x,y,z). More...
 
 Coordinate ()
 Constructs a Coordinate at (0,0,NaN). More...
 
 Coordinate (Coordinate c)
 Constructs a Coordinate having the same (x,y,z) values as other. More...
 
 Coordinate (double x, double y)
 Constructs a Coordinate at (x,y,NaN). More...
 
void setCoordinate (Coordinate other)
 Sets this Coordinates (x,y,z) values to that of other. More...
 
double getX ()
 Retrieves the value of the X ordinate. More...
 
void setX (double x)
 Sets the X ordinate value. More...
 
double getY ()
 Retrieves the value of the Y ordinate. More...
 
Date getT ()
 
void setY (double y)
 Sets the Y ordinate value. More...
 
double getZ ()
 Retrieves the value of the Z ordinate, if present. More...
 
void setZ (double z)
 Sets the Z ordinate value. More...
 
void setT (Date t)
 
void setT (Long t)
 
double getM ()
 Retrieves the value of the measure, if present. More...
 
void setM (double m)
 Sets the measure value, if supported. More...
 
double getOrdinate (int ordinateIndex)
 Gets the ordinate value for the given index. More...
 
void setOrdinate (int ordinateIndex, double value)
 Sets the ordinate for the given index to a given value. More...
 
boolean equals2D (Coordinate other)
 Returns whether the planar projections of the two Coordinates are equal. More...
 
boolean equals2D (Coordinate c, double tolerance)
 Tests if another Coordinate has the same values for the X and Y ordinates, within a specified tolerance value. More...
 
boolean equals3D (Coordinate other)
 Tests if another coordinate has the same values for the X, Y and Z ordinates. More...
 
boolean equalInZ (Coordinate c, double tolerance)
 Tests if another coordinate has the same value for Z, within a tolerance. More...
 
boolean equals (Object other)
 Returns true if other has the same values for the x and y ordinates. More...
 
int compareTo (Coordinate o)
 Compares this Coordinate with the specified Coordinate for order. More...
 
String toString ()
 Returns a String of the form (x,y,z) . More...
 
Object clone ()
 
Coordinate copy ()
 Creates a copy of this Coordinate. More...
 
double distance (Coordinate c)
 Computes the 2-dimensional Euclidean distance to another location. More...
 
double distance3D (Coordinate c)
 Computes the 3-dimensional Euclidean distance to another location. More...
 
int hashCode ()
 Gets a hashcode for this coordinate. More...
 

Static Public Member Functions

static int hashCode (double x)
 Computes a hash code for a double value, using the algorithm from Joshua Bloch's book Effective Java" More...
 

Public Attributes

double x
 The x-ordinate. More...
 
double y
 The y-ordinate. More...
 
double z
 The z-ordinate. More...
 
Date t =null
 

Static Public Attributes

static final double NULL_ORDINATE = Double.NaN
 The value used to indicate a null or missing ordinate value. More...
 
static final int X = 0
 Standard ordinate index value for, where X is 0. More...
 
static final int Y = 1
 Standard ordinate index value for, where Y is 1. More...
 
static final int T = 4
 Standard ordinate index value for, where Y is 1. More...
 
static final int Z = 2
 Standard ordinate index value for, where Z is 2. More...
 
static final int M = 3
 Standard ordinate index value for, where M is 3. More...
 

Static Private Attributes

static final long serialVersionUID = 6683108902428366910L
 

Detailed Description

A lightweight class used to store coordinates on the 2-dimensional Cartesian plane.

It is distinct from Point, which is a subclass of Geometry. Unlike objects of type Point (which contain additional information such as an envelope, a precision model, and spatial reference system information), a Coordinate only contains ordinate values and accessor methods.

Coordinates are two-dimensional points, with an additional Z-ordinate. If an Z-ordinate value is not specified or not defined, constructed coordinates have a Z-ordinate of NaN (which is also the value of NULL_ORDINATE). The standard comparison functions ignore the Z-ordinate. Apart from the basic accessor functions, JTS supports only specific operations involving the Z-ordinate.

Implementations may optionally support Z-ordinate and M-measure values as appropriate for a CoordinateSequence. Use of getZ() and getM() accessors, or getOrdinate(int) are recommended.

Version
1.16

Constructor & Destructor Documentation

◆ Coordinate() [1/4]

Coordinate ( double  x,
double  y,
double  z 
)

Constructs a Coordinate at (x,y,z).

Parameters
xthe x-ordinate
ythe y-ordinate
zthe z-ordinate

References Coordinate.x, Coordinate.y, and Coordinate.z.

107  {
108  this.x = x;
109  this.y = y;
110  this.z = z;
111  }
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84
double z
The z-ordinate.
Definition: Coordinate.java:96

◆ Coordinate() [2/4]

Constructs a Coordinate at (0,0,NaN).

Referenced by Coordinate.clone(), Coordinate.compareTo(), and Coordinate.copy().

116  {
117  this(0.0, 0.0);
118  }

◆ Coordinate() [3/4]

Constructs a Coordinate having the same (x,y,z) values as other.

Parameters
cthe Coordinate to copy.

References Coordinate.getZ(), Coordinate.x, and Coordinate.y.

126  {
127  this(c.x, c.y, c.getZ());
128  }

◆ Coordinate() [4/4]

Coordinate ( double  x,
double  y 
)

Constructs a Coordinate at (x,y,NaN).

Parameters
xthe x-value
ythe y-value

References Coordinate.NULL_ORDINATE, Coordinate.x, and Coordinate.y.

136  {
137  this(x, y, NULL_ORDINATE);
138  }
static final double NULL_ORDINATE
The value used to indicate a null or missing ordinate value.
Definition: Coordinate.java:54
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84

Member Function Documentation

◆ clone()

Object clone ( )

References Coordinate.clone(), and Coordinate.Coordinate().

Referenced by Coordinate.clone().

400  {
401  try {
402  Coordinate coord = (Coordinate) super.clone();
403 
404  return coord; // return the clone
405  } catch (CloneNotSupportedException e) {
406  Assert.shouldNeverReachHere(
407  "this shouldn't happen because this class is Cloneable");
408 
409  return null;
410  }
411  }
Coordinate()
Constructs a Coordinate at (0,0,NaN).
Definition: Coordinate.java:116

◆ compareTo()

int compareTo ( Coordinate  o)

Compares this Coordinate with the specified Coordinate for order.

This method ignores the z value when making the comparison. Returns:

  • -1 : this.x < other.x || ((this.x == other.x) && (this.y < other.y))
  • 0 : this.x == other.x && this.y = other.y
  • 1 : this.x > other.x || ((this.x == other.x) && (this.y > other.y))

Note: This method assumes that ordinate values are valid numbers. NaN values are not handled correctly.

Parameters
othe Coordinate with which this Coordinate is being compared
Returns
-1, zero, or 1 as this Coordinate is less than, equal to, or greater than the specified Coordinate

References Coordinate.Coordinate(), Coordinate.x, and Coordinate.y.

381  {
382  Coordinate other = (Coordinate) o;
383 
384  if (x < other.x) return -1;
385  if (x > other.x) return 1;
386  if (y < other.y) return -1;
387  if (y > other.y) return 1;
388  return 0;
389  }
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84
Coordinate()
Constructs a Coordinate at (0,0,NaN).
Definition: Coordinate.java:116

◆ copy()

Coordinate copy ( )

Creates a copy of this Coordinate.

Returns
a copy of this coordinate.

References Coordinate.Coordinate().

418  {
419  return new Coordinate(this);
420  }
Coordinate()
Constructs a Coordinate at (0,0,NaN).
Definition: Coordinate.java:116

◆ distance()

double distance ( Coordinate  c)

Computes the 2-dimensional Euclidean distance to another location.

The Z-ordinate is ignored.

Parameters
ca point
Returns
the 2-dimensional Euclidean distance between the locations

References Coordinate.x, and Coordinate.y.

Referenced by LocatePoint.compute(), LocatePoint.pointAlongSegment(), and LongestLine.relation().

429  {
430  double dx = x - c.x;
431  double dy = y - c.y;
432  return Math.sqrt(dx * dx + dy * dy);
433  }
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ distance3D()

double distance3D ( Coordinate  c)

Computes the 3-dimensional Euclidean distance to another location.

Parameters
ca coordinate
Returns
the 3-dimensional Euclidean distance between the locations

References Coordinate.getZ(), Coordinate.x, and Coordinate.y.

441  {
442  double dx = x - c.x;
443  double dy = y - c.y;
444  double dz = getZ() - c.getZ();
445  return Math.sqrt(dx * dx + dy * dy + dz * dz);
446  }
double getZ()
Retrieves the value of the Z ordinate, if present.
Definition: Coordinate.java:197
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ equalInZ()

boolean equalInZ ( Coordinate  c,
double  tolerance 
)

Tests if another coordinate has the same value for Z, within a tolerance.

Parameters
ca coordinate
tolerancethe tolerance value
Returns
true if the Z ordinates are within the given tolerance

References Coordinate.getZ().

343  {
344  return NumberUtil.equalsWithTolerance(this.getZ(), c.getZ(), tolerance);
345  }
double getZ()
Retrieves the value of the Z ordinate, if present.
Definition: Coordinate.java:197

◆ equals()

boolean equals ( Object  other)

Returns true if other has the same values for the x and y ordinates.

Since Coordinates are 2.5D, this routine ignores the z value when making the comparison.

Parameters
othera Coordinate with which to do the comparison.
Returns
true if other is a Coordinate with the same values for the x and y ordinates.

References Coordinate.equals2D().

356  {
357  if (!(other instanceof Coordinate)) {
358  return false;
359  }
360  return equals2D((Coordinate) other);
361  }
boolean equals2D(Coordinate other)
Returns whether the planar projections of the two Coordinates are equal.
Definition: Coordinate.java:293
Coordinate()
Constructs a Coordinate at (0,0,NaN).
Definition: Coordinate.java:116

◆ equals2D() [1/2]

boolean equals2D ( Coordinate  other)

Returns whether the planar projections of the two Coordinates are equal.

Parameters
othera Coordinate with which to do the 2D comparison.
Returns
true if the x- and y-coordinates are equal; the z-coordinates do not have to be equal.

References Coordinate.x, and Coordinate.y.

Referenced by Coordinate.equals().

293  {
294  if (x != other.x) {
295  return false;
296  }
297  if (y != other.y) {
298  return false;
299  }
300  return true;
301  }
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ equals2D() [2/2]

boolean equals2D ( Coordinate  c,
double  tolerance 
)

Tests if another Coordinate has the same values for the X and Y ordinates, within a specified tolerance value.

The Z ordinate is ignored.

Parameters
ca Coordinate with which to do the 2D comparison.
tolerancethe tolerance value to use
Returns
true if other is a Coordinate with the same values for X and Y.

References Coordinate.x, and Coordinate.y.

313  {
314  if (! NumberUtil.equalsWithTolerance(this.x, c.x, tolerance)) {
315  return false;
316  }
317  if (! NumberUtil.equalsWithTolerance(this.y, c.y, tolerance)) {
318  return false;
319  }
320  return true;
321  }

◆ equals3D()

boolean equals3D ( Coordinate  other)

Tests if another coordinate has the same values for the X, Y and Z ordinates.

Parameters
othera Coordinate with which to do the 3D comparison.
Returns
true if other is a Coordinate with the same values for X, Y and Z.

References Coordinate.getZ(), Coordinate.x, and Coordinate.y.

330  {
331  return (x == other.x) && (y == other.y) &&
332  ((getZ() == other.getZ()) ||
333  (Double.isNaN(getZ()) && Double.isNaN(other.getZ())));
334  }
double getZ()
Retrieves the value of the Z ordinate, if present.
Definition: Coordinate.java:197
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ getM()

double getM ( )

Retrieves the value of the measure, if present.

If no measure value is present returns NaN.

Returns
the value of the measure, or NaN

Referenced by TWKBWriter.ExtendedPrecisionWriter.checkCoordinate(), Zmflag.operation(), and TWKBWriter.write().

224  {
225  return Double.NaN;
226  }

◆ getOrdinate()

double getOrdinate ( int  ordinateIndex)

Gets the ordinate value for the given index.

The base implementation supports values for the index are X, Y, and Z.

Parameters
ordinateIndexthe ordinate index
Returns
the value of the ordinate
Exceptions
IllegalArgumentExceptionif the index is not valid

References Coordinate.getZ(), Coordinate.X, Coordinate.x, Coordinate.Y, Coordinate.y, and Coordinate.Z.

248  {
249  switch (ordinateIndex) {
250  case X: return x;
251  case Y: return y;
252  case Z: return getZ(); // sure to delegate to subclass rather than offer direct field access
253  }
254  throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
255  }
static final int Z
Standard ordinate index value for, where Z is 2.
Definition: Coordinate.java:71
static final int Y
Standard ordinate index value for, where Y is 1.
Definition: Coordinate.java:60
double getZ()
Retrieves the value of the Z ordinate, if present.
Definition: Coordinate.java:197
double y
The y-ordinate.
Definition: Coordinate.java:89
static final int X
Standard ordinate index value for, where X is 0.
Definition: Coordinate.java:57
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ getT()

Date getT ( )

References Coordinate.t.

178  {
179  return t;
180  }
Date t
Definition: Coordinate.java:98

◆ getX()

double getX ( )

Retrieves the value of the X ordinate.

Returns
the value of the X ordinate

References Coordinate.x.

Referenced by TWKBWriter.PrecisionWriter.checkCoordinate().

156  {
157  return x;
158  }
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ getY()

double getY ( )

Retrieves the value of the Y ordinate.

Returns
the value of the Y ordinate

References Coordinate.y.

Referenced by TWKBWriter.PrecisionWriter.checkCoordinate().

174  {
175  return y;
176  }
double y
The y-ordinate.
Definition: Coordinate.java:89

◆ getZ()

double getZ ( )

Retrieves the value of the Z ordinate, if present.

If no Z value is present returns NaN.

Returns
the value of the Z ordinate, or NaN

References Coordinate.z.

Referenced by TWKBWriter.ExtendedPrecisionWriter.checkCoordinate(), Coordinate.DimensionalComparator.compare(), Coordinate.Coordinate(), Coordinate.distance3D(), Coordinate.equalInZ(), Coordinate.equals3D(), Coordinate.getOrdinate(), Zmflag.operation(), Coordinate.setCoordinate(), Coordinate.toString(), and TWKBWriter.write().

197  {
198  return z;
199  }
double z
The z-ordinate.
Definition: Coordinate.java:96

◆ hashCode() [1/2]

int hashCode ( )

Gets a hashcode for this coordinate.

Returns
a hashcode for this coordinate
453  {
454  //Algorithm from Effective Java by Joshua Bloch [Jon Aquino]
455  int result = 17;
456  result = 37 * result + hashCode(x);
457  result = 37 * result + hashCode(y);
458  return result;
459  }
int hashCode()
Gets a hashcode for this coordinate.
Definition: Coordinate.java:453
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ hashCode() [2/2]

static int hashCode ( double  x)
static

Computes a hash code for a double value, using the algorithm from Joshua Bloch's book Effective Java"

Parameters
xthe value to compute for
Returns
a hashcode for x
468  {
469  long f = Double.doubleToLongBits(x);
470  return (int)(f^(f>>>32));
471  }
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ setCoordinate()

void setCoordinate ( Coordinate  other)

Sets this Coordinates (x,y,z) values to that of other.

Parameters
otherthe Coordinate to copy

References Coordinate.getZ(), Coordinate.x, and Coordinate.y.

145  {
146  x = other.x;
147  y = other.y;
148  z = other.getZ();
149  }
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84
double z
The z-ordinate.
Definition: Coordinate.java:96

◆ setM()

void setM ( double  m)

Sets the measure value, if supported.

Parameters
mthe value to set as M

Referenced by TWKBReader.ExtendedPrecisionReader.readPoint(), and TWKBReader.ExtendedPrecisionReader.readPointArray().

233  {
234  throw new IllegalArgumentException("Invalid ordinate index: " + M);
235  }
static final int M
Standard ordinate index value for, where M is 3.
Definition: Coordinate.java:79

◆ setOrdinate()

void setOrdinate ( int  ordinateIndex,
double  value 
)

Sets the ordinate for the given index to a given value.

The base implementation supported values for the index are X, Y, and Z.

Parameters
ordinateIndexthe ordinate index
valuethe value to set
Exceptions
IllegalArgumentExceptionif the index is not valid

References Coordinate.setZ(), Coordinate.X, Coordinate.Y, and Coordinate.Z.

269  {
270  switch (ordinateIndex) {
271  case X:
272  x = value;
273  break;
274  case Y:
275  y = value;
276  break;
277  case Z:
278  setZ(value); // delegate to subclass rather than offer direct field access
279  break;
280  default:
281  throw new IllegalArgumentException("Invalid ordinate index: " + ordinateIndex);
282  }
283  }
void setZ(double z)
Sets the Z ordinate value.
Definition: Coordinate.java:206
static final int Z
Standard ordinate index value for, where Z is 2.
Definition: Coordinate.java:71
static final int Y
Standard ordinate index value for, where Y is 1.
Definition: Coordinate.java:60
double y
The y-ordinate.
Definition: Coordinate.java:89
static final int X
Standard ordinate index value for, where X is 0.
Definition: Coordinate.java:57
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ setT() [1/2]

void setT ( Date  t)

References Coordinate.t.

210  {
211  this.t = t;
212  }
Date t
Definition: Coordinate.java:98

◆ setT() [2/2]

void setT ( Long  t)
214  {
215  this.t = new Date(t);
216 }
Date t
Definition: Coordinate.java:98

◆ setX()

void setX ( double  x)

Sets the X ordinate value.

Parameters
xthe value to set as X

References Coordinate.x.

165  {
166  this.x = x;
167  }
double x
The x-ordinate.
Definition: Coordinate.java:84

◆ setY()

void setY ( double  y)

Sets the Y ordinate value.

Parameters
ythe value to set as Y

References Coordinate.y.

187  {
188  this.y = y;
189  }
double y
The y-ordinate.
Definition: Coordinate.java:89

◆ setZ()

void setZ ( double  z)

Sets the Z ordinate value.

Parameters
zthe value to set as Z

References Coordinate.z.

Referenced by TWKBReader.ExtendedPrecisionReader.readPoint(), TWKBReader.ExtendedPrecisionReader.readPointArray(), and Coordinate.setOrdinate().

206  {
207  this.z = z;
208  }
double z
The z-ordinate.
Definition: Coordinate.java:96

◆ toString()

String toString ( )

Returns a String of the form (x,y,z) .

Returns
a String of the form (x,y,z)

References Coordinate.getZ().

Referenced by AsOSMLink.operation().

396  {
397  return "(" + x + ", " + y + ", " + getZ() + ")";
398  }
double getZ()
Retrieves the value of the Z ordinate, if present.
Definition: Coordinate.java:197
double y
The y-ordinate.
Definition: Coordinate.java:89
double x
The x-ordinate.
Definition: Coordinate.java:84

Member Data Documentation

◆ M

final int M = 3
static

Standard ordinate index value for, where M is 3.

This constant assumes XYZM coordinate sequence definition, please check this assumption using getDimension() and getMeasures() before use.

◆ NULL_ORDINATE

final double NULL_ORDINATE = Double.NaN
static

The value used to indicate a null or missing ordinate value.

In particular, used for the value of ordinates for dimensions greater than the defined dimension of a coordinate.

Referenced by Coordinate.Coordinate().

◆ serialVersionUID

final long serialVersionUID = 6683108902428366910L
staticprivate

◆ T

final int T = 4
static

Standard ordinate index value for, where Y is 1.

◆ t

Date t =null

Referenced by Coordinate.getT(), and Coordinate.setT().

◆ X

final int X = 0
static

Standard ordinate index value for, where X is 0.

Referenced by Coordinate.getOrdinate(), and Coordinate.setOrdinate().

◆ x

◆ Y

final int Y = 1
static

Standard ordinate index value for, where Y is 1.

Referenced by Coordinate.getOrdinate(), and Coordinate.setOrdinate().

◆ y

◆ Z

final int Z = 2
static

Standard ordinate index value for, where Z is 2.

This constant assumes XYZM coordinate sequence definition, please check this assumption using getDimension() and getMeasures() before use.

Referenced by Coordinate.getOrdinate(), and Coordinate.setOrdinate().

◆ z

double z

The z-ordinate.

Direct access to this field is discouraged; use getZ().

Referenced by Coordinate.Coordinate(), Coordinate.getZ(), LineLength3D.length3D(), and Coordinate.setZ().