2 min read

OSM 2 - interactive maps

Introducing the leaflet package

Plain images of map data is so web 1.0. R htmlwidgets mean I don’t have to acttuully learn Javascript to mess around with Javascript libraries in this site. One of the flagship examples leaflet works with OSM out of the box.

Last time I wrote a file containing name and distance of my coffee shops. I now need to run that script again so I can get longitude and latitude. Let’s load it into a data frame and plot it.

coffee <- read_csv(here("static","data", "OSM", "QH coffee.csv")) %>%
  mutate(`walking distance`  = round(`walking distance`) )

coffee_icon <- makeIcon("../img/coffee-cup.svg",   iconWidth = 38, iconHeight = 38)

m <- leaflet(data=coffee) %>%
    addMarkers(~longitude, ~latitude, popup = ~as.character(`walking distance`), label = ~as.character(name), icon=coffee_icon) %>%
  addTiles()
m

Given that the map includes the actual location of the coffee shop, not just the nearest node on the graph, I can see where errors have happened. In particular, There also seem to be a few that are totally in walking distance, but aren’t marked on my map.

However, my processing of the OSM data for points of interest is terrible, and I’ve learned to start reading fork data on GitHub. There’s a pending change to osmnx where you’ll be able to select points of interest in a much more elegant way than I’ve done.

I’m looking at modifying how it loads OSM format XML files, because I want to make large road maps. Currently it loads the entire dump to memory, and only then starts filtering out what it doesn’t need. I want to read one element at a time and decide if it is worth keeping or not, like I did above.

But this is much more elegant than my flat images. I can actually include both the name and the distance in the graph. (Yes, a map can be a graph.) Also, the leaflet package is seriously painless.

So there’s probably not going to be many blog posts on this in the near future as I update some of the backend.

If I were running Shiny, I could let you input a location and a walking distance and it would update the map. I don’t have Shiny. I’ll take requests for additional maps. Give me a location, and some combination of points of interest from the OSM Wiki.