This question has been asked before, but I find the answer confusing since there are no reproducible examples that link the question and the answer, and the answer doesn't actually use the writeOGR
function.
Use lapply and writeOGR to write multiple shapefiles within a list
I have a list of spatial objects. I want to export all of the using writeOGR
. How can I apply this function to the list of spatial objects?
The solution I've come up with applies writeOGR
to the outer list element, and I suppose I need to apply it to the inner list element, but I don't know how to do this.
library(GISTools)
library(rgdal)
data(tornados)
torn_list <- list(torn, torn2)
lapply(torn_list, function(x) writeOGR(obj = x, dsn = "tempdir", layer = x, driver = "ESRI Shapefile"))
Error in as.character.default(layer) :
no method for coercing this S4 class to a vector
Any suggestions?
To clarify, I do want to do this to a list of shape files that have different coordinate reference systems. I would like to create a different shapefile for every element of the list. I realize now I would need to have some way of making the "layer =
" argument for each element of this list as well.