Class Geometry
- All Implemented Interfaces:
Comparable<Geometry>
- Direct Known Subclasses:
Curve,GeometryCollection,Point,Surface
Construction from standard formats:
fromWkt(String)- parses WKT or EWKT; model from EWKT prefix or CARTESIANfromWkt(String, Model)- parses WKT with explicit modelfromWkt(String, int)- parses WKT with explicit SRIDfromWkb(byte[], int)- parses ISO WKB with explicit SRID
Output formats:
toWKT()- ISO WKT without SRIDtoEWKT()- EWKT with "SRID=n;" prefixtoWKB()- 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 TypeMethodDescriptionfinal intCompares this geometry with another in a total order that is consistent withequals(Object): the result is 0 exactly when the two values are equal.abstract booleanExact equality: same type, same SRID, same coordinates (including z flag).static GeometryfromWkb(byte[] wkb, int srid) Parses ISO WKB bytes with an explicit SRID.static GeometryParses WKT or EWKT text.static GeometryParses WKT text with an explicit SRID.static GeometryParses WKT text with an explicit model.abstract inthashCode()Hash code consistent with equals.final booleanhasZ()Returns true if this geometry has z coordinates.abstract booleanisEmpty()Returns true if this geometry is empty (has no coordinates).final Modelmodel()Returns the coordinate interpretation model derived from the SRID.final intsrid()Returns the spatial reference ID.final StringtoEWKT()Returns EWKT with "SRID=n;" prefix.final StringtoString()Returns the WKT representation (same astoWKT()).final byte[]toWKB()Returns ISO WKB in big-endian byte order.final StringtoWKT()Returns ISO WKT without the SRID prefix.
-
Method Details
-
fromWkt
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 isModel.CARTESIAN.The text is read under ISO rules, where a
Ztag marks a 3D value. Text that holds noZtag 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. TheMdimension is not supported.- Parameters:
wktOrEwkt- the WKT or EWKT string- Returns:
- the parsed geometry
- Throws:
IllegalArgumentException- ifwktOrEwktis null, empty or only whitespace; if anSRID=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 typesCIRCULARSTRING,COMPOUNDCURVE,CURVEPOLYGON,MULTICURVE,MULTISURFACE,TRIANGLE,TINandPOLYHEDRALSURFACE; if anMorZMdimension tag is present, whether written apart from the type name as inPOINT Mor against it as inPOINTM,POINTZMorMULTIPOINTM; if a coordinate holdsNaNorInfinity, is not a number, or has a fourth ordinate; if the text holds aZtag 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 theZtag 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
Parses WKT text with an explicit model. An EWKTSRID=prefix is accepted only when its SRID is the SRID ofmodel, which is 0 forModel.CARTESIANand 4326 forModel.GEOGRAPHIC.- Parameters:
wkt- the WKT stringmodel-Model.CARTESIANorModel.GEOGRAPHIC- Returns:
- the parsed geometry
- Throws:
IllegalArgumentException- ifmodelis null orModel.OTHER; if the text carries an EWKTSRID=prefix whose SRID is not the SRID ofmodel; or if the text is malformed in any of the ways listed onfromWkt(String), which withModel.GEOGRAPHICincludes an x outside -180 to 180 or a y outside -90 to 90- Since:
- 4.0.0
-
fromWkt
Parses WKT text with an explicit SRID. An EWKTSRID=prefix is accepted only when its SRID equalssrid.- Parameters:
wkt- the WKT stringsrid- the spatial reference ID, applied as given and not itself validated- Returns:
- the parsed geometry
- Throws:
IllegalArgumentException- if the text carries an EWKTSRID=prefix whose SRID is notsrid, or if the text is malformed in any of the ways listed onfromWkt(String), which whensridis 4326 includes an x outside -180 to 180 or a y outside -90 to 90- Since:
- 4.0.0
-
fromWkb
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
NaNreads as an empty point. Such a point is accepted on its own and as an element of aMultiPoint, but not as a vertex of aLineStringor 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 bytessrid- the spatial reference ID, applied as given and not itself validated- Returns:
- the parsed geometry
- Throws:
IllegalArgumentException- ifwkbis null or zero length; if a byte order marker is neither0x00for big-endian nor0x01for little-endian; if a type code carries an EWKB flag bit,0x80000000for Z,0x40000000for M or0x20000000for 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 hasNaNx and y, which encodes an empty point; if only one of x and y isNaN, an x or y is infinite, or the z of a point that is not empty isNaNor infinite; if an element of aMultiPoint,MultiLineStringorMultiPolygonis not of that collection's kind; if collection nesting is deeper than 32 levels; or ifsridis 4326 and an x is outside -180 to 180 or a y is outside -90 to 90- Since:
- 4.0.0
-
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
Returns ISO WKT without the SRID prefix.- Returns:
- the WKT string
- Since:
- 4.0.0
-
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
Exact equality: same type, same SRID, same coordinates (including z flag). Coordinates are compared numerically, so -0.0 equals 0.0. -
hashCode
public abstract int hashCode()Hash code consistent with equals. Zero values are normalized so -0.0 and 0.0 hash alike. -
compareTo
Compares this geometry with another in a total order that is consistent withequals(Object): the result is 0 exactly when the two values are equal. Each key below is used only when every key before it ties:- the SRID
- emptiness, so empty geometries sort first
- 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
- the coordinates in WKB order, including z, compared numerically, so that -0.0 and 0.0 tie. The shorter sequence sorts first
- the kind, by WKB type code: Point 1, LineString 2, Polygon 3, MultiPoint 4, MultiLineString 5, MultiPolygon 6, GeometryCollection 7
- 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:
compareToin interfaceComparable<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- ifotheris null- Since:
- 4.0.0
-
toString
Returns the WKT representation (same astoWKT()).
-