I am trying to add grid cell labels to a leaflet map in R using shapefiles provided by TIGER/Line Census Data. The desired result is a leaflet map displaying the raster ID layer labelled from
1:ncell(r) that is clipped by the urban boundary of Albuquerque NM. I'm almost there, but I have been stuck trying to get the grid cell labels to show up when using leaflet.
The grid cells appear to be clickable, i.e. the cursor changes when hovering over the grid. However, no popup is displayed when clicking a cell.
The desired output does not have to be a popup. A static ID number centered within each grid cell would also be really helpful.
I am loosely following this tutorial.
library(dplyr) library(rgeos) library(rgdal) library(sp) library(leaflet) library(raster) library(stringr) # reading in shapefiles for the entire US urban_areas <- readOGR(dsn = "./tl_2017_us_uac10.shp") # pulling out only Albuquerque, NM shapefiles albuq <- subset(urban_areas, str_detect(NAME10, "Albuquerque, NM")) # determine the bounding box of the city boundary bbox(albuq) e <- extent(bbox(albuq)) # convert to a raster object r <- raster(e) # divide this raster into a 10 by 10 grid dim(r) <- c(10, 10) ## spatially explicit cell sizes are ideal, but this works for now... projection(r) <- crs(proj4string(albuq)) # add label ID to our grid cells r <- setValues(r, 1:ncell(r)) # reconvert back to shapefile with the goal of creating a popup of the cell ID for each polygon shape <- rasterToPolygons(r, dissolve = TRUE) # clip the grid cells that contain the Albuquerque polygon p <- shape[albuq, ] # trim the grid perimeter to match the Albuquerque polygon map <- gIntersection(p, albuq, byid = TRUE, drop_lower_td = TRUE) # create a popup using the grid cell label layer pop <- p@data$layer # creating our map leaflet(map) %>% addTiles %>% addPolygons(stroke = TRUE, opacity = 1, fillOpacity = 0.05, popup = pop smoothFactor = 0.5, color="black", fillColor = NULL, weight = 0.5)