Skip to content

Instantly share code, notes, and snippets.

@kadyb
Last active May 16, 2025 01:54
Show Gist options
  • Save kadyb/69b55caca364ea7f6a8167079d78772f to your computer and use it in GitHub Desktop.
Save kadyb/69b55caca364ea7f6a8167079d78772f to your computer and use it in GitHub Desktop.
Directly load and save zipped shapefiles in {sf}
library("sf")
file = system.file("shape/nc.shp", package = "sf")
shp = read_sf(file)
## write zipped shapefile
write_sf(shp, "myshapefile.shp.zip", driver = "ESRI Shapefile")
## read zipped shapefile
read_sf("myshapefile.shp.zip")
## read zipped shapefile using `vsizip`
## see: https://gdal.org/user/virtual_file_systems.html
f = paste0("/vsizip/", "myshapefile.shp.zip")
read_sf(f)
@defuneste
Copy link

It also works great with vsicurl!

state_zip <- "/vsizip//vsicurl/https://www2.census.gov/geo/tiger/TIGER2024/STATE/tl_2024_us_state.zip"
 
us_states <- sf::read_sf(state_zip)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment