# How to calculate distance between two points along the LineString provided

**URL:** https://discourse.julialang.org/t/how-to-calculate-distance-between-two-points-along-the-linestring-provided/106023
**Category:** Geo
**Tags:** geo, geometry, osm, geospatial
**Created:** [November 10, 2023, 2:10am UTC](https://discourse.julialang.org/t/how-to-calculate-distance-between-two-points-along-the-linestring-provided/106023 "2023-11-10T02:10:39Z")
**Posts on this page:** 5
**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 10, 2023, 2:10am UTC](https://discourse.julialang.org/t/how-to-calculate-distance-between-two-points-along-the-linestring-provided/106023/1 "2023-11-10T02:10:40Z")

</div>

I have a line string and two points with me. I need to understand how to calculate the distance between two points along the provided line string. Also, note that in some cases, the provided points can be the same as the points in the line strings.

Case 1:

```julia
point_1 = [145.01335373083108, -37.83531345902471]
point_2 = [145.0133087,-37.8355784]
line_string = [[145.0123086, -37.8323874], [145.0124038, -37.8323978], [145.0127101, -37.8324314], [145.0130083, -37.8324631], [145.0131113, -37.8325173], [145.0131586, -37.8325914], [145.0135326, -37.8332436], [145.0136158, -37.833392], [145.0136377, -37.8334482], [145.0136437, -37.8335063], [145.0136287, -37.833663], [145.0135595, -37.8340508], [145.013449, -37.8346943], [145.0134461, -37.8347113], [145.0134418, -37.8347953], [145.0133087, -37.8355784], [145.0132317, -37.8360771], [145.0132176, -37.8361681]]

```

Case 2:

```julia
point_1 = [145.0135595,-37.8340508]
point_2 = [145.0133087,-37.8355784]
line_string = [[145.0123086, -37.8323874], [145.0124038, -37.8323978], [145.0127101, -37.8324314], [145.0130083, -37.8324631], [145.0131113, -37.8325173], [145.0131586, -37.8325914], [145.0135326, -37.8332436], [145.0136158, -37.833392], [145.0136377, -37.8334482], [145.0136437, -37.8335063], [145.0136287, -37.833663], [145.0135595, -37.8340508], [145.013449, -37.8346943], [145.0134461, -37.8347113], [145.0134418, -37.8347953], [145.0133087, -37.8355784], [145.0132317, -37.8360771], [145.0132176, -37.8361681]]

```

 ![Screenshot 2023-11-10 at 12.52.53 pm](https://global.discourse-cdn.com/julialang/original/3X/f/3/f3a6ad90df05aa61cab8be04461e0796b4d2ae89.png)

---

<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: [November 10, 2023, 3:15am UTC](https://discourse.julialang.org/t/how-to-calculate-distance-between-two-points-along-the-linestring-provided/106023/2 "2023-11-10T03:15:13Z")

</div>

A breakdown of this problem could be to:

1. find nearest point on Line String from Point 1 (also called projecting Point 1 to the Line String), call it A.
2. find nearest point on Line String from Point 2, call it B.
3. add length of partial segment from A in direction of Point 2.
4. add length of full segments of Line String until segment containing point B.
5. add length of partial segment towards point B.
6. return sum of lengths.

---

<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 10, 2023, 3:50am UTC](https://discourse.julialang.org/t/how-to-calculate-distance-between-two-points-along-the-linestring-provided/106023/3 "2023-11-10T03:50:54Z")

</div>

This is exactly what I thought as well, but the issue I’m having is how to decide which side the nearest point is to the given point. That’s another logic I need to run, isn’t it?

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 10, 2023, 9:33am UTC](https://discourse.julialang.org/t/how-to-calculate-distance-between-two-points-along-the-linestring-provided/106023/4 "2023-11-10T09:33:31Z")

</div>

Do 1. and 2. above and next compute the accumulated distance along the road.  
mapproject has an option that reports in which line segment will be point A and B, otherwise you’ll have to do it yourself by computing the closest point in line for each segment individually and pick the segment based on the smallest distance from point to line.

---

<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 12, 2023, 11:11pm UTC](https://discourse.julialang.org/t/how-to-calculate-distance-between-two-points-along-the-linestring-provided/106023/5 "2023-11-12T23:11:11Z")

</div>

Thanks mate, yes that’s what I’m doing now.
