# A Quadkey grid of stores locations and simple cannibalization analysis

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FhB2W9xXbzzo0kEuXMe3S%2Fintermediate%20banner.png?alt=media&#x26;token=4acd2cc7-c7e8-46c0-9669-6f6b73c030dd" alt="Intermediate difficulty banner" width="175"><figcaption></figcaption></figure></div>

## Requirements

To run this example you'll need:

* The latest version of the [Analytics Toolbox C](https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-snowflake/step-by-step-tutorials/broken-reference)[ore](https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-snowflake/step-by-step-tutorials/broken-reference) Native App installed in your Snowflake database
* *Optional:* An active CARTO organization to visualize the results in a map

## Example

### Building the Quadkey grid <a href="#bulding-the-quadkey-grid" id="bulding-the-quadkey-grid"></a>

We are going to demonstrate how fast and easy it is to make a visualization of a Quadkey grid to identify the concentration of Starbucks locations in the US. With a single query, we are going to calculate how many Starbucks locations fall within each quadkey grid cell of resolution 10.

```sql
WITH data AS (
  SELECT CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.QUADBIN_FROMGEOGPOINT(geog, 10) AS qk,
  COUNT(*) AS agg_total
  FROM CARTO_ANALYTICS_TOOLBOX_CORE.PUBLIC.STARBUCKS_LOCATIONS_USA
  WHERE geog IS NOT null
  GROUP BY qk
)
SELECT
  qk,
  agg_total,
  CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.QUADBIN_BOUNDARY(qk) AS geom
FROM
  data
```

This query adds two new columns to our dataset: `geom`, representing the boundary of each of the Quadkey grid cells where there’s at least one Starbucks, and `agg_total`, containing the total number of locations that fall within each cell. Finally, we can visualize the result.

<figure><img src="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/cU1yOUwgBPH2C9JsfGFB/sf_quadkey_map1.png" alt=""><figcaption></figcaption></figure>

### Using finer resolution Quadkeys for simple cannibalization analysis <a href="#using-finer-resolution-quadkeys-for-simple-cannibalization-analysis" id="using-finer-resolution-quadkeys-for-simple-cannibalization-analysis"></a>

Next, we will analyze in finer detail the grid cells in Las Vegas to identify potential cannibalizations through to analysis of the surroundings of each store. We present the same analysis applying two different methods, which simply differ in the way the influence areas around each store are defined.

The first method uses the `QUADBIN_KRING` function with distance 1 to define an area of influence around each store, and then aggregates the quadkey indexes around each Starbucks location.

```sql
WITH data AS (
  SELECT CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.QUADBIN_KRING(
  CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.QUADBIN_FROMGEOGPOINT(geog, 15), 1) AS qk
  FROM CARTO_ANALYTICS_TOOLBOX_CORE.PUBLIC.STARBUCKS_LOCATIONS_USA
  WHERE city = 'Las Vegas' AND geog IS NOT null
),
flat_qks AS(
  SELECT VALUE::BIGINT AS qk,
  count(*) AS agg_total,
  CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.QUADBIN_BOUNDARY(VALUE) AS geom
  FROM data, lateral FLATTEN(input => qk)
  GROUP BY VALUE
)
SELECT * FROM flat_qks
```

Visualizing the results we can clearly identify the areas in Las Vegas that are overserved by Starbucks, represented by the darkest colours.

<figure><img src="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/cBrUk373qIHQTxZ3yT41/sf_quadkey_map2.png" alt=""><figcaption></figcaption></figure>

The second approach calculates how many Starbucks fall within a radius of three kilometers around each location. We first use the function `ST_MAKEELLIPSE` to generate the buffer around each point. Notice that we are reducing the number of default steps used to calculate these buffers, as it will not cause major differences in the result but it greatly improves performance.

```sql
WITH data AS (
  SELECT CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.QUADBIN_POLYFILL(
  CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.ST_MAKEELLIPSE(geog, 3, 3, 0, 'kilometers', 12), 15) AS qk
  FROM CARTO_ANALYTICS_TOOLBOX_CORE.PUBLIC.STARBUCKS_LOCATIONS_USA
  WHERE city = 'Las Vegas' AND geog IS NOT null
),
flat_qks AS(
  SELECT VALUE::BIGINT AS qk,
  count(*) AS agg_total,
  CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.QUADBIN_BOUNDARY(VALUE) AS geom
  FROM data, lateral FLATTEN(input => qk)
  GROUP BY VALUE
)
SELECT * FROM flat_qks
```

Darkest areas on the map are those with the higher coverage by Starbucks.

<figure><img src="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/hTuYXoS8wg1Yd0xCTY32/sf_quadkey_map3.png" alt=""><figcaption></figcaption></figure>
