# 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):

`library(sf)library(tidyverse)library(h3jsr)library(ggrepel)`
`#Create a dataframenumber_of_points <- 1000set.seed(04)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 functionPointToCellFunction <- 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 conversionsample_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'])             cell1 863d93cefffffff2 8660922efffffff3 863d93457ffffff4 863d93407ffffff5 863d93897ffffff6 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)`
`CellVizFunction(sample_data, 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 cellsunlist(cell_to_point(data_with_cell\$cell))-> lat_long_listlat_long_list[seq(1, length(lat_long_list), 2)]-> long_listlat_long_list[seq(2, length(lat_long_list), 2)]-> lat_list#Add the centroidsdata_with_cell %>%  cbind(cen_lat = lat_list,        cen_long = long_list)-> data_with_celldata_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 framedata_with_cell %>%  distinct(cell) %>%  pull() -> list_cell#Get all the polygons for theh3_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')+  theme_dark()`