Analytics Toolbox for BigQuery

Analytics Toolbox for BigQuery

s2

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 Overview section of this documentation.

S2_BOUNDARY

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

1
2
SELECT `carto-os`.carto.S2_BOUNDARY(1735346007979327488);
-- POLYGON((40.6346851320784 -3.8440544113597, 40.6346851320784 ...

S2_CENTER

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

1
2
SELECT `carto-os`.carto.S2_CENTER(1735346007979327488);
-- POINT(40.4720004343497 -3.72646193231851)

S2_FROMGEOGPOINT

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

1
2
SELECT `carto-os`.carto.S2_FROMGEOGPOINT(ST_GEOGPOINT(40.4168, -3.7038), 8);
-- 1735346007979327488

S2_FROMHILBERTQUADKEY

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

1
2
SELECT `carto-os`.carto.S2_FROMHILBERTQUADKEY('0/30002221');
-- 1735346007979327488

S2_FROMLONGLAT

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

1
2
SELECT `carto-os`.carto.S2_FROMLONGLAT(-3.7038, 40.4168, 8);
-- 955378847514099712

S2_FROMTOKEN

Description

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

  • token: STRING S2 cell hexified ID.

Return type

INT64

Example

1
2
SELECT `carto-os`.carto.S2_FROMTOKEN('89c25a3000000000');
-- -8520148382826627072

S2_FROMUINT64REPR

Description

Returns the cell ID from a UINT64 representation.

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

Return type

INT64

Example

1
2
SELECT `carto-os`.carto.S2_FROMUINT64REPR('9926595690882924544');
-- -8520148382826627072

S2_RESOLUTION

Description

Returns the S2 cell resolution as an integer.

  • index: STRING The S2 cell index.

Return type

INT64

Example

1
2
SELECT `carto-os`.carto.S2_RESOLUTION(-6432928348669739008);
-- 11

S2_TOCHILDREN

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

1
2
3
4
5
SELECT `carto-os`.carto.S2_TOCHILDREN(-6432928348669739008, 12);
-- 6432928554828169216
-- 6432928417389215744
-- 6432928279950262272
-- 6432928142511308800

S2_TOHILBERTQUADKEY

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

1
2
SELECT `carto-os`.carto.S2_TOHILBERTQUADKEY(1735346007979327488);
-- 0/30002221

S2_TOTOKEN

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

1
2
SELECT `carto-os`.carto.S2_TOTOKEN(-8520148382826627072);
-- 89c25a3000000000

S2_TOUINT64REPR

Description

Returns the UINT64 representation of a cell ID.

  • id: INT64 S2 cell ID.

Return type

STRING

Example

1
2
SELECT `carto-os`.carto.S2_TOUINT64REPR(-8520148382826627072);
-- 9926595690882924544
EU flag

This project has received funding from the European Union’s Horizon 2020 research and innovation programme under grant agreement No 960401.