# Analyzing multiple drive-time catchment areas dynamically

## **Context**

<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>

In this tutorial, discover how to harness CARTO Builder for analyzing multiple drive time catchment areas at specific times of the day, tailored to various business needs. We'll demonstrate how to create five distinct catchments at 10, 15, 30, 45, and 60 minutes of driving time for a chosen time - 8:00 AM local time, using CARTO Workflows. You'll then learn to craft an interactive dashboard in Builder, employing SQL Parameters to enable users to select and focus on a catchment area that aligns with their specific interests or business objectives.

## **Step-by-Step Guide:**

In this guide, we'll walk you through:

* [**Generate drive-time catchment areas with Workflows**](#generate-drive-time-catchment-areas-with-workflows)
* [**Create an interactive map with Builder**](#create-an-interactive-map-with-builder)

***

### Generate drive-time catchment areas with Workflows

* Access *Workflows* from your CARTO Workspace using the Navigation menu.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F5UMtS52Iiquqr8iEgh6T%2Fimage.png?alt=media&#x26;token=81821232-6272-4f69-a9f1-ad4200649774" alt=""><figcaption></figcaption></figure>

* Select the data warehouse where you have the data accessible. We'll be using the CARTO Data Warehouse, which should be available to all users.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FjRHVi3fGkpg2EneihRNY%2Fimage.png?alt=media&#x26;token=e62d9d74-c923-4a65-bd62-db90b95bff60" alt=""><figcaption></figcaption></figure>

* In the *Sources* section location on the left panel, navigate to *demo\_data > demo tables* within CARTO Data Warehouse. Drag and drop the `retail_stores` source to the canvas.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2Fns5WDu3Q5W9hTkHi8JgM%2Fimage.png?alt=media&#x26;token=bc18a2a3-55f4-41f5-9ca0-09ad2b308719" alt=""><figcaption></figcaption></figure>

* We are going to focus our analysis in two states: Montana and Wyoming. Luckily, `retail_stores` source contains a column named `state` with each state's abbreviation. First, add one **Simple Filter** component to extract stores whose state column is equal to `MT`. Then click on "Run".

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FYmCdcesB2Qkwp13sC5qD%2Fimage.png?alt=media&#x26;token=f801d3b9-01c0-414a-a696-aeefb34121a4" alt=""><figcaption></figcaption></figure>

* To filter those stores in Wyoming, repeat *Step 4* by adding another **Simple Filter** to the canvas and setting the node configuration to filter those equal to `WY`. Then click on "Run".

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F2lLUeycxTIPtEizUGOL9%2Fimage.png?alt=media&#x26;token=7bd0b495-4521-4a5c-8046-0ec37b8e369f" alt=""><figcaption></figcaption></figure>

* Then, add a **Union All** component to the canvas and add both Simple Filter output to combine both into a single table again.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FuzbOFpGcX083Plj21YkA%2Fimage.png?alt=media&#x26;token=8463ca18-cc24-4d4e-9461-7900cd394ee4" alt=""><figcaption></figcaption></figure>

To do a quick verification, click the **Union All** component to activate it, expand the results panel at the bottom of the Workflows canvas, click the Data Preview tab, and then on the `state` field click the "*Show column stats*" button. After that, the stats should now show the counts for only stores available for `MT` and `WY`.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2Fvapbdz9VUinvtCw9y5yG%2Fimage.png?alt=media&#x26;token=f85e0515-c7af-45f9-8f11-e4ceea1be551" alt=""><figcaption></figcaption></figure>

* In the Components tab, search for the **Create Isolines** component and drag 5 of them into the canvas, connecting each to the Union component from the steps prior. You can edit the component description by double-clicking the text reading "Create Isolines" under each component's icon in the canvas and edit the component name to be more descriptive.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FcEA14CBMByCXb7YiG66A%2Fimage.png?alt=media&#x26;token=43a4fefc-7022-497f-8248-2dd466db795b" alt=""><figcaption></figcaption></figure>

* Now, set up the **Create Isolines** components, which will create the catchment areas. Using the example given below for 10 minute drive time for a car, add the proper settings to each respective component. We will be adding an *Isoline Option* for custom departure time, which will allow each component to mimic driving conditions at that date & time. For that, make sure to enter the following JSON structure in the Isoline Options: `{"departure_time":"2023-12-27T08:00:00"}`. Once the configuration is set, click on "Run".

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FIvV2lkx8NujPUBSSLqfL%2Fimage.png?alt=media&#x26;token=efc9c4d2-d36d-4dc6-935c-0ee55f1cd9cf" alt=""><figcaption></figcaption></figure>

* Now, we will create a new column to store the drive time category, so we can later use it to filter the different catchment areas using a parameter control in Builder. To do so, drag 5 **Create column** components into the canvas and connect each of them with a Create isoline output. In the configuration, set the 'Name for new column' value as "drive\_time" and set the expression to the appropriate distance given for each component such as `10`.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F37QaGP7uOFcvzV8Agszq%2Fimage.png?alt=media&#x26;token=be276c7d-ddff-4c34-ad17-b7d0e8ce82d2" alt=""><figcaption></figcaption></figure>

* Add a **Union all** component and connect all 5 of the Create Column components to it to merge all of these into one single table.
* Finally, let's save our output as a table by using **Save as table** component. Add the component to the canvas and connect it to the Union All component. Set the destination to *CARTO Data Warehouse > organization > private* and save the table as `catchment_regions`. Then, click "Run" to execute the last part of the Workflows.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FrPxqrFgMNVXSgDZ1DrAw%2Fimage.png?alt=media&#x26;token=7426b59e-cd26-4244-8327-a9214a4ea543" alt=""><figcaption></figcaption></figure>

* Before closing the Workflows, set a suitable name to the Workflows such as "*Generating multiple drive time regions*" and add Annotations to facilitate readability.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FpF2ZvWGqnSwglm2CpQpM%2Fimage.png?alt=media&#x26;token=cad862c5-3ddd-41e2-9e7d-89454b0d54e8" alt=""><figcaption></figcaption></figure>

* Before moving to Builder, for the visualization part, we can review the output of the saved table from Map Preview of Workflows itself, when the Save as table component is empty, or we can review it in the Data Explorer. To do so, navigate to Data Explorer section, using the Navigation panel.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FMO1QtWyz6rllUgQ9jzXA%2Fimage.png?alt=media&#x26;token=779d7223-f27e-4ff5-bf15-e1508ae35da0" alt=""><figcaption></figcaption></figure>

* In the Data Explorer section, navigate to CARTO Data Warehouse > organization data > private and look for catchment\_regions table. Click and inspect the source using the Data and Map Preview. Then, click on "*Copy qualified name*" as we will be using in the next Steps of our tutorial.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FHbq01KQmJjOuDwz2JrYA%2Fimage.png?alt=media&#x26;token=ed02c147-df19-47c5-9aa3-151e9f8aed8d" alt=""><figcaption></figcaption></figure>

***

## Create an interactive map with Builder

* In the CARTO Workspace, access the "Maps" sections from the navigation panel.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2Fa99z3j1yTkTfT8t0or5x%2Fimage.png?alt=media&#x26;token=95c30e1b-998b-427f-879f-2dda39b700d3" alt=""><figcaption></figcaption></figure>

* Click on "New map". A new Builder map is opened in a new tab.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F0dAUpFnLy4nxTzSDFJHY%2Fimage.png?alt=media&#x26;token=2e6f0260-7bfa-481f-9ef5-ff7ffd1e8870" alt=""><figcaption></figcaption></figure>

* Name your Builder map to "Analyzing multiple drive-time catchment areas"&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FA5Pu9ZUKkoCx7g1QiizW%2Fimage.png?alt=media&#x26;token=231d0f7e-b606-4f96-b632-7b95c5be6ce9" alt=""><figcaption></figcaption></figure>

* Now, we will add our source as  a SQL Query. To do so, follow these steps:
  * Click on "*Add sources from..*." and select "Custom Query (SQL)"
  * Click on the *CARTO Data Warehouse* connection.
  * Select *Type your own query*.
  * Click on the "*Add Source button"*.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FMMZdxHCPrGyfM0CdjkEq%2Fimage.png?alt=media&#x26;token=8836daf1-2d4d-45b5-a8c9-7493fc82f062" alt=""><figcaption></figcaption></figure>

The SQL Editor panel appears.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F2CfECEOvfrsD3OYRJc8U%2Fimage.png?alt=media&#x26;token=d4d3f0df-596b-461e-93c1-a0bd21394af6" alt=""><figcaption></figcaption></figure>

* Add the resulted table to your map. To do so, the following SQL query in the Editor replacing the qualified table name by yours in *Step 13*.  and click on "*Run*".&#x20;

```sql
SELECT * FROM carto-dw-ac-dp1glsh.private_atena_onboardingdemomaps_ca2c4d8c.catchment_regions
```

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FHLKW2xdcTu8YpS0NrpFz%2Fimage.png?alt=media&#x26;token=b462a681-7a65-4dde-bc66-f53a49a31a03" alt=""><figcaption></figcaption></figure>

Once successfully executed, a map layer is added to the map.&#x20;

* Rename the layer to "Catchment regions". Then, access the layer panel and within **Fill Color** section, color based on `travel_time` column. Just below, `disable` the **Stroke Color** using the toggle butto&#x6E;**.**

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F7g0lOa1ZpPyEWmRNyJCV%2Fimage.png?alt=media&#x26;token=f26282fe-82b3-4b64-8658-ed789239f480" alt=""><figcaption></figcaption></figure>

* Now, let's add a SQL Text Parameter that will allow users to select their desired drive time to analyse the catchment areas around the store locations. To do so, access "*Create a SQL Parameter*" functionality located at the top right corner of the data sources panel.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FCVMGqo1Qcs7m3YjxingM%2Fimage.png?alt=media&#x26;token=f452a86a-dcc2-47a9-bbc7-697df185b891" alt=""><figcaption></figcaption></figure>

* Once the SQL Parameter modal is opened, select Text Parameter type and fill the configuration as per below. Please note you should enter the values manually to provide users with a friendly name to pick the drive time of their choice.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FHxMxVxJsn4zuErtOwaqt%2Fimage.png?alt=media&#x26;token=17c171d5-bf02-434c-91b6-c44a1294f1ee" alt=""><figcaption></figcaption></figure>

* Once the parameter is configured, click on "*Create parameter*". After that, a parameter control is added to the right panel. Copy the *SQL name* so you can add it to the SQL query source.&#x20;
* Now, let's open the SQL Editor of our `catchment_regions` source. As the `travel_time` column is a numeric one, we will be using a regex to select the correct drive time value to filter by the SQL parameter. Update your SQL Query using the below and click on "Run".

```sql
SELECT * FROM carto-dw-ac-dp1glsh.private_atena_onboardingdemomaps_ca2c4d8c.catchment_regions
WHERE travel_time IN (SELECT CAST(REGEXP_EXTRACT(t, r'\d*') AS NUMERIC) FROM {{drive_time}} AS t)
```

Once successfully executed, the layer will be reinstantiated and the parameter control will displayed the selectable values. Now, users can dynamically filter their interested drive time according to their needs.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FQgQVFe8v4ZGKuVNK4Mfs%2Fimage.png?alt=media&#x26;token=4bd692ba-68aa-41b9-8226-886b63339f1d" alt=""><figcaption></figcaption></figure>

* We are ready to publish and share our map. To do so, click on the [Share](https://academy.carto.com/building-interactive-maps/data-analysis/broken-reference) button located at the top right corner and set the permission to Public. In the 'Shared Map Settings', enable SQL Parameter. Copy the URL link to seamlessly share this interactive web map app with others.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FTZkHIPX8TmXNs7K24v1T%2Fimage.png?alt=media&#x26;token=86a618f4-f090-4387-abf8-52c44f6c6e7c" alt=""><figcaption></figcaption></figure>

Finally, we can visualize the results!

{% embed url="<https://clausa.app.carto.com/map/3e33eb02-b525-4666-afaa-8e171e99d794>" %}
