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

cells(x, y) output does not respect the order of a vector y argument #1487

Closed
florisvdh opened this issue Apr 24, 2024 · 3 comments
Closed

Comments

@florisvdh
Copy link

It seems that the cell numbers from cells(x, y) cannot be matched with the values in a provided y vector, since the cell numbers are sorted.

Shouldn't the cell numbers be sorted in correspondence with the provided vector (in case y is indeed a vector), so that both vectors can be aligned?

I have a usecase with a raster of unique values, similar to below reprex.

library(terra)
#> terra 1.7.75
r <- rast(ncols=10, nrows=10)
values(r) <- 1:ncell(r)
selected_vals <- c(85, 45, 99)
cells(r, selected_vals)
#> $lyr.1
#> [1] 45 85 99
cells(r, rev(selected_vals))
#> $lyr.1
#> [1] 45 85 99
cells(r, sort(selected_vals))
#> $lyr.1
#> [1] 45 85 99

Created on 2024-04-24 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.3 (2024-02-29)
#>  os       Linux Mint 21.3
#>  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     2024-04-24
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [3] RSPM (R 4.3.0)
#>  codetools     0.2-20  2024-03-31 [3] RSPM (R 4.3.0)
#>  digest        0.6.35  2024-03-11 [3] RSPM (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [3] RSPM (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [3] RSPM (R 4.2.0)
#>  fs            1.6.3   2023-07-20 [3] RSPM (R 4.2.0)
#>  glue          1.7.0   2024-01-09 [3] RSPM (R 4.3.0)
#>  htmltools     0.5.8.1 2024-04-04 [3] RSPM (R 4.3.0)
#>  knitr         1.46    2024-04-06 [3] RSPM (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [3] RSPM (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [3] RSPM (R 4.2.0)
#>  purrr         1.0.2   2023-08-10 [3] RSPM (R 4.2.0)
#>  R.cache       0.16.0  2022-07-21 [3] RSPM (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [3] RSPM (R 4.2.0)
#>  R.oo          1.26.0  2024-01-24 [3] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [3] RSPM (R 4.3.0)
#>  Rcpp          1.0.12  2024-01-09 [3] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [3] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [3] RSPM (R 4.3.0)
#>  rmarkdown     2.26    2024-03-05 [3] RSPM (R 4.3.0)
#>  rstudioapi    0.16.0  2024-03-24 [3] RSPM (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [3] RSPM (R 4.2.0)
#>  styler        1.10.3  2024-04-07 [3] RSPM (R 4.3.0)
#>  terra       * 1.7-75  2024-04-24 [1] Github (rspatial/terra@d141089)
#>  vctrs         0.6.5   2023-12-01 [3] RSPM (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [3] RSPM (R 4.3.2)
#>  xfun          0.43    2024-03-25 [3] RSPM (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [3] RSPM (R 4.3.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
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@rhijmans
Copy link
Member

That is tricky because it would only work well for a raster with unique values and all the selected values present. In other cases you can get things like

library(terra)
r <- rast(ncols=10, nrows=10)
values(r) <- round(1:ncell(r)/4)
cells(r, c(1,2))
#$lyr.1
#[1]  3  4  5  6  7  8  9 10

cells(r, c(-1,0))
#$lyr.1
#[1] 1 2

I think that what you are after would require an additional argument to indicate you want a different type of output. Something like this?

cells(r, c(1,2), pairs=TRUE)
#     value cell
#[1,]     1    3
#[2,]     1    4
#[3,]     1    5
#[4,]     1    6
#[5,]     2    7
#[6,]     2    8
#[7,]     2    9
#[8,]     2   10

rhijmans added a commit that referenced this issue Apr 26, 2024
@rhijmans
Copy link
Member

rhijmans commented Apr 26, 2024

Here is an implementation. Does that meet your needs effectively?

library(terra)
#terra 1.7.75
r <- rast(ncols=10, nrows=10)
values(r) <- round(1:ncell(r)/4)
cells(r, c(1,2))
#$lyr.1
#[1]  3  4  5  6  7  8  9 10

cells(r, c(1,2), pairs=T)
#$lyr.1
#     cell value
#[1,]    3     1
#[2,]    4     1
#[3,]    5     1
#[4,]    6     2
#[5,]    7     2
#[6,]    8     2
#[7,]    9     2
#[8,]   10     2

@florisvdh
Copy link
Author

That is tricky because it would only work well for a raster with unique values and all the selected values present.

Of course, you are right!

Your solution fully covers the needs, including the usecase I had. Thank you for this quick implementation!

library(terra)
#> terra 1.7.75
r <- rast(ncols=10, nrows=10)
values(r) <- 1:ncell(r)
selected_vals <- c(85, 45, 99)
cells(r, selected_vals, pairs = TRUE)
#> $lyr.1
#>      cell value
#> [1,]   45    45
#> [2,]   85    85
#> [3,]   99    99

Created on 2024-04-26 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.3 (2024-02-29)
#>  os       Linux Mint 21.3
#>  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     2024-04-26
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.2   2023-12-11 [3] RSPM (R 4.3.0)
#>  codetools     0.2-20  2024-03-31 [3] RSPM (R 4.3.0)
#>  digest        0.6.35  2024-03-11 [3] RSPM (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [3] RSPM (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [3] RSPM (R 4.2.0)
#>  fs            1.6.3   2023-07-20 [3] RSPM (R 4.2.0)
#>  glue          1.7.0   2024-01-09 [3] RSPM (R 4.3.0)
#>  htmltools     0.5.8.1 2024-04-04 [3] RSPM (R 4.3.0)
#>  knitr         1.46    2024-04-06 [3] RSPM (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [3] RSPM (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [3] RSPM (R 4.2.0)
#>  purrr         1.0.2   2023-08-10 [3] RSPM (R 4.2.0)
#>  R.cache       0.16.0  2022-07-21 [3] RSPM (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [3] RSPM (R 4.2.0)
#>  R.oo          1.26.0  2024-01-24 [3] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [3] RSPM (R 4.3.0)
#>  Rcpp          1.0.12  2024-01-09 [3] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [3] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [3] RSPM (R 4.3.0)
#>  rmarkdown     2.26    2024-03-05 [3] RSPM (R 4.3.0)
#>  rstudioapi    0.16.0  2024-03-24 [3] RSPM (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [3] RSPM (R 4.2.0)
#>  styler        1.10.3  2024-04-07 [3] RSPM (R 4.3.0)
#>  terra       * 1.7-75  2024-04-26 [1] Github (rspatial/terra@7ba182c)
#>  vctrs         0.6.5   2023-12-01 [3] RSPM (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [3] RSPM (R 4.3.2)
#>  xfun          0.43    2024-03-25 [3] RSPM (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [3] RSPM (R 4.3.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
#> 
#> ──────────────────────────────────────────────────────────────────────────────

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