# Scaling common geoprocessing tasks with Spatial Indexes

So, you've decided to start scaling your analysis using Spatial Indexes - great! When using these grid systems, some common spatial processing tasks require a slightly different approach to when using geometries.&#x20;

To help you get started, we've created a reference guide below for how you can use Spatial Indexes to complete common geoprocessing tasks - from buffers to clips. Once you're up and running, you'll be amazed at how much more quickly - and cheaply - these operations can run! Remember - you can always revert back to geometries if needed.&#x20;

All of these tasks are undertaken with CARTO Workflows - our low-code tool for automating spatial analyses. Find more tutorials on using Workflows [here](https://academy.carto.com/creating-workflows/introduction-to-carto-workflows).

***

## Buffer

The humble buffer is one of the most basic - but most useful - forms of spatial analysis. It's used to create a fixed-distance ring around an input feature.

* **With geometries...** use the ST Buffer tool.
* **With Spatial Indexes...** [convert the input geometry](https://academy.carto.com/working-with-geospatial-data/introduction-to-spatial-indexes/create-or-enrich-an-index) to a Spatial Index, then use a H3/Quadbin [K-Ring](https://academy.carto.com/working-with-geospatial-data/work-with-unique-spatial-index-properties#create-k-rings) component to approximate a buffer. Lookup H3 resolutions [here](https://h3geo.org/docs/core-library/restable/) and Quadbin resolutions [here](https://learn.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system) to work out the K-Ring size needed.&#x20;

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXccDjmtO3UMdWhRpnRqBXVAbR6qZGScrPZIGLta6L0UH0siRYO58J-aFm6h6NnrPOTnLvKxxw_ONuQUk9wJ5PxsrCiYz1hgIhdi-MC5eST8QHkGAhbhOQ1IPxdbcXDsUs8hcZxzOvu81HrF9SQrXZBviAY?key=6Kv-Wq6-SnDp4BHN6FWCzg" alt=""><figcaption></figcaption></figure>

## Clip/intersect

Where does geometry A overlap with geometry B? It’s one of the most common spatial tasks, but heavy geometries can make this straightforward task a pain.

* **With geometries...** use the **ST Intersection** tool. This may look like a simple process, but it can be incredibly computationally expensive.&#x20;
* **With Spatial Indexes...** [convert both input geometries](https://academy.carto.com/working-with-geospatial-data/introduction-to-spatial-indexes/create-or-enrich-an-index) to a Spatial Index, then use a **Join** (inner) to keep only cells which can be found in both inputs.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcCA7Hep5d-rqNHxHGoWL0x1XmAemI3gZhdikD1yitYsjC0XnjSvrWRXQIQHVIiZe3ZCx1Sfoz9I9LS1ghm2eupYCvLUeuX7bIh1jYGIXWP-fL6tJIWM___QnDPgEPEonxgF_oiFLJNEzMkce3ePlMs4OY?key=6Kv-Wq6-SnDp4BHN6FWCzg" alt=""><figcaption></figcaption></figure>

## Difference

For a “difference” process, we want the result to be the opposite of the previous intersection, retaining all areas which do not intersect.&#x20;

* **With geometries...** use the **ST Difference** tool. Again, while this may look straightforward, it can be slow and computationally expensive.&#x20;
* **With Spatial Indexes...** again [convert both input geometries](https://academy.carto.com/working-with-geospatial-data/introduction-to-spatial-indexes/create-or-enrich-an-index) to a Spatial Index, this time using a  full outer **Join**. A Where component can then be used to filter only "different" cells (where h3 IS null AND h3\_joined IS not null) - at a fraction of the calculation size.&#x20;

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfpvnG6rz4881XskKg1c51_njbf1VIKGJhr3dJB8a-IzNvQ2ZnZzmRwIlJq-eYgyot_RFfRWNOW-BpA920A0u4vzSrQQ_4M89KhltQXVmJ9thg7KPVQbDdJv0YaOBM7AWXavWguOO04QIR7mNvRj9oELge6?key=6Kv-Wq6-SnDp4BHN6FWCzg" alt=""><figcaption></figcaption></figure>

## Spatial Join

Spatial Joins are the "bread and butter" of spatial analysis. They can be used to answer questions like "how many people live within a 10-minute drive of store X?" or "what is the total property value in this flooded area?"

Our [Analytics Toolbox](https://academy.carto.com/advanced-spatial-analytics/introduction-to-the-analytics-toolbox) provides a series of **Enrichment** tools which make these types of analyses easy. Enrichment tools for both geometries and Spatial Indexes are available - but we've estimated the latter of these are up to 98% faster!

* **With geometries...** use the **Enrich Polygons** component.
* **With Spatial Indexes...** use the **Enrich H3 / Quadbin Grid** component.&#x20;

Check out the full guide to enriching Spatial Indexes [here](https://academy.carto.com/working-with-geospatial-data/create-or-enrich-an-index#enrich-an-index).&#x20;

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeXloZz07Bsk2tjeZc6cE_28aoNpjDfth428ja_UvxAjEUbyPevMJqMcDe8b_vDkBydtzSjvzHCqYKefqrXK6KNyXJSP4sLtShICktEh0ZNbanVC6-BgOuIHz8odCqpJpkbtNsw06kT83xcYrzHZqu6Hl4?key=6Kv-Wq6-SnDp4BHN6FWCzg" alt=""><figcaption></figcaption></figure>

## Aggregate within a distance

Say you wanted to know the population within 30 miles of&#x20;

For instance, in the example below we want to create a new column holding the number of stores in a 1km radius.&#x20;

* **With Geometries...** create a Buffer, run a Spatial Join and then use Group by to aggregate the results.&#x20;
* **With Spatial Indexes...** have the inputs stored as a H3 grid with both the source and target features in the same table. Like in the earlier Buffer example, use the H3 K-Ring component to create your "search area." Now, you can use the Group by component - grouping by the newly created H3 K-Ring ID - to sum the number of stores within the search area.&#x20;

This is a fairly simple example, but let's imagine something more complex - say you wanted to calculate the population within 30 miles of a series of input features. Creating and enriching buffers of this size - particularly when you have tens of thousands of inputs - will be incredibly slow, particularly when your input data is very detailed. This type of calculation could take hours - or even days - without Spatial Indexes.&#x20;

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf8Kj1poQd4dnHe4ikYTR2DQTYgztM8MBY9C2aNL4na9ueU1tqjtMFrPUQI2zSsHUc-YMOzeCfvyhy1yAUpnzd028vfFXVR3HH2rWTnGPZWO3iEGDjJB5EKj_PBCrx1sc-EIPlIiXD8798X37tgvNwTpek?key=6Kv-Wq6-SnDp4BHN6FWCzg" alt=""><figcaption></figcaption></figure>

***

## Next up...

{% embed url="<https://academy.carto.com/working-with-geospatial-data/introduction-to-spatial-indexes/using-spatial-indexes-for-analysis>" %}
