Skip to content

Transitions between adjacent cells with finite and infinite values are missing. #25

@famuvie

Description

@famuvie

Hi! First, thanks a lot for the work put into the fantastic package {gdistance}!

I wanted to report a behaviour that I think should be considered a bug, concerning transitionLayer.

Given two adjacent cells with finite values $x$ and $y$, the transition between them is $f(x, y)$, where $f$ is the transition function. Say that $f$ is the mean() function, for instance. When $y$ grows bigger, the transition grows bigger as well. So, if $y = \infty$, I would expect the transition to be $\infty$, for consistency.
Yet, the result is a missing value, as if the two cells were not connected, or as if $y$ was NA.

A MRE follows:

## MRE gdistance::transition
library(gdistance)
#> Loading required package: raster
#> Loading required package: sp
#> Loading required package: igraph
#> 
#> Attaching package: 'igraph'
#> The following object is masked from 'package:raster':
#> 
#>     union
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
#> Loading required package: Matrix
#> 
#> Attaching package: 'gdistance'
#> The following object is masked from 'package:igraph':
#> 
#>     normalize
m0 <- matrix(1:4, 2, 2)
r0 <- raster(m0)

m2 <- m1 <- m0

m1[2, 2] <- NA
r1 <- raster(m1)

m2[2, 2] <- Inf
r2 <- raster(m2)

tr0 <- transition(r0, mean, directions = 4)
tr1 <- transition(r1, mean, directions = 4)
tr2 <- transition(r2, mean, directions = 4)

transitionMatrix(tr0)
#> 4 x 4 sparse Matrix of class "dsCMatrix"
#>                     
#> [1,] .   2.0 1.5 .  
#> [2,] 2.0 .   .   3.5
#> [3,] 1.5 .   .   3.0
#> [4,] .   3.5 3.0 .
transitionMatrix(tr1)
#> 4 x 4 sparse Matrix of class "dsCMatrix"
#>                 
#> [1,] .   2 1.5 .
#> [2,] 2.0 . .   .
#> [3,] 1.5 . .   .
#> [4,] .   . .   .
transitionMatrix(tr2)
#> 4 x 4 sparse Matrix of class "dsCMatrix"
#>                 
#> [1,] .   2 1.5 .
#> [2,] 2.0 . .   .
#> [3,] 1.5 . .   .
#> [4,] .   . .   .


## Inputs are different, but results are the identical
identical(r1, r2)  # FALSE
#> [1] FALSE
identical(transitionMatrix(tr1), transitionMatrix(tr2))  # TRUE
#> [1] TRUE

sessionInfo()
#> R version 4.5.1 (2025-06-13)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Linux Mint 22.1
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>  [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>  [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Europe/Paris
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] gdistance_1.6.4 Matrix_1.7-4    igraph_2.1.4    raster_3.6-32  
#> [5] sp_2.2-0       
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.37     codetools_0.2-20  fastmap_1.2.0     xfun_0.52        
#>  [5] lattice_0.22-7    magrittr_2.0.3    glue_1.8.0        knitr_1.50       
#>  [9] pkgconfig_2.0.3   htmltools_0.5.8.1 rmarkdown_2.29    lifecycle_1.0.4  
#> [13] cli_3.6.5         terra_1.8-60      grid_4.5.1        reprex_2.1.1     
#> [17] withr_3.0.2       compiler_4.5.1    tools_4.5.1       evaluate_1.0.5   
#> [21] Rcpp_1.1.0        yaml_2.3.10       rlang_1.1.6       fs_1.6.6

Created on 2025-09-02 with reprex v2.1.1

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions