Class Envelope2D

All Implemented Interfaces:
Shape, Serializable, Cloneable, Emptiable, Envelope

public class Envelope2D extends Rectangle2D.Double implements Envelope, Emptiable, Cloneable
A two-dimensional envelope on top of Java2D rectangle. 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.

Crossing the anti-meridian of a Geographic CRS

The Web Coverage Service (WCS) specification authorizes (with special treatment) cases where upper < lower at least in the longitude case. They are envelopes crossing the anti-meridian, like the red box below (the green box is the usual case). For Envelope2D objects, they are rectangle with negative width or height field values. The default implementation of methods listed in the right column can handle such cases. The get­Min­X(), get­Min­Y(), get­Max­X(), get­Max­Y(), get­Center­X(), get­Center­Y(), get­Width() and get­Height() methods delegate to the above-cited methods.
Since:
0.3
See Also:

Defined in the sis-referencing module

  • Constructor Details

    • Envelope2D

      public Envelope2D()
      Constructs an initially empty envelope with no CRS.
    • Envelope2D

      Constructs a two-dimensional envelope defined by the specified coordinates. The lower­Corner and upper­Corner arguments are not necessarily the minimal and maximal values respectively. See the class javadoc about crossing the anti-meridian for more details.
      Parameters:
      lower­Corner - the fist position.
      upper­Corner - the second position.
      Throws:
      Mismatched­Reference­System­Exception - if the two positions don't use the same CRS.
      Mismatched­Dimension­Exception - if the two positions are not two-dimensional.
    • Envelope2D

      public Envelope2D(Envelope envelope) throws MismatchedDimensionException
      Constructs a two-dimensional envelope defined by an other Envelope.
      Parameters:
      envelope - the envelope to copy (can not be null).
      Throws:
      Mismatched­Dimension­Exception - if the given envelope is not two-dimensional.
    • Envelope2D

      public Envelope2D(GeographicBoundingBox box)
      Constructs a new envelope with the same data than the specified geographic bounding box. The coordinate reference system is set to the default geographic CRS. Axis order is (longitude, latitude).
      Parameters:
      box - The bounding box to copy (can not be null).
    • Envelope2D

      Constructs two-dimensional envelope defined by an other Rectangle2D. If the given rectangle has negative width or height, they will be interpreted as an envelope crossing the anti-meridian.
      Parameters:
      crs - the coordinate reference system, or null.
      rect - the rectangle to copy (can not be null).
      Throws:
      Mismatched­Dimension­Exception - if the given CRS is not two-dimensional.
    • Envelope2D

      public Envelope2D(CoordinateReferenceSystem crs, double x, double y, double width, double height) throws MismatchedDimensionException
      Constructs two-dimensional envelope defined by the specified coordinates. 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 minimal value.
      y - the y minimal value.
      width - the envelope width. May be negative for envelope crossing the anti-meridian.
      height - the envelope height. May be negative for envelope crossing the anti-meridian.
      Throws:
      Mismatched­Dimension­Exception - if the given CRS is not two-dimensional.
  • Method Details

    • getCoordinateReferenceSystem

      public final CoordinateReferenceSystem getCoordinateReferenceSystem()
      Returns the coordinate reference system in which the coordinates are given.
      Specified by:
      get­Coordinate­Reference­System in interface Envelope
      Returns:
      the coordinate reference system, or null.
    • setCoordinateReferenceSystem

      public void setCoordinateReferenceSystem(CoordinateReferenceSystem crs)
      Sets the coordinate reference system in which the coordinate are given. This method does not reproject the envelope. If the envelope coordinates need to be transformed to the new CRS, consider using Envelopes​.transform(Envelope, Coordinate­Reference­System) instead.
      Parameters:
      crs - the new coordinate reference system, or null.
    • setRect

      public void setRect(Rectangle2D rect)
      Sets this envelope to the given rectangle. If the given rectangle is also an instance of Envelope (typically as another Envelope2D) and has a non-null Coordinate Reference System (CRS), then the CRS of this envelope will be set to the CRS of the given envelope.
      Overrides:
      set­Rect in class Rectangle2D​.Double
      Parameters:
      rect - the rectangle to copy coordinates from.
      Since:
      0.8
    • getDimension

      public final int getDimension()
      Returns the number of dimensions, which is always 2.
      Specified by:
      get­Dimension in interface Envelope
      Returns:
      always 2 for bi-dimensional objects.
    • getLowerCorner

      public DirectPosition2D getLowerCorner()
      The limits in the direction of decreasing coordinate values for the two dimensions. This is typically a coordinate position consisting of the minimal coordinates for the two dimensions for all points within the Envelope.

      The object returned by this method is a copy. Change in the returned position will not affect this envelope, and conversely.

      Note: The Web Coverage Service (WCS) 1.1 specification uses an extended interpretation of the bounding box definition. In a WCS 1.1 data structure, the lower corner defines the edges region in the directions of decreasing coordinate values in the envelope CRS. This is usually the algebraic minimum coordinates, but not always. For example, an envelope crossing the anti-meridian could have a lower corner longitude greater than the upper corner longitude. Such extended interpretation applies mostly to axes having WRAPAROUND range meaning.
      Specified by:
      get­Lower­Corner in interface Envelope
      Returns:
      a copy of the lower corner, typically (but not necessarily) containing minimal coordinate values.
      See Also:
    • getUpperCorner

      public DirectPosition2D getUpperCorner()
      The limits in the direction of increasing coordinate values for the two dimensions. This is typically a coordinate position consisting of the maximal coordinates for the two dimensions for all points within the Envelope.

      The object returned by this method is a copy. Change in the returned position will not affect this envelope, and conversely.

      Note: The Web Coverage Service (WCS) 1.1 specification uses an extended interpretation of the bounding box definition. In a WCS 1.1 data structure, the upper corner defines the edges region in the directions of increasing coordinate values in the envelope CRS. This is usually the algebraic maximum coordinates, but not always. For example, an envelope crossing the anti-meridian could have an upper corner longitude less than the lower corner longitude. Such extended interpretation applies mostly to axes having WRAPAROUND range meaning.
      Specified by:
      get­Upper­Corner in interface Envelope
      Returns:
      a copy of the upper corner, typically (but not necessarily) containing maximal coordinate values.
      See Also:
    • getMedian

      public DirectPosition2D getMedian()
      A coordinate position consisting of all the median coordinate values.

      The object returned by this method is a copy. Change in the returned position will not affect this envelope, and conversely.

      Returns:
      a copy of the median coordinates.
      Since:
      1.1
      See Also:
    • getMinimum

      public double getMinimum(int dimension) throws IndexOutOfBoundsException
      Returns the minimal coordinate along the specified dimension. This method handles anti-meridian as documented in the Abstract­Envelope​.get­Minimum(int) method.
      Specified by:
      get­Minimum in interface Envelope
      Parameters:
      dimension - the dimension to query.
      Returns:
      the minimal coordinate value along the given dimension.
      Throws:
      Index­Out­Of­Bounds­Exception - if the given index is out of bounds.
    • getMaximum

      public double getMaximum(int dimension) throws IndexOutOfBoundsException
      Returns the maximal coordinate along the specified dimension. This method handles anti-meridian as documented in the Abstract­Envelope​.get­Maximum(int) method.
      Specified by:
      get­Maximum in interface Envelope
      Parameters:
      dimension - the dimension to query.
      Returns:
      the maximal coordinate value along the given dimension.
      Throws:
      Index­Out­Of­Bounds­Exception - if the given index is out of bounds.
    • getMedian

      public double getMedian(int dimension) throws IndexOutOfBoundsException
      Returns the median coordinate along the specified dimension. This method handles anti-meridian as documented in the Abstract­Envelope​.get­Median(int) method.
      Specified by:
      get­Median in interface Envelope
      Parameters:
      dimension - the dimension to query.
      Returns:
      the mid coordinate value along the given dimension.
      Throws:
      Index­Out­Of­Bounds­Exception - if the given index is out of bounds.
      See Also:
    • getSpan

      public double getSpan(int dimension) throws IndexOutOfBoundsException
      Returns the envelope span along the specified dimension. This method handles anti-meridian as documented in the Abstract­Envelope​.get­Span(int) method.
      Specified by:
      get­Span in interface Envelope
      Parameters:
      dimension - the dimension to query.
      Returns:
      the rectangle width or height, depending the given dimension.
      Throws:
      Index­Out­Of­Bounds­Exception - if the given index is out of bounds.
    • getMinX

      public double getMinX()
      Returns the minimal coordinate value for dimension 0. The default implementation invokes get­Minimum(0). The result is the standard Rectangle2D value (namely x) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Min­X in class Rectangular­Shape
      Returns:
      the minimal coordinate value for dimension 0.
    • getMinY

      public double getMinY()
      Returns the minimal coordinate value for dimension 1. The default implementation invokes get­Minimum(1). The result is the standard Rectangle2D value (namely y) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Min­Y in class Rectangular­Shape
      Returns:
      the minimal coordinate value for dimension 1.
    • getMaxX

      public double getMaxX()
      Returns the maximal coordinate value for dimension 0. The default implementation invokes get­Minimum(0). The result is the standard Rectangle2D value (namely x + width) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Max­X in class Rectangular­Shape
      Returns:
      the maximal coordinate value for dimension 0.
    • getMaxY

      public double getMaxY()
      Returns the maximal coordinate value for dimension 1. The default implementation invokes get­Minimum(1). The result is the standard Rectangle2D value (namely y + height) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Max­Y in class Rectangular­Shape
      Returns:
      the maximal coordinate value for dimension 1.
    • getCenterX

      public double getCenterX()
      Returns the median coordinate value for dimension 0. The default implementation invokes get­Median(0). The result is the standard Rectangle2D value (namely x + width/2) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Center­X in class Rectangular­Shape
      Returns:
      the median coordinate value for dimension 0.
    • getCenterY

      public double getCenterY()
      Returns the median coordinate value for dimension 1. The default implementation invokes get­Median(1). The result is the standard Rectangle2D value (namely y + height/2) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Center­Y in class Rectangular­Shape
      Returns:
      the median coordinate value for dimension 1.
    • getWidth

      public double getWidth()
      Returns the span for dimension 0. The default implementation invokes get­Span(0). The result is the standard Rectangle2D value (namely width) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Width in class Rectangle2D​.Double
      Returns:
      the span for dimension 0.
    • getHeight

      public double getHeight()
      Returns the span for dimension 1. The default implementation invokes get­Span(1). The result is the standard Rectangle2D value (namely height) only if the envelope is not crossing the anti-meridian.
      Overrides:
      get­Height in class Rectangle2D​.Double
      Returns:
      the span for dimension 1.
    • isEmpty

      public boolean isEmpty()
      Determines whether the envelope is empty. A negative Rectangle2D.Double.width or (@linkplain #height} is considered as a non-empty area if the corresponding axis has the wraparound range meaning.

      Note that if the Rectangle2D.Double.width or Rectangle2D.Double.height value is Na­N, then the envelope is considered empty. This is different than the default Rectangle2D​.Double​.is­Empty() implementation, which doesn't check for Na­N values.

      Specified by:
      is­Empty in interface Emptiable
      Overrides:
      is­Empty in class Rectangle2D​.Double
      Returns:
      true if this envelope is empty.
    • toRectangles

      public Rectangle2D.Double[] toRectangles()
      Returns this envelope as non-empty Java2D rectangle objects. This method returns an array of length 0, 1, 2 or 4 depending on whether the envelope crosses the anti-meridian or the limit of any other axis having wraparound range meaning. More specifically:
      • If this envelope is empty, then this method returns an empty array.
      • If this envelope does not have any wraparound behavior, then this method returns a copy of this envelope as an instance of Rectangle2D​.Double in an array of length 1.
      • If this envelope crosses the anti-meridian (a.k.a. date line) then this method represents this envelope as two separated rectangles.
      • While uncommon, the envelope could theoretically crosses the limit of other axis having wraparound range meaning. If wraparound occur along the two axes, then this method represents this envelope as four separated rectangles.
      API note: The return type is the Rectangle2D​.Double implementation class rather than the Rectangle2D abstract class because the Envelope2D class hierarchy already exposes this implementation choice.
      Returns:
      a representation of this envelope as an array of non-empty Java2D rectangles. The array never contains this.
      Since:
      0.4
      See Also:
    • contains

      public boolean contains(double px, double py)
      Tests if a specified coordinate is inside the boundary of this envelope. If it least one of the given coordinate value is Na­N, then this method returns false.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than Abstract­Envelope​.contains(Direct­Position).
      Specified by:
      contains in interface Shape
      Overrides:
      contains in class Rectangle2D
      Parameters:
      px - the first coordinate value of the point to text.
      py - the second coordinate value of the point to text.
      Returns:
      true if the specified coordinate is inside the boundary of this envelope; false otherwise.
    • contains

      public boolean contains(Rectangle2D rect)
      Returns true if this envelope completely encloses the specified rectangle. If this envelope or the given rectangle have at least one Na­N value, then this method returns false.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than Abstract­Envelope​.contains(Envelope).
      Specified by:
      contains in interface Shape
      Overrides:
      contains in class Rectangular­Shape
      Parameters:
      rect - the rectangle to test for inclusion.
      Returns:
      true if this envelope completely encloses the specified rectangle.
    • contains

      public boolean contains(double rx, double ry, double rw, double rh)
      Returns true if this envelope completely encloses the specified rectangle. If this envelope or the given rectangle have at least one Na­N value, then this method returns false.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than Abstract­Envelope​.contains(Envelope).
      Specified by:
      contains in interface Shape
      Overrides:
      contains in class Rectangle2D
      Parameters:
      rx - the x coordinate of the lower corner of the rectangle to test for inclusion.
      ry - the y coordinate of the lower corner of the rectangle to test for inclusion.
      rw - the width of the rectangle to test for inclusion. May be negative if the rectangle spans the anti-meridian.
      rh - the height of the rectangle to test for inclusion. May be negative.
      Returns:
      true if this envelope completely encloses the specified one.
    • intersects

      public boolean intersects(Rectangle2D rect)
      Returns true if this envelope intersects the specified envelope. If this envelope or the given rectangle have at least one Na­N value, then this method returns false.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than Abstract­Envelope​.intersects(Envelope).
      Specified by:
      intersects in interface Shape
      Overrides:
      intersects in class Rectangular­Shape
      Parameters:
      rect - the rectangle to test for intersection.
      Returns:
      true if this envelope intersects the specified rectangle.
    • intersects

      public boolean intersects(double rx, double ry, double rw, double rh)
      Returns true if this envelope intersects the specified envelope. If this envelope or the given rectangle have at least one Na­N value, then this method returns false.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than Abstract­Envelope​.intersects(Envelope).
      Specified by:
      intersects in interface Shape
      Overrides:
      intersects in class Rectangle2D
      Parameters:
      rx - the x coordinate of the lower corner of the rectangle to test for intersection.
      ry - the y coordinate of the lower corner of the rectangle to test for intersection.
      rw - the width of the rectangle to test for inclusion. May be negative if the rectangle spans the anti-meridian.
      rh - the height of the rectangle to test for inclusion. May be negative.
      Returns:
      true if this envelope intersects the specified rectangle.
    • createIntersection

      public Envelope2D createIntersection(Rectangle2D rect)
      Returns the intersection of this envelope with the specified rectangle. If this envelope or the given rectangle have at least one Na­N values, then this method returns an empty envelope.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than General­Envelope​.intersect(Envelope).
      Overrides:
      create­Intersection in class Rectangle2D​.Double
      Parameters:
      rect - the rectangle to be intersected with this envelope.
      Returns:
      the intersection of the given rectangle with this envelope.
    • createUnion

      public Envelope2D createUnion(Rectangle2D rect)
      Returns the union of this envelope with the specified rectangle. The default implementation clones this envelope, then delegates to add(Rectangle2D).
      Overrides:
      create­Union in class Rectangle2D​.Double
      Parameters:
      rect - the rectangle to add to this envelope.
      Returns:
      the union of the given rectangle with this envelope.
    • add

      public void add(Rectangle2D rect)
      Adds an other rectangle to this rectangle. The resulting rectangle is the union of the two Rectangle objects.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than General­Envelope​.add(Envelope), except if the result is a rectangle expanding to infinities. In that later case, the field values are set to Na­N because infinite values are a problematic in Rectangle2D objects.
      Overrides:
      add in class Rectangle2D
      Parameters:
      rect - the rectangle to add to this envelope.
    • add

      public void add(double px, double py)
      Adds a point to this rectangle. The resulting rectangle is the smallest rectangle that contains both the original rectangle and the specified point.

      After adding a point, a call to contains(double, double) with the added point as an argument will return true, except if one of the point coordinates was Double​.Na­N in which case the corresponding coordinate has been ignored.

      Crossing the anti-meridian of a Geographic CRS

      This method supports anti-meridian in the same way than General­Envelope​.add(Direct­Position).
      Overrides:
      add in class Rectangle2D
      Parameters:
      px - the first coordinate of the point to add.
      py - the second coordinate of the point to add.
    • equals

      public boolean equals(Object object)
      Compares the specified object with this envelope for equality. If the given object is not an instance of Envelope2D, then the two objects are compared as plain rectangles, i.e. the coordinate reference system of this envelope is ignored.

      Note on hash­Code()

      This class does not override the Rectangle2D​.hash­Code() method for consistency with the Rectangle2D​.equals(Object) method, which compare arbitrary Rectangle2D implementations.
      Overrides:
      equals in class Rectangle2D
      Parameters:
      object - the object to compare with this envelope.
      Returns:
      true if the given object is equal to this envelope.
    • boundsEquals

      public boolean boundsEquals(Envelope that, int xDim, int yDim, double eps)
      Returns true if this envelope bounds is equal to that envelope bounds in two specified dimensions. The coordinate reference system is not compared, since it doesn't need to have the same number of dimensions.
      Parameters:
      that - the envelope to compare to.
      x­Dim - the dimension of that envelope to compare to the x dimension of this envelope.
      y­Dim - the dimension of that envelope to compare to the y dimension of this envelope.
      eps - a small tolerance number for floating point number comparisons. This value will be scaled according this envelope width and height.
      Returns:
      true if the envelope bounds are the same (up to the specified tolerance level) in the specified dimensions, or false otherwise.
    • clone

      public Envelope2D clone()
      Returns a clone of this envelope.
      Overrides:
      clone in class Rectangular­Shape
      Returns:
      a clone of this envelope.
    • toString

      public String toString()
      Formats this envelope as a "BOX" element. The output is of the form "BOX(lower corner,upper corner)". Example:
      BOX(-90 -180, 90 180)
      Overrides:
      to­String in class Rectangle2D​.Double
      See Also: