# Analyzing store location coverage using a Voronoi diagram

<div align="left"><figure><img src="/files/OG2RD7V5fNQpaw0lxn8Y" 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](broken://pages/1eMONTIWiApJb33x3dGZ)[ore](broken://pages/KWPwnuCkuf4Pm0qTGejg) Native App installed in your Snowflake database
* *Optional:* An active CARTO organization to visualize the results in a map

## Example

Voronoi diagrams are a very useful tool to build influence regions from a set of points and the Analytics Toolbox provides a convenient function to build them. An example application of these diagrams is the calculation of the coverage areas of a series of Starbucks stores. In the following query we are going to calculate these influence areas in Atlanta.

The result can be seen in the visualization below, where the color of each polygon indicates its area, which gives an insight on the coverage provided by each store.

```sql
WITH starbucks AS
(
  SELECT geog
  FROM CARTO_ANALYTICS_TOOLBOX_CORE.PUBLIC.STARBUCKS_LOCATIONS_USA
  WHERE CITY = 'Atlanta' AND geog IS NOT NULL
  ORDER BY id
),
starbucks_array AS (
  SELECT ARRAY_AGG(ST_ASGEOJSON(geog)::STRING) AS geog_array
  FROM starbucks
),
voronoi_array AS (
  SELECT CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.ST_ENVELOPE_ARR(geog_array) AS envelope,
  CARTO_ANALYTICS_TOOLBOX_CORE.CARTO.ST_VORONOIPOLYGONS(geog_array, ARRAY_CONSTRUCT(ST_XMIN(envelope), ST_YMIN(envelope), ST_XMAX(envelope), ST_YMAX(envelope))) AS nested_voronoi
  FROM starbucks_array
)
SELECT TO_GEOGRAPHY(VALUE) AS geom, ST_AREA(geom) AS area FROM voronoi_array, lateral FLATTEN(input => nested_voronoi)
```

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

Prior to the calculation of the Voronoi diagrams, we use `ST_ENVELOPE` in order to calculate a boundary that covers all the Starbucks stores in our selection. This boundary is used to clip the resulting Voronoi polygons. If the bounding box parameter were not passed to `ST_VORONOIPOLYGONS`, the polygons would extend all over the map.

With this simple analysis we can identify at a glance areas where the coverage could be improved and therefore new stores could be placed.


---

# 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-snowflake/step-by-step-tutorials/analyzing-store-location-coverage-using-a-voronoi-diagram.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.
