Skip to main content

Geospatial Functions

Databend ships with two complementary sets of geospatial capabilities: PostGIS-style geometry functions for building and analysing shapes, and H3 utilities for global hexagonal indexing. The tables below group the functions by task so you can quickly locate the right tool, similar to the layout used in the Snowflake documentation.

Constructors

FunctionDescriptionNoteExample
ST_MAKEGEOMPOINT / ST_GEOM_POINTConstruct a Point geometryGEOMETRY onlyST_MAKEGEOMPOINT(-122.35, 37.55)POINT(-122.35 37.55)
ST_MAKEPOINT / ST_POINTConstruct a Point geographyGEOGRAPHY onlyST_MAKEPOINT(-122.35, 37.55)POINT(-122.35 37.55)
ST_MAKELINE / ST_MAKE_LINECreate a LineString from pointsST_MAKELINE(ST_MAKEGEOMPOINT(-122.35, 37.55), ST_MAKEGEOMPOINT(-122.40, 37.60))LINESTRING(-122.35 37.55, -122.40 37.60)
ST_MAKEPOLYGONCreate a Polygon from a closed LineStringST_MAKEPOLYGON(ST_MAKELINE(...))POLYGON(...)
ST_POLYGONCreate a Polygon from coordinate ringsGEOMETRY onlyST_POLYGON(...)POLYGON(...)

Conversion

FunctionDescriptionNoteExample
ST_GEOMETRYFROMTEXT / ST_GEOMFROMTEXTConvert WKT to geometryGEOMETRY onlyST_GEOMETRYFROMTEXT('POINT(-122.35 37.55)')POINT(-122.35 37.55)
ST_GEOMETRYFROMWKB / ST_GEOMFROMWKBConvert WKB to geometryGEOMETRY onlyST_GEOMETRYFROMWKB(...)POINT(...)
ST_GEOMETRYFROMEWKT / ST_GEOMFROMEWKTConvert EWKT to geometryGEOMETRY onlyST_GEOMETRYFROMEWKT('SRID=4326;POINT(-122.35 37.55)')POINT(-122.35 37.55)
ST_GEOMETRYFROMEWKB / ST_GEOMFROMEWKBConvert EWKB to geometryGEOMETRY onlyST_GEOMETRYFROMEWKB(...)POINT(...)
ST_GEOGRAPHYFROMWKT / ST_GEOGFROMWKTConvert WKT/EWKT to geographyGEOGRAPHY onlyST_GEOGRAPHYFROMWKT('POINT(-122.35 37.55)')POINT(-122.35 37.55)
ST_GEOGRAPHYFROMWKB / ST_GEOGFROMWKBConvert WKB/EWKB to geographyGEOGRAPHY onlyST_GEOGRAPHYFROMWKB(...)POINT(...)
ST_GEOMFROMGEOHASHConvert GeoHash to geometryGEOMETRY onlyST_GEOMFROMGEOHASH('9q8yyk8')POLYGON(...)
ST_GEOMPOINTFROMGEOHASHConvert GeoHash to Point geometryGEOMETRY onlyST_GEOMPOINTFROMGEOHASH('9q8yyk8')POINT(...)
ST_GEOGFROMGEOHASHConvert GeoHash to geography polygonGEOGRAPHY onlyST_GEOGFROMGEOHASH('9q8yyk8')POLYGON(...)
ST_GEOGPOINTFROMGEOHASHConvert GeoHash to geography pointGEOGRAPHY onlyST_GEOGPOINTFROMGEOHASH('9q8yyk8')POINT(...)
TO_GEOMETRYParse various formats into geometryGEOMETRY onlyTO_GEOMETRY('POINT(-122.35 37.55)')POINT(-122.35 37.55)
TO_GEOGRAPHY / TRY_TO_GEOGRAPHYParse various formats into geographyGEOGRAPHY onlyTO_GEOGRAPHY('POINT(-122.35 37.55)')POINT(-122.35 37.55)

Output

FunctionDescriptionNoteExample
ST_ASTEXTConvert geometry to WKTST_ASTEXT(ST_MAKEGEOMPOINT(-122.35, 37.55))'POINT(-122.35 37.55)'
ST_ASWKTConvert geometry to WKTST_ASWKT(ST_MAKEGEOMPOINT(-122.35, 37.55))'POINT(-122.35 37.55)'
ST_ASBINARY / ST_ASWKBConvert geometry to WKBST_ASBINARY(ST_MAKEGEOMPOINT(-122.35, 37.55))WKB representation
ST_ASEWKTConvert geometry to EWKTST_ASEWKT(ST_MAKEGEOMPOINT(-122.35, 37.55))'SRID=4326;POINT(-122.35 37.55)'
ST_ASEWKBConvert geometry to EWKBST_ASEWKB(ST_MAKEGEOMPOINT(-122.35, 37.55))EWKB representation
ST_ASGEOJSONConvert geometry to GeoJSONST_ASGEOJSON(ST_MAKEGEOMPOINT(-122.35, 37.55))'{"type":"Point","coordinates":[-122.35,37.55]}'
ST_GEOHASHConvert geometry to GeoHashST_GEOHASH(ST_MAKEGEOMPOINT(-122.35, 37.55), 7)'9q8yyk8'
TO_STRINGConvert geometry to stringTO_STRING(ST_MAKEGEOMPOINT(-122.35, 37.55))'POINT(-122.35 37.55)'

Accessors & Properties

FunctionDescriptionNoteExample
ST_DIMENSIONReturn the topological dimensionST_DIMENSION(ST_MAKEGEOMPOINT(-122.35, 37.55))0
ST_SRIDReturn the SRID of a geometryST_SRID(ST_MAKEGEOMPOINT(-122.35, 37.55))4326
ST_POINTNReturn a specific point from a LineStringST_POINTN(ST_MAKELINE(...), 1)POINT(-122.35 37.55)
ST_STARTPOINTReturn the first point in a LineStringST_STARTPOINT(ST_MAKELINE(...))POINT(-122.35 37.55)
ST_ENDPOINTReturn the last point in a LineStringST_ENDPOINT(ST_MAKELINE(...))POINT(-122.40 37.60)
ST_X / ST_YReturn the X or Y coordinate of a PointST_X(ST_MAKEGEOMPOINT(-122.35, 37.55))-122.35
ST_XMIN / ST_XMAXReturn the min/max X coordinateST_XMIN(ST_MAKELINE(...))-122.40
ST_YMIN / ST_YMAXReturn the min/max Y coordinateST_YMAX(ST_MAKELINE(...))37.60

Relationship and measurement

FunctionDescriptionNoteExample
HAVERSINECompute great-circle distance between coordinatesHAVERSINE(37.55, -122.35, 37.60, -122.40)6.12
ST_AREAMeasure the area of geometry or geography objectST_AREA(TO_GEOMETRY('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'))1.0
ST_CONTAINSTest whether one geometry contains anotherGEOMETRY onlyST_CONTAINS(ST_MAKEPOLYGON(...), ST_MAKEGEOMPOINT(...))TRUE
ST_CONVEXHULLCompute the convex hull of a geometryGEOMETRY onlyST_CONVEXHULL(TO_GEOMETRY('POLYGON((0 0,2 0,2 2,0 2,0 0))'))POLYGON((0 0,2 0,2 2,0 2,0 0))
ST_NPOINTSCount points in a geometryST_NPOINTS(ST_MAKELINE(...))2
ST_NUMPOINTSCount points in a geometryGEOMETRY onlyST_NUMPOINTS(ST_MAKELINE(...))2
ST_INTERSECTSTest whether two geometries intersectGEOMETRY onlyST_INTERSECTS(TO_GEOMETRY('LINESTRING(0 0, 2 2)'), TO_GEOMETRY('LINESTRING(0 2, 2 0)'))TRUE
ST_DISJOINTTest whether two geometries are disjointGEOMETRY onlyST_DISJOINT(TO_GEOMETRY('POINT(3 3)'), TO_GEOMETRY('POLYGON((0 0,2 0,2 2,0 2,0 0))'))TRUE
ST_WITHINTest whether one geometry is within anotherGEOMETRY onlyST_WITHIN(TO_GEOMETRY('POINT(1 1)'), TO_GEOMETRY('POLYGON((0 0,2 0,2 2,0 2,0 0))'))TRUE
ST_EQUALSTest whether two geometries are spatially equalGEOMETRY onlyST_EQUALS(TO_GEOMETRY('POINT(1 1)'), TO_GEOMETRY('POINT(1 1)'))TRUE
ST_LENGTHMeasure the length of a LineStringST_LENGTH(ST_MAKELINE(...))5.57
ST_DISTANCEMeasure the distance between geometriesST_DISTANCE(ST_MAKEGEOMPOINT(-122.35, 37.55), ST_MAKEGEOMPOINT(-122.40, 37.60))5.57

Transformation

FunctionDescriptionNoteExample
ST_HILBERTEncode geometry or geography into a Hilbert curve indexST_HILBERT(TO_GEOMETRY('POINT(0.5 0.5)'), [0, 0, 1, 1])715827882
ST_SETSRIDAssign an SRID to a geometryGEOMETRY onlyST_SETSRID(ST_MAKEGEOMPOINT(-122.35, 37.55), 3857)POINT(-122.35 37.55)
ST_TRANSFORMTransform geometry to a new SRIDGEOMETRY onlyST_TRANSFORM(ST_MAKEGEOMPOINT(-122.35, 37.55), 3857)POINT(-13618288.8 4552395.0)

H3 Indexing & Conversion

FunctionDescriptionExample
GEO_TO_H3Convert longitude/latitude to an H3 indexGEO_TO_H3(37.7950, 55.7129, 15)644325524701193974
H3_TO_GEOConvert an H3 index to longitude/latitudeH3_TO_GEO(644325524701193974)[37.7950, 55.7129]
H3_TO_STRINGConvert an H3 index to its string formH3_TO_STRING(644325524701193974)'8f2830828052d25'
STRING_TO_H3Convert an H3 string to an indexSTRING_TO_H3('8f2830828052d25')644325524701193974
GEOHASH_ENCODEEncode longitude/latitude to GeoHashGEOHASH_ENCODE(37.7950, 55.7129, 12)'ucfv0nzpt3s7'
GEOHASH_DECODEDecode a GeoHash to longitude/latitudeGEOHASH_DECODE('ucfv0nzpt3s7')[37.7950, 55.7129]

H3 Cell Properties

FunctionDescriptionExample
H3_GET_RESOLUTIONReturn the resolution of an H3 indexH3_GET_RESOLUTION(644325524701193974)15
H3_GET_BASE_CELLReturn the base cell numberH3_GET_BASE_CELL(644325524701193974)14
H3_IS_VALIDCheck whether an H3 index is validH3_IS_VALID(644325524701193974)TRUE
H3_IS_PENTAGONCheck whether an H3 index is a pentagonH3_IS_PENTAGON(644325524701193974)FALSE
H3_IS_RES_CLASS_IIICheck whether an H3 index is class IIIH3_IS_RES_CLASS_III(644325524701193974)FALSE
H3_GET_FACESReturn intersecting icosahedron facesH3_GET_FACES(644325524701193974)[7]
H3_TO_PARENTReturn the parent index at a lower resolutionH3_TO_PARENT(644325524701193974, 10)622236721289822207
H3_TO_CHILDRENReturn child indexes at a higher resolutionH3_TO_CHILDREN(622236721289822207, 11)[...]
H3_TO_CENTER_CHILDReturn the center child for a resolutionH3_TO_CENTER_CHILD(622236721289822207, 11)625561602857582591
H3_CELL_AREA_M2Return the area of a cell in square metersH3_CELL_AREA_M2(644325524701193974)0.8953
H3_CELL_AREA_RADS2Return the area of a cell in square radiansH3_CELL_AREA_RADS2(644325524701193974)2.2e-14
H3_HEX_AREA_KM2Return the average hexagon area in km²H3_HEX_AREA_KM2(10)0.0152
H3_HEX_AREA_M2Return the average hexagon area in m²H3_HEX_AREA_M2(10)15200
H3_TO_GEO_BOUNDARYReturn the boundary of a cellH3_TO_GEO_BOUNDARY(644325524701193974)[[lon1,lat1], ...]
H3_NUM_HEXAGONSReturn the number of hexagons at a resolutionH3_NUM_HEXAGONS(2)5882
GEO_DISTANCEApproximate distance in meters using WGS84GEO_DISTANCE(0, 0, 0, 0)0
GREAT_CIRCLE_DISTANCEGreat-circle distance in metersGREAT_CIRCLE_DISTANCE(0, 0, 0, 0)0
GREAT_CIRCLE_ANGLEGreat-circle central angle in degreesGREAT_CIRCLE_ANGLE(0, 0, 45, 0)45
POINT_IN_POLYGONCheck if a point lies inside a polygonPOINT_IN_POLYGON([lon, lat], [[p1_lon, p1_lat], ...])TRUE
POINT_IN_ELLIPSESCheck if a point lies inside any ellipsePOINT_IN_ELLIPSES(10, 10, 10, 9.1, 1, 0.9999)1

H3 Neighborhoods

FunctionDescriptionExample
H3_DISTANCEReturn the grid distance between two indexesH3_DISTANCE(599119489002373119, 599119491149856767)1
H3_INDEXES_ARE_NEIGHBORSTest whether two indexes are neighborsH3_INDEXES_ARE_NEIGHBORS(599119489002373119, 599119491149856767)TRUE
H3_K_RINGReturn all indexes within k distanceH3_K_RING(599119489002373119, 1)[599119489002373119, ...]
H3_HEX_RINGReturn indexes exactly k steps awayH3_HEX_RING(599119489002373119, 1)[599119491149856767, ...]
H3_LINEReturn indexes along a pathH3_LINE(from_h3, to_h3)[from_h3, ..., to_h3]

H3 Edge Operations

FunctionDescriptionExample
H3_GET_UNIDIRECTIONAL_EDGEReturn the edge between two adjacent cellsH3_GET_UNIDIRECTIONAL_EDGE(from_h3, to_h3)edge_index
H3_UNIDIRECTIONAL_EDGE_IS_VALIDCheck whether an edge index is validH3_UNIDIRECTIONAL_EDGE_IS_VALID(edge_index)TRUE
H3_GET_ORIGIN_INDEX_FROM_UNIDIRECTIONAL_EDGEReturn the origin cell from an edgeH3_GET_ORIGIN_INDEX_FROM_UNIDIRECTIONAL_EDGE(edge_index)from_h3
H3_GET_DESTINATION_INDEX_FROM_UNIDIRECTIONAL_EDGEReturn the destination cell from an edgeH3_GET_DESTINATION_INDEX_FROM_UNIDIRECTIONAL_EDGE(edge_index)to_h3
H3_GET_INDEXES_FROM_UNIDIRECTIONAL_EDGEReturn both cells for an edgeH3_GET_INDEXES_FROM_UNIDIRECTIONAL_EDGE(edge_index)[from_h3, to_h3]
H3_GET_UNIDIRECTIONAL_EDGES_FROM_HEXAGONList edges originating from a cellH3_GET_UNIDIRECTIONAL_EDGES_FROM_HEXAGON(h3_index)[edge1, edge2, ...]
H3_GET_UNIDIRECTIONAL_EDGE_BOUNDARYReturn the boundary of an edgeH3_GET_UNIDIRECTIONAL_EDGE_BOUNDARY(edge_index)[[lon1,lat1], [lon2,lat2]]

H3 Measurements & Angles

FunctionDescriptionExample
H3_EDGE_LENGTH_KMReturn the average edge length in kilometresH3_EDGE_LENGTH_KM(10)0.065
H3_EDGE_LENGTH_MReturn the average edge length in metresH3_EDGE_LENGTH_M(10)65.91
H3_EXACT_EDGE_LENGTH_KMReturn the exact edge length in kilometresH3_EXACT_EDGE_LENGTH_KM(edge_index)0.066
H3_EXACT_EDGE_LENGTH_MReturn the exact edge length in metresH3_EXACT_EDGE_LENGTH_M(edge_index)66.12
H3_EXACT_EDGE_LENGTH_RADSReturn the exact edge length in radiansH3_EXACT_EDGE_LENGTH_RADS(edge_index)0.00001
H3_EDGE_ANGLEReturn the angle in radians between two edgesH3_EDGE_ANGLE(edge1, edge2)1.047
Try Databend Cloud for FREE

Multimodal, object-storage-native warehouse for BI, vectors, search, and geo.

Snowflake-compatible SQL with automatic scaling.

Sign up and get $200 in credits.

Try it today