jmsigner / amt

37 stars 13 forks source link

hr_isopleths possibly not working with Terra #88

Closed sjclements9 closed 1 year ago

sjclements9 commented 1 year ago

Hello, I came across a problem with the hr_isopleths() function potentially not being compatible with terra. I am in the process of updating code that worked before the recent updates to amt. This is an example with different raster data types with the amt fisher data:

# Example of amt hr_isopleths function with different raster types
# with amt fisher data

library(amt)
library(tidyverse)
# new spatial
library(sf)
library(terra)
# old spatial
library(sp)
library(raster)

# old code that worked
#hr_isopleths(hr$ud, level=0.95, longlat=T)

# test on fisher
# tried putting in kde, SpatRaster, Raster, and PackedSpatRaster..

b <- amt_fisher %>% 
  filter(id == "F1")

hr <- hr_kde(b)
class(hr) #"kde"     "hr_prob" "hr"      "list"   
plot(hr$ud)
class(hr$ud) # SpatRaster
hri <- hr_isopleths(x=hr, levels=0.95) # Error in do.call(rbind, y) : second argument must be a list
hri <- hr_isopleths(x=hr$ud, levels=0.95) # Error in do.call(rbind, y) : second argument must be a list

hr2 <- as(hr$ud, 'Raster')
class(hr2) # raster
hri2 <- hr_isopleths(x=hr2, levels=0.95) # can't use hr_isopleths on raster

hr3 <- as(hr, 'kde')
class(hr3) # "kde"     "hr_prob" "hr"      "list"  
hri3 <- hr_isopleths(x=hr3, levels=0.95) #Error in do.call(rbind, y) : second argument must be a list

hr4 <- terra::wrap(hr$ud) # make it PackedSpatRaster
class(hr4) # PackedSpatRaster
hri4 <- hr_isopleths(x=hr4, levels=0.95) # Error in do.call(rbind, y) : second argument must be a list

#> sessionInfo()
#R version 4.2.2 (2022-10-31 ucrt)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 22621)

#attached base packages:
#  [1] stats     graphics  grDevices utils     datasets  methods   base     

#other attached packages:
#  [1] raster_3.6-11   sp_1.5-1        terra_1.6-47    sf_1.0-9        forcats_0.5.2  
#[6] stringr_1.5.0   dplyr_1.0.10    purrr_1.0.0     readr_2.1.3     tidyr_1.2.1    
#[11] tibble_3.1.8    ggplot2_3.4.0   tidyverse_1.3.2 amt_0.2    

Thank you!

sjclements9 commented 1 year ago

Sorry about the odd formatting on that message, I am a bit new at github!

jmsigner commented 1 year ago

Thanks for reporting this. What would you like to achieve? Is it just to calculate the isopleth, with hr_isopleth()? As you illustrated? That work on my side.

sjclements9 commented 1 year ago

Thank you for your response! Yes, that is what I am looking to do here (ultimately for plotting home ranges). I believe all of my packages are up to date, but is this something that could be a result of a particular version of another package being run?

jmsigner commented 1 year ago

The only difference I see is with terra, you are using version 1.6.-47 and there is a newer version and I think to remember that there was in issue with terra. Could you try to update terra and then run the code again?

sjclements9 commented 1 year ago

Thank you! It worked after updating terra.