Pandas beginner here. I have a set of coordinates of ships positions and a set of coordinates of segments that make up the ship route. I would like to know which point on the route is the closest to the ship. I then use that point to calculate the total distance from the ship location to the next point and the next till the end of the set. There is a similar question but they are slightly different problems: similar questions here and here.
SHIP LAT SHIP LNG
40.77110 14.0301
40.77110 14.0501
ROUTE 1 LAT ROUTE 1 LNG
40.74556 13.93871
40.74477 13.94019
40.75093 13.94584
40.76589 13.99974
40.77114 14.01122
40.77109 14.0301
40.76585 14.02673
I have achieved what I want with without using panda and struggling it to apply to a dataframe.
for segment in segments:
proximity = min(haversine(lng,lat, point[0], point[1]) for point in segment)
print('proximity point')
print(proximity)
for point in segment:
if haversine(lng,lat, point[0], point[1]) == proximity:
closest_point = point
point_index = segment.index(point)
print('closest point')
print(point_index)
total_distance = proximity
i = point_index
last_index = len(segment)-1
while i < last_index:
seg_distance = haversine(segment[i][0], segment[i][1], segment[i+1][0], segment[i+1][1])
print('segment distance')
print(seg_distance)
total_distance += seg_distance
i += 1