A brief hands-on article on accessing Uber’s H3 hexagonal indexes utilizing its python API
It’s now been 4 years since Uber open sourced its H3 grid. The corporate had initially developed the indexing system for journey optimization, nevertheless it turned out to be helpful for all types of duties that contain visualizing and exploring spatial information.
Whereas Uber needed to give you these hexagonal hierarchichal indexes to set dynamic costs, researchers and practitioners within the geospatial subject have been utilizing grid methods to bucket occasions into cells for a very long time. One of the vital recognized makes use of of hexagons in fashions is Christaller’s Central Place Concept, a concept in city geography that makes an attempt to clarify the explanations behind the distribution patterns, measurement, and plenty of cities and cities world wide. The idea offers an hexagonal grid framework by which areas may be studied for his or her locational patterns.
The hexagon form is good right here as a result of it permits the triangles fashioned by the central place vertexes to attach, and permits for every cell to the touch (and probably join and set up a movement, for example) with six different cells in the identical manner.
Whereas hexagons undergo from much less vital distortion, they can’t be completely subdivided nor do they align to streets. All of those limitations are addressed by the bias of clustering, aggregating cells that in flip simmulate shapes rather more helpful for evaluation.
The H3 system offers cells with comparable sizes and styles internationally. This offers with a possible for establishing a normal by which any spatial dataset may be set to. Because of this the extra spatial analysis use this grid, the extra the datasets may be interchangeable and in comparison with each other.
Take for example Montréal-based Anagraph’s demographic dataset of Canada, which used completely different sources — amongst which the Canadian 2021 inhabitants census — to mannequin a inhabitants hexagonal grid dataset that makes use of the H3 index.
The identical goes for influential mobility researcher Rafael Pereira, whose analysis on entry to alternatives in Brazil makes good use of H3 indexes to raised seize the spatial dynamics of phenomenons that contain motion paths or connectivity.
In the event you work with spatial information, you could be anticipating to have the ability to get a hyperlink to obtain a shapefile someplace. That’s not the way in which it’ll go right here.
To get it, we’ll want to make use of the H3 API. In case you’re questioning, an utility programming interface (API) units the instruments that can be utilized to make calls to (that’s, to entry) the server that holds the information we’d like. Mainly because of this we will work together with this utility and its information by utilizing a set of pre-defined guidelines by sending sure messages to the system.
With that in thoughts, let’s use the strategies described within the H3 API documentation to extract all polygons intersecting an space of curiosity. Step one might be ingesting the stated space by making use of geopandas.
Now that we received ourselves a geodataframe that incorporates the world for which we need to extract the hexagons, let’s begin utilizing the precise H3 library. We’ll begin by utilizing h3’s polyfill methodology, a operate that may take the geometry of the primary and solely function from the geodataframe and a scale — for the reason that H3 system is multiscalar, there are a number of sizes of hexagons we might extract — which right here might be 8, essentially the most granular degree.
The hexs variable will return a quite simple checklist with the IDs of all hexagons at degree 8 that intersect the world supplied.
Subsequent we’ll use that checklist to ask the H3 API for the precise geometries that correspond to every of them. To take action, we’ll create a lambda operate that passes an enter (a hex ID) to the h3_to_geo_boundary methodology and remodel the response right into a shapely’s polygon.
We’ll then go the checklist of IDs (the hexs variable) to the polygonise operate by utilizing python’s map methodology. This in flip is reworked into an inventory of polygons that may populate a geoseries (or an inventory of geopanda’s spatial sorts).
That is wanting precisely like what we have been in search of, so all that’s left is to create a geodataframe with the outcomes and export it to a geopackage (or a shapefile should you’re old fashioned).
That is it! We efficiently extracted the h3 hexagons for our space of curiosity with only a few strains of code.
If in case you have questions or options, don’t hesitate to drop me a line anytime. In the event you loved this text, think about shopping for me a espresso so I hold writing extra of these!