terra
terra copied to clipboard
`writeVector`: `overwrite = TRUE` doesn't work when a path to a directory is provided
When using writeVector
, you can provide the name of a directory that doesn't exist yet rather than a path to a specific .shp file - it creates the directory and then saves the files associated with the shapefile inside that directory - this is a feature I find very convenient. However, if you try to overwrite a shapefile using the same method (providing a path to a directory rather than to a .shp file), it doesn't work, even when setting overwrite = TRUE
.
library(terra)
#> terra 1.6.47
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
out <- tempfile()
out
#> [1] "/var/folders/w5/21t55k2s2l5g7kj4vvvwhshm0000gn/T//Rtmpvj9mWM/file3746170764a9"
writeVector(v, out) # write out the shapefile - creates a directory at 'out' and saves the files inside this directory
dir(out)
#> [1] "file3746170764a9.cpg" "file3746170764a9.dbf" "file3746170764a9.prj"
#> [4] "file3746170764a9.shp" "file3746170764a9.shx"
writeVector(v, out) # try to write it again, without setting 'overwrite = TRUE'
#> Error: [writeVector] file exists. Use 'overwrite=TRUE' to overwrite it
writeVector(v, out, overwrite = TRUE) # try it again, this time with 'overwrite = TRUE'
#> Warning: Layer 'file3746170764a9' already exists (GDAL error 1)
#> Error: [writeVector] Layer creation failed
out_shp <- dir(out, pattern = "[.]shp$", full.names = TRUE) # get the path to the actual .shp file
writeVector(v, out_shp, overwrite = TRUE) # works
Created on 2022-12-15 with reprex v2.0.2