-
Notifications
You must be signed in to change notification settings - Fork 301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
point created by intersecting two linestrings does not intersect them #2410
Comments
Please add |
Point taken about I know that line intersection is notoriously challenging, but that seems rather extreme! |
and so on. Precision means converting the coordinates from double precision to an integer grid by multiplying by say
|
I know that precision is set in the opposite sense to how one might expect i.e., using a 'scale' multiplier, per the JTS documentation at https://locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/PrecisionModel.html linked from the But the above is not what I get...
In any case... I'm not jittering the point, I'm changing the ends of the lines, and they are intersecting to yield the point, which by definition ought therefore to intersect the lines that generated it? It also seems wrong that to get this result we need a grid as crude as 0.01 resolution (and on my machine I have to go to 0.1, i.e.
Also worth noting how apparent the poor resolution is when these objects are plotted. The plot below is for the objects generated with
For what it's worth
Is there some way in which this could be machine-dependent? MacOS ARM architecture here. |
How was |
Trying on an aarch64 with R 4.4.1 and |
I would have installed from CRAN. I'm glad to hear that I am probably not missing something obvious! |
Also... this:
where I get some variation on |
It looks as though the lines would need to be noded at the intersection:
See also libgeos/geos#585:
|
So this is obviously the 'feature-not-a-bug' that prevents me using |
Sorry for the delay (away from Wifi this week!). Definitely reproducible outside sf! library(geos)
sd <- 0.01
coords <- rbind(
matrix(c(-1, 1, -1, 1) + rnorm(4, 0, sd), 2, 2),
matrix(c(-1, 1, 1, -1) + rnorm(4, 0, sd), 2, 2)
)
ids <- c(1, 1, 2, 2)
linestrings <- geos_make_linestring(coords[, 1], coords[, 2], feature_id = ids)
pt <- geos_intersection(linestrings[1], linestrings[2])
geos_intersects(pt, linestrings[1])
#> [1] FALSE
geos_intersects(pt, linestrings[2])
#> [1] FALSE A slightly smaller scale version of that is the orientation index (which should be zero of the point exactly intersects a segment): library(geos)
set.seed(1234)
sd <- 0.01
coords1 <- matrix(c(-1, 1, -1, 1) + rnorm(4, 0, sd), 2, 2)
coords2 <- matrix(c(-1, 1, 1, -1) + rnorm(4, 0, sd), 2, 2)
coords <- rbind(coords1, coords2)
ids <- c(1, 1, 2, 2)
linestrings <- geos_make_linestring(
coords[, 1],
coords[, 2],
feature_id = ids
)
pt <- geos_intersection(linestrings[1], linestrings[2])
geos_orientation_index(
list(coords1[1, 1], coords1[1, 2], coords1[2, 1], coords1[2, 2]),
list(geos_x(pt), geos_y(pt))
)
#> [1] 1 There is somewhere on the internet a very cool javascript animation of why the point-segment intersection problem is very difficult, although I can't find it at the moment. I think that GEOS uses exact or very high floating point math for this kind of operation; however, it is probably just that the exact point of intersection just can't be represented with a As Roger noted, a "distance within" check is really what you need here. |
Thanks for the information. The irony is that since I am making the points by intersecting the lines, I already know that they intersect them. As I noted in passing at the top, I ran into this while trying to |
|
Describe the bug
A point I generate by intersecting two linestrings fails expected
st_intersects
and does not yield expectedst_relate
pattern.To Reproduce
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).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. thest_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 assf
) what might be a viable workaround insf
?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.─ 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
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────
The text was updated successfully, but these errors were encountered: