Analytics Toolbox for BigQuery

Analytics Toolbox for BigQuery

Census areas in the UK within tiles of multiple resolutions

In this example we are going to use the ST_TILEENVELOPE function, which returns the bounding geometry of quadkey tile given its 3D coordinates (x, y and resolution), to showcase the extent of quadkey tiles at different resolutions. For this purpose we are using the United Kingdom census areas dataset from CARTO’s Data Observatory.

Taking as input the longitude and latitude from a geographic point in the city of London, the resulting map depicts the belonging census areas for different tile resolutions (from 11 to 15 zoom levels).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
WITH census_data AS (
    SELECT geom, geoid
    FROM `carto-do-public-data.gbr_cdrc.geography_gbr_outputarea_2011`
),
point_data AS (
    SELECT -0.10481368396112065 AS longitude, 51.51493384150647 AS latitude, resolution
    FROM UNNEST (GENERATE_ARRAY(11, 15)) AS resolution
),
quadkey_tiles AS (
    SELECT `carto-un`.quadkey.ZXY_FROMQUADINT(
            `carto-un`.quadkey.LONGLAT_ASQUADINT(longitude, latitude, resolution)
        ) AS index
    FROM point_data
)
SELECT ANY_VALUE(census.geom) AS area, MAX(index.z) AS level
FROM quadkey_tiles
JOIN census_data AS census
ON 
ST_INTERSECTS(`carto-un`.constructors.ST_TILEENVELOPE(index.z, index.x, index.y), census.geom)
GROUP BY census.geoid