org.geotools.geometry
Class DirectPosition2D

Object
  extended by Point2D
      extended by Point2D.Double
          extended by DirectPosition2D
All Implemented Interfaces:
Serializable, Cloneable, Position, DirectPosition

public class DirectPosition2D
extends Point2D.Double
implements DirectPosition, Serializable, Cloneable

Holds the coordinates for a two-dimensional position within some coordinate reference system.

Note 1:

This class inherits x and y fields. But despite their names, they don't need to be oriented toward East and North. The (x,y) axis can have any orientation and should be understood as "ordinate 0" and "ordinate 1" values instead. This is not specific to this implementation; in Java2D too, the visual axis orientation depend on the affine transform in the graphics context.

The rational for avoiding axis orientation restriction is that other DirectPosition implementation do not have such restriction, and it would be hard to generalize (what to do with North-East direction?).

Note 2:

Do not mix instances of this class with ordinary Point2D instances in a HashSet or as HashMap keys. It is not possible to meet both Point2D.hashCode() and DirectPosition.hashCode() contract, and this class choose to implements the later. Concequently, hashCode() is inconsistent with Point2D.equals(java.lang.Object) (but is consistent with DirectPosition.equals(java.lang.Object)).

In other words, it is safe to add instances of DirectPosition2D in a HashSet<DirectPosition>, but it is unsafe to add them in a HashSet<Point2D>. Collections that do not rely on Object.hashCode(), like ArrayList, are safe in all cases.

Since:
2.0
Author:
Martin Desruisseaux (IRD)
See Also:
DirectPosition1D, GeneralDirectPosition, Point2D, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class Point2D
Point2D.Double, Point2D.Float
 
Field Summary
 
Fields inherited from class Point2D.Double
x, y
 
Constructor Summary
DirectPosition2D()
          Constructs a position initialized to (0,0) with a null coordinate reference system.
DirectPosition2D(CoordinateReferenceSystem crs)
          Constructs a position with the specified coordinate reference system.
DirectPosition2D(CoordinateReferenceSystem crs, double x, double y)
          Constructs a 2D position from the specified ordinates in the specified CRS.
DirectPosition2D(DirectPosition point)
          Constructs a position initialized to the same values than the specified point.
DirectPosition2D(double x, double y)
          Constructs a 2D position from the specified ordinates.
DirectPosition2D(Point2D point)
          Constructs a position from the specified Point2D.
 
Method Summary
 DirectPosition2D clone()
          Returns a clone of this point.
 boolean equals(Object object)
          Compares this point with the specified object for equality.
 double[] getCoordinate()
          Returns a sequence of numbers that hold the coordinate of this position in its reference system.
 CoordinateReferenceSystem getCoordinateReferenceSystem()
          Returns the coordinate reference system in which the coordinate is given.
 int getDimension()
          The length of coordinate sequence (the number of entries).
 DirectPosition getDirectPosition()
          Returns always this, the direct position for this position.
 double getOrdinate(int dimension)
          Returns the ordinate at the specified dimension.
 int hashCode()
          Returns a hash value for this coordinate.
 void setCoordinateReferenceSystem(CoordinateReferenceSystem crs)
          Set the coordinate reference system in which the coordinate is given.
 void setLocation(DirectPosition position)
          Set this coordinate to the specified direct position.
 void setOrdinate(int dimension, double value)
          Sets the ordinate value along the specified dimension.
 Point2D toPoint2D()
          Returns a Point2D with the same coordinate as this direct position.
 String toString()
          Returns a string representation of this coordinate.
 
Methods inherited from class Point2D.Double
getX, getY, setLocation
 
Methods inherited from class Point2D
distance, distance, distance, distanceSq, distanceSq, distanceSq, setLocation
 
Methods inherited from class Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DirectPosition2D

public DirectPosition2D()
Constructs a position initialized to (0,0) with a null coordinate reference system.


DirectPosition2D

public DirectPosition2D(CoordinateReferenceSystem crs)
Constructs a position with the specified coordinate reference system.

Parameters:
crs - The coordinate reference system, or null.

DirectPosition2D

public DirectPosition2D(double x,
                        double y)
Constructs a 2D position from the specified ordinates. Despite their name, the (x,y) coordinates don't need to be oriented toward (East, North). Those parameter names simply match the x and y fields. See the class javadoc for details.

Parameters:
x - The x value.
y - The y value.

DirectPosition2D

public DirectPosition2D(CoordinateReferenceSystem crs,
                        double x,
                        double y)
Constructs a 2D position from the specified ordinates in the specified CRS. Despite their name, the (x,y) coordinates don't need to be oriented toward (East, North). Those parameter names simply match the x and y fields. The actual axis orientations are determined by the specified CRS. See the class javadoc for details.

Parameters:
crs - The coordinate reference system, or null.
x - The x value.
y - The y value.

DirectPosition2D

public DirectPosition2D(Point2D point)
Constructs a position from the specified Point2D.

Parameters:
point - The point to copy.

DirectPosition2D

public DirectPosition2D(DirectPosition point)
Constructs a position initialized to the same values than the specified point.

Parameters:
point - The point to copy.
Method Detail

getDirectPosition

public DirectPosition getDirectPosition()
Returns always this, the direct position for this position.

Specified by:
getDirectPosition in interface Position
Returns:
The direct position (may be this).
Since:
2.5

getCoordinateReferenceSystem

public final CoordinateReferenceSystem getCoordinateReferenceSystem()
Returns the coordinate reference system in which the coordinate is given. May be null if this particular DirectPosition is included in a larger object with such a reference to a coordinate reference system.

Specified by:
getCoordinateReferenceSystem in interface DirectPosition
Returns:
The coordinate reference system, or null.

setCoordinateReferenceSystem

public void setCoordinateReferenceSystem(CoordinateReferenceSystem crs)
Set the coordinate reference system in which the coordinate is given.

Parameters:
crs - The new coordinate reference system, or null.

getDimension

public final int getDimension()
The length of coordinate sequence (the number of entries). This is always 2 for DirectPosition2D objects.

Specified by:
getDimension in interface DirectPosition
Returns:
The dimensionality of this position.

getCoordinate

public double[] getCoordinate()
Returns a sequence of numbers that hold the coordinate of this position in its reference system.

Specified by:
getCoordinate in interface DirectPosition
Returns:
The coordinates

getOrdinate

public final double getOrdinate(int dimension)
                         throws IndexOutOfBoundsException
Returns the ordinate at the specified dimension.

Specified by:
getOrdinate in interface DirectPosition
Parameters:
dimension - The dimension in the range 0 to 1 inclusive.
Returns:
The coordinate at the specified dimension.
Throws:
IndexOutOfBoundsException - if the specified dimension is out of bounds.

setOrdinate

public final void setOrdinate(int dimension,
                              double value)
                       throws IndexOutOfBoundsException
Sets the ordinate value along the specified dimension.

Specified by:
setOrdinate in interface DirectPosition
Parameters:
dimension - the dimension for the ordinate of interest.
value - the ordinate value of interest.
Throws:
IndexOutOfBoundsException - if the specified dimension is out of bounds.

setLocation

public void setLocation(DirectPosition position)
                 throws MismatchedDimensionException
Set this coordinate to the specified direct position. If the specified position contains a coordinate reference system, then the CRS for this position will be set to the CRS of the specified position.

Parameters:
position - The new position for this point.
Throws:
MismatchedDimensionException - if this point doesn't have the expected dimension.

toPoint2D

public Point2D toPoint2D()
Returns a Point2D with the same coordinate as this direct position.

Returns:
This position as a point.

toString

public String toString()
Returns a string representation of this coordinate. The default implementation formats this coordinate using a shared instance of CoordinateFormat. This is okay for occasional formatting (for example for debugging purpose). But if there is a lot of positions to format, users will get better performance and more control by using their own instance of CoordinateFormat.

Overrides:
toString in class Point2D.Double

hashCode

public int hashCode()
Returns a hash value for this coordinate. This method implements the DirectPosition.hashCode() contract, not the Point2D.hashCode() contract.

Specified by:
hashCode in interface DirectPosition
Overrides:
hashCode in class Point2D
Returns:
A hash code value for this position.

equals

public boolean equals(Object object)
Compares this point with the specified object for equality. If the given object implements the DirectPosition interface, then the comparaison is performed as specified in its DirectPosition.equals(java.lang.Object) contract. Otherwise the comparaison is performed as specified in Point2D.equals(java.lang.Object).

Specified by:
equals in interface DirectPosition
Overrides:
equals in class Point2D
Parameters:
object - The object to compare with this position.
Returns:
true if the given object is equals to this position.

clone

public DirectPosition2D clone()
Returns a clone of this point.

Overrides:
clone in class Point2D
Returns:
A clone of this position.
See Also:
Object.clone()


Copyright © 1996-2014 Geotools. All Rights Reserved.