# Create or enrich an index

The tutorials on this page will teach you the fundamentals for working with Spatial Indexes; how to create them!&#x20;

* [Convert points to a Spatial Index](#converting-points-to-a-spatial-index); convert a point geometry dataset to a Spatial Index grid, and then aggregate this information.
* [Convert polygons to a Spatial Index](#convert-polygons-to-a-spatial-index).
* [Converting lines to a Spatial Index.](#converting-lines-to-a-spatial-index)
* [Enrich an index](#enrich-an-index); take numeric data from a geometry input such as a census tract, and aggregate it to a Spatial Index.

Note that when you're running any of these conversions, you aren't replacing your geometry - you're just creating a new column with a Spatial Index ID in it. Your geometry column will still be available for you, and you can easily use either - or both - spatial format depending on your use case.&#x20;

***

## Convert points to a Spatial Index

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8iAlvj1s2Th85m6IvcG5%2Fbeginner%20banner.png?alt=media&#x26;token=10e8f40a-3632-4920-87c3-6b0dca2a6775" alt="Beginner difficulty level banner" width="175"><figcaption></figcaption></figure></div>

In this tutorial, we will be building the below simple workflow to convert points to a Spatial Index and then generate a count for how many of those points fall within each Spatial Index cell.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FYncHcqWr8090ptyilVOl%2FAcademy_SpatialIndexes_PointtoH3.png?alt=media&#x26;token=4d18a26e-7f0c-4606-b44e-e8f006e31a3a" alt=""><figcaption><p>Converting points to Spatial Indexes - the Workflow</p></figcaption></figure>

💡 You will need access to a point dataset - we'll be using San Francisco Trees, which all CARTO users can access via the [CARTO Data Warehouse](https://docs.carto.com/carto-user-manual/connections/carto-data-warehouse) - but you can substitute this for any point dataset.

1. Once logged into your CARTO account, head to the **Workflows** tab and **Create a new workflow**. Select a connection. If you're using the same input data as us, you can use the **CARTO Data Warehouse** - otherwise select the connection with your source data.
2. Switch to the **Sources** tab and navigate to your point table (for us, that's CARTO Data Warehouse > Organization > demo\_tables > san\_francisco\_street\_trees) then drag it onto the workflow canvas.&#x20;
3. Next, switch to the **Components** tab and drag the **H3 from GeoPoint** onto the canvas, connecting it to the point dataset. This will convert each point input to the H3 cell which it falls inside. Alternatively, you could use the **Quadbin from GeoPoint** if you wanted to create a square grid instead. Learn more about which Spatial Index is right for you [here](https://academy.carto.com/working-with-geospatial-data/introduction-to-spatial-indexes).&#x20;
4. Here we can **change the resolution** of the H3 output; the larger the number, the smaller the H3 resolution, and the more geographically detailed your analysis will be. If you're following our example, change the resolution to 10. Note if you're using a different point table, you may wish to experiment with different resolutions to find one which adequately represents your data and will generate the insights you're looking for.&#x20;
5. **Run your workflow** and examine the results! Under the table preview, you should see a new variable has been added: H3. This index functions to geolocate each H3 cell.&#x20;
6. Next, add a **Group by** component; we will use this to count the number of trees which fall within each H3 cell. Draw a connection between this and the output (right) node of H3 from GeoPoint. Select H3 in both the **Group by** and **Aggregation** parameters, and set the aggregation type to **Count**. At this point, you can also input any numeric variables you wish to aggregate and operators such as **Sum** and **Average**.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F1TAJqSjfocnjBLJFVtrc%2Fspatial_indexes_point_parameters.png?alt=media&#x26;token=2208dec1-1676-44fe-ae52-e589c74b4b55" alt="" width="255"><figcaption><p>Setting the Group by parameters</p></figcaption></figure>

**Run your workflow** again!&#x20;

If you've been following along with this example, you should now be able to create a tree count map like the below!

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FuPg1dPxEbILqTtbwMmOE%2Fspatial_indexes_points_to_index.png?alt=media&#x26;token=07ad2f25-208c-4055-9141-8deadc87b5f0" alt=""><figcaption><p>Converting points to polygons - the results!</p></figcaption></figure>

***

## Convert polygons to a Spatial Index

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8iAlvj1s2Th85m6IvcG5%2Fbeginner%20banner.png?alt=media&#x26;token=10e8f40a-3632-4920-87c3-6b0dca2a6775" alt="Beginner difficulty banner" width="175"><figcaption></figcaption></figure></div>

In this tutorial, we will build the below simple workflow to convert a polygon to a Spatial Index.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F9CGp8w1ROitBVbTenT0X%2Fspatial_indexes_polygon_to_index_workflow.png?alt=media&#x26;token=0e42500d-3aa1-40d5-b812-32daa232cd2d" alt=""><figcaption><p>Converting polygons to Spatial Indexes - the Workflow</p></figcaption></figure>

💡 You will need access to a polygon dataset. We will use [US Counties](https://carto.com/spatial-data-catalog/browser/geography/cdb_county_7fc835db/) (which you can subscribe to for free from the CARTO Data Observatory) but - again - you're welcome to use any polygon dataset for this.

1. Drag the polygon "area of interest" table onto the workflow canvas. You can do this again through the Sources tab, and if you - like us - are using a table that you've subscribed to from our Data Observatory, then switch to the **Data Observatory** tab (at the bottom of the screen). For our example, we need to navigate to CARTO > County - United States of America (2019).
2. If the table you've just added contains some superfluous features you can use a **Simple Filter** to omit these. For instance, we'll filter the counties table to the feature which has the "do\_label" of San Francisco.&#x20;
3. Next, drag a **H3 Polyfill** onto the canvas (or a Quadbin polyfill if you chose to work with that Index). Select the resolution you wish to use; we'll use 10. Please note if you are using multiple polygons as your "area of interest" then duplicate H3 cells may be generated along touching borders; you can use **Group by** to omit these duplicates in the same way that we did earlier (but with no need to include a count aggregation).&#x20;
4. **Run your workflow**! If you're following our example, you should see that we have 7,779 H3 cells comprising the area of San Francisco.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FSXc2LizOIcK3rAxiSWfm%2Fspatial_indexes_polyfill_map.png?alt=media&#x26;token=499ba62d-2c15-4244-ae2d-da43829c6b91" alt=""><figcaption><p>H3 Polyfill results</p></figcaption></figure>

***

## Converting lines to a Spatial Index

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8iAlvj1s2Th85m6IvcG5%2Fbeginner%20banner.png?alt=media&#x26;token=10e8f40a-3632-4920-87c3-6b0dca2a6775" alt="Beginner difficulty banner" width="175"><figcaption></figcaption></figure></div>

If you have a line geometry that you wish to convert to a Spatial Index, the approach is slightly different. First, you need to convert the data to a polygon by **buffering** it - and then converting that polygon to a Spatial Index like in the tutorial [above](#convert-polygons-to-a-spatial-index).&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FIIBJgNyhMtH5MD7d8LO9%2Fspatial%20indexes%20workflows%20h3%20lines.png?alt=media&#x26;token=f81879f7-027b-40ec-9d91-975380d2fb47" alt=""><figcaption></figcaption></figure>

💡 Looking for a line-based table to practice on? In the CARTO Data Warehouse under demo data > demo tables, try the bristol\_cycle\_network table.

Let's build out the above workflow!

1. Drag your line source onto the Workflows canvas.
2. Connect this to an **ST Buffer** component. Set the buffer distance as 1 meter.
3. Connect this to a **H3 Polyfill** component. You'll likely want this fairly detailed - the larger the resolution number the more detailed the grid will be (we've used a resolution of 12). To ensure a continuous grid along your whole line, change the mode to **Intersects**.

And **Run**! ⚡ Your results should look something like the below:

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FvPsz60PBnm0nhQtoG932%2Fspatial%20indexes%20builder%20h3%20lines.png?alt=media&#x26;token=b10011b4-611b-41ab-978c-6044f3a1eb8e" alt=""><figcaption><p>A H3 grid representing linear features</p></figcaption></figure>

***

## Enrich an index

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8iAlvj1s2Th85m6IvcG5%2Fbeginner%20banner.png?alt=media&#x26;token=10e8f40a-3632-4920-87c3-6b0dca2a6775" alt="Beginner difficulty banner" width="175"><figcaption></figcaption></figure></div>

In this tutorial, you will learn how to take numeric data from a geometry input, and aggregate it to a Spatial Index. This is really useful for understanding things like the average age or total population per cell.

💡 You will need access to a Spatial Index table for this. You can follow either of the above tutorials to create one - we'll be using the results from the [Convert polygons to a Spatial Index](#convert-polygons-to-a-spatial-index) tutorial. You will also need access to a source dataset which contains the numeric information you want to aggregate. In our example, we want to find out the total population and average income for each Spatial Index cell; we will use "[Sociodemographics, 2018, 5yrs - United States of America (Census Block Group)](https://carto.com/spatial-data-catalog/browser/dataset/acs_sociodemogr_95c726f9/)" which you can subscribe to for free from the CARTO Spatial Data Catalog.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FKR8l5CA2AeljvkdVc4zN%2Fspatial_indexes_enrichment_workflow.png?alt=media&#x26;token=7626969a-d1e4-48ca-89b4-6ad382b44981" alt=""><figcaption><p>Enrich an index workflow</p></figcaption></figure>

1. Drag both your source dataset and Spatial Index dataset onto a workflow canvas. If you're building on an existing workflow such as one of the above, you can just continue to edit.
2. Next drag an **Enrich H3 Grid** component onto the canvas. Note you can also use an **Enrich Quadbin Grid** if you are working with this type of index.&#x20;
3. Connect your **target** H3 grid to the top input, and your **source** geometry (for us, that's Census block groups) to the bottom input.
4. Set the following parameters:
   1. Target H3 column: H3 (or whichever field is holding your H3 index)
   2. Source geo column: geom (or - again - whichever field is holding your source geometry data)
   3. Variables: select the variables and aggregation types. For us, that's total\_pop\_3409f36f (SUM) and median\_income\_6eb619a2 (AVG). Be mindful of whether your variables are extensive or intensive when doing this.
   4. You can also set a K-ring and decay function to incorporate neighborhood statistics in the enrichment.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FQlcIMaPmOkomX5Mxt6sF%2Fspatial_indexes_enrichment_parameters.png?alt=media&#x26;token=289bb017-d7ec-4e9d-bcff-673ba88d8ad4" alt="" width="249"><figcaption><p>Enrichment parameters</p></figcaption></figure>

**Run!** The result of this should be a table with three columns; a H3 index, total population and average income.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FMExREiPJJUFrm6az6L4E%2Fspatial_indexes_enrichment_result.png?alt=media&#x26;token=f6dd3716-dc23-4715-9280-a4044800ce8f" alt=""><figcaption><p>Enrich an index result</p></figcaption></figure>

***
