s2

CORE

Our S2 module is based on a port of the official S2 geometry library created by Google. For more information about S2 check the library's website or the Spatial Indexes section of this documentation.

S2_BOUNDARY

S2_BOUNDARY(id)

Description

Returns the boundary for a given S2 cell ID. We extract the boundary by getting the corner longitudes and latitudes, then enclose it in a GeoJSON and finally transform it into geography.

  • id: INT64 S2 cell ID to get the boundary geography from.

Return type

GEOGRAPHY

Example

SELECT `carto-un`.carto.S2_BOUNDARY(955378847514099712);
-- POLYGON((-3.41955272426037 40.25850071217, -3.41955272426037  ...

S2 Cell edges are spherical geodesics. The boundary edges can be interpreted as straight lines in other GIS tools, so to export the exact shape of the cells, use ST_GEOGFROM(ST_ASGEOJSON(geog). In this process, BigQuery will add intermediate points to preserve the geodesic curves.

S2_CENTER

S2_CENTER(id)

Description

Returns a POINT corresponding to the centroid of an S2 cell, given its ID.

  • id: INT64 S2 cell ID to get the boundary geography from.

Return type

GEOGRAPHY

Example

SELECT `carto-un`.carto.S2_CENTER(955378847514099712);
-- POINT(-3.58126923539589 40.4167087628243)

S2_FROMGEOGPOINT

S2_FROMGEOGPOINT(point, resolution)

Description

Returns the S2 cell ID of a given point at a given level of detail.

  • point: GEOGRAPHY point to get the ID from.

  • resolution: INT64 level of detail or zoom.

Return type

INT64

Example

SELECT `carto-un`.carto.S2_FROMGEOGPOINT(ST_GEOGPOINT(-3.7038, 40.4168), 8);
-- 955378847514099712

S2_FROMHILBERTQUADKEY

S2_FROMHILBERTQUADKEY(hquadkey)

Description

Returns the conversion of a Hilbert quadkey (a.k.a Hilbert curve quadtree ID) into a S2 cell ID.

  • hquadkey: STRING Hilbert quadkey to be converted.

Return type

INT64

Example

SELECT `carto-un`.carto.S2_FROMHILBERTQUADKEY('0/12220101');
-- 955378847514099712

S2_FROMLONGLAT

S2_FROMLONGLAT(longitude, latitude, resolution)

Description

Returns the S2 cell ID for a given longitude, latitude and zoom resolution.

  • longitude: FLOAT64 horizontal coordinate on the map.

  • latitude: FLOAT64 vertical coordinate on the map.

  • resolution: INT64 level of detail or zoom.

Return type

INT64

Example

SELECT `carto-un`.carto.S2_FROMLONGLAT(-3.7038, 40.4168, 8);
-- 955378847514099712

S2_FROMTOKEN

S2_FROMTOKEN(token)

Description

Returns the conversion of a token into a S2 cell ID.

  • token: STRING S2 cell hexified ID.

Return type

INT64

Example

SELECT `carto-un`.carto.S2_FROMTOKEN('0d42300000000000');
-- 955378847514099712

S2_FROMUINT64REPR

S2_FROMUINT64REPR(uid)

Description

Returns the cell ID from a UINT64 representation.

  • uid: STRING UINT64 representation of a S2 cell ID.

Return type

INT64

Example

SELECT `carto-un`.carto.S2_FROMUINT64REPR('9926595690882924544');
-- -8520148382826627072

S2_RESOLUTION

S2_RESOLUTION(index)

Description

Returns the S2 cell resolution as an integer.

  • index: STRING The S2 cell index.

Return type

INT64

Example

SELECT `carto-un`.carto.S2_RESOLUTION(955378847514099712);
-- 9

S2_TOCHILDREN

S2_TOCHILDREN(index, resolution)

Description

Returns an array with the S2 indexes of the children/descendents of the given hexagon at the given resolution.

  • index: STRING The S2 cell index.

  • resolution: INT64 number between 0 and 30 with the S2 resolution.

Return type

ARRAY<STRING>

Example

SELECT `carto-un`.carto.S2_TOCHILDREN(955378847514099712, 10);
-- 955365653374566400
-- 955374449467588608
-- 955383245560610816
-- 955392041653633024

S2_TOHILBERTQUADKEY

S2_TOHILBERTQUADKEY(id)

Description

Returns the conversion of a S2 cell ID into a Hilbert quadkey (a.k.a Hilbert curve quadtree ID).

  • id: INT64 S2 cell ID to be converted.

Return type

STRING

Example

SELECT `carto-un`.carto.S2_TOHILBERTQUADKEY(955378847514099712);
-- 0/12220101

S2_TOTOKEN

S2_TOTOKEN(id)

Description

Returns the conversion of a S2 cell ID into a token (S2 cell hexified ID).

  • id: INT64 S2 cell ID.

Return type

STRING

Example

SELECT `carto-un`.carto.S2_TOTOKEN(955378847514099712);
-- 0d42300000000000

S2_TOUINT64REPR

S2_TOUINT64REPR(id)

Description

Returns the UINT64 representation of a cell ID.

  • id: INT64 S2 cell ID.

Return type

STRING

Example

SELECT `carto-un`.carto.S2_TOUINT64REPR(-8520148382826627072);
-- 9926595690882924544

Last updated