I have a map data whose format is rds. Now I want to use this data in another software which asks for shp format. How to convert rds format data into shp format in R?
1 Answer
If it is spatial object saved as a R-specific binary file of "Serialization Interface for Single Objects" type (see ?readRDS) probably created at some point by saveRDS(), read your file with
library(rgdal)
library(sp)
x <- readRDS("path/to/the/rds_file.rds")
and then write it with:
rgdal::writeOGR(x, "path/to/destination", "filename", driver = "ESRI Shapefile")
Be sure not to put ".shp" at the end of your output filename.
Also be sure not to put a / at the end of the destination folder. Otherwise you might face the error
Creation of output file failed
When the error
Error: inherits(obj, "Spatial") is not TRUE
you might have forgotten the x as the first argument in the writeOGR function.
6 Comments
Vesper
I successfully read the rds file but when I run the output code, there is the Error: inherits(obj, "Spatial") is not TRUE. Why?
loki
you probably forgot to put the object (in my example
x) into the writeOGR function. Also see my edit for the needed libraries.Vesper
I have installed two packages and put the x in my codes: '>library(rgdal) > library(sp) > a=readRDS('D:/data/chinamap/chinamap.rds') > rgdal::writeOGR(a, "D:/data/chinamap/", "china", driver = "ESRI Shapefile") ', but the error I said still appears. I am very confused.
loki
if this doesn't work either, could you please add the output of
class(a) and typeof(a)?Vesper
It doesn't work. The output of 'class(a)' is "data.frame", and the output of 'typeof(a)' is "list". Should I covert 'a' to another format?
|