JuliaGeo / LibGEOS.jl

Julia package for manipulation and analysis of planar geometric objects
MIT License
72 stars 23 forks source link

Accuracy issue with `nearestPoints` method #202

Open chamikabm opened 7 months ago

chamikabm commented 7 months ago

I have a line string as follows, consisting of a few points (longitude/latitude) coordinates in Paris. I tried to find the closest snapped point on the given line and from a give-point

line_string = [
            [
                145.0296835,
                -37.8089915
            ],
            [
                145.0297712,
                -37.809002
            ],
            [
                145.0298903,
                -37.8090163
            ],
            [
                145.0303226,
                -37.8090682
            ],
            [
                145.0304063,
                -37.8090783
            ],
            [
                145.0313321,
                -37.8091894
            ],
            [
                145.0322665,
                -37.8093016
            ],
            [
                145.032952,
                -37.8093839
            ],
            [
                145.033411,
                -37.809439
            ],
            [
                145.0336591,
                -37.8094688
            ],
            [
                145.0337068,
                -37.8094745
            ],
            [
                145.0348664,
                -37.8096137
            ],
            [
                145.0349955,
                -37.8096292
            ]
        ]

given_point = [
   145.03317585555192,
    -37.809289649922064
]

I’m using the LibGEOS library to do this as follows, but here’s the thing: visually, it looks like the point is snapping to the closest spot, but when I check with the intersect method using the same library, it turns out it’s not lining up with the provided line:

geos_point = LibGEOS.Point(given_point[1], given_point[2])
geos_line_string = LibGEOS.LineString(line_string)
point_on_linestring, point_on_point = LibGEOS.nearestPoints(geos_line_string, geos_point)
nearest_point = [ LibGEOS.getGeomX(point_on_linestring), LibGEOS.getGeomY(point_on_linestring) ]

But when I check the nearest point:

LibGEOS.intersects(nearest_point, geos_line_string) ==> False

Output of the above function gives as false, but If I input a point in the line_string separately, then it gives me true as follows:

point_in_the_line_string = LibGEOS.Point(145.0349955,-37.8096292)

LibGEOS.intersects(point_in_the_line_string, geos_line_string) ==> True

image

jaakkor2 commented 7 months ago

As points are Float64, I would buffer the line a bit

LibGEOS.intersects(nearest_point, buffer(geos_line_string,1e-12))  # true
chamikabm commented 6 months ago

@jaakkor2 , Checking with a buffer would work, but what I wanna know, is what would be the most accurate way to create such a snapped point?