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

rangeFill(... circular = T) generates any circular range as TRUE from 1:limit #1460

Closed
legallyahc opened this issue Mar 14, 2024 · 1 comment

Comments

@legallyahc
Copy link

legallyahc commented Mar 14, 2024

I'm encountering a bug when using rangeFill to generate T/F layers for a given year of data when a range goes beyond the limit value. When circular = F in the options, the cells do not generate, as expected, but when circular = T the cells display as true even beyond their expected limits (first:limit, 1:end).

library(terra)
packageVersion("terra")
#[1] ‘1.7.71’

I managed to capture this behaviour in a small example:

x <- y <- rast(ncol=2, nrow =3)
values(x) <- c(NA, 1, 90, 180, 270, 360)
values(y) <- c(NA, 180, 270, 360, 1, 90)
t <- rangeFill(c(x, y), 365, circular = T)
f <- rangeFill(c(x, y), 365, circular = F)
plot(t[[181]]) 
plot(f[[181]])

t181
f181

At layer 181, neither 270:1 nor 360:90 should appear as TRUE as they are beyond their range. As expected, they do not appear when circular is set to FALSE. This anomalous behaviour is confirmed by checking the sum of the layers:

plot(sum(t))

tsum

Where both cells whose ranges are circular report as having a sum of 365.

@rhijmans
Copy link
Member

Thank you for reporting this. I now get:

library(terra)
#terra 1.7.74
x <- y <- rast(ncol=2, nrow=2)
values(x) <- c(NA, c(1, 5, 6))
values(y) <- c(NA, c(4, 1, 2))

rangeFill(c(x, y), 8) |> values()
#     lyr1 lyr2 lyr3 lyr4 lyr5 lyr6 lyr7 lyr8
#[1,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
#[2,]    1    1    1    1    0    0    0    0
#[3,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
#[4,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

rangeFill(c(x, y), 8, circular=TRUE) |> values()
#     lyr1 lyr2 lyr3 lyr4 lyr5 lyr6 lyr7 lyr8
#[1,]  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
#[2,]    1    1    1    1    0    0    0    0
#[3,]    1    0    0    0    1    1    1    1
#[4,]    1    1    0    0    0    1    1    1

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