H3: A hexagonal hierarchical geospatial indexing system developed by Uber Technologies

Krati Agarwal
Analytics Vidhya
Published in
3 min readApr 6, 2020

What is an h3 geospatial indexing system?

The H3 geospatial indexing system is a multi-precision hexagonal tiling of the sphere indexed with hierarchical linear indexes. The H3 Core Library provides functions for converting between latitude/longitude coordinates and H3 geospatial indexes. Specifically, the major library entry point functions (defined in h3api.h) provide the functionality:

· Given a latitude/longitude point, find the index of the containing H3 cell at a particular resolution.

· Given an H3 index, find the latitude/longitude cell centre.

· Given an H3 index, determine the cell boundary in latitude/longitude coordinates and many more.

What are the use cases of H3?

It helps in the analysis of data with the help of longitude and latitude, or basically, we can coordinate points such as locations of cars in a city, maybe done by bucketing locations and with various options for partitioning the area into buckets or clusters like partitioning the area using surface grid or drawing regions based upon human knowledge.

Some of the draws of drawing regions based upon human knowledge:

· It is very difficult to calculate the centroid of data every time for the different data points.

· Edges of the partition are not exactly known.

· Manually defined partitions may require updating if our understanding of the system changes.

· Manually defining a large number of partitions may be very costly and time-consuming.

Advantages of using H3

H3 provides a regular grid to the data points with smooth gradients and the ability to measure the difference between cells of data points. The grid system helps in defining the correct cell shape. There are only three polygons that tile regularly: the triangle, the square, and the hexagon. Of these, triangles and squares have neighbours with different distances. Triangles have three different distances, and squares have two different distances. For hexagons, all neighbours are equidistant. This property allows for a simpler analysis of movement.

Different Grid system provided by H3

“The H3 geospatial indexing system is a multi-precision hexagonal tiling of the sphere indexed with hierarchical linear indexes.”

Some of the generally used H3 Python API’S

. h3.edge_length
. h3.hex_area
· h3.geo_to_h3
· h3.h3_to_geo_boundary
· h3.h3_get_resolution
· h3.k_ring_distances
· h3.h3_distance
· h3.polyfill
· h3.compact
· h3.hex_ring

Visualizing H3 spatial index for bucketing locations

Step1: Prepare the geojson data of locations
Step2: Read a GeoJSON file into a dataframe and drop rows with null geometry.
Step3: Extract the latitude and longitude as separate columns from the geometry coordinates.
Step4: Visualize the data on a map of raw data.

Preparing the Data

Code

def read_geojson_points(filepath):
with open(filepath) as f:
stops_geodata = json.load(f)
df = pd.DataFrame(json_normalize(stops_geodata[‘features’]))
n_rows_orig = df.shape[0]
df.dropna(subset=[“geometry.coordinates”], inplace = True, axis = 0)
n_rows_clean = df.shape[0]
print(“Cleaning null geometries, eliminated “, n_rows_orig — n_rows_clean,
“ rows out of the original “,n_rows_orig, “ rows”)
df[‘longitude’] = df[“geometry.coordinates”].apply(lambda x: x[0])
df[‘latitude’] = df[“geometry.coordinates”].apply(lambda x: x[1])
return df

Visualization on map

Quick visualization on a map of raw data
Code

m = Map(location= [43.600378, 1.445478], zoom_start=11, tiles=”cartodbpositron”,attr= ‘© <a href=”http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href=”http://cartodb.com/attributions#basemaps">CartoDB</a>’)
mc = MarkerCluster()
for i,row in df_raw.iterrows():
mk = Marker(location=[row[“latitude”],row[“longitude”]])
mk.add_to(mc)
mc.add_to(m)
m.save(‘source/2_markers_busstops.html’)
m

Map View

For more information visit

https://github.com/uber/h3/blob/master/docs/overview/mainpage.md

--

--

Krati Agarwal
Analytics Vidhya

Data Science and Machine Learning enthusiastic. I believe in working on data to make data work for us.