jtenzi
December 13, 2021, 5:26am
1
Hello!
I'm a complete beginning with R - :S
I received a .rds file which I know contains 4 polygons - I would like to convert them to ESRI shapefiles. I searched for how to do this and tried the following:
library(rgdal)
library(sp)
x <- readRDS("path/to/the/rds_file.rds")
rgdal::writeOGR(x, "path/to/destination", "filename", driver = "ESRI Shapefile")
But I always get the following error:
Error: inherits(obj, "Spatial") is not TRUE
The outputs for classof(x) and type(x) are both lists.
Can anyone help me?
Is it a multipolgon file?
You could just use st_write()
in the the sf package if it is.
e.g. st_write(object, "filename.shp")
Otherwise, you could write them separately, or merge them if you want them in one object, then write.
jtenzi
December 13, 2021, 5:33am
3
Thank you for responding so quickly!
I tried this but I get the following error message:
no applicable method for 'st_write' applied to an object of class "list"
So is your object a list of four polygons?
1 Like
jtenzi
December 13, 2021, 5:40am
5
yes it is - It's a list with four elements, all also lists, containing a polygon each... (from what I can tell)
I suppose you could bind rows (using dplyr) or rbind (in base). Then export. It would be one polygon.
x <- readRDS("path/to/the/rds_file.rds") %>%
bind_rows()
1 Like
jtenzi
December 13, 2021, 5:57am
7
Thanks!!
This command seems to have worked. I followed with st_write(object, "filename.shp")
which also seems to have worked!
But now I don't know where my shapefile got saved - it's not in my working directory... any idea? Sorry for these really basic questions!!!
Try getwd()
and see the output of that.
Best to use the projects workflow though. Have a read.
https://r4ds.had.co.nz/workflow-projects.html
1 Like
jtenzi
December 13, 2021, 6:25am
9
Thank you so much for your help! It seems to have worked but the only problem is that there was some data loss, so I tried extracting each element from the list, and did the same thing one by one to create four shapefiles - and so far so good! Couldn't have done it without you!
1 Like
system
Closed
January 3, 2022, 6:26am
10
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.