6.19. Geospatial Functions
Presto Geospatial functions support the SQL/MM specification.They are compliant with the Open Geospatial Consortium’s (OGC) OpenGIS Specifications.As such, many Presto Geospatial functions require, or more accurately, assume thatgeometries that are operated on are both simple and valid. For example, it does notmake sense to calculate the area of a polygon that has a hole defined outside of thepolygon, or to construct a polygon from a non-simple boundary line.
Presto Geospatial functions support the Well-Known Text (WKT) form of spatial objects:
- POINT (0 0)
- LINESTRING (0 0, 1 1, 1 2)
- POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1))
- MULTIPOINT (0 0, 1 2)
- MULTILINESTRING ((0 0, 1 1, 1 2), (2 3, 3 2, 5 4))
- MULTIPOLYGON (((0 0, 4 0, 4 4, 0 4, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1)), ((-1 -1, -1 -2, -2 -2, -2 -1, -1 -1)))
- GEOMETRYCOLLECTION (POINT(2 3), LINESTRING (2 3, 3 4))
Constructors
STPoint
(_double, double) → Point
Returns a geometry type point object with the given coordinate values.
STLineFromText
(_varchar) → LineString
Returns a geometry type linestring object from WKT representation.
STPolygon
(_varchar) → Polygon
Returns a geometry type polygon object from WKT representation.
STGeometryFromText
(_varchar) → Geometry
Returns a geometry type object from WKT representation.
STAsText
(_Geometry) → varchar
Returns the WKT representation of the geometry. For empty geometries,ST_AsText(ST_LineFromText('LINESTRING EMPTY'))
will produce'MULTILINESTRING EMPTY'
andST_AsText(ST_Polygon('POLYGON EMPTY'))
will produce'MULTIPOLYGON EMPTY'
.
Relationship Tests
STContains
(_Geometry, Geometry) → boolean
Returnstrue
if and only if no points of the second geometry lie in the exteriorof the first geometry, and at least one point of the interior of the first geometrylies in the interior of the second geometry.
STCrosses
(_Geometry, Geometry) → boolean
Returnstrue
if the supplied geometries have some, but not all, interior points in common.
STDisjoint
(_Geometry, Geometry) → boolean
Returnstrue
if the give geometries do not spatially intersect –if they do not share any space together.
STEquals
(_Geometry, Geometry) → boolean
Returnstrue
if the given geometries represent the same geometry.
STIntersects
(_Geometry, Geometry) → boolean
Returnstrue
if the given geometries spatially intersect in two dimensions(share any portion of space) andfalse
if they don not (they are disjoint).
STOverlaps
(_Geometry, Geometry) → boolean
Returnstrue
if the given geometries share space, are of the same dimension,but are not completely contained by each other.
STRelate
(_Geometry, Geometry) → boolean
Returnstrue
if first geometry is spatially related to second geometry.
STTouches
(_Geometry, Geometry) → boolean
Returnstrue
if the given geometries have at least one point in common,but their interiors do not intersect.
STWithin
(_Geometry, Geometry) → boolean
Returnstrue
if first geometry is completely inside second geometry.
Operations
STBoundary
(_Geometry) → Geometry
Returns the closure of the combinatorial boundary of this geometry.
STBuffer
(_Geometry, distance) → Geometry
Returns the geometry that represents all points whose distance from the specified geometryis less than or equal to the specified distance.
STDifference
(_Geometry, Geometry) → Geometry
Returns the geometry value that represents the point set difference of the given geometries.
STEnvelope
(_Geometry) → Geometry
Returns the bounding rectangular polygon of a geometry.
STEnvelopeAsPts
(_Geometry) → Geometry
Returns an array of two points: the lower left and upper right corners of the boundingrectangular polygon of a geometry. Returns null if input geometry is empty.
STExteriorRing
(_Geometry) → Geometry
Returns a line string representing the exterior ring of the input polygon.
STIntersection
(_Geometry, Geometry) → Geometry
Returns the geometry value that represents the point set intersection of two geometries.
STSymDifference
(_Geometry, Geometry) → Geometry
Returns the geometry value that represents the point set symmetric difference of two geometries.
STUnion
(_Geometry, Geometry) → Geometry
Returns a geometry that represents the point set union of the input geometries.
This function doesn’t support geometry collections.
Accessors
STArea
(_Geometry) → double
Returns the 2D Euclidean area of a geometry.
For Point and LineString types, returns 0.0.For GeometryCollection types, returns the sum of the areas of the individualgeometries.
STCentroid
(_Geometry) → Geometry
Returns the point value that is the mathematical centroid of a geometry.
STConvexHull
(_Geometry) → Geometry
Returns the minimum convex geometry that encloses all input geometries.This function doesn’t support geometry collections.
STCoordDim
(_Geometry) → bigint
Return the coordinate dimension of the geometry.
STDimension
(_Geometry) → bigint
Returns the inherent dimension of this geometry object, which must beless than or equal to the coordinate dimension.
STDistance
(_Geometry, Geometry) → double
Returns the 2-dimensional cartesian minimum distance (based on spatial ref)between two geometries in projected units.
STGeometryN
(_Geometry, index) → Geometry
Returns the geometry element at a given index (indices start at 1).If the geometry is a collection of geometries (e.g., GEOMETRYCOLLECTION or MULTI),returns the geometry at a given index.If the given index is less than 1 or greater than the total number of elements in the collection,returnsNULL
.Use :func:STNumGeometries
to find out the total number of elements.Singular geometries (e.g., POINT, LINESTRING, POLYGON), are treated as collections of one element.Empty geometries are treated as empty collections.
ST_InteriorRingN
(_Geometry, index) → Geometry
Returns the interior ring element at the specified index (indices start at 1). Ifthe given index is less than 1 or greater than the total number of interior ringsin the input geometry, returnsNULL
. Throws an error if the input geometry isnot a polygon.Use :func:STNumInteriorRing
to find out the total number of elements.
ST_GeometryType
(_Geometry) → varchar
Returns the type of the geometry.
STIsClosed
(_Geometry) → boolean
Returnstrue
if the linestring’s start and end points are coincident.
STIsEmpty
(_Geometry) → boolean
Returnstrue
if this Geometry is an empty geometrycollection, polygon, point etc.
STIsSimple
(_Geometry) → boolean
Returnstrue
if this Geometry has no anomalous geometric points, such as self intersection or self tangency.
STIsRing
(_Geometry) → boolean
Returnstrue
if and only if the line is closed and simple.
STIsValid
(_Geometry) → boolean
Returnstrue
if and only if the input geometry is well formed.Usegeometry_invalid_reason()
to determine why the geometry is not well formed.
STLength
(_Geometry) → double
Returns the length of a linestring or multi-linestring using Euclidean measurement on atwo dimensional plane (based on spatial ref) in projected units.
STPointN
(_LineString, index) → Point
Returns the vertex of a linestring at a given index (indices start at 1).If the given index is less than 1 or greater than the total number of elements in the collection,returnsNULL
.Use :func:STNumPoints
to find out the total number of elements.
ST_XMax
(_Geometry) → double
Returns X maxima of a bounding box of a geometry.
STYMax
(_Geometry) → double
Returns Y maxima of a bounding box of a geometry.
STXMin
(_Geometry) → double
Returns X minima of a bounding box of a geometry.
STYMin
(_Geometry) → double
Returns Y minima of a bounding box of a geometry.
STStartPoint
(_Geometry) → point
Returns the first point of a LineString geometry as a Point.This is a shortcut for STPointN(geometry, 1).
simplify_geometry
(_Geometry, double) → Geometry
Returns a “simplified” version of the input geometry using the Douglas-Peucker algorithm.Will avoid creating derived geometries (polygons in particular) that are invalid.
STEndPoint
(_Geometry) → point
Returns the last point of a LineString geometry as a Point.This is a shortcut for STPointN(geometry, ST_NumPoints(geometry)).
ST_X
(_Point) → double
Return the X coordinate of the point.
STY
(_Point) → double
Return the Y coordinate of the point.
STInteriorRings
(_Geometry) → Geometry
Returns an array of all interior rings found in the input geometry, or an emptyarray if the polygon has no interior rings. Returns null if the input geometryis empty. Throws an error if the input geometry is not a polygon.
STNumGeometries
(_Geometry) → bigint
Returns the number of geometries in the collection.If the geometry is a collection of geometries (e.g., GEOMETRYCOLLECTION or MULTI),returns the number of geometries,for single geometries returns 1,for empty geometries returns 0.
STGeometries
(_Geometry) → Geometry
Returns an array of geometries in the specified collection. Returns a one-element arrayif the input geometry is not a multi-geometry. Returns null if input geometry is empty.
STNumPoints
(_Geometry) → bigint
Returns the number of points in a geometry. This is an extension to the SQL/MMSTNumPoints
function which only applies to point and linestring.
ST_NumInteriorRing
(_Geometry) → bigint
Returns the cardinality of the collection of interior rings of a polygon.
linelocate_point
(_LineString, Point) → double
Returns a float between 0 and 1 representing the location of the closest point onthe LineString to the given Point, as a fraction of total 2d line length.
Returnsnull
if a LineString or a Point is empty ornull
.
geometryinvalid_reason
(_Geometry) → varchar
Returns the reason for why the input geometry is not valid.Returns null if the input is valid.
greatcircle_distance
(_latitude1, longitude1, latitude2, longitude2) → double
Returns the great-circle distance between two points on Earth’s surface in kilometers.
Bing Tiles
These functions convert between geometries andBing tiles.
bingtile
(_x, y, zoom_level) → BingTile
Creates a Bing tile object from XY coordinates and a zoom level.Zoom levels from 1 to 23 are supported.
bingtile
(_quadKey) → BingTile
Creates a Bing tile object from a quadkey.
bingtile_at
(_latitude, longitude, zoom_level) → BingTile
Returns a Bing tile at a given zoom level containing a point at a given latitudeand longitude. Latitude must be within[-85.05112878, 85.05112878]
range.Longitude must be within[-180, 180]
range. Zoom levels from 1 to 23 are supported.
bingtiles_around
(_latitude, longitude, zoom_level) → array<BingTile>
Returns a collection of Bing tiles that surround the point specifiedby the latitude and longitude arguments at a given zoom level.
bingtile_coordinates
(_tile) → row<x, y>
Returns the XY coordinates of a given Bing tile.
bingtile_polygon
(_tile) → Geometry
Returns the polygon representation of a given Bing tile.
bingtile_quadkey
(_tile) → varchar
Returns the quadkey of a given Bing tile.
bingtile_zoom_level
(_tile) → tinyint
Returns the zoom level of a given Bing tile.
geometryto_bing_tiles
(_geometry, zoom_level) → array<BingTile>
Returns the minimum set of Bing tiles that fully covers a given geometry ata given zoom level. Zoom levels from 1 to 23 are supported.
原文: https://prestodb.io/docs/current/functions/geospatial.html