OSM Resource

library(tidyverse)
library(sf)

I just discovered Geofabrik’s clean daily shapefiles of OpenStreetMap data for basically every country on EARTH. In this post, I’ll work with data from Bangladesh which you can download by clicking the [.shp.zip] file next to Bangladesh here. As you start playing with this data, you’ll probably also want to review the metadata for the shapefiles, which you can find here. I dropped the downloaded shapefiles in a data folder on my machine. Note that all files for Bangladesh are around 2.7 GB, so if you’re following along on a storage-challenged machine, you might want to go with a smaller country.

Let’s start by inspecting what’s in this folder. We can print out all of the files with extension .shp by running the following code (remember, this assumes you’ve moved all of your data to a /data sub-folder in your working directory):

shp_list <- Sys.glob("./data/*.shp")
shp_list
##  [1] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_buildings_a_free_1.shp"
##  [2] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_landuse_a_free_1.shp"  
##  [3] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_natural_a_free_1.shp"  
##  [4] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_natural_free_1.shp"    
##  [5] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_places_a_free_1.shp"   
##  [6] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_places_free_1.shp"     
##  [7] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_pofw_a_free_1.shp"     
##  [8] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_pofw_free_1.shp"       
##  [9] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_pois_a_free_1.shp"     
## [10] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_pois_free_1.shp"       
## [11] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_railways_free_1.shp"   
## [12] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_roads_free_1.shp"      
## [13] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_traffic_a_free_1.shp"  
## [14] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_traffic_free_1.shp"    
## [15] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_transport_a_free_1.shp"
## [16] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_transport_free_1.shp"  
## [17] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_water_a_free_1.shp"    
## [18] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_waterways_free_1.shp"  
## [19] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/roi.shp"

Lots to dig through. We can also use this new my_shp list to help us open files. Let’s build a function to help us inspect the contents of each shapefile.

# Note that some of the shapefiles might take awhile to load
inspect_shp <- function(dir) {
  
  shp <- st_read(dir, quiet = T)
  print(paste0(dir, " is a ", st_geometry_type(shp)[1], " with ", nrow(shp), " observations."))
  
  fclass_list <- unique(shp$fclass)
  
  shp_plt <- ggplot(shp) +
    geom_sf() +
    theme_minimal() +
    labs(title = dir,
         subtitle = "From Geofabrik.de")

  # dump anything you want to return from the function in this list
  out_list <- list(shp, fclass_list, shp_plt)
  
  return(out_list)
}

Let’s try out our inspect_shp() function:

roads <- inspect_shp(shp_list[12])
## [1] "C:/Users/eburchf/OneDrive - Emory University/WF/Bangladesh/data/gis_osm_roads_free_1.shp is a LINESTRING with 331452 observations."
roads[[2]]
##  [1] residential    trunk          primary        tertiary       service       
##  [6] pedestrian     unclassified   secondary      trunk_link     living_street 
## [11] secondary_link primary_link   footway        path           track         
## [16] unknown        bridleway      steps          track_grade1   track_grade4  
## [21] tertiary_link  cycleway       track_grade3   track_grade2   track_grade5  
## 25 Levels: bridleway cycleway footway living_street path pedestrian ... unknown
roads[[3]]

If you want to learn more about working with OSM in R, you can also check out the osmdata package, which looks really helpful if you want to pull data directly from OSM.

Dr. Emily Burchfield
Dr. Emily Burchfield
Assistant Professor