Uber h3 in R: functions and use cases

This is an extension of the post about h3jsr library which covers the basic operations of working with Uber’s h3 cells in R. If you’re someone whose work involves geospatial data analysis, then this post might interest you. Before that, if you do some or a lot of geospatial data analysis or you need to use some concepts of computational geometry for your work and you’re unaware of Uber’s h3 geospatial library then have a look at this video:

Point to cell for a list of points:

Now, let’s come to some basic operations in R. Suppose we have a data frame with coordinates (lat, long) of many points. And we want to get the cell for a given resolution for that point. This can easily be done by combining R’s ‘mapply’ function with a user created function. Remember R is optimized for vectorization.

A sample code for this operation (the link for the whole GitHub repo is given at the end of the article):

First let’s load the libraries.

#Create a dataframe
number_of_points <- 1000
sample_data <- data.frame(lat = runif(number_of_points, 22, 23),
long = runif(number_of_points, 77, 78),
id = 1:number_of_points)

#Create the function
PointToCellFunction <- function(long, lat, input_resolution){

point <- c(long, lat)
(st_sfc(st_point(point), crs = 4326))-> point_details

return(point_to_cell(point_details, res=input_resolution))

#Now use the function for the cell conversion
sample_data %>%
mutate(cell = mapply(PointToCellFunction, long, lat, 6))-> data_with_cell

Let’s have a look at the output column:

> head(data_with_cell['cell'])
1 863d93cefffffff
2 8660922efffffff
3 863d93457ffffff
4 863d93407ffffff
5 863d93897ffffff
6 86609262fffffff

Similarly, another very important thing to do is to visualize these cells with the points. Let’s do that!

Visualizing the cells:

For this purpose, I have created a function called CellVizFunction (code available in the GitHub repo). The CellVizFunction takes 2 inputs: the data frame with lat & long column, and the resolution at which we would like to visualize the cells. It also has a dependency on the PointToCellFunction that was previously created.

We can visualize these points with cells easily by that:

CellVizFunction(sample_data, 6)
The cells and the points at resolution 6
CellVizFunction(sample_data, 5)
The cells and the points at resolution 5

Cell to centroid:

Another major use case is to find the centroid of each cell. This can be essential for a lot of purposes. Like, if one wants to find the distance of one point from the centroid, or if one wants to find the inter cell distances.

#Get all the centroids of the cells
unlist(cell_to_point(data_with_cell$cell))-> lat_long_list
lat_long_list[seq(1, length(lat_long_list), 2)]-> long_list
lat_long_list[seq(2, length(lat_long_list), 2)]-> lat_list

#Add the centroids
data_with_cell %>%
cbind(cen_lat = lat_list,
cen_long = long_list)-> data_with_cell

data_with_cell %>%
distinct(cen_lat, cen_long) %>%
mutate(polygon_no = 1:n()) -> centroid_details

#Get the unique cells that are present there in the data frame
data_with_cell %>%
distinct(cell) %>%
pull() -> list_cell

#Get all the polygons for the
h3_addresses <- cell_to_polygon(list_cell, simple = FALSE)

Let’s visualize!

ggplot() +
geom_sf(data = h3_addresses,
fill = 'green',
col = 'black')+
geom_point(data = centroid_details, aes(x=cen_long, y=cen_lat), col='black')+
H3 cells with centroids

GitHub link: https://github.com/shibaprasadb/uber_h3_usecases



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shibaprasad Bhattacharya

Data Nerd. R enthusiast. Analytics professional. OR practitioner. @shibaprasad_b on twitter. Get in touch: https://www.linkedin.com/in/shibaprasad-bhattacharya/