Monday, 4 December 2017

Splitting vector layer in R


Is there a way to replicate the Split Vector Layer function from QGIS in R?


I have a Shapefile containing around 20 polygons that I'd like to split into 20 separate files.


I guess since it's in QGIS there would be a solution in Python, but I'd like to integrate it into a R workflow if possible.



Answer



Yes, you can just loop through the individual features and save them like so:


library (sf)


dat <- read_sf ("~/data.shp")

for (i in seq_len (nrow (dat)))
{
fname <- paste0 ("~/split_", i, ".shp")
write_sf (dat [i, ], fname)
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...