Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path expansion for writing vector files #1055

Closed
brownag opened this issue Mar 8, 2023 · 1 comment
Closed

path expansion for writing vector files #1055

brownag opened this issue Mar 8, 2023 · 1 comment

Comments

@brownag
Copy link
Contributor

brownag commented Mar 8, 2023

I noticed that expansion of a filename containing ~ does not work for writeVector()

When writing vector, creation fails with e.g. Warning: Failed to create file ~/test\lux.shp: No such file or directory (GDAL error 1). Path expansion appears to work for read of vector and read/write of raster.

library(terra)
#> terra 1.7.19

x <- vect(system.file("ex", "lux.shp", package = "terra"))
y <- rast(system.file("ex", "elev.tif", package = "terra"))

dir.create("~/test", showWarnings = FALSE)

# doesnt work
writeVector(x, "~/test/lux.shp")
#> Warning: Failed to create file ~/test\lux.shp: No such file or directory (GDAL
#> error 1)
#> Error: [writeVector] Layer creation failed

# works
writeVector(x, path.expand("~/test/lux.shp"))

# works
vect("~/test/lux.shp")
#>  class       : SpatVector 
#>  geometry    : polygons 
#>  dimensions  : 12, 6  (geometries, attributes)
#>  extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#>  source      : lux.shp
#>  coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#>  names       :  ID_1   NAME_1  ID_2   NAME_2  AREA   POP
#>  type        : <num>    <chr> <num>    <chr> <num> <int>
#>  values      :     1 Diekirch     1 Clervaux   312 18081
#>                    1 Diekirch     2 Diekirch   218 32543
#>                    1 Diekirch     3  Redange   259 18664

# works
writeRaster(y, "~/test/elev.tif")

# works 
rast("~/test/elev.tif")
#> class       : SpatRaster 
#> dimensions  : 90, 95, 1  (nrow, ncol, nlyr)
#> resolution  : 0.008333333, 0.008333333  (x, y)
#> extent      : 5.741667, 6.533333, 49.44167, 50.19167  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source      : elev.tif 
#> name        : elevation 
#> min value   :       141 
#> max value   :       547

unlink("~/test", recursive = TRUE)
@rhijmans
Copy link
Member

rhijmans commented Mar 9, 2023

Thank you. This seems to work now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants