# Census areas in the UK within tiles of multiple resolutions

<div align="left"><figure><img src="/files/YUiOye9yS8uvdGFnFppD" alt="Advanced difficulty banner" width="175"><figcaption></figcaption></figure></div>

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](https://carto.com/spatial-data-catalog/browser/geography/drd_output_area_73f0b4a3/) 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).

{% tabs %}
{% tab title="carto-un" %}

```sql
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`.carto.QUADINT_TOZXY(
            `carto-un`.carto.QUADINT_FROMLONGLAT(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`.carto.ST_TILEENVELOPE(index.z, index.x, index.y), census.geom)
GROUP BY census.geoid;
```

{% endtab %}

{% tab title="carto-un-eu" %}

```sql
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-eu`.carto.QUADINT_TOZXY(
            `carto-un-eu`.carto.QUADINT_FROMLONGLAT(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-eu`.carto.ST_TILEENVELOPE(index.z, index.x, index.y), census.geom)
GROUP BY census.geoid;
```

{% endtab %}

{% tab title="manual" %}

```sql
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.QUADINT_TOZXY(
            carto.QUADINT_FROMLONGLAT(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.ST_TILEENVELOPE(index.z, index.x, index.y), census.geom)
GROUP BY census.geoid;
```

{% endtab %}
{% endtabs %}

<figure><img src="/files/T8LfSjXmx866Y97if4yX" alt=""><figcaption></figcaption></figure>

<img src="/files/4m1BK9j4Wq34gat4HHd2" alt="EU flag" data-size="line"> This project has received funding from the [European Union’s Horizon 2020](https://ec.europa.eu/programmes/horizon2020/en) research and innovation programme under grant agreement No 960401.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-bigquery/step-by-step-tutorials/census-areas-in-the-uk-within-tiles-of-multiple-resolutions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
