jmsigner / amt

37 stars 13 forks source link

hr_to_sf returns object length mismatch error #84

Open mainahandmaker opened 1 year ago

mainahandmaker commented 1 year ago

I used the hr_kde function to calculate KDE daily home ranges for many individuals, and am now attempting to use the hr_to_sf() function to convert the list column with KDE home range estimates into sf objects for plotting and other sf operations. This used to work perfectly for me, but now is generating the following error:

Error in sp::SpatialPolygonsDataFrame(con, df) : Object length mismatch: con has 1 Polygons objects, but df has 2 rows

Any thoughts on what might be causing the hr_to_sf() function to return this error? Many thanks!

Session info below:

R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows Server x64 (build 19044)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

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

other attached packages: [1] furrr_0.3.1 future_1.30.0 tidyr_1.2.1 readr_2.1.3 purrr_0.3.5 mapview_2.11.0
[7] lubridate_1.9.0 timechange_0.1.1 dplyr_1.0.10 sf_1.0-9 ggmap_3.0.1 ggplot2_3.4.0
[13] amt_0.1.7

loaded via a namespace (and not attached): [1] Rcpp_1.0.9 lattice_0.20-45 listenv_0.9.0 png_0.1-8 class_7.3-20
[6] assertthat_0.2.1 digest_0.6.30 utf8_1.2.2 parallelly_1.33.0 R6_2.5.1
[11] plyr_1.8.8 backports_1.4.1 stats4_4.2.1 e1071_1.7-12 httr_1.4.4
[16] pillar_1.8.1 Rdpack_2.4 RgoogleMaps_1.4.5.3 rlang_1.0.6 rstudioapi_0.14
[21] raster_3.6-11 Matrix_1.4-1 checkmate_2.1.0 splines_4.2.1 webshot_0.5.4
[26] stringr_1.5.0 htmlwidgets_1.5.4 munsell_0.5.0 proxy_0.4-27 compiler_4.2.1
[31] pkgconfig_2.0.3 base64enc_0.1-3 rgeos_0.5-9 globals_0.16.2 htmltools_0.5.3
[36] tidyselect_1.2.0 tibble_3.1.8 codetools_0.2-18 fansi_1.0.3 tzdb_0.3.0
[41] withr_2.5.0 bitops_1.0-7 rbibutils_2.2.10 grid_4.2.1 satellite_1.0.4
[46] gtable_0.3.1 lifecycle_1.0.3 DBI_1.1.3 magrittr_2.0.3 units_0.8-0
[51] scales_1.2.1 KernSmooth_2.23-20 cli_3.4.1 stringi_1.7.8 leaflet_2.1.1
[56] sp_1.5-1 ellipsis_0.3.2 generics_0.1.3 vctrs_0.5.1 tools_4.2.1
[61] leafem_0.2.0 glue_1.6.2 hms_1.1.2 crosstalk_1.2.0 jpeg_0.1-10
[66] parallel_4.2.1 fastmap_1.1.0 survival_3.3-1 colorspace_2.0-3 terra_1.6-47
[71] classInt_0.4-8

jmsigner commented 1 year ago

The following works for me:

library(amt)
#> 
#> Attaching package: 'amt'
#> The following object is masked from 'package:stats':
#> 
#>     filter
data(deer)

deer |> mutate(month = lubridate::month(t_)) |> 
  nest(dat = -month) |> 
  mutate(hr = map(dat, hr_kde)) |> hr_to_sf(hr)
#> Warning in fun(libname, pkgname): rgeos: versions of GEOS runtime 3.10.1-CAPI-1.16.0
#> and GEOS at installation 3.9.1-CAPI-1.14.2differ
#> Simple feature collection with 7 features and 3 fields
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: 4308459 ymin: 3441894 xmax: 4320290 ymax: 3452486
#> Projected CRS: ETRS89 / LAEA Europe
#>   level     what           area                       geometry
#> 1  0.95 estimate  7587092 [m^2] POLYGON ((4311006 3445134, ...
#> 2  0.95 estimate 10331200 [m^2] POLYGON ((4310787 3446195, ...
#> 3  0.95 estimate 50075268 [m^2] POLYGON ((4309621 3446230, ...
#> 4  0.95 estimate 11790773 [m^2] POLYGON ((4313355 3449232, ...
#> 5  0.95 estimate  9816956 [m^2] POLYGON ((4313768 3449268, ...
#> 6  0.95 estimate  9768871 [m^2] MULTIPOLYGON (((4311317 344...
#> 7  0.95 estimate 23912372 [m^2] POLYGON ((4308498 3443105, ...

Created on 2023-01-13 by the reprex package (v2.0.1)

Could you send me a small subset of your data (by email is fine) that reproduces the problem?

mainahandmaker commented 1 year ago

Hi Dr. Signer,

Thank you so much for your response.

We figured out that the cause for the error was that some of the KDE home ranges were so tiny that they could not be plotted as sf objects at the template raster resolution we were using. We study the movements of a shorebird population that spreads out over great distances during the day, but all concentrate on a small island at night -- this is why hr_to_sf() worked fine for diurnal tracks, but some nightly home ranges were so small that it hr_kde could not create a 50% KDE, only a 90% KDE, which is why hr_to_sf() did not work. We need to keep the template raster size the same for diurnal and nocturnal data so that I can compare home range overlap, area, etc., so for now our solution was to identify and remove the few nights that were causing the errors. We did this using functions in the purrr package, but if you think other amt users could benefit from a way to identify this type of error within the package, I can still send you a small subset of the data so you can reproduce the issue. I think, though, that this may have been a problem unique to our data set.

Thank you for this fantastic package and for your willingness to help!

All the best, Maina

Maina Handmaker she/her/hers PhD Student | Senner Lab http://www.sennerlab.com/ UMass Amherst Department of Environmental Conservation https://eco.umass.edu/ @.***| 502.523.1157 mainahandmaker.com

On Fri, Jan 13, 2023 at 12:27 AM Johannes Signer @.***> wrote:

The following works for me:

library(amt)#> #> Attaching package: 'amt'#> The following object is masked from 'package:stats':#> #> filter data(deer) deer |> mutate(month = lubridate::month(t_)) |> nest(dat = -month) |> mutate(hr = map(dat, hr_kde)) |> hr_to_sf(hr)#> Warning in fun(libname, pkgname): rgeos: versions of GEOS runtime 3.10.1-CAPI-1.16.0#> and GEOS at installation 3.9.1-CAPI-1.14.2differ#> Simple feature collection with 7 features and 3 fields#> Geometry type: GEOMETRY#> Dimension: XY#> Bounding box: xmin: 4308459 ymin: 3441894 xmax: 4320290 ymax: 3452486#> Projected CRS: ETRS89 / LAEA Europe#> level what area geometry#> 1 0.95 estimate 7587092 [m^2] POLYGON ((4311006 3445134, ...#> 2 0.95 estimate 10331200 [m^2] POLYGON ((4310787 3446195, ...#> 3 0.95 estimate 50075268 [m^2] POLYGON ((4309621 3446230, ...#> 4 0.95 estimate 11790773 [m^2] POLYGON ((4313355 3449232, ...#> 5 0.95 estimate 9816956 [m^2] POLYGON ((4313768 3449268, ...#> 6 0.95 estimate 9768871 [m^2] MULTIPOLYGON (((4311317 344...#> 7 0.95 estimate 23912372 [m^2] POLYGON ((4308498 3443105, ...

Created on 2023-01-13 by the reprex package https://reprex.tidyverse.org (v2.0.1)

Could you send me a small subset of your data (by email is fine) that reproduces the problem?

— Reply to this email directly, view it on GitHub https://github.com/jmsigner/amt/issues/84#issuecomment-1381332735, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASYRBKTNJ3BFOPHQYPJM5PTWSDRUFANCNFSM6AAAAAATPD5GLY . You are receiving this because you authored the thread.Message ID: @.***>