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

Support needed in st_as_sf() for '<dsn>|layername=<name>' output as obtained from qgis_process #84

Closed
florisvdh opened this issue Jan 31, 2022 · 3 comments · Fixed by #80

Comments

@florisvdh
Copy link
Member

If the output file and layer destination are prespecified in 'the QGIS way', i.e. like ogr:dbname=world.gpkg table=norway or ogr:dbname=world.gpkg table=norway (geom) these are recognized well by the qgis_process command. The latter adapts to the way these parameters are seen in the processing tool dialog (from which they can be copied as JSON and fed to qgis_process and to qgis_run_algorithm() in R).

In its output, qgis_process transforms this output specification into a <dsn>|layername=<name> syntaxis.

However when passing the same QGIS syntaxis either as a regular qgis_run_algorithm() argument [1], or as JSON (typically copied from the QGIS processing tool dialog) [2], st_as_sf() fails to read the <dsn>|layername=<name> syntaxis of the output element(s).

A reprex for [2] is given in #74 (comment).

A reprex for [1] is below:

library(qgisprocess)
#> Using 'qgis_process' at '/usr/bin/qgis_process'.
#> QGIS version: 3.22.3-Białowieża
#> Configuration loaded from '~/.cache/R-qgisprocess/cache-0.0.0.9000.rds'
#> Run `qgis_configure()` for details.
#> Using JSON for output serialization
library(sf)
#> Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE

curl::curl_download(
  "https://github.com/qgis/QGIS/raw/master/resources/data/world_map.gpkg",
  "world_map.gpkg"
)

qgis_run_algorithm(
    "native:extractbyattribute",
    FIELD = "NAME",
    INPUT = "world_map.gpkg|layername=countries",
    OPERATOR = 0,
    VALUE = "Norway",
    OUTPUT = "ogr:dbname=world.gpkg table=norway",
    FAIL_OUTPUT = "ogr:dbname=world.gpkg table=except_norway"
  ) %>% 
  st_as_sf
#> Running /usr/bin/qgis_process --json run 'native:extractbyattribute' \
#>   '--INPUT=world_map.gpkg|layername=countries' '--FIELD=NAME' '--OPERATOR=0' \
#>   '--VALUE=Norway' '--OUTPUT=ogr:dbname=world.gpkg table=norway' \
#>   '--FAIL_OUTPUT=ogr:dbname=world.gpkg table=except_norway'
#> qt5ct: using qt5ct plugin
#> Error: Cannot open "world.gpkg|layername=except_norway"; The file doesn't seem to exist.

Created on 2022-01-31 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       Linux Mint 20
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language nl_BE:nl
#>  collate  nl_BE.UTF-8
#>  ctype    nl_BE.UTF-8
#>  tz       Europe/Brussels
#>  date     2022-01-31
#>  pandoc   2.14.0.3 @ /usr/lib/rstudio/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.0)
#>  class         7.3-20     2022-01-13 [4] CRAN (R 4.1.2)
#>  classInt      0.4-3      2020-04-07 [1] CRAN (R 4.1.0)
#>  cli           3.1.0      2021-10-27 [1] CRAN (R 4.1.2)
#>  crayon        1.4.2      2021-10-29 [1] CRAN (R 4.1.2)
#>  curl          4.3.2      2021-06-23 [1] CRAN (R 4.1.1)
#>  DBI           1.1.2      2021-12-20 [1] CRAN (R 4.1.2)
#>  digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.2)
#>  dplyr         1.0.7      2021-06-18 [1] CRAN (R 4.1.1)
#>  e1071         1.7-9      2021-09-16 [1] CRAN (R 4.1.1)
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         1.0.2      2022-01-14 [1] CRAN (R 4.1.2)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.2      2021-12-08 [1] CRAN (R 4.1.2)
#>  generics      0.1.1      2021-10-25 [1] CRAN (R 4.1.2)
#>  glue          1.6.0      2021-12-17 [1] CRAN (R 4.1.2)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.1)
#>  jsonlite      1.7.3      2022-01-17 [1] CRAN (R 4.1.2)
#>  KernSmooth    2.23-20    2021-05-03 [4] CRAN (R 4.1.0)
#>  knitr         1.37       2021-12-16 [1] CRAN (R 4.1.2)
#>  lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.0)
#>  pillar        1.6.4      2021-10-18 [1] CRAN (R 4.1.2)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
#>  processx      3.5.2      2021-04-30 [1] CRAN (R 4.1.0)
#>  proxy         0.4-26     2021-06-07 [1] CRAN (R 4.1.0)
#>  ps            1.6.0      2021-02-28 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
#>  qgisprocess * 0.0.0.9000 2022-01-31 [1] Github (paleolimbot/qgisprocess@3768999)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.1)
#>  rappdirs      0.3.3      2021-01-31 [1] CRAN (R 4.1.0)
#>  Rcpp          1.0.8      2022-01-13 [1] CRAN (R 4.1.2)
#>  reprex        2.0.1      2021-08-05 [1] CRAN (R 4.1.1)
#>  rlang         0.4.12     2021-10-18 [1] CRAN (R 4.1.2)
#>  rmarkdown     2.11       2021-09-14 [1] CRAN (R 4.1.1)
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.2)
#>  sf          * 1.0-5      2021-12-17 [1] CRAN (R 4.1.2)
#>  stringi       1.7.6      2021-11-29 [1] CRAN (R 4.1.2)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)
#>  tibble        3.1.6      2021-11-07 [1] CRAN (R 4.1.2)
#>  tidyselect    1.1.1      2021-04-30 [1] CRAN (R 4.1.0)
#>  units         0.7-2      2021-06-08 [1] CRAN (R 4.1.1)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.1)
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)
#>  withr         2.4.3      2021-11-30 [1] CRAN (R 4.1.2)
#>  xfun          0.29       2021-12-14 [1] CRAN (R 4.1.2)
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.1.0)
#> 
#>  [1] /home/floris/lib/R/library
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

A suggested remedy will be added in PR #80.

Note: passing "world.gpkg|layername=except_norway" to qgis_process or (in R) qgis_run_algorithm() for the output is not a good option. It writes a GPKG named as world.gpkg|layername=except_norway.gpkg (which isn't a problem to st_as_sf(), then, but it's clearly not what we need).

@paleolimbot
Copy link
Collaborator

Since this is unique to qgis output as far as I know, we'll probably have to make a dedicated function for this in qgisprocess. I think strsplit(input, "|layername=", fixed = TRUE)[[1]] should do the trick?

@florisvdh
Copy link
Member Author

we'll probably have to make a dedicated function for this in qgisprocess

Does the approach PR #80 already fix this for you, where it is done for st_as_sf()? :

https://github.com/paleolimbot/qgisprocess/blob/dca0591553c0829c435b0b56b293a5b8116a1e87/R/compat-sf.R#L27-L29

So, does the <dsn>|layername=<name> syntaxis also occur in other than vector outputs?

@paleolimbot
Copy link
Collaborator

Oh yeah! I thought I saw a test for that but didn't put together that you filed a separate issue.

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