3

I am trying to merge all road data from the OS Open Map Local file, to create a single line shapefile of all the roads in the UK. I will then use this data to create huff catchments. I would like to do this all in one step as opposed to spRbind 50 + times. I was able to batch load the shapes into my R environment but am now stuck. Any advice?

shps <- dir("C:/R_Code/MSc_Engagement_Model/1.Data/OS_Road_Local/Roads_Only", "*.shp")

for (shp in shps) assign(shp, readOGR(shp))

roads <- spRbind(HP_Road.shp, HT_Road.shp) #??
2

Don't use assign, it's the "wrong solution". :)

You have the shapefiles in a vector, so read all into a list and do call rbind:

shps <- dir("C:/R_Code/MSc_Engagement_Model/1.Data/OS_Road_Local/Roads_Only", "*.shp")
do.call(rbind, lapply(shps, rgdal::readOGR))

This didn't used to work, so there's a lot of clunky workaround code around that fudge-edits the strict internal IDs. But it's easier now.

Note that it still "won't work" if the columns don't match across all shapefiles, it's possible to fix but difficult to get right without experimenting.

(Note that readOGR used also require a layer name, but that seems to have changed - replace 'readOGR' with 'raster::shapefile' if there's any problems, say on older versions - it's the same code, just with a wrapper to make it easy to use).

Also, check out new sf package, which is faster - but also different - see ?read_sf.

  • Thank you! This resolved my issue. I also tried the sf package and that has been helpful with other tasks as well! – Mary Lennon Aug 3 '17 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.