Class DirectPosition2D

  • All Implemented Interfaces:
    Serializable, Cloneable, Position, Direct­Position

    public class DirectPosition2D
    extends Point2D.Double
    implements DirectPosition, Cloneable
    A two-dimensional position on top of Java2D point. This implementation is provided for inter-operability between Java2D and GeoAPI.

    This class inherits x and y fields. But despite their names, they don't need to be oriented toward East and North respectively. The (x,y) axis can have any direction and should be understood as coordinate 0 and coordinate 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.

    Note: The rational for avoiding axis orientation restriction is that other Direct­Position implementations do not have such restriction, and it would be hard to generalize. For example there is no clear "x" or "y" classification for North-East direction.
    Caution when used in collections
    Do not mix instances of this class with ordinary Point2D instances in a Hash­Set or as Hash­Map keys. It is not possible to met both Point2D​.hash­Code() and Direct­Position​.hash­Code() contracts, and this class chooses to implements the later. Consequently, the hash­Code() method of this class is inconsistent with Point2D​.equals(Object) but is consistent with Direct­Position​.equals(Object).

    In other words, it is safe to add instances of Direct­Position2D in a Hash­Set<Direct­Position>, but it is unsafe to add them in a Hash­Set<Point2D>. Collections that do not rely on hash codes, like Array­List, are safe in all cases.

    Since:
    0.3
    See Also:
    Direct­Position1D, General­Direct­Position, Point2D, Coordinate­Format, Serialized Form

    Defined in the sis-referencing module

    • 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 initialized to (0,0) 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 coordinates. Despite their names, 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 first coordinate value (not necessarily horizontal).
        y - the second coordinate value (not necessarily vertical).
      • DirectPosition2D

        public DirectPosition2D​(CoordinateReferenceSystem crs,
                                double x,
                                double y)
        Constructs a 2D position from the specified coordinates in the specified CRS. Despite their names, 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 first coordinate value (not necessarily horizontal).
        y - the second coordinate value (not necessarily vertical).
    • Method Detail

      • getDimension

        public final int getDimension()
        The length of coordinate sequence (the number of entries). This is always 2 for Direct­Position2D objects.
        Specified by:
        get­Dimension in interface Direct­Position
        Returns:
        the dimensionality of this position.
      • getCoordinateReferenceSystem

        public final CoordinateReferenceSystem getCoordinateReferenceSystem()
        Returns the coordinate reference system in which the coordinate is given. May be null if this particular Direct­Position is included in a larger object with such a reference to a CRS.
        Specified by:
        get­Coordinate­Reference­System in interface Direct­Position
        Returns:
        the coordinate reference system, or null.
      • setCoordinateReferenceSystem

        public void setCoordinateReferenceSystem​(CoordinateReferenceSystem crs)
        Sets the coordinate reference system in which the coordinate is given.
        Parameters:
        crs - the new coordinate reference system, or null.
      • getCoordinate

        public final double[] getCoordinate()
        Returns a sequence of numbers that hold the coordinate of this position in its reference system.
        API note: This method is final for ensuring consistency with the x and y fields, which are public.
        Specified by:
        get­Coordinate in interface Direct­Position
        Returns:
        the coordinate.
      • getOrdinate

        public final double getOrdinate​(int dimension)
                                 throws IndexOutOfBoundsException
        Returns the coordinate at the specified dimension.
        API note: This method is final for ensuring consistency with the x and y fields, which are public.
        Specified by:
        get­Ordinate in interface Direct­Position
        Parameters:
        dimension - the dimension in the range 0 to 1 inclusive.
        Returns:
        the coordinate at the specified dimension.
        Throws:
        Index­Out­Of­Bounds­Exception - if the specified dimension is out of bounds.
      • setLocation

        public void setLocation​(Point2D position)
        Sets this coordinate to the specified point. If the specified position is also a Direct­Position containing a non-null coordinate reference system (CRS), then the CRS for this position will be set to the CRS of the given point.
        Overrides:
        set­Location in class Point2D
        Parameters:
        position - the new position for this point.
      • toString

        public String toString()
        Formats this position in the Well Known Text (WKT) format. The output is like below:
        POINT(x y)
        The string returned by this method can be parsed by the Direct­Position2D constructor.
        Overrides:
        to­String in class Point2D​.Double
      • equals

        public boolean equals​(Object object)
        Compares this point with the specified object for equality. If the given object implements the Direct­Position interface, then the comparison is performed as specified in the Direct­Position​.equals(Object) contract. Otherwise the comparison is performed as specified in the Point2D​.equals(Object) contract.
        Specified by:
        equals in interface Direct­Position
        Overrides:
        equals in class Point2D
        Parameters:
        object - the object to compare with this position.
        Returns:
        true if the given object is equal to this position.
      • clone

        public DirectPosition2D clone()
        Returns a clone of this point.
        Overrides:
        clone in class Point2D
        Returns:
        a clone of this position.