How to find a snapped point to a line string from a given point?

I have a line string as follows, consisting of a few points (longitude/latitude) coordinates in Paris. The task is to find the snapped point to the given line and a 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 follow, 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

It would be great if someone could tell me what the issue is with this code and if there’s a better way to accomplish the same thing.

Trying to intersect a line and a point specified with floating point numbers can suffer from accuracy issues. Did you check the distance between the line and the point? (perhaps it is less than a reasonable epsilon)

After looking at LibGEOS github, there are some issues relating to these accuracy problems. The solution seems to be using setPrecision function to make the intersects and touches predicates return the desired results.

See this issue: precision model or setPrecision · Issue #64 · JuliaGeo/LibGEOS.jl · GitHub