jmsigner / amt

37 stars 13 forks source link

Fix hr_locoh() errors ID'd in Issue #66 #68

Closed JoshOBrien closed 2 years ago

JoshOBrien commented 2 years ago

This commit fixes the problems with hr_locoh() caused by recent changes to the sf package (discussed in #66)

With this commit, (as part of its move away from any reliance on the rgeos package) the sf package modified the code it uses to implement sf:::st_as_sfc.SpatialPolygons(). One consequence of this change is that st_as_sfc() now handles SpatialPolygons containing several overlapping polygons differently than it did before. Previously it converted them faithfully to an sfc MULTIPOLYGON. Now (while executing these two lines of code) it often splits overlapping polygons up into multiple smaller polygons. (It's the call to st_make_valid() in particular that alters the component polygons, following rules I can guess at but haven't fully unpacked.)

In any case, we can sidestep this issue in amt by the simple step of giving each of the polygons that make up ff (which is passed to st_as_sf() here in amt:::hr_locoh.track_xy()) its own ID. By making that single change, this commit restores hr_locoh()'s functionality in the half dozen cases in which I've tested it.

JoshOBrien commented 2 years ago

In case it helps, here are images of the home ranges produced by the following code, first using the version of amt currently on CRAN, amt-0.1.7, then the version in this PR. (In both cases, I used sf-1.0.7, the version of sf currently on CRAN.)

library(sf)  
library(amt)
library(tidyverse)

data(deer)

max_dist <- deer %>%
  select(x_, y_) %>%
  dist() %>%
  max()

locoh <- deer %>%
  hr_locoh(levels = 0.95,
           keep.data = TRUE,
           n = max_dist,
           type = "a",
           rand_buffer = 1e-05)

locoh$locoh %>%
  filter(level == max(level)) %>%
  ggplot() +
  geom_sf(aes(fill = level))

amt-0 1 7

amt-debug

jmsigner commented 2 years ago

@JoshOBrien Thanks for fixing this and taking are of it!

JoshOBrien commented 2 years ago

@jmsigner You bet. Thanks right back at you for all your work on this package.