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

patches() returns unexpected results #495

Closed
Nowosad opened this issue Jan 20, 2022 · 5 comments
Closed

patches() returns unexpected results #495

Nowosad opened this issue Jan 20, 2022 · 5 comments

Comments

@Nowosad
Copy link
Contributor

Nowosad commented Jan 20, 2022

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
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@rhijmans
Copy link
Member

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.

@georod

This comment was marked as off-topic.

@aloboa
Copy link

aloboa commented Mar 21, 2022

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

@rhijmans
Copy link
Member

You can now use

plot(patches(checker_map, 8, values=T))

@aloboa
Copy link

aloboa commented Nov 28, 2024

While this works,

library(RStoolbox)
> library(terra)
terra 1.8.3
> dim(lsat)
[1] 310 287       7
klsat <- k_means(lsat, centers=4, maxcell=10000)
plot(klsat,col=map.pal("random"))
a <- patches(klsat, values=TRUE)
plot(a, col=map.pal("random"))

Using a larger image crashes a system with 64 GB of RAM:

lsat2 <- disagg(lsat,8)
dim(lsat2)
[1] 2480 2296       7
klsat2 <- k_means(lsat2, centers=4, maxcell=10000)
plot(klsat2,col=map.pal("random"))
a <- patches(klsat2, values=TRUE)

image

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 5, 2025
# version 1.8-5

## bug fixes

- `Spatsample(method='stratified', ext=e)` returned the wrong sampling
  coordinates [#1628](rspatial/terra#1628)
  by Barnabas Harris

- `spatSample(method='stratified')` could fail with small sample sizes
  [#1503](rspatial/terra#1503) by karluf

- transparency (alpha) did not work with RGB
  plotting. [#1642](rspatial/terra#1642) by
  Timothée Giraud

- rasterization failed on very large rasters
  [#1636](rspatial/terra#1636) by Mary
  Fisher, [#1463](rspatial/terra#1463) by
  Nic Spono and [#1281](rspatial/terra#1281)
  by Sebastian Dunnett

- `tmpFiles` only looked in the default temp files folder
  [#1630](rspatial/terra#1630) by
  smckenzie1986

- `where.min` did not work well if there were negative values
  [#1634](rspatial/terra#1634) by Michael
  Sumner

- `plet<SpatRaster>` now works for RGB rasters and rasters with a
  color table [#1596](rspatial/terra#1596)
  by Agustin Lobo

- `vect<MULTIPOINT WKT>` did not work properly
  [#1376](rspatial/terra#1376) by
  silasprincipe

- `compareGeom<SpatVector>` did not work
  [#1654](rspatial/terra#1654) by Jason
  Flower

- `buffer<SpatVector>` is now more accurate buffers for lonlat
  polygons [#1616](rspatial/terra#1616) by
  Roberto Amaral-Santos

- `terra:interpNear` used square windows, not circles, beyond 100
  points [#1509](rspatial/terra#1509) by
  Jean-Luc Dupouey

- `vect` read INT64 fields as integers, sometimes leading to
  overflows. [#1666](rspatial/terra#1666) by
  bengannon-fc

- `plot` showed a legend title even if none was requestd if title
  parameters were specified
  . [#1664](rspatial/terra#1664) by Márcia
  Barbosa



## enhancements

n- improved documentation of `writeVector` overwrite when using
  layers. [#1573](rspatial/terra#1573) by
  Todd West

- improved treatment of (supposedly) flipped rasters by Timothée
  Giraud [#1627](rspatial/terra#1627) and
  fchianucci [#1646](rspatial/terra#1646)

- added `map.pal("random")`
  [#1631](rspatial/terra#1631) by Agustin
  Lobo

- expressions can now be used in legend titles
  [#1626](rspatial/terra#1626) by Noah
  Goodkind

- `app` and `tapp` now emit a warning when factors are coerced to
  numeric [#1566](rspatial/terra#1566) by
  shuysman

- `plet<SpatRaster>` now has argument "stretch" for RGB rasters
  [#1596](rspatial/terra#1596) by Agustin

- `%%` and `%/%` now behave the same for SpatRaster as for (base R)
  numbers [#1661](rspatial/terra#1661) by
  Klaus Huebert

## new

- `patches` with option `valus=TRUE` can now distinguish regions based
  on their cell values (instead of only NA vs not-NA)
  [#495](rspatial/terra#495) by Jakub
  Nowosad and [#1632](rspatial/terra#1632)
  by Agustin Lobo

- `rowSums`, `rowMeans`, `colSums` and `colMeans` for SpatRaster

- `metags` for SpatRasterDataset
  [#1624](rspatial/terra#1624) by Andrea
  Manica

- `metags` for layers (bands) of SpatRaster are now saved to and read
  from GTiff files
  [#1071](rspatial/terra#1071) by Mike
  Koontz

- `global` has new effcient functions "anyNA" and "anynotNA"
  [#1540](rspatial/terra#1540) by Kevin J
  Wolz

- `wrap`, `saveRDS` and `serialize` for
  SpatExtent. [#1430](rspatial/terra#1430)
  by BastienFR

- `vect<SpatGraticule>` method suggested in relation to [tidyterra
  #155](dieghernan/tidyterra#155) by Diego
  Hernangómez

- `toMemory<SpatRaster>` and `<SpatRasterDataset>` methods
  [#1660](rspatial/terra#1660) by Derek Friend
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

4 participants