# Finding the nearest way (line string) to a given point

**URL:** <https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882>\
**Category:** Specific Domains\
**Tags:** linearalgebra, math, geometry, geospatial\
**Created:** [November 7, 2023, 1:34am UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882 "2023-11-07T01:34:06Z")\
**Posts on this page:** 7\
**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:** [November 7, 2023, 1:34am UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882/1 "2023-11-07T01:34:06Z")

</div>

I have got a few ways (i.e line strings) and a point, using the point, I need to find the closest way to a given point. How do I do that.

Ignore the grey box and the small red circle in the image.

 ![bb](https://global.discourse-cdn.com/julialang/original/3X/a/5/a515bff3ce427ea76d16ac1704553f863e7755d1.jpeg)

```julia
ways_map = Dict(
"OSM&&WAY10592122" => [[145.0352298,-37.8374751],[145.0349665,-37.8374204],[145.0347479,-37.8373876],[145.0344542,-37.8373537],[145.0342236,-37.8373346],[145.034013,-37.837324],[145.0338078,-37.8373198],[145.0335839,-37.8373219],[145.0333626,-37.8373304],[145.0331507,-37.8373431],[145.0329643,-37.8373621],[145.032747,-37.8373886],[145.0325713,-37.837414],[145.0323943,-37.8374469],[145.0321448,-37.8374988],[145.0318029,-37.837575],[145.0315467,-37.8376343],[145.0312879,-37.8376894],[145.0310827,-37.8377339],[145.0308949,-37.8377678],[145.0306723,-37.8378038],[145.0304939,-37.837826],[145.0303169,-37.8378451],[145.030128,-37.8378619],[145.0299588,-37.8378674],[145.029759,-37.8378684],[145.0295414,-37.8378635],[145.0293322,-37.8378507],[145.0291689,-37.8378332],[145.0289908,-37.8378107],[145.0287668,-37.8377738],[145.0284598,-37.837689],[145.0281995,-37.8376017],[145.0280017,-37.8375254],[145.0277925,-37.8374397],[145.0276122,-37.8373553],[145.0274176,-37.8372527],[145.0272614,-37.8371596],[145.0271053,-37.8370584],[145.0269363,-37.8369281],[145.026669,-37.8367093],[145.0265103,-37.8365467],[145.0263191,-37.8363432],[145.0259387,-37.8359123],[145.0254405,-37.8353373],[145.0248245,-37.8346331],[145.024076,-37.8337694]],
"OSM&&WAY10592129" => [[145.0242202,-37.833699],[145.0249771,-37.8345629],[145.0255818,-37.8352643],[145.0261807,-37.8359464],[145.0263563,-37.8361408],[145.0266897,-37.8364842],[145.0269932,-37.8367649],[145.0271473,-37.8368942],[145.0274437,-37.8370893],[145.0276962,-37.8372252],[145.0279431,-37.8373415],[145.0282033,-37.8374456],[145.0284439,-37.8375247],[145.0286924,-37.837598],[145.0289724,-37.8376538],[145.0292708,-37.8377009],[145.0295253,-37.8377236],[145.0297765,-37.8377265],[145.0300567,-37.8377233],[145.0303277,-37.8377053],[145.0305851,-37.8376725],[145.0308601,-37.837629],[145.031135,-37.8375761],[145.0315011,-37.8374903],[145.031764,-37.8374268],[145.0321489,-37.837341],[145.0324144,-37.8372869],[145.032684,-37.8372425],[145.0329562,-37.8372064],[145.0332164,-37.8371842],[145.033499,-37.8371691],[145.0337609,-37.8371651],[145.0340264,-37.8371715],[145.0342987,-37.8371863],[145.0345682,-37.8372086],[145.0348405,-37.8372467],[145.0350497,-37.8372816],[145.0352732,-37.8373251]],
"OSM&&WAY756852509" => [[145.0290871,-37.834018],[145.0289588,-37.8340363],[145.028506,-37.8339769],[145.0283699,-37.8339298]],
"OSM&&WAY756852510" => [[145.0283699,-37.8339298],[145.0285162,-37.8339201],[145.0289616,-37.8339774],[145.0290871,-37.834018]]
)

point_input = [
          145.02898273876292,
          -37.83403333426016
        ]

output :
OSM&&WAY756852509

```

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [November 7, 2023, 2:24am UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882/2 "2023-11-07T02:24:59Z")

</div>

_Melbourne, by the river, 2 kms, I’ll take it_

 ![](https://global.discourse-cdn.com/julialang/original/3X/e/a/ea94c5e982c2f16b7647675c3cd6fa7be1323793.png)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [November 7, 2023, 6:23am UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882/3 "2023-11-07T06:23:04Z")

</div>

What’s this a reference to??

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 7, 2023, 7:38am UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882/4 "2023-11-07T07:38:52Z")

</div>

Is this a Julia question or are you asking about an algorithm? What have you tried? Why hasn’t it worked?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [November 7, 2023, 7:43am UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882/5 "2023-11-07T07:43:30Z")

</div>

> [@jd-foster](#):
>
> What’s this a reference to?

rainbolt, the guy who’s memorized Google Maps…

---

<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:** [November 7, 2023, 8:46am UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882/6 "2023-11-07T08:46:00Z")

</div>

Looking for a algorithm + julia solution to the above problem.

What I’ve tried.

1. Calculate the nearest point to the each way

```julia
geos_point = LibGEOS.Point(point_to_find_nearest[1], point_to_find_nearest[2])
 geos_line_string = LibGEOS.LineString(line_string)
point_on_linestring, point_on_point = LibGEOS.nearestPoints(geos_line_string, geos_point)

```

1. Calculate the distance from each point on the way to the given point using `haversine`

```julia
function get_point_distance(
    point1::Vector{Float64},
    point2::Vector{Float64},
)::Float64
    return haversine(point1[2], point1[1], point2[2], point2[1])
end

```

1. Then find the linestring which creates the shortest distance between the given point and the point on the line string.

Not sure, whether this is the correct approach or the optimum way to achieve this, but it seems to be producing the results which I wanted.

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [November 7, 2023, 12:57pm UTC](https://discourse.julialang.org/t/finding-the-nearest-way-line-string-to-a-given-point/105882/7 "2023-11-07T12:57:51Z")

</div>

A term of art for this type of problem is “route planning” algorithms.

Of course there is a lot involved if your plans involve constraints  
like staying on a road or path, going around obstacles,…
