Use Boundaries in your application
Last updated
Was this helpful?
Last updated
Was this helpful?
A very common problem for map makers and developers is how to aggregate large amounts of data, such as millions of records; into known or custom boundaries, such as postal codes or census areas. Traditionally, it's hard to make these visualizations perform great, and even harder to build them with the ability to apply dynamic filtering and aggregations.
With CARTO this problem is solved thanks to a new method called "Boundaries".
This guide will teach you the fundamentals of Boundaries, and it will guide you through creating your first visualization using both predefined and custom boundaries.
Boundaries in CARTO work by completely separating the data from the static geometries that we want to aggregate into, as seen in this image.
Because both the data and geometries share a matching column with an ID (eg: zip code), we are now able to perform the aggregation without using the geometry, only to incorporate the unchanged geometries later to visualize the result.
This approach provides an alternative to traditional methods of preparing, storing, and serving pre-calculated geometries for each filter combination, offering a more streamlined and efficient solution.
As we've covered above, we need to make sure that our properties (table or SQL query) contains a column called geoid
that represents the common matching attribute, such as the zip code ID.
For SQL queries, you can simply do something such as:
Now that our properties are ready, let's find the corresponding boundaries:
We have created a catalog that includes known boundaries for some countries. It currently covers different types of boundaries for the United States, Canada, Mexico, United Kingdom and Spain.
Select a country and the corresponding tileset to the boundary you're looking for. Then select the data warehouse between BigQuery and Snowflake, and click on "Use this boundary in your app" button.
This dialog shows all the necessary information to use a boundary in an application. Depending on the data warehouse selected in the explorer, you will find a different type of Boundary ID.
For BigQuery, you also need to select a region that matches the region where your data is hosted. The boundaries' tilesets are available in different public BigQuery datasets maintained by CARTO.
Copy the Boundary ID and use it directly in your application's code following the provided example.
Once you have the CARTO Boundaries database available in your Snowflake account, use the Boundary ID, making sure you use the appropriate database name.
While the collection of boundaries provided by CARTO will be expanding and covering more territories over time, you might need to use a custom set of geographies in your application. For example, when working with agricultural parcel data or disputed territory boundaries.
The key for a tileset to be usable as a boundary is to include an additional geoid
column. For this, make sure that you add the "calculate_geoids": true
option to the tileset creation procedure.
These are examples for creating boundary tileset in BigQuery and Snowflake:
As with any other interaction with the CARTO APIs, using Boundaries requires the user to provide a valid API Access Token with the appropriate grants.
Type or paste the Boundary ID, as well as the source for the data to be joined with.
See below the most relevant part of the example, where we select our properties as propertiesSqlQuery
and our boundaries as tilesetTableName
.
Please note that you could change your properties query dynamically to implement easy, performant and scalable filtering and aggregation methods.
Voilá! 🎉
You should now be able to add Boundaries-based visualizations to your applications, and experience first-hand the improved performance, especially when changing dynamically the properties, filtering, or tweaking the aggregation method.
Known boundaries: predefined boundaries made available by CARTO. You can find them in the .
In our , the properties are financial transactions, the boundaries are zip codes in the US (known boundary from CARTO), and the geoid column contains the actual zip codes.
For tables, you can use to rename or add columns.
Explore the catalog from the application.
For Snowflake, CARTO publishes the collection of boundaries in .
Generating a tileset that can be used as a boundary is easy using the CARTO Analytics Toolbox procedures in the tiler module. See the reference for and .
Check to learn more about creating API Access Tokens.
Take a look at the live example for an application using Boundaries .