> For the complete documentation index, see [llms.txt](https://academy.carto.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-bigquery/step-by-step-tutorials/a-nyc-subway-connection-graph-using-delaunay-triangulation.md).

# A NYC subway connection graph using Delaunay triangulation

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

Providing a good network connection between subway stations is critical to ensure an efficient mobility system in big areas. Let’s imagine we need to design a well-distributed subway network to connect the stations of a brand-new subway system. A simple and effective solution to this problem is to build a Delaunay triangulation of the predefined stations, which ensures a good connection distribution.

For this particular example we are choosing the New York city subway stations to build the cited triangulation. The following query will construct the triangulation using the `ST_DELAUNAYLINES` function from the processing module of the Analytics Toolbox.

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

```sql
WITH data AS (
    SELECT ARRAY(
        SELECT geom FROM `cartobq.docs.nyc_subway_stations`
    ) AS array_points
),
delaunay_array AS (
    SELECT `carto-un`.carto.ST_DELAUNAYLINES(array_points) AS nested
    FROM data
),
delaunay_triangles AS (
    SELECT geom
    FROM delaunay_array, UNNEST(nested) AS geom
)
select * from delaunay_triangles
```

{% endtab %}

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

```sql
WITH data AS (
    SELECT ARRAY(
        SELECT geom FROM `cartobq.docs.nyc_subway_stations`
    ) AS array_points
),
delaunay_array AS (
    SELECT `carto-un-eu`.carto.ST_DELAUNAYLINES(array_points) AS nested
    FROM data
),
delaunay_triangles AS (
    SELECT geom
    FROM delaunay_array, UNNEST(nested) AS geom
)
select * from delaunay_triangles
```

{% endtab %}

{% tab title="manual" %}

```sql
WITH data AS (
    SELECT ARRAY(
        SELECT geom FROM `cartobq.docs.nyc_subway_stations`
    ) AS array_points
),
delaunay_array AS (
    SELECT carto.ST_DELAUNAYLINES(array_points) AS nested
    FROM data
),
delaunay_triangles AS (
    SELECT geom
    FROM delaunay_array, UNNEST(nested) AS geom
)
select * from delaunay_triangles
```

{% endtab %}
{% endtabs %}

In the visualization below, each subway station is represented by a point where the color and size represent the number of received connections.

{% embed url="<https://clausa.app.carto.com/map/631a843f-d1e9-49c9-9e75-45ad02df1abf>" %}

This map has been created by grouping the delauney connections by station ID, using the below SQL:

```sql
//WITH data AS (
```

As a curiosity, the average number of connections between the existing New Yorks subway stations is close to six, which indicates a balanced distribution of the stations according to the [Delaunay triangulation properties](https://en.wikipedia.org/wiki/Delaunay_triangulation#Properties).

<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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://academy.carto.com/advanced-spatial-analytics/spatial-analytics-for-bigquery/step-by-step-tutorials/a-nyc-subway-connection-graph-using-delaunay-triangulation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
