Create or enrich an index

Get started with Spatial Indexes

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


Convert points to a Spatial Index

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.

💡 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 - 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.

  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.

  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.

  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.

  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.

Run your workflow again!

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


Convert polygons to a Spatial Index

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

💡 You will need access to a polygon dataset. We will use US Counties (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.

  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).

  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.


Converting lines to a Spatial Index

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.

💡 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:


Enrich an index

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 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)" which you can subscribe to for free from the CARTO Spatial Data Catalog.

  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.

  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.

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


Last updated