- Notifications
You must be signed in to change notification settings - Fork6
Open
Description
This is likely an issue for terra, but I post it here first because I know you have experience with both terra and geotargets.
In below reprex, I slightly adapted one of your unit tests to show the issue. When one of the branches happens to result in empty records (0 rows), the internally usedterra::writeVector() will error.
I am aware of theerror = "null" option, but I don't think it is a suitable workaround for this issue.
At the bottom of the reprex, I show thatsf::st_write can deal with this edge case. So likely something needs to be done on theterra side@brownag ?
Or can you advice me on other ways to workaround this issue?
targets::tar_test("tar_terra_vect() works with dynamic branching", {targets::tar_script({list(geotargets::tar_terra_vect(my_vect,terra::vect(system.file("ex","lux.shp",package="terra")) ),targets::tar_target(to_sub, c("Clervaux","Redange","empty_subset") ),geotargets::tar_terra_vect(my_vect_subs,my_vect[my_vect$NAME_2==to_sub],pattern=to_sub ) ) })targets::tar_make(reporter="silent")testthat::expect_length(targets::tar_read(my_vect_subs),3)})#> Warning message:#> package 'targets' was built under R version 4.5.2#> ── Error: tar_terra_vect() works with dynamic branching ────────────────────────#> <tar_condition_run/tar_condition_targets/rlang_error/rlang_error/error/condition>#> Error: Error in tar_make():#> Error storing output: [writeVector] SpatVector has no records to write#> See https://books.ropensci.org/targets/debugging.html#> Backtrace:#> ▆#> 1. └─targets::tar_make(reporter = "silent")#> 2. └─targets:::callr_outer(...)#> 3. ├─targets:::if_any(...)#> 4. └─targets:::callr_error(traced_condition = out, fun = fun)#> 5. └─targets::tar_throw_run(message, class = class(traced_condition$condition))#> 6. └─targets::tar_error(...)#> 7. └─rlang::abort(message = message, class = class, call = tar_envir_base)#> Error:#> ! Test failed# issue is that writeVector errors with no recordsmy_vect<-terra::vect(system.file("ex","lux.shp",package="terra"))out<-my_vect[my_vect$NAME_2=="empty_subset"]out#> class : SpatVector#> geometry : none#> dimensions : 0, 6 (geometries, attributes)#> extent : 0, 0, 0, 0 (xmin, xmax, ymin, ymax)#> 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> <num>#> values : NA NA NA NA NA NAterra::writeVector(out, tempfile(fileext=".gpkg"))#> Error: [writeVector] SpatVector has no records to write# noteworthy: sf::st_write does allow to write empty recordsout_sf<-sf::st_as_sf(out)sffile<- tempfile(fileext=".gpkg")sf::st_write(out_sf,sffile)#> Writing layer `file4c805138716' to data source#> `C:\Users\HANS_V~1\AppData\Local\Temp\RtmpQDGAyX\file4c805138716.gpkg' using driver `GPKG'#> Writing 0 features with 0 fields and geometry type Unknown (any).out_sf_check<-sf::st_read(sffile)#> Reading layer `file4c805138716' from data source#> `C:\Users\hans_vancalster\AppData\Local\Temp\RtmpQDGAyX\file4c805138716.gpkg'#> using driver `GPKG'#> Simple feature collection with 0 features and 0 fields#> Bounding box: xmin: NA ymin: NA xmax: NA ymax: NA#> Geodetic CRS: WGS 84out_sf#> Simple feature collection with 0 features and 0 fields#> Bounding box: xmin: NA ymin: NA xmax: NA ymax: NA#> Geodetic CRS: WGS 84#> [1] geometry#> <0 rows> (or 0-length row.names)out_sf_check#> Simple feature collection with 0 features and 0 fields#> Bounding box: xmin: NA ymin: NA xmax: NA ymax: NA#> Geodetic CRS: WGS 84#> [1] geom#> <0 rows> (or 0-length row.names)
Created on 2025-11-04 withreprex v2.1.1
Session info
sessioninfo::session_info()#> Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", :#> running command '"quarto"#> TMPDIR=C:/Users/hans_vancalster/AppData/Local/Temp/RtmpQDGAyX/file4c803073dfb#> -V' had status 1#> ─ Session info ───────────────────────────────────────────────────────────────#> setting value#> version R version 4.5.1 (2025-06-13 ucrt)#> os Windows 11 x64 (build 26100)#> system x86_64, mingw32#> ui RTerm#> language (EN)#> collate Dutch_Belgium.utf8#> ctype Dutch_Belgium.utf8#> tz Europe/Brussels#> date 2025-11-04#> pandoc 3.4 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)#> quarto NA @ C:\\PROGRA~1\\RStudio\\RESOUR~1\\app\\bin\\quarto\\bin\\quarto.exe#>#> ─ Packages ───────────────────────────────────────────────────────────────────#> package * version date (UTC) lib source#> backports 1.5.0 2024-05-23 [1] CRAN (R 4.5.0)#> base64url 1.4 2018-05-14 [1] CRAN (R 4.5.0)#> brio 1.1.5 2024-04-24 [1] CRAN (R 4.5.0)#> callr 3.7.6 2024-03-25 [1] CRAN (R 4.5.0)#> class 7.3-23 2025-01-01 [2] CRAN (R 4.5.1)#> classInt 0.4-11 2025-01-08 [1] CRAN (R 4.5.0)#> cli 3.6.5 2025-04-23 [1] CRAN (R 4.5.0)#> codetools 0.2-20 2024-03-31 [2] CRAN (R 4.5.1)#> data.table 1.17.8 2025-07-10 [1] CRAN (R 4.5.1)#> DBI 1.2.3 2024-06-02 [1] CRAN (R 4.5.0)#> desc 1.4.3 2023-12-10 [1] CRAN (R 4.5.0)#> digest 0.6.37 2024-08-19 [1] CRAN (R 4.5.0)#> e1071 1.7-16 2024-09-16 [1] CRAN (R 4.5.0)#> evaluate 1.0.4 2025-06-18 [1] CRAN (R 4.5.1)#> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.5.0)#> fs 1.6.6 2025-04-12 [1] CRAN (R 4.5.0)#> glue 1.8.0 2024-09-30 [1] CRAN (R 4.5.0)#> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)#> igraph 2.1.4 2025-01-23 [1] CRAN (R 4.5.0)#> KernSmooth 2.23-26 2025-01-01 [2] CRAN (R 4.5.1)#> knitr 1.50 2025-03-16 [1] CRAN (R 4.5.0)#> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.5.0)#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.5.0)#> pillar 1.11.0 2025-07-04 [1] CRAN (R 4.5.1)#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.5.0)#> pkgload 1.4.0 2024-06-28 [1] CRAN (R 4.5.0)#> prettyunits 1.2.0 2023-09-24 [1] CRAN (R 4.5.0)#> processx 3.8.6 2025-02-21 [1] CRAN (R 4.5.0)#> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.5.0)#> ps 1.9.1 2025-04-12 [1] CRAN (R 4.5.0)#> R6 2.6.1 2025-02-15 [1] CRAN (R 4.5.0)#> Rcpp 1.1.0 2025-07-02 [1] CRAN (R 4.5.1)#> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.5.0)#> rlang 1.1.6 2025-04-11 [1] CRAN (R 4.5.0)#> rmarkdown 2.29 2024-11-04 [1] CRAN (R 4.5.0)#> rprojroot 2.1.0 2025-07-12 [1] CRAN (R 4.5.1)#> rstudioapi 0.17.1 2024-10-22 [1] CRAN (R 4.5.0)#> secretbase 1.0.5 2025-03-04 [1] CRAN (R 4.5.0)#> sessioninfo 1.2.3 2025-02-05 [1] CRAN (R 4.5.0)#> sf 1.0-21 2025-05-15 [1] CRAN (R 4.5.0)#> targets 1.11.4 2025-09-13 [1] CRAN (R 4.5.2)#> terra 1.8-70 2025-09-27 [1] CRAN (R 4.5.2)#> testthat 3.2.3 2025-01-13 [1] CRAN (R 4.5.0)#> tibble 3.3.0 2025-06-08 [1] CRAN (R 4.5.1)#> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.5.0)#> units 0.8-7 2025-03-11 [1] CRAN (R 4.5.0)#> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.5.0)#> waldo 0.6.2 2025-07-11 [1] CRAN (R 4.5.1)#> withr 3.0.2 2024-10-28 [1] CRAN (R 4.5.0)#> xfun 0.53 2025-08-19 [1] CRAN (R 4.5.1)#> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.5.0)#>#> [1] C:/R/library#> [2] C:/R/R-4.5.1/library#>#> ──────────────────────────────────────────────────────────────────────────────
Metadata
Metadata
Assignees
Labels
No labels