How to calculate distance between two points along the LineString provided

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:

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:

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]]

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.
1 Like

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?

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.

1 Like

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