How do I convert an sf object back to a SpatialPolygonsDataFrame?
I am running R 3.4.0 and sf version 0.4.1. The sf documentation mentions an as_Spatial function, but this isn't available in my install (for some reason).
How do I convert an sf object back to a SpatialPolygonsDataFrame?
I am running R 3.4.0 and sf version 0.4.1. The sf documentation mentions an as_Spatial function, but this isn't available in my install (for some reason).
I used
as(nc, 'Spatial')
as part 2 of the vignette by Edzer Pebesma indicated (Scroll to the bottom of the page).
tmp <-as(nc, 'Spatial'), then tmpid <- sapply(slot(tmp, "polygons"), function(x) slot(x, "ID")) then row.names(tmp) <- tmpid
– Simon
May 15 '17 at 11:50
I had to use sf:::as_Spatial() as workaround.
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# sf -> sp
nc_sp <- as_Spatial(nc$geom) # Error: could not find function "as_Spatial"
nc_sp <- sf:::as_Spatial(nc$geom) # This works
library(sp)
plot(nc_sp)
as(sf, "Spatial") by @mdsumner , I confirm it produces SpatialPolygonsDataFrame, which is better for your requirement. It seems as_Spatial() takes sfc (ie geometry) only.
– Kazuhito
May 04 '17 at 08:08