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

**URL:** <https://discourse.julialang.org/t/how-to-find-a-snapped-point-to-a-line-string-from-a-given-point/111169>\
**Category:** Geo\
**Tags:** linearalgebra, math, geometry, geospatial\
**Created:** [March 5, 2024, 12:44am UTC](https://discourse.julialang.org/t/how-to-find-a-snapped-point-to-a-line-string-from-a-given-point/111169 "2024-03-05T00:44:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chamika\_Kasun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chamika_kasun/32/202926_2.png) [@Chamika\_Kasun](https://discourse.julialang.org/u/Chamika_Kasun)\
**Post date:** [March 5, 2024, 12:44am UTC](https://discourse.julialang.org/t/how-to-find-a-snapped-point-to-a-line-string-from-a-given-point/111169/1 "2024-03-05T00:44:16Z")

</div>

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

```julia
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:

```julia
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.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/9/e9a68e79fcd53c7c6caa983b2345c4a172c3e9d2.jpeg)

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 5, 2024, 12:51am UTC](https://discourse.julialang.org/t/how-to-find-a-snapped-point-to-a-line-string-from-a-given-point/111169/2 "2024-03-05T00:51:29Z")

</div>

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](https://github.com/JuliaGeo/LibGEOS.jl/issues/64)
