terra icon indicating copy to clipboard operation
terra copied to clipboard

patches() returns unexpected results

Open Nowosad opened this issue 3 years ago • 3 comments

I tried to use patches() recently. It works well on the example code from the help file, but returns only one patch on various of my own data. Do I misunderstand the function or is there a bug there?

library(terra)
#> terra 1.5.13
checker_values = rep(c(rep(c(1, 2), 8), rep(c(2, 1), 8)), 8)

checker_map = rast(ncol = 16, nrow = 16, resolution = 1,
                   xmin = 0, xmax = 16, ymin = 0, ymax = 16,
                   vals = checker_values)

plot(checker_map)

plot(patches(checker_map))

Created on 2022-01-20 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       Fedora Linux 35 (Thirty Five)
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Warsaw
#>  date     2022-01-20
#>  pandoc   2.14.0.3 @ /usr/libexec/rstudio/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  backports     1.4.1   2021-12-13 [2] CRAN (R 4.1.2)
#>  cli           3.1.0   2021-10-27 [2] CRAN (R 4.1.1)
#>  codetools     0.2-18  2020-11-04 [2] CRAN (R 4.1.1)
#>  crayon        1.4.2   2021-10-29 [2] CRAN (R 4.1.1)
#>  curl          4.3.2   2021-06-23 [2] CRAN (R 4.1.1)
#>  digest        0.6.28  2021-09-23 [2] CRAN (R 4.1.1)
#>  ellipsis      0.3.2   2021-04-29 [2] CRAN (R 4.1.1)
#>  evaluate      0.14    2019-05-28 [2] CRAN (R 4.1.1)
#>  fansi         0.5.0   2021-05-25 [2] CRAN (R 4.1.1)
#>  fastmap       1.1.0   2021-01-25 [2] CRAN (R 4.1.1)
#>  fs            1.5.2   2021-12-08 [2] CRAN (R 4.1.2)
#>  glue          1.5.1   2021-11-30 [2] CRAN (R 4.1.2)
#>  highr         0.9     2021-04-16 [2] CRAN (R 4.1.1)
#>  htmltools     0.5.2   2021-08-25 [2] CRAN (R 4.1.1)
#>  httr          1.4.2   2020-07-20 [2] CRAN (R 4.1.1)
#>  knitr         1.36    2021-09-29 [2] CRAN (R 4.1.2)
#>  lifecycle     1.0.1   2021-09-24 [2] CRAN (R 4.1.1)
#>  magrittr      2.0.1   2020-11-17 [2] CRAN (R 4.1.1)
#>  mime          0.12    2021-09-28 [2] CRAN (R 4.1.2)
#>  pillar        1.6.4   2021-10-18 [2] CRAN (R 4.1.1)
#>  pkgconfig     2.0.3   2019-09-22 [2] CRAN (R 4.1.1)
#>  purrr         0.3.4   2020-04-17 [2] CRAN (R 4.1.1)
#>  R.cache       0.15.0  2021-04-30 [2] CRAN (R 4.1.1)
#>  R.methodsS3   1.8.1   2020-08-26 [2] CRAN (R 4.1.1)
#>  R.oo          1.24.0  2020-08-26 [2] CRAN (R 4.1.1)
#>  R.utils       2.11.0  2021-09-26 [2] CRAN (R 4.1.1)
#>  R6            2.5.1   2021-08-19 [2] CRAN (R 4.1.1)
#>  Rcpp          1.0.7   2021-07-07 [2] CRAN (R 4.1.1)
#>  reprex        2.0.1   2021-08-05 [2] CRAN (R 4.1.1)
#>  rlang         0.4.12  2021-10-18 [2] CRAN (R 4.1.1)
#>  rmarkdown     2.11    2021-09-14 [2] CRAN (R 4.1.1)
#>  rstudioapi    0.13    2020-11-12 [2] CRAN (R 4.1.1)
#>  sessioninfo   1.2.2   2021-12-06 [2] CRAN (R 4.1.2)
#>  stringi       1.7.5   2021-10-04 [2] CRAN (R 4.1.2)
#>  stringr       1.4.0   2019-02-10 [2] CRAN (R 4.1.1)
#>  styler        1.6.2   2021-09-23 [2] CRAN (R 4.1.1)
#>  terra       * 1.5-13  2022-01-20 [1] Github (rspatial/terra@2c9d861)
#>  tibble        3.1.6   2021-11-07 [2] CRAN (R 4.1.1)
#>  utf8          1.2.2   2021-07-24 [2] CRAN (R 4.1.1)
#>  vctrs         0.3.8   2021-04-29 [2] CRAN (R 4.1.1)
#>  withr         2.4.3   2021-11-30 [2] CRAN (R 4.1.2)
#>  xfun          0.28    2021-11-04 [2] CRAN (R 4.1.2)
#>  xml2          1.3.3   2021-11-30 [2] CRAN (R 4.1.2)
#>  yaml          2.2.1   2020-02-01 [2] CRAN (R 4.1.1)
#> 
#>  [1] /home/jn/R/x86_64-redhat-linux-gnu-library/4.1
#>  [2] /usr/local/lib/R/library
#>  [3] /usr/lib64/R/library
#>  [4] /usr/share/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Nowosad avatar Jan 20 '22 17:01 Nowosad

patches detects patches (connect cells that have any value) surrounded by NA (or zero). You assume that it detects patches of cells surrounded by cells with any other value. That is not unreasonable. I will have look.

rhijmans avatar Jan 20 '22 17:01 rhijmans

Perhaps a related issue. Is this the correct behaviour when using directions=8? I was expecting 1, 2 and 4 to be one single big patch directions=8 (last plot). The same with 7 and 8. The function behaves as I expected when directions=4. I am using terra version 1.3.22. (Update: terra 1.5.17 is now working as expected. Thanks.)

library(terra)

val1 <- c(c(0,1,0,1,1,1,0,1,0,0,1,0,0,1,0,0),
          c(1,1,0,0,0,0,0,0,1,1,0,0,1,1,0,0),
          c(0,1,1,0,0,0,0,0,1,1,0,0,1,0,0,0),
          c(0,0,0,1,0,0,0,1,1,0,0,0,0,1,1,1))

r1 <- rast(ncol = 8, nrow = 8, resolution = 1,
                    xmin = 0, xmax = 8, ymin = 0, ymax = 8,
                    vals = val1)
plot(r1)

terrra_patch1

r2 <- patches(r1, directions=4, zeroAsNA=TRUE)
plot(r2, main= "directions=4")
text(r2, halo=FALSE)

terrra_patch_dir4

r3 <- patches(r1, directions=8, zeroAsNA=TRUE)

plot(r3, main= "directions=8")

text(r3, halo=FALSE)

terrra_patch_dir8

georod avatar Feb 12 '22 16:02 georod

Please open and modify as suggested by Nowosad, it would be much more useful.

aloboa avatar Mar 21 '22 09:03 aloboa