When determining the ordering of the track that has been input by the user there is some issues with the ordering of the segments of the track. This arises due to a comparison of the point locations where there is floating point number issues.
Added below is a catch term that corrects this bug by determining the distance between the end point of the segment and all possible next segments. This correction then allows for a small floating point number issue
from shapely import distance
def order_track(df, track_points):
"""
Order crossing points into a track along the route
Args:
df (DataFrame): Route info in dataframe format
track_points (dict): Dictionary of crossing points and cell ids
Returns:
user_track (DataFrame): DataFrame of ordered crossing points and cell ids
"""
start_point = Point(df.iloc[0]['Long'], df.iloc[0]['Lat'])
end_point = Point(df.iloc[-1]['Long'], df.iloc[-1]['Lat'])
pathing = True
track_id = np.where(track_points['startPoints'] == start_point)[0][0]
path_point = []
cell_ids = []
# Loop through crossing points to order them into a track along the route
while pathing:
print(track_id)
start_point_segment = track_points['startPoints'].iloc[track_id]
end_point_segment = track_points['endPoints'].iloc[track_id]
path_point.append(start_point_segment)
cell_ids.append(track_points['cellID'].iloc[track_id])
if len(track_points['midPoints'].iloc[track_id]) != 0:
for midpnt in track_points['midPoints'].iloc[track_id]:
path_point.append(midpnt)
cell_ids.append(track_points['cellID'].iloc[track_id])
if distance(end_point_segment,end_point) < 1e-10:
pathing = False
else:
track_id = np.argmin([distance(entry,end_point_segment) for entry in track_points['startPoints']])
track_misfit = min([distance(entry,end_point_segment) for entry in track_points['startPoints']])
if track_misfit >= 1e-10:
raise Exception('Path Segmentment not adding - ID={},Misfit={},distance from end={}'.format(track_id,track_misfit,distance(end_point_segment,end_point)))
user_track = pd.DataFrame({'Point': path_point, 'CellID': cell_ids})
return user_track
When determining the ordering of the track that has been input by the user there is some issues with the ordering of the segments of the track. This arises due to a comparison of the point locations where there is floating point number issues.
Added below is a catch term that corrects this bug by determining the distance between the end point of the segment and all possible next segments. This correction then allows for a small floating point number issue