# 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="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/emCzkZB5k5zMtjbJd4Ec/Screenshot%202023-10-18%20at%2015.37.07.png" 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="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/H0QPHBnILKPreIwF1J6a/Screenshot%202023-10-18%20at%2016.03.01.png" 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="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/3xXpL6L6LkSnVeYcmuMv/Screenshot%202023-10-18%20at%2016.43.58.png" 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="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/8m39L1ImCIh9yk4escKh/Screenshot%202023-10-18%20at%2017.01.40.png" 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="https://content.gitbook.com/content/FEElAdsRIl9DzfMhbRlB/blobs/j5yZRpgyo67unvgI5Odv/Screenshot%202023-10-20%20at%2018.21.56.png" alt="" width="375"><figcaption></figcaption></figure></div>
