r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.35k stars 299 forks source link

st_distance units incompatible with difftime units. #2365

Closed ratnanil closed 8 months ago

ratnanil commented 8 months ago

Describe the bug

I'm not sure if I should place this issue here or here.

In any case, st_distance units seem to be incompatible with difftime units. Is there a possibility to fix this?

To Reproduce

library("sf")
#> Linking to GEOS 3.12.0, GDAL 3.7.1, PROJ 9.2.1; sf_use_s2() is TRUE
library("units")
#> udunits database from /usr/share/xml/udunits/udunits2.xml
df <- data.frame(
  time = as.POSIXct(c("2023-03-19 12:00:00", "2023-03-19 12:15:00")), 
  E = c(2694032, 2693958), 
  N = c(1230504, 1231202)
) |>
  st_as_sf(coords = c("E", "N"), crs = 2056)

timediff <- difftime(df$time[2],df$time[1], units = "secs")
steplength <- st_distance(df$geometry[2], df$geometry[1], by_element = TRUE)

speed <- steplength/timediff   # this triggers a warning
#> Warning: Incompatible methods ("Ops.units", "/.difftime") for "/"

speed
#> 0.7799019 [m]

The object speed has the wrong unit (m instead of m/s). timediff needs to be converted with as_units()


speed2 <- steplength/as_units(timediff) # does not trigger a warning

speed2
#> 0.7799019 [m/s]

Now the unit is correct

``` r sessionInfo() #> R version 4.3.1 (2023-06-16) #> Platform: x86_64-pc-linux-gnu (64-bit) #> Running under: Ubuntu 23.10 #> #> Matrix products: default #> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.11.0 #> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.11.0 #> #> locale: #> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C #> [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_US.UTF-8 #> [5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_US.UTF-8 #> [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C #> [9] LC_ADDRESS=C LC_TELEPHONE=C #> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C #> #> time zone: Europe/Zurich #> tzcode source: system (glibc) #> #> attached base packages: #> [1] stats graphics grDevices utils datasets methods base #> #> loaded via a namespace (and not attached): #> [1] styler_1.10.2 digest_0.6.34 fastmap_1.1.1 xfun_0.42 #> [5] magrittr_2.0.3 glue_1.7.0 R.utils_2.12.3 knitr_1.45 #> [9] htmltools_0.5.7 rmarkdown_2.25 lifecycle_1.0.4 cli_3.6.2 #> [13] R.methodsS3_1.8.2 vctrs_0.6.5 reprex_2.1.0 withr_3.0.0 #> [17] compiler_4.3.1 R.oo_1.26.0 R.cache_0.16.0 purrr_1.0.2 #> [21] rstudioapi_0.15.0 tools_4.3.1 evaluate_0.23 yaml_2.3.8 #> [25] rlang_1.1.3 fs_1.6.3 sf::sf_extSoftVersion() #> GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H #> "3.12.0" "3.7.1" "9.2.1" "true" "true" #> PROJ #> "9.2.1" ``` Created on 2024-03-19 with [reprex v2.1.0](https://reprex.tidyverse.org)
edzer commented 8 months ago
options(warn=2)

?

ratnanil commented 8 months ago

I was rather hoping for a fix. Not for me, but my students... But feel free to close this as wontfix. I also posted this here for other people stumbling over this issue.

edzer commented 8 months ago

Teach your students that in R, warnings are not to be ignored.

If anything, this is an units issue, not an sf issue.