Skip to content

point created by intersecting two linestrings does not intersect them #2410

Open
@DOSull

Description

@DOSull

Describe the bug
A point I generate by intersecting two linestrings fails expected st_intersects and does not yield expected st_relate pattern.

To Reproduce

sd <- 0.01
lines <- st_multilinestring(list(matrix(c(-1, 1, -1, 1) + 
                                           rnorm(4, 0, sd), 2, 2), 
                                  matrix(c(-1, 1, 1, -1) + 
                                           rnorm(4, 0, sd), 2, 2))) |> 
  st_sfc() |> st_cast("LINESTRING")
pt <- st_intersection(lines[1], lines[2]) |> st_sfc()

st_intersects(pt, lines)
Sparse geometry binary predicate list of length 1, where the predicate was `intersects'
 1: (empty)

st_relate(pt, lines)
     [,1]        [,2]       
[1,] "FF0FFF102" "FF0FFF102"

So the point resulting from intersecting two lines does not intersect them, and is not related to them as expected ("0FFFFF102"). Inspection of the objects shows that the point is at the intersection of the two lines close to (0, 0).

> lines
Geometry set for 2 features 
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -0.9997257 ymin: -1.020536 xmax: 1.012496 ymax: 0.9998001
CRS:           NA
LINESTRING (-0.9929678 -0.9947806, 1.012496 0.9...
LINESTRING (-0.9997257 0.9998001, 0.9876878 -1....
> pt
Geometry set for 1 feature 
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 0.00154847 ymin: -0.01806063 xmax: 0.00154847 ymax: -0.01806063
CRS:           NA
POINT (0.00154847 -0.01806063)

Additional context
If I make lines running from (-1 -1) to (1 1) and (-1 1) to (1 -1) exactly (by setting sd <- 0) then they intersect at (0 0) and that point satisfies expected spatial predicates against the generating intersecting lines, i.e. the st_relate pattern is "0FFFFF102".

I have experimented with setting precision on the geometries but have only obtained expected results reliably when precision is set very crudely. If the problem is deemed 'upstream' in GEOS, as I suspect it might be (geos::geos_relate produces the same results on these geometries as sf) what might be a viable workaround in sf?

I initially encountered this problem working with lwgeom::st_split() trying to split lines by their intersection points, and getting many fewer output linestrings than expected. On looking more closely this was what I ran into.

> sessioninfo::session_info() ─ Session info ────────────────────────────────────────────────────────── setting value version R version 4.3.3 (2024-02-29) os macOS Sonoma 14.5 system aarch64, darwin20 ui RStudio language (EN) collate en_US.UTF-8 ctype en_US.UTF-8 tz Pacific/Auckland date 2024-07-01 rstudio 2024.04.1+748 Chocolate Cosmos (desktop) pandoc NA

─ Packages ───────────────────────────────────────────────────────────
package * version date (UTC) lib source
class 7.3-22 2023-05-03 [1] CRAN (R 4.3.3)
classInt 0.4-10 2023-09-05 [1] CRAN (R 4.3.0)
cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.1)
codetools 0.2-19 2023-02-01 [1] CRAN (R 4.3.3)
DBI 1.2.2 2024-02-16 [1] CRAN (R 4.3.1)
e1071 1.7-14 2023-12-06 [1] CRAN (R 4.3.1)
KernSmooth 2.23-22 2023-07-10 [1] CRAN (R 4.3.3)
lattice 0.22-5 2023-10-24 [1] CRAN (R 4.3.3)
lwgeom * 0.2-14 2024-02-21 [1] CRAN (R 4.3.1)
magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0)
Matrix 1.6-5 2024-01-11 [1] CRAN (R 4.3.3)
proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.0)
raster 3.6-26 2023-10-14 [1] CRAN (R 4.3.1)
Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.1)
rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.0)
sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0)
sf * 1.0-16 2024-03-24 [1] CRAN (R 4.3.1)
simplextree 1.0.1 2020-09-12 [1] CRAN (R 4.3.0)
sp 2.1-4 2024-04-30 [1] CRAN (R 4.3.1)
terra 1.7-71 2024-01-31 [1] CRAN (R 4.3.1)
units 0.8-5 2023-11-28 [1] CRAN (R 4.3.1)

[1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────

sf_extSoftVersion()
GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H PROJ
"3.11.0" "3.5.3" "9.1.0" "true" "true" "9.1.0"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions