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 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:INT64S2 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 ...SELECT `carto-un-eu`.carto.S2_BOUNDARY(955378847514099712);
-- POLYGON((-3.41955272426037 40.25850071217, -3.41955272426037 ...SELECT carto.S2_BOUNDARY(955378847514099712);
-- POLYGON((-3.41955272426037 40.25850071217, -3.41955272426037 ...S2_CENTER
S2_CENTER(id)Description
Returns a POINT corresponding to the centroid of an S2 cell, given its ID.
id:INT64S2 cell ID to get the boundary geography from.
Return type
GEOGRAPHY
Example
SELECT `carto-un`.carto.S2_CENTER(955378847514099712);
-- POINT(-3.58126923539589 40.4167087628243)SELECT `carto-un-eu`.carto.S2_CENTER(955378847514099712);
-- POINT(-3.58126923539589 40.4167087628243)SELECT 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 requested resolution.
point:GEOGRAPHYpoint to get the ID from.resolution:INT64level of detail or zoom.
Return type
INT64
Example
SELECT `carto-un`.carto.S2_FROMGEOGPOINT(ST_GEOGPOINT(-3.7038, 40.4168), 8);
-- 955378847514099712SELECT `carto-un-eu`.carto.S2_FROMGEOGPOINT(ST_GEOGPOINT(-3.7038, 40.4168), 8);
-- 955378847514099712SELECT carto.S2_FROMGEOGPOINT(ST_GEOGPOINT(-3.7038, 40.4168), 8);
-- 955378847514099712S2_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:STRINGHilbert quadkey to be converted.
Return type
INT64
Example
SELECT `carto-un`.carto.S2_FROMHILBERTQUADKEY('0/12220101');
-- 955378847514099712SELECT `carto-un-eu`.carto.S2_FROMHILBERTQUADKEY('0/12220101');
-- 955378847514099712SELECT carto.S2_FROMHILBERTQUADKEY('0/12220101');
-- 955378847514099712S2_FROMLONGLAT
S2_FROMLONGLAT(longitude, latitude, resolution)Description
Returns the S2 cell ID for a given longitude, latitude and zoom resolution.
longitude:FLOAT64horizontal coordinate on the map.latitude:FLOAT64vertical coordinate on the map.resolution:INT64level of detail or zoom.
Return type
INT64
Example
SELECT `carto-un`.carto.S2_FROMLONGLAT(-3.7038, 40.4168, 8);
-- 955378847514099712SELECT `carto-un-eu`.carto.S2_FROMLONGLAT(-3.7038, 40.4168, 8);
-- 955378847514099712SELECT carto.S2_FROMLONGLAT(-3.7038, 40.4168, 8);
-- 955378847514099712S2_FROMTOKEN
S2_FROMTOKEN(token)Description
Returns the conversion of a token into a S2 cell ID.
token:STRINGS2 cell hexified ID.
Return type
INT64
Example
SELECT `carto-un`.carto.S2_FROMTOKEN('0d42300000000000');
-- 955378847514099712SELECT `carto-un-eu`.carto.S2_FROMTOKEN('0d42300000000000');
-- 955378847514099712SELECT carto.S2_FROMTOKEN('0d42300000000000');
-- 955378847514099712S2_FROMUINT64REPR
S2_FROMUINT64REPR(uid)Description
Returns the cell ID from a UINT64 representation.
uid:STRINGUINT64 representation of a S2 cell ID.
Return type
INT64
Example
SELECT `carto-un`.carto.S2_FROMUINT64REPR('9926595690882924544');
-- -8520148382826627072SELECT `carto-un-eu`.carto.S2_FROMUINT64REPR('9926595690882924544');
-- -8520148382826627072SELECT carto.S2_FROMUINT64REPR('9926595690882924544');
-- -8520148382826627072S2_RESOLUTION
S2_RESOLUTION(index)Description
Returns the S2 cell resolution as an integer.
index:STRINGThe S2 cell index.
Return type
INT64
Example
SELECT `carto-un`.carto.S2_RESOLUTION(955378847514099712);
-- 9SELECT `carto-un-eu`.carto.S2_RESOLUTION(955378847514099712);
-- 9SELECT carto.S2_RESOLUTION(955378847514099712);
-- 9S2_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:STRINGThe S2 cell index.resolution:INT64number between 0 and 30 with the S2 resolution.
Return type
ARRAY<STRING>
Example
SELECT `carto-un`.carto.S2_TOCHILDREN(955378847514099712, 10);
-- 955365653374566400
-- 955374449467588608
-- 955383245560610816
-- 955392041653633024SELECT `carto-un-eu`.carto.S2_TOCHILDREN(955378847514099712, 10);
-- 955365653374566400
-- 955374449467588608
-- 955383245560610816
-- 955392041653633024SELECT carto.S2_TOCHILDREN(955378847514099712, 10);
-- 955365653374566400
-- 955374449467588608
-- 955383245560610816
-- 955392041653633024S2_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:INT64S2 cell ID to be converted.
Return type
STRING
Example
SELECT `carto-un`.carto.S2_TOHILBERTQUADKEY(955378847514099712);
-- 0/12220101SELECT `carto-un-eu`.carto.S2_TOHILBERTQUADKEY(955378847514099712);
-- 0/12220101SELECT carto.S2_TOHILBERTQUADKEY(955378847514099712);
-- 0/12220101S2_TOTOKEN
S2_TOTOKEN(id)Description
Returns the conversion of a S2 cell ID into a token (S2 cell hexified ID).
id:INT64S2 cell ID.
Return type
STRING
Example
SELECT `carto-un`.carto.S2_TOTOKEN(955378847514099712);
-- 0d42300000000000SELECT `carto-un-eu`.carto.S2_TOTOKEN(955378847514099712);
-- 0d42300000000000SELECT carto.S2_TOTOKEN(955378847514099712);
-- 0d42300000000000S2_TOUINT64REPR
S2_TOUINT64REPR(id)Description
Returns the UINT64 representation of a cell ID.
id:INT64S2 cell ID.
Return type
STRING
Example
SELECT `carto-un`.carto.S2_TOUINT64REPR(-8520148382826627072);
-- 9926595690882924544SELECT `carto-un-eu`.carto.S2_TOUINT64REPR(-8520148382826627072);
-- 9926595690882924544SELECT carto.S2_TOUINT64REPR(-8520148382826627072);
-- 9926595690882924544
This project has received funding from the European Union’s Horizon 2020 research and innovation programme under grant agreement No 960401.
Last updated
Was this helpful?
