==================== Geospatial Operators ==================== Geometry Functions ------------------ Velox has a set of functions that operator on Geometries, conforming to the Open Geospatial Consortium's (OGC) `OpenGIS Specifications`_. These are piecewise-linear planar geometries that are 0-, 1-, or 2-dimensional and collections of such geometries. They support a variety of operations, including spatial relationship predicates (e.g., "do these geometries intersect?"), binary operations (e.g., "what is the union of these geometries?"), and unary operations (e.g., "what is the area of this geometry?"). The basis for the Geometry type is a plane. The shortest path between two points on the plane is a straight line. That means calculations on geometries (areas, distances, lengths, intersections, etc) can be calculated using cartesian mathematics and straight line vectors. Functions that begin with the ST_ prefix are compliant with the `SQL/MM Part 3: Spatial`_ specification`. As such, many Geometry functions assume that geometries that are operated on are both simple and valid. Functions may raise an exception or return an arbitrary value for non-simple or non-valid geometries. For example, it does not make sense to calculate the area of a polygon that has a hole defined outside of the polygon, or to construct a polygon from a non-simple boundary line. Geometries can be constructed in several ways. Two of the most common are ``ST_GeometryFromText`` and ``ST_GeomFromBinary``. ``ST_GeometryFromText`` accepts the Well-Known Text (WKT) format:: * 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)) ``ST_GeomFromBinary`` accepts the Well-Known Binary (WKB) format. In WKT/WKB, the coordinate order is (x, y). The details of both WKT and WKB can be found `here `_. Geometry Constructors --------------------- .. function:: ST_GeometryFromText(wkt: varchar) -> geometry: Geometry Creates a Geometry object from the Well-Known Text (WKT) formatted string. Ill-formed strings will return a User Error. .. function:: ST_GeomFromBinary(wkb: varbinary) -> geometry: Geometry Creates a Geometry object from the Well-Known Binary (WKB) formatted binary. Ill-formed binary will return a User Error. .. function:: ST_AsText(geometry: Geometry) -> wkt: varchar Returns the Well-Known Text (WKT) formatted string of the Geometry object. .. function:: ST_AsBinary(geometry: Geometry) -> wkb: varbinary Returns the Well-Known Binary (WKB) formatted binary of the Geometry object. .. function:: ST_Point(x: double, y: double) -> geometry: Geometry Returns the Point geometry at the given coordinates. This will raise an error if ``x`` or ``y`` is ``NaN`` or ``infinity``. .. function:: ST_Polygon(wkt: varchar) -> polygon: Geometry Returns a geometry type polygon object from WKT representation. .. function:: ST_LineFromText(wkt: varchar) -> linestring: Geometry Returns a geometry type linestring object from WKT representation. An error is returned if the input WKT represents a valid non-LineString geometry. Null input returns null output. .. function:: ST_LineString(points: array(Geometry)) -> linestring: Geometry Returns a LineString formed from an array of points. If there are fewer than two non-empty points in the input array, an empty LineString will be returned. Throws an exception if any element in the array is null or empty or same as the previous one. The returned geometry may not be simple, e.g. may self-intersect or may contain duplicate vertexes depending on the input. .. function:: ST_MultiPoint(points: array(Geometry)) -> multipoint: Geometry Returns a MultiPoint geometry object formed from the specified points. Return null if input array is empty. Throws an exception if any element in the array is null or empty. The returned geometry may not be simple and may contain duplicate points if input array has duplicates. .. function:: to_spherical_geography(input: Geometry) -> output: SphericalGeography Converts a ``Geometry`` object to a SphericalGeography object on the sphere of the Earth’s radius. For each point of the input geometry, it verifies that point.x is within [-180.0, 180.0] and point.y is within [-90.0, 90.0], and uses them as (longitude, latitude) degrees to construct the shape of the ``SphericalGeography`` result. .. function:: to_geometry(input: SphericalGeography) -> output: Geometry Converts a SphericalGeography object to a Geometry object. Spatial Predicates ------------------ Spatial Predicates are binary functions that return a boolean value indicating whether a given spatial relationship holds. These relations are defined by the `DE-9IM`_ intersection matrix model. The model defines intersection patterns such as "overlaps" or "touches." The exact conditions can be surprising (especially for empty geometries), so be sure to read the documentation for the specific function you are using. .. function:: ST_Contains(geometry1: Geometry, geometry2: Geometry) -> boolean Returns ``true`` if and only if no points of the second geometry lie in the exterior of the first geometry, and at least one point of the interior of the first geometry lies in the interior of the second geometry. .. function:: ST_Crosses(geometry1: Geometry, geometry2: Geometry) -> boolean Returns ``true`` if the supplied geometries have some, but not all, interior points in common. .. function:: ST_Disjoint(geometry1: Geometry, geometry2: Geometry) -> boolean Returns ``true`` if the give geometries do not spatially intersect -- if they do not share any space together. .. function:: ST_Equals(geometry1: Geometry, geometry2: Geometry) -> boolean Returns ``true`` if the given geometries represent the same geometry according to ISO SQL/MM semantics. Also returns ``true`` if both geometries are empty, regardless of their geometry types. .. function:: ST_Intersects(geometry1: Geometry, geometry2: Geometry) -> boolean Returns ``true`` if the given geometries spatially intersect in two dimensions (share any portion of space) and ``false``0 if they do not (they are disjoint). .. function:: ST_Overlaps(geometry1: Geometry, geometry2: Geometry) -> boolean Returns ``true`` if the given geometries share space, are of the same dimension, but are not completely contained by each other. .. function:: ST_Relate(geometry1: Geometry, geometry2: Geometry, relation: varchar) -> boolean Returns true if first geometry is spatially related to second geometry as described by the relation. The relation is a string like ``'"1*T***T**'``: please see the description of DM-9IM for more details. .. function:: ST_Touches(geometry1: Geometry, geometry2: Geometry) -> boolean Returns ``true`` if the given geometries have at least one point in common, but their interiors do not intersect. .. function:: ST_Within(geometry1: Geometry, geometry2: Geometry) -> boolean Returns true if first geometry is completely inside second geometry. .. _DE-9IM: https://en.wikipedia.org/wiki/DE-9IM Spatial Operations ------------------ .. function:: ST_Boundary(geometry: Geometry) -> boundary: Geometry Returns the closure of the combinatorial boundary of ``geometry``. Empty geometry inputs result in empty output. .. function:: ST_Difference(geometry1: Geometry, geometry2: Geometry) -> difference: Geometry Returns the geometry that represents the portion of ``geometry1`` that is not contained in ``geometry2``. .. function:: ST_Intersection(geometry1: Geometry, geometry2: Geometry) -> intersection: Geometry Returns the geometry that represents the portion of ``geometry1`` that is also contained in ``geometry2``. .. function:: ST_SymDifference(geometry1: Geometry, geometry2: Geometry) -> symdifference: Geometry Returns the geometry that represents the portion of ``geometry1`` that is not contained in ``geometry2`` as well as the portion of ``geometry1`` that is not congtained in ``geometry1``. .. function:: ST_Union(geometry1: Geometry, geometry2: Geometry) -> intersection: Geometry Returns the geometry that represents the all points in either ``geometry1`` or ``geometry2``. .. function:: ST_Envelope(geometry: Geometry) -> envelope: Geometry Returns the bounding rectangular polygon of a ``geometry``. Empty input will result in empty output. .. function:: ST_ExteriorRing(geometry: Geometry) -> output: Geometry Returns a LineString representing the exterior ring of the input polygon. Empty or null inputs result in null output. Non-polygon types will return an error. .. function:: expand_envelope(geometry: Geometry, distance: double) -> output: Geometry Returns the bounding rectangular polygon of a geometry, expanded by a distance. Empty geometries will return an empty polygon. Negative or NaN distances will return an error. Positive infinity distances may lead to undefined results. .. function:: geometry_union(geometries: array(Geometry)) -> union: Geometry Returns a geometry that represents the point set union of the input geometries. Performance of this function, in conjunction with array_agg() to first aggregate the input geometries, may be better than geometry_union_agg(), at the expense of higher memory utilization. Null elements in the input array are ignored. Empty array input returns null. .. function:: geometry_union_agg(geometry: Geometry) -> union: Geometry Returns a geometry that represents the point set union of the aggregated input geometries. The result type depends on the input geometries: * Geometries of the same type are merged into the corresponding ``MULTI*`` type (e.g., multiple ``POINT`` inputs yield a ``MULTIPOINT``). * Adjacent or overlapping polygons are dissolved into a single ``POLYGON``. * Geometries of different types are combined into a ``GEOMETRYCOLLECTION``. * A geometry that is fully contained within another is absorbed and does not appear separately in the result. NULL inputs are ignored. Empty geometry inputs (e.g., ``POINT EMPTY``) contribute nothing to the union; if all inputs are empty geometries the result is ``GEOMETRYCOLLECTION EMPTY``. Returns NULL when there are no rows or all inputs are NULL. The union semantics are defined by the underlying GEOS `Geometry::Union() `_ operation. .. function:: convex_hull_agg(geometry: Geometry) -> hull: Geometry Returns a geometry that represents the convex hull of the combined point set of all aggregated input geometries. The result type depends on the geometry of the inputs: * A single point, or multiple identical points, yields a ``POINT``. * Collinear points yield a ``LINESTRING``. * All other non-empty inputs yield a ``POLYGON``. * If all inputs are empty geometries the result is ``GEOMETRYCOLLECTION EMPTY``. NULL inputs are ignored. Empty geometry inputs (e.g., ``POINT EMPTY``) contribute no points to the hull computation. Returns NULL when there are no rows or all inputs are NULL. The convex hull semantics are defined by the underlying GEOS `Geometry::convexHull() `_ operation. Accessors --------- .. function:: ST_IsValid(geometry: Geometry) -> valid: bool Returns if ``geometry`` is valid, according to `SQL/MM Part 3: Spatial`_. Examples of non-valid geometries include Polygons with self-intersecting shells. .. function:: ST_IsSimple(geometry: Geometry) -> simple: bool Returns if ``geometry`` is simple, according to `SQL/MM Part 3: Spatial`_. Examples of non-simple geometries include LineStrings with self-intersections, Polygons with empty rings for holes, and more. .. function:: ST_IsClosed(geometry: Geometry) -> closed: bool Returns true if the LineString’s start and end points are coincident. Will return an error if the input geometry is not a LineString or MultiLineString. .. function:: ST_IsRing(geometry: Geometry) -> ring: bool Returns true if and only if the line is closed and simple. Will return an error if input geometry is not a LineString. .. function:: ST_IsEmpty(geometry: Geometry) -> empty: bool Returns true if and only if this Geometry is an empty GeometryCollection, Polygon, Point etc. .. function:: ST_Length(geometry: Geometry) -> length: double Returns the length of a LineString or MultiLineString using Euclidean measurement on a two dimensional plane (based on spatial ref) in projected units. Will return an error if the input geometry is not a LineString or MultiLineString. .. function:: ST_Length(sphericalgeography: SphericalGeography) -> length: double :noindex: Returns the length of a ``LineString`` or ``MultiLineString`` on a spherical model of the Earth. This is equivalent to the sum of great-circle distances between adjacent points on the ``LineString``. .. function:: ST_PointN(linestring: Geometry, index: integer) -> point: geometry 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, returns NULL. .. function:: ST_Points(geometry: Geometry) -> points: array(geometry) Returns an array of points in a geometry. Empty or null inputs return null. .. function:: ST_NumPoints(geometry: Geometry) -> points: bigint Returns the number of points in a geometry. This is an extension to the SQL/MM ``ST_NumPoints`` function which only applies to point and linestring. .. function:: geometry_nearest_points(geometry1: Geometry, geometry2: Geometry) -> points: array(geometry) Returns the points on each geometry nearest the other. If either geometry is empty, return null. Otherwise, return an array of two Points that have the minimum distance of any two points on the geometries. The first Point will be from the first Geometry argument, the second from the second Geometry argument. If there are multiple pairs with the minimum distance, one pair is chosen arbitrarily. .. function:: ST_EnvelopeAsPts(geometry: Geometry) -> points: array(geometry) Returns an array of two points: the lower left and upper right corners of the bounding rectangular polygon of a geometry. Empty or null inputs return null. .. function:: geometry_invalid_reason(geometry: Geometry) -> reason: varchar If ``geometry`` is not valid or not simple, return a description of the reason. If the geometry is valid and simple (or ``NULL``), return ``NULL``. This function is relatively expensive. .. function:: great_circle_distance(latitude1, longitude1, latitude2, longitude2) -> double Returns the great-circle distance between two points on Earth's surface in kilometers. .. function:: ST_Area(geometry: Geometry) -> area: double Returns the 2D Euclidean area of ``geometry``. For Point and LineString types, returns 0.0. For GeometryCollection types, returns the sum of the areas of the individual geometries. Empty geometries return 0. .. function:: ST_Area(sphericalgeography: SphericalGeography) -> area: double :noindex: Returns the area of a polygon or multi-polygon in square meters using a spherical model for Earth. .. function:: ST_Centroid(geometry: Geometry) -> geometry: Geometry Returns the point value that is the mathematical centroid of ``geometry``. Empty geometry inputs result in null output. .. function:: ST_Centroid(SphericalGeography) -> Point :noindex: Returns the point value that is the mathematical centroid of a spherical geometry. Empty geometry inputs result in null output. It supports Points and MultiPoints as input and returns the three-dimensional centroid projected onto the surface of the (spherical) Earth. For example, MULTIPOINT (0 -45, 0 45, 30 0, -30 0) returns Point(0, 0). Note: In the case that the three-dimensional centroid is at (0, 0, 0) (e.g. MULTIPOINT (0 0, -180 0)), the spherical centroid is undefined and an arbitrary point will be returned. .. function:: ST_Distance(geometry1: Geometry, geometry2: Geometry) -> distance: double Returns the 2-dimensional cartesian minimum distance (based on spatial ref) between two geometries in projected units. Empty geometries result in null output. .. function:: ST_Distance(sphericalgeography1: SphericalGeography, sphericalgeography2: SphericalGeography) -> distance: double :noindex: Returns the great-circle distance in meters between two SphericalGeography points. .. function:: ST_GeometryType(geometry: Geometry) -> type: varchar Returns the type of the geometry. .. function:: ST_X(geometry: Geometry) -> x: double Returns the ``x`` coordinate of the geometry if it is a Point. Returns ``null`` if the geometry is empty. Raises an error if the geometry is not a Point and not empty. .. function:: ST_Y(geometry: Geometry) -> x: double Returns the ``y`` coordinate of the geometry if it is a Point. Returns ``null`` if the geometry is empty. Raises an error if the geometry is not a Point and not empty. .. function:: ST_XMin(geometry: Geometry) -> x: double Returns the minimum ``x`` coordinate of the geometries bounding box. Returns ``null`` if the geometry is empty. .. function:: ST_YMin(geometry: Geometry) -> y: double Returns the minimum ``y`` coordinate of the geometries bounding box. Returns ``null`` if the geometry is empty. .. function:: ST_XMax(geometry: Geometry) -> x: double Returns the maximum ``x`` coordinate of the geometries bounding box. Returns ``null`` if the geometry is empty. .. function:: ST_YMax(geometry: Geometry) -> y: double Returns the maximum ``y`` coordinate of the geometries bounding box. Returns ``null`` if the geometry is empty. .. function:: ST_StartPoint(geometry: Geometry) -> point: Geometry Returns the first point of a LineString geometry as a Point. This is a shortcut for ``ST_PointN(geometry, 1)``. Empty input will return ``null``. .. function:: ST_EndPoint(geometry: Geometry) -> point: Geometry Returns the last point of a LineString geometry as a Point. This is a shortcut for ``ST_PointN(geometry, ST_NumPoints(geometry))``. Empty input will return ``null``. .. function:: ST_GeometryN(geometry: Geometry, index: integer) -> geometry: 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, returns NULL. Use ``:func:ST_NumGeometries`` 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. .. function:: ST_InteriorRingN(geometry: Geometry, index: integer) -> geometry: Geometry Returns the interior ring element at the specified index (indices start at 1). If the given index is less than 1 or greater than the total number of interior rings in the input ``geometry``, returns NULL. Throws an error if the input geometry is not a polygon. Use ``:func:ST_NumInteriorRing`` to find out the total number of elements. .. function:: ST_NumGeometries(geometry: Geometry) -> output: integer 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. Note that empty geometries inside of a GeometryCollection will count as a geometry if and only if there is at least 1 non-empty geometry in the collection. e.g. ``ST_NumGeometries(ST_GeometryFromText('GEOMETRYCOLLECTION(POINT EMPTY)'))`` will evaluate to 0, but ``ST_NumGeometries(ST_GeometryFromText('GEOMETRYCOLLECTION(POINT EMPTY, POINT (1 2))'))`` will evaluate to 1. .. function:: ST_InteriorRings(geometry: Geometry) -> output: array(geometry) Returns an array of all interior rings found in the input geometry, or an empty array if the polygon has no interior rings. Returns null if the input geometry is empty. Throws an error if the input geometry is not a polygon. .. function:: ST_Geometries(geometry: Geometry) -> output: array(geometry) Returns an array of geometries in the specified collection. Returns a one-element array if the input geometry is not a multi-geometry. Returns null if input geometry is empty. For example, a MultiLineString will create an array of LineStrings. A GeometryCollection will produce an un-flattened array of its constituents: GEOMETRYCOLLECTION (MULTIPOINT(0 0, 1 1), GEOMETRYCOLLECTION (MULTILINESTRING((2 2, 3 3))) ) would produce array[MULTIPOINT(0 0, 1 1), GEOMETRYCOLLECTION( MULTILINESTRING((2 2, 3 3)) )] .. function:: flatten_geometry_collections(geometry: Geometry) -> output: array(geometry) Recursively flattens any GeometryCollections in Geometry, returning an array of constituent non-GeometryCollection geometries. The order of the array is arbitrary and should not be relied upon. null input results in null output. Examples: POINT (0 0) -> [POINT (0 0)], MULTIPOINT (0 0, 1 1) -> [MULTIPOINT (0 0, 1 1)], GEOMETRYCOLLECTION (POINT (0 0), GEOMETRYCOLLECTION (POINT (1 1))) -> [POINT (0 0), POINT (1 1)], GEOMETRYCOLLECTION EMPTY -> []. .. function:: ST_NumInteriorRing(geometry: Geometry) -> output: bigint Returns the cardinality of the collection of interior rings of a polygon. .. function:: ST_ConvexHull(geometry: Geometry) -> output: Geometry Returns the minimum convex geometry that encloses all input geometries. .. function:: ST_CoordDim(geometry: Geometry) -> output: tinyint Return the coordinate dimension of the geometry. .. function:: ST_Dimension(geometry: Geometry) -> output: tinyint Returns the inherent dimension of this geometry object, which must be less than or equal to the coordinate dimension. .. function:: ST_ExteriorRing(geometry: Geometry) -> output: Geometry :noindex: Returns a line string representing the exterior ring of the input polygon. .. function:: ST_Buffer(geometry: Geometry, distance: double) -> output: Geometry Returns the geometry that represents all points whose distance from the specified ``geometry`` is less than or equal to the specified ``distance``. If the points of the ``geometry`` are extremely close together (delta < 1e-8), this might return an empty geometry. Empty inputs return null. .. function:: simplify_geometry(geometry: Geometry, tolerance: double) -> output: Geometry Returns a "simplified" version of the input geometry using the Douglas-Peucker algorithm. Will avoid creating geometries (polygons in particular) that are invalid. Tolerance must be a non-negative finite value. Using tolerance of 0 will return the original geometry. Empty geometries will also be returned as-is. .. function:: line_locate_point(linestring: Geometry, point: Geometry) -> output: double Returns a float between 0 and 1 representing the location of the closest point on the LineString to the given Point, as a fraction of total 2d line length. Returns null if a LineString or a Point is empty or null. .. function:: line_interpolate_point(linestring: Geometry, fraction: double) -> output: geometry Returns the Point on the LineString at a fractional distance given by the double argument. Throws an exception if the distance is not between 0 and 1. Returns an empty Point if the LineString is empty. Returns null if either the LineString or double is null. .. function:: geometry_as_geojson(geometry: Geometry) -> output: varchar Returns the GeoJSON encoded defined by the input geometry. If the geometry is atomic (non-multi) empty, this function would return null. Null input returns null output. .. function:: geometry_from_geojson(geometry: varchar) -> output: geometry Returns the geometry type object from the GeoJSON representation. The geometry cannot be empty if it is an atomic (non-multi) geometry type. Null input returns null output. Bing Tile Functions ------------------- Bing tiles are a convenient quad-tree representation of the WGS84 projection of Earth's surface. They can be used to partition geospatial data, perform quick proximity or intersection checks, and more. Each tile is defined by a `zoom` level (how far down the quad-tree the tile lives), and an `x` and `y` coordinate specifying where it is in that `zoom` level. Velox supports `zoom` levels from 0 to 23. For a given zoom level, `x` and `y` must be between 0 and `2**zoom - 1` inclusive. Lower `x` values are west of higher `x` values, and lower `y` values are north of higher `y` values. Bing tiles can be cast to and from an efficient BIGINT representation. While every Bing tile can be cast to a valid BIGINT, not every BIGINT is a valid Bing tile, so casting BIGINT to BINGTILE may fail. :: CAST(CAST(tile AS BIGINT) AS BINGTILE) See https://learn.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system for more details. .. function:: bing_tile(x: integer, y: integer, zoom_level: tinyint) -> tile: BingTile Creates a Bing tile object from ``x``, ``y`` coordinates and a ``zoom_level``. Zoom levels from 0 to 23 are supported, with valid ``x`` and ``y`` coordinates described above. Invalid parameters will return a User Error. .. function:: bing_tile(quadKey: varchar) -> tile: BingTile :noindex: Creates a Bing tile object from a quadkey. An invalid quadkey will return a User Error. .. function:: bing_tiles_around(latitude, longitude, zoom_level) -> array(BingTile) Returns a collection of Bing tiles that surround the point specified by the latitude and longitude arguments at a given zoom level. .. function:: bing_tiles_around(latitude, longitude, zoom_level, radius_in_km) -> array(BingTile) :noindex: Returns a minimum set of Bing tiles at specified zoom level that cover a circle of specified radius in km around a specified (latitude, longitude) point. .. function:: bing_tile_coordinates(tile: BingTile) -> coords: row(integer,integer) Returns the ``x``, ``y`` coordinates of a given Bing tile as ``row(x, y)``. .. function:: bing_tile_zoom_level(tile: BingTile) -> zoom_level: tinyint Returns the zoom level of a given Bing tile. .. function:: bing_tile_parent(tile) -> parent: BingTile Returns the parent of the Bing tile at one lower zoom level. Throws an exception if tile is at zoom level 0. .. function:: bing_tile_parent(tile, parentZoom) -> parent: BingTile :noindex: Returns the parent of the Bing tile at the specified lower zoom level. Throws an exception if parentZoom is less than 0, or parentZoom is greater than the tile's zoom. .. function:: bing_tile_children(tile) -> children: array(BingTile) Returns the children of the Bing tile at one higher zoom level. Throws an exception if tile is at max zoom level. .. function:: bing_tile_children(tile, childZoom) -> children: array(BingTile) :noindex: Returns the children of the Bing tile at the specified higher zoom level. Throws an exception if childZoom is greater than the max zoom level, or childZoom is less than the tile's zoom. The order is deterministic but not specified. .. function:: bing_tile_polygon(tile) -> Geometry Returns the polygon representation of a given Bing tile. .. function:: bing_tile_at(latitude, longitude, zoom_level) -> BingTile Returns a Bing tile at a given zoom level containing a point at a given latitude and 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. .. function:: bing_tile_quadkey() -> quadKey: varchar Returns the quadkey representing the provided bing tile. .. function:: geometry_to_bing_tiles(geometry: Geometry, zoom_level: tinyint) -> tiles: array(BingTile) Returns the minimum set of Bing tiles that fully covers a given geometry at a given zoom level. Empty inputs return an empty array, and null inputs return null. .. function:: geometry_to_dissolved_bing_tiles(geometry: Geometry, max_zoom_level: tinyint) -> tile: array(BingTile) Returns the minimum set of Bing tiles that fully covers a given geometry at a given zoom level, recursively dissolving full sets of children into parents. This results in a smaller array of tiles of different zoom levels. For example, if the non-dissolved covering is [“00”, “01”, “02”, “03”, “10”], the dissolved covering would be [“0”, “10”]. Zoom levels from 0 to 23 are supported. S2 Cell Functions ----------------- `S2 Geometry `_ is a library for spherical geometry that decomposes the Earth's surface into a hierarchy of cells. Unlike planar tiling systems (e.g., Bing Tiles), S2 cells have near-uniform area across all latitudes. Each cell is identified by a 64-bit **cell ID** (stored as ``BIGINT``), which encodes both the cell's position and level in the hierarchy. Cells are organized in 31 levels (0–30), where level 0 cells are the largest (covering roughly 1/6 of Earth's surface) and level 30 cells are the smallest (sub-centimeter). Cells can also be represented as compact hexadecimal **tokens** (e.g., ``'8085808b'``), which are shorter and human-readable. Use ``s2_cell_from_token`` and ``s2_cell_to_token`` to convert between the two representations. All functions operate on cell IDs (``BIGINT``) rather than tokens because cell IDs support direct integer comparison, efficient equi-joins and GROUP BY, and compose without casting (e.g., ``s2_cell_contains(s2_cell_parent(id, 10), id)``). Tokens are useful for human-readable output and interop with external systems that use the token format. .. function:: s2_cell_area_sq_km(cell_id: bigint) -> area: double Returns the area of the S2 cell in square kilometers. Returns an error if the cell ID is invalid. .. function:: s2_cell_contains(parent_cell_id: bigint, child_cell_id: bigint) -> boolean Returns ``true`` if the first S2 cell contains the second. Containment is hierarchical: a cell contains all of its descendants at finer levels. Returns an error if either cell ID is invalid. .. function:: s2_cell_from_token(cell_token: varchar) -> cell_id: bigint Returns the 64-bit S2 cell ID for the given cell token. The ``cell_token`` is a compact hexadecimal representation of the S2 cell. Returns an error if the cell token is invalid. .. function:: s2_cell_level(cell_id: bigint) -> level: integer Returns the level of the S2 cell, from 0 (coarsest) to 30 (finest). Returns an error if the cell ID is invalid. .. function:: s2_cell_parent(cell_id: bigint, level: integer) -> parent_id: bigint Returns the parent S2 cell ID at the given ``level``. If the cell is already at or above the given level, returns the same cell ID. The ``level`` must be in the ``[0, 30]`` range. Returns an error if the cell ID is invalid or the level is out of range. .. function:: s2_cell_to_token(cell_id: bigint) -> cell_token: varchar Returns the compact hexadecimal token representation of the S2 cell. Returns an error if the cell ID is invalid. .. function:: s2_cells(geometry: Geometry, level: integer) -> cell_ids: array(bigint) Returns the set of S2 cell IDs that cover the given geometry at a fixed ``level``. All returned cells are at the same level. Supports Point, LineString, Polygon, and their Multi variants. Empty geometries return an empty array, null geometries return null. The ``level`` must be in the ``[0, 30]`` range. .. function:: s2_cells(geometry: Geometry, min_level: integer, max_level: integer, max_cells: integer) -> cell_ids: array(bigint) :noindex: Returns a compact set of S2 cell IDs at mixed levels that cover the given geometry, similar to ``geometry_to_dissolved_bing_tiles``. The coverer uses large cells (at ``min_level``) for interiors and small cells (up to ``max_level``) for boundaries, targeting at most ``max_cells`` cells. This is useful for compact spatial indexing of regions like cities or countries. Both levels must be in the ``[0, 30]`` range with ``min_level <= max_level``, and ``max_cells`` must be >= 1. Empty geometries return an empty array, null geometries return null. Note: ``max_cells`` is a soft limit. Up to 6 cells may be returned regardless of ``max_cells`` if the region intersects multiple cube faces of the S2 projection. ``min_level`` takes priority over ``max_cells`` — cells below ``min_level`` are never used even if this causes more cells to be returned. If ``max_cells`` is less than 4, the covering area may be significantly larger than the original region. A value of 8 or higher is recommended for a reasonable approximation. Google Polyline Functions -------------------------- .. function:: google_polyline_decode(encoded: varchar) -> points: array(geometry) Decodes a Google Polyline encoded string into an array of Point geometries. Uses a fixed precision of 10^5 (100,000), which assumes coordinates were encoded with 5 decimal places of precision. See https://developers.google.com/maps/documentation/utilities/polylinealgorithm for details on the encoding format. .. function:: google_polyline_decode(encoded: varchar, precision_exponent: bigint) -> points: array(geometry) Decodes a Google Polyline encoded string into an array of Point geometries using the specified precision exponent. The precision exponent must be between 1 and 16 (inclusive). The precision used for decoding is 10^precision_exponent. See https://developers.google.com/maps/documentation/utilities/polylinealgorithm for details on the encoding format. .. function:: google_polyline_encode(points: array(Geometry)) -> encoded: varchar Encodes an array of Point geometries into a Google Polyline encoded string. Uses a fixed precision of 10^5 (100,000), which rounds coordinates to 5 decimal places. See https://developers.google.com/maps/documentation/utilities/polylinealgorithm for details on the encoding format. .. function:: google_polyline_encode(points: array(Geometry), precision_exponent: bigint) -> encoded: varchar Encodes an array of Point geometries into a Google Polyline encoded string using the specified precision exponent. The precision exponent must be between 1 and 16 (inclusive). The precision used for encoding is 10^precision_exponent. See https://developers.google.com/maps/documentation/utilities/polylinealgorithm for details on the encoding format. .. _OpenGIS Specifications: https://www.ogc.org/standards/ogcapi-features/ .. _SQL/MM Part 3: Spatial: https://www.iso.org/standard/31369.html