Simple st_intersect gives unexpected result

I'm using the sf package in R to simulate a sample of agents moving between different nodes in a network across space-time.

I'm currently puzzled though by some behavior from st_intersects : I have the agents moving between nodes between each corner of the coordinate unit square as well as through the center at (.5,.5) . However when I try to detect an agent at st_point(c(.1,.9)) intersecting with the geometry st_linestring(c(st_point(c(0,0)),st_point(c(0.5,0.5)))) I get an empty predicate return.

In contrast if I detect an agent moving along the x-axis or y-axis only, I am able to detect the point correctly. Why is this?

reprex below.

library(sf)
l1 <- st_linestring(c(st_point(c(0,1)),st_point(c(0.5,0.5))))
#> Error in st_linestring(c(st_point(c(0, 1)), st_point(c(0.5, 0.5)))): could not find function "st_linestring"
p1 <- st_point(c(.1,.9)) ## on the line between (0,1) and (.5,.5); y=1-x x = f(t)
#> Error in st_point(c(0.1, 0.9)): could not find function "st_point"


st_intersects(p1,l1) ## empty
#> Error in st_intersects(p1, l1): could not find function "st_intersects"
#Sparse geometry binary predicate list of length 1, where the predicate was `intersects'
# 1: (empty)


## in contrast
l2 <- st_linestring(c(st_point(c(0,0)),st_point(c(1,0))))
#> Error in st_linestring(c(st_point(c(0, 0)), st_point(c(1, 0)))): could not find function "st_linestring"
p2 <- st_point(c(.1,0)) ## on the line between (0,0) and (1,0) ; y = 0; x = f(t)
#> Error in st_point(c(0.1, 0)): could not find function "st_point"

st_intersects(p2,l2) ## returns 1 as I would expect
#> Error in st_intersects(p2, l2): could not find function "st_intersects"
#Sparse geometry binary predicate list of length 1, where the predicate was `intersects'
# 1: 1

Created on 2021-06-22 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       macOS Big Sur 11.4          
#>  system   aarch64, darwin20           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2021-06-22                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  cli           2.5.0   2021-04-26 [1] CRAN (R 4.1.0)
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.1.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.1.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.1.0)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.1.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.1.0)
#>  knitr         1.33    2021-04-24 [1] CRAN (R 4.1.0)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.0)
#>  reprex        2.0.0   2021-04-02 [1] CRAN (R 4.1.0)
#>  rlang         0.4.11  2021-04-30 [1] CRAN (R 4.1.0)
#>  rmarkdown     2.9     2021-06-15 [1] CRAN (R 4.1.0)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.1.0)
#>  stringi       1.6.2   2021-05-17 [1] CRAN (R 4.1.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.1.0)
#>  withr         2.4.2   2021-04-18 [1] CRAN (R 4.1.0)
#>  xfun          0.24    2021-06-15 [1] CRAN (R 4.1.0)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.1.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library

Is sf installed? It can't find any of the functions that you have used. If you look at l1 you can see that nothing has been generated there.

There seems to be a tiny rounding difference, causing the fault - see the result of sf::st_distance() below. Floating point numbers are constant nuisance.

As a workaround I suggest to use sf::st_is_within_distance() instead, with a sufficiently negligible tolerance / the one thousandth of a unit here could be a start...

library(sf)
l1 <- st_linestring(c(st_point(c(0,1)),
                      st_point(c(0.5,0.5))))
p1 <- st_point(c(.1,.9)) ## on the line between (0,1) and (.5,.5); y=1-x x = f(t)

st_distance(l1, p1)[1,1]
# [1] 1.962616e-17

st_is_within_distance(p1,l1, 1/1000) 
# Sparse geometry binary predicate list of length 1, where the
# predicate was `is_within_distance'
# 1: 1
1 Like

I suppose I'll have to. Crazy that I have to use a work around on such a basic operation.

Thanks for your help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.