[ad_1]
Every single day and each minute, Uber receives a number of requests of their market. Every occasion occurs at a particular location, for instance a rider asks for a trip in a single location and a driver accepts the drive in a close-by location. Deriving info and insights from the info, for instance setting dynamical pricing that’s primarily based on the demand, requires analyzing knowledge throughout a complete metropolis. However as cities are geographically very various, this evaluation should occur in high-quality granularity. With the H3 hexagon grid system, every knowledge level will be bucketed to at least one hexagon space or cell, after which one Uber can calculate provide and demand for surge pricing on every hexagon in all of the cities the place they’ve companies. The hexagons come in several sizes so one should select the decision that most closely fits the aim of the evaluation.
Technically one may construct a worldwide grid system utilizing any type of constructing block that facilitates an entire tiling all through the 3D globe. For instance one may use triangles (3 sides) or squares (4 sides) as a substitute of hexagons (6 sides) to cowl the entire Earth. Nonetheless, utilizing hexagons has many benefits. For instance, the centerpoint of a triangle has three and a sq. has two totally different distances to its neighbors’ centerpoints whereas the centerpoint of a hexagon has equal distance to all of its neighbors’ which makes it a handy system to approximate radiuses (see picture under).
Nonetheless, the world can’t be divided utterly into hexagons so few pentagons (5 sides) are wanted as nicely (12 to be precise, on every decision). The pentagons introduce discontinuities to the grid however typically they’re positioned far-off from the land so it causes issues for primarily marine knowledge evaluation. Regardless of the presence of some pentagons, the hexagon grid affords the benefit of offering constructing blocks which are comparatively uniform in measurement on the 3D spherical floor. In case one needs to learn extra in regards to the geometrics of a hexagon grid, right here’s supply for it. Notice that defining the hexagon areas is extremely arbitrary, and they don’t observe any pure options corresponding to lakes, rivers, mountains or nation borders.
The H3 library is open supply, accessible on GitHub and written in C. It has bindings accessible on a number of languages, for instance on Python, C, Java and Javascript. H3 comes with a hierarchical indexing system which makes it very environment friendly. One can look at the hexagons additional utilizing an internet H3 hexagon knowledge viewer. The desk under summarizes the properties of the 16 totally different resolutions that H3 supplies.
Subsequent, we are going to introduce a number of the most vital functionalities of the H3 library.
On this article we are going to use the H3 hexagon system to cluster location knowledge into hexagons. The documentation of H3 library will be discovered right here. There are two most important variations of this library, variations 3 and 4, and in our notebooks we are going to use model 3.7.6. Notice that there are important variations in operate names between model 3.x and 4.x as listed in right here.
H3 Python package deal is simple to put in for instance with pip:
pip set up h3
If you wish to specify which model you wish to use, add there the model quantity, for instance h3==3.7.6. Then import H3 to your Python pocket book with
import h3
Subsequent, we are going to introduce a number of the most vital capabilities of the H3 library.
Hexagon index
H3 makes use of a hierarchical indexing system, which transforms latitude and longitude pairs to a 64-bit H3 index that identifies every grid cell. With given coordinates (latitude and longitude) and with chosen decision, we get the hexagon index:
# Model 3.X:
hexagon_index = h3.geo_to_h3(lat, lng, decision)# Model 4.X:
hexagon_index = h3.latlng_to_cell(lat, lng, decision)
For instance
h3.geo_to_h3(60.169833, 24.938163, 6)
returns index ‘861126d37ffffff’. If you’d like, you should utilize an on-line H3 hexagon knowledge viewer to verify the place this hexagon is positioned.
So after we know the exact coordinates for an information level, we are able to decide its hexagon index at varied resolutions and affiliate it with hexagons of various sizes.
Hexagon boundaries
To make use of hexagons in our plots, we should decide the hexagon boundaries from the hexagon index. Notice that coordinates in some techniques are introduced as (lng, lat), whereas in others, they observe the format (lat, lng). The geo_json=True/False possibility lets you swap these coordinates.
# Model 3.X:
boundary = h3.h3_to_geo_boundary(hexagon_index, geo_json = False)# Model 4.X:
boundary = h3.cell_to_boundary(hexagon_index, geo_json = False)
For instance
h3.h3_to_geo_boundary('861126d37ffffff', geo_json = False)# Returns:
((60.15652369744344, 24.856525761155346),
(60.13498207546084, 24.895664284494664),
(60.14431977678549, 24.948769321085937),
(60.175221029708474, 24.962796993345798),
(60.19677983831024, 24.92362795620145),
(60.187420192445906, 24.870461733016352))
These six coordinate pairs correspond to the beginning and ending factors of the hexagon edges.
Neighboring hexagons
Typically we have to establish the neighbors of a particular hexagon, or “kring” across the hexagon. With okay=0 the operate returns the origin index, with okay=1 it returns the origin index and its all neighboring indices, and with okay=2 it returns the origin index, its neighboring and next-to-neighboring indices, and so forth.
# Model 3.X:
kring = h3.k_ring(hexagon_index, okay)# Model 4.X:
kring = h3.grid_disk(hexagon_index, okay)
Additionally there’s a operate that can be utilized to calculate the grid distance between two cells:
# Model 3.X:
kring = h3.h3_distance(hexagon_index_a, hexagon_index_a)# Model 4.X:
kring = h3.grid_distance(hexagon_index_a, hexagon_index_a)
We will use these capabilities within the following means:
# Nearest neighbours of the hexagon:
h3.k_ring('861126d37ffffff', 1)# Returns:
{'86089969fffffff',
'86089ba4fffffff',
'86089ba6fffffff',
'861126d07ffffff',
'861126d17ffffff',
'861126d27ffffff',
'861126d37ffffff'}
# Distance between two hexagons:
h3.h3_distance('861126d37ffffff', '86089ba4fffffff')
# Returns
1
Plotting the hexagons
There are a number of methods on learn how to plot hexagons on a map however a few of them are fairly stiff, time consuming to make use of and never well-documented. For simplicity, we’re primarily utilizing matplotlib for visualizations however we additionally experiment and take screenshots of visualizations with folium maps. Extra particulars on these plotting strategies will be discovered from the GitHub repository.
On the left within the above determine we use matplotlib for plotting the hexagons. We make the most of the GADM library to fetch the multipolygon representing the Helsinki area and plot it with inexperienced shade. We use blue within the background to symbolize our bodies of water. Moreover, we embody a marker denoting the Helsinki metropolis middle on the map. The hexagons are simply rendered utilizing the plot_polygon operate from the shapely library and knowledge factors will be added to the plot by utilizing scatterplot. This makes the plotting very straightforward and fast.
We additionally experimented with different plotting strategies, corresponding to utilizing folium maps that enables us to create an interactive HTML map that enables us to zoom out and in within the map. On the proper within the above determine we present a screenshot of such a map. Despite the fact that the result’s aesthetically good, it is rather time consuming so as to add new options (corresponding to colorbars or heatmaps) to the map so it’s not the very best software for exploratory knowledge evaluation. The pocket book for plotting the interactive folium maps will be present in right here.
[ad_2]