java.lang.Object
org.apache.tinkerpop.gremlin.structure.geo.Geometry
All Implemented Interfaces:
Comparable<Geometry>
Direct Known Subclasses:
Curve, GeometryCollection, Point, Surface

public abstract class Geometry extends Object implements Comparable<Geometry>
Base class for all geometry types. Geometry values are immutable.

Construction from standard formats:

Output formats:

  • toWKT() - ISO WKT without SRID
  • toEWKT() - EWKT with "SRID=n;" prefix
  • toWKB() - ISO WKB in big-endian byte order

Order. Geometries have a total natural ordering that is consistent with equals(Object), defined by compareTo(Geometry).

Since:
4.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    final int
    Compares this geometry with another in a total order that is consistent with equals(Object): the result is 0 exactly when the two values are equal.
    abstract boolean
    Exact equality: same type, same SRID, same coordinates (including z flag).
    static Geometry
    fromWkb(byte[] wkb, int srid)
    Parses ISO WKB bytes with an explicit SRID.
    static Geometry
    fromWkt(String wktOrEwkt)
    Parses WKT or EWKT text.
    static Geometry
    fromWkt(String wkt, int srid)
    Parses WKT text with an explicit SRID.
    static Geometry
    fromWkt(String wkt, Model model)
    Parses WKT text with an explicit model.
    abstract int
    Hash code consistent with equals.
    final boolean
    Returns true if this geometry has z coordinates.
    abstract boolean
    Returns true if this geometry is empty (has no coordinates).
    final Model
    Returns the coordinate interpretation model derived from the SRID.
    final int
    Returns the spatial reference ID.
    final String
    Returns EWKT with "SRID=n;" prefix.
    final String
    Returns the WKT representation (same as toWKT()).
    final byte[]
    Returns ISO WKB in big-endian byte order.
    final String
    Returns ISO WKT without the SRID prefix.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • fromWkt

      public static Geometry fromWkt(String wktOrEwkt)
      Parses WKT or EWKT text. If an EWKT prefix is present (for example "SRID=4326;POINT(1 2)"), the SRID and so the model are derived from it. Otherwise the model is Model.CARTESIAN.

      The text is read under ISO rules, where a Z tag marks a 3D value. Text that holds no Z tag at all is read as 3D when a coordinate has a third ordinate, which is how PostGIS EWKT writes a 3D value (for example "SRID=4326;POINT(1 2 3)"). A value is 2D or 3D throughout. The M dimension is not supported.

      Parameters:
      wktOrEwkt - the WKT or EWKT string
      Returns:
      the parsed geometry
      Throws:
      IllegalArgumentException - if wktOrEwkt is null, empty or only whitespace; if an SRID= prefix has no digits, has more than 10 digits, holds a value outside the range of a 32-bit integer, or is not followed by ';'; if the geometry type name is unknown, or is one of the unsupported types CIRCULARSTRING, COMPOUNDCURVE, CURVEPOLYGON, MULTICURVE, MULTISURFACE, TRIANGLE, TIN and POLYHEDRALSURFACE; if an M or ZM dimension tag is present, whether written apart from the type name as in POINT M or against it as in POINTM, POINTZM or MULTIPOINTM; if a coordinate holds NaN or Infinity, is not a number, or has a fourth ordinate; if the text holds a Z tag and a part without one has a coordinate with a third ordinate; if a parenthesis, comma or other delimiter is missing, or text follows the geometry; if one value mixes 2D and 3D parts or coordinates, whether marked by the Z tag or by the number of ordinates; if a line string has exactly 1 point; if a polygon ring has fewer than 4 points or is not closed; if collection nesting is deeper than 32 levels; or if the SRID resolves to 4326 and an x is outside -180 to 180 or a y is outside -90 to 90
      Since:
      4.0.0
    • fromWkt

      public static Geometry fromWkt(String wkt, Model model)
      Parses WKT text with an explicit model. An EWKT SRID= prefix is accepted only when its SRID is the SRID of model, which is 0 for Model.CARTESIAN and 4326 for Model.GEOGRAPHIC.
      Parameters:
      wkt - the WKT string
      model - Model.CARTESIAN or Model.GEOGRAPHIC
      Returns:
      the parsed geometry
      Throws:
      IllegalArgumentException - if model is null or Model.OTHER; if the text carries an EWKT SRID= prefix whose SRID is not the SRID of model; or if the text is malformed in any of the ways listed on fromWkt(String), which with Model.GEOGRAPHIC includes an x outside -180 to 180 or a y outside -90 to 90
      Since:
      4.0.0
    • fromWkt

      public static Geometry fromWkt(String wkt, int srid)
      Parses WKT text with an explicit SRID. An EWKT SRID= prefix is accepted only when its SRID equals srid.
      Parameters:
      wkt - the WKT string
      srid - the spatial reference ID, applied as given and not itself validated
      Returns:
      the parsed geometry
      Throws:
      IllegalArgumentException - if the text carries an EWKT SRID= prefix whose SRID is not srid, or if the text is malformed in any of the ways listed on fromWkt(String), which when srid is 4326 includes an x outside -180 to 180 or a y outside -90 to 90
      Since:
      4.0.0
    • fromWkb

      public static Geometry fromWkb(byte[] wkb, int srid)
      Parses ISO WKB bytes with an explicit SRID.

      This method accepts ISO WKB only. EWKB (PostGIS extended format with flag bits 0x80000000, 0x40000000, or 0x20000000 in the type code) is rejected.

      ISO WKB has no length prefix for a point, so a point whose x and y are both NaN reads as an empty point. Such a point is accepted on its own and as an element of a MultiPoint, but not as a vertex of a LineString or a polygon ring. Its z ordinate, if the type code carries a Z, is read and discarded without being checked.

      Parameters:
      wkb - the ISO WKB bytes
      srid - the spatial reference ID, applied as given and not itself validated
      Returns:
      the parsed geometry
      Throws:
      IllegalArgumentException - if wkb is null or zero length; if a byte order marker is neither 0x00 for big-endian nor 0x01 for little-endian; if a type code carries an EWKB flag bit, 0x80000000 for Z, 0x40000000 for M or 0x20000000 for SRID; if a type code is in the M range 2001 to 2007 or the ZM range 3001 to 3007; if a type code is outside 1 to 7 and 1001 to 1007; if some geometries carry a Z type code and others do not; if the input is truncated, a declared point, ring or geometry count is negative or too large to address, or a count needs more bytes than remain; if bytes remain after the geometry; if a line string declares exactly 1 point; if a polygon ring declares fewer than 4 points or is not closed; if a line string or ring vertex has NaN x and y, which encodes an empty point; if only one of x and y is NaN, an x or y is infinite, or the z of a point that is not empty is NaN or infinite; if an element of a MultiPoint, MultiLineString or MultiPolygon is not of that collection's kind; if collection nesting is deeper than 32 levels; or if srid is 4326 and an x is outside -180 to 180 or a y is outside -90 to 90
      Since:
      4.0.0
    • model

      public final Model model()
      Returns the coordinate interpretation model derived from the SRID. SRID 0 is CARTESIAN, SRID 4326 is GEOGRAPHIC, all others are OTHER.
      Returns:
      the model
      Since:
      4.0.0
    • srid

      public final int srid()
      Returns the spatial reference ID.
      Returns:
      the SRID (0 for CARTESIAN, 4326 for GEOGRAPHIC, or custom)
      Since:
      4.0.0
    • isEmpty

      public abstract boolean isEmpty()
      Returns true if this geometry is empty (has no coordinates). For collections, returns true if all parts are empty.
      Returns:
      true if empty
      Since:
      4.0.0
    • hasZ

      public final boolean hasZ()
      Returns true if this geometry has z coordinates.
      Returns:
      true if 3D
      Since:
      4.0.0
    • toWKT

      public final String toWKT()
      Returns ISO WKT without the SRID prefix.
      Returns:
      the WKT string
      Since:
      4.0.0
    • toEWKT

      public final String toEWKT()
      Returns EWKT with "SRID=n;" prefix.
      Returns:
      the EWKT string
      Since:
      4.0.0
    • toWKB

      public final byte[] toWKB()
      Returns ISO WKB in big-endian byte order.
      Returns:
      the WKB bytes
      Since:
      4.0.0
    • equals

      public abstract boolean equals(Object o)
      Exact equality: same type, same SRID, same coordinates (including z flag). Coordinates are compared numerically, so -0.0 equals 0.0.
      Overrides:
      equals in class Object
    • hashCode

      public abstract int hashCode()
      Hash code consistent with equals. Zero values are normalized so -0.0 and 0.0 hash alike.
      Overrides:
      hashCode in class Object
    • compareTo

      public final int compareTo(Geometry other)
      Compares this geometry with another in a total order that is consistent with equals(Object): the result is 0 exactly when the two values are equal. Each key below is used only when every key before it ties:
      1. the SRID
      2. emptiness, so empty geometries sort first
      3. the level-16 Hilbert key of the center of the 2D bounding box, for non-empty geometries, so that geometries which lie near each other sort near each other. The key is a 32-bit value compared as an unsigned integer
      4. the coordinates in WKB order, including z, compared numerically, so that -0.0 and 0.0 tie. The shorter sequence sorts first
      5. the kind, by WKB type code: Point 1, LineString 2, Polygon 3, MultiPoint 4, MultiLineString 5, MultiPolygon 6, GeometryCollection 7
      6. the structure: for each nested geometry, taken recursively, its kind, its number of parts, and whether it holds parts of its own, one with none first. This is not the emptiness of key 2: it only separates an empty point from a point with a coordinate. When the whole structure ties, a geometry without z sorts before one with z
      Specified by:
      compareTo in interface Comparable<Geometry>
      Parameters:
      other - the geometry to compare with
      Returns:
      a negative value, zero or a positive value as this geometry sorts before, with or after the other
      Throws:
      NullPointerException - if other is null
      Since:
      4.0.0
    • toString

      public final String toString()
      Returns the WKT representation (same as toWKT()).
      Overrides:
      toString in class Object