# Spatial Analysis

### Find clusters of customers with K-Means algorithm&#x20;

<table data-full-width="false"><thead><tr><th align="center">CARTO DW</th><th width="154" align="center">BigQuery</th><th align="center">Snowflake</th><th align="center">Redshift</th><th align="center">PostgreSQL</th><th data-hidden>Snowflake</th><th data-hidden>Redshift</th><th data-hidden>PostgreSQL</th><th data-hidden>CARTO DW</th></tr></thead><tbody><tr><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td></td><td></td><td></td><td></td></tr></tbody></table>

This example demonstrates how to use Workflows to find clusters of points using the K-Means algorithm.

[**Download example**](https://storage.googleapis.com/carto-workflows-examples/files/kmeans.sql)

<div align="left"><figure><img src="/files/mJOMi4j5DGrAc5N1mLxx" alt="" width="375"><figcaption></figcaption></figure></div>

### Find points that lie within a set of polygons and add properties&#x20;

<table data-full-width="false"><thead><tr><th align="center">CARTO DW</th><th width="154" align="center">BigQuery</th><th align="center">Snowflake</th><th align="center">Redshift</th><th align="center">PostgreSQL</th><th data-hidden>Snowflake</th><th data-hidden>Redshift</th><th data-hidden>PostgreSQL</th><th data-hidden>CARTO DW</th></tr></thead><tbody><tr><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td></td><td></td><td></td><td></td></tr></tbody></table>

This example demonstrates how to use Workflows to perform a geospatial intersection, finding points within polygons and adding properties.

[**Download example**](https://storage.googleapis.com/carto-workflows-examples/files/point_in_polygon.sql)

<div align="left"><figure><img src="/files/cKvwsxU32t3P3dqI3Eh3" alt="" width="375"><figcaption></figcaption></figure></div>

### Aggregate point data into polygons&#x20;

<table data-full-width="false"><thead><tr><th align="center">CARTO DW</th><th width="154" align="center">BigQuery</th><th align="center">Snowflake</th><th align="center">Redshift</th><th align="center">PostgreSQL</th><th data-hidden>Snowflake</th><th data-hidden>Redshift</th><th data-hidden>PostgreSQL</th><th data-hidden>CARTO DW</th></tr></thead><tbody><tr><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td></td><td></td><td></td><td></td></tr></tbody></table>

This example demonstrates how to use Workflows to perform an spatial intersection between points and polygons, adding aggregated data from the points into the polygons.

[**Download example**](https://storage.googleapis.com/carto-workflows-examples/files/point_in_polygon_agg.sql)

<div align="left"><figure><img src="/files/D3kC4PwZldzUcoQqL1hq" alt="" width="375"><figcaption></figcaption></figure></div>

### Generate Voronoi polygons from a set of points&#x20;

<table data-full-width="false"><thead><tr><th align="center">CARTO DW</th><th width="154" align="center">BigQuery</th><th align="center">Snowflake</th><th align="center">Redshift</th><th align="center">PostgreSQL</th><th data-hidden>Snowflake</th><th data-hidden>Redshift</th><th data-hidden>PostgreSQL</th><th data-hidden>CARTO DW</th></tr></thead><tbody><tr><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td></td><td></td><td></td><td></td></tr></tbody></table>

This example demonstrates how to use Workflows to generate Voronoi polygons from a set of points. Voronoi polygons are often used to find service areas for market analysis.

[**Download example**](https://storage.googleapis.com/carto-workflows-examples/files/voronoi.sql)

<div align="left"><figure><img src="/files/29rKanOjRZH8lN0H1hk3" alt="" width="359"><figcaption></figcaption></figure></div>

### Custom SQL Select using 2 inputs

<table data-full-width="false"><thead><tr><th align="center">CARTO DW</th><th width="154" align="center">BigQuery</th><th align="center">Snowflake</th><th align="center">Redshift</th><th align="center">PostgreSQL</th><th data-hidden>Snowflake</th><th data-hidden>Redshift</th><th data-hidden>PostgreSQL</th><th data-hidden>CARTO DW</th></tr></thead><tbody><tr><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td align="center"><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span></td><td></td><td></td><td></td><td></td></tr></tbody></table>

This example demonstrates how to use the "Custom SQL Select" component, using placeholders to reference two different inputs.

[**Download example**](https://storage.googleapis.com/carto-workflows-examples/files/custom_sql.sql)

<div align="left"><figure><img src="/files/5yJNg0Y2YypunT6g3hcu" alt="" width="375"><figcaption></figcaption></figure></div>


---

# 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/creating-workflows/workflow-templates/spatial-analysis.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.
