Issues with draw_path function: Lines not aligned with roads
bodong66 opened this issue · comments
Description:
I've been using the PyTrack
package for map matching and encountered an issue when visualizing the results using the draw_path
function. Some of the plotted lines are not aligned with the roads, leading to a visually inaccurate representation.
Expected behavior:
The plotted path should align accurately with the roads on the map, reflecting the correct map-matched result.
Actual behavior:
Some lines are plotted off the roads, suggesting an issue with either the map matching or the visualization.
Screenshots:
![image](https://private-user-images.githubusercontent.com/69793004/267672552-3d683bfb-8718-4a84-8988-e13c789d9a11.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzMDg3MDEsIm5iZiI6MTcyNDMwODQwMSwicGF0aCI6Ii82OTc5MzAwNC8yNjc2NzI1NTItM2Q2ODNiZmItODcxOC00YTg0LTg5ODgtZTEzYzc4OWQ5YTExLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIyVDA2MzMyMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTVkM2MyZmI2ODkyYWQ0NjJiM2I5MjQxNWNjMGFkNjA5ZGQzNGQyOTZmYTU1MzBiNzQyMTk2ZDM0Y2JlZTIzY2YmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.BQkkAKpRF2BbnGhzYFSL17pFfIPZsBWsE5vR57w4mW0)
I hope this can be addressed soon, and I'd appreciate any guidance or workarounds in the meantime. Thank you for your hard work on this package!
This is strange behaviour. I think it is because the points are very spread out. Is it possible to get the simulation data to reproduce the error?
See you soon
Hello,
I apologize for the delay in responding; I have been quite busy and, unfortunately, overlooked the GitHub issues. Attached, please find the files necessary for reproduction.
Thank you for your patience.
code snippet:
import pandas as pd
import numpy as np
from pytrack.graph import graph, distance
from pytrack.analytics import visualization
from pytrack.matching import candidate, mpmatching_utils, mpmatching
import matplotlib.pyplot as plt
df = pd.read_csv("reproduce.csv")
# Extract latitude and longitude directly as a list of tuples
points = [(lat, lon) for lat, lon in zip(df["latitude"], df["longitude"])]
# Calculate the bounding box using numpy functions
north, east = np.max(points, axis=0)
south, west = np.min(points, axis=0)
# Extract road network graph
G = graph.graph_from_bbox(*distance.enlarge_bbox(north, south, west, east, 500), simplify=False, network_type='drive')
# Visualize the extracted graph on a map centered around the mean location
maps = visualization.Map(location=(np.mean(df["latitude"]), np.mean(df["longitude"])))
maps.add_graph(G, plot_nodes=False)
# Extract and plot candidates
G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=30)
maps.draw_candidates(candidates, 30)
# Extract trellis DAG graph
trellis = mpmatching_utils.create_trellis(candidates)
trellis_draw = visualization.draw_trellis(trellis, figsize=(15, 100), dpi=100)
plt.show()
# Perform the map-matching process
path_prob, predecessor = mpmatching.viterbi_search(G_interp, trellis, "start", "target")
# Plot map-matching results
maps.draw_path(G_interp, trellis, predecessor)
maps.save("reproduce.html")
Try changing the parameters of graph.graph_from_bbox and candidate.get_candidates as follows:
import pandas as pd
import numpy as np
from pytrack.graph import graph, distance
from pytrack.analytics import visualization
from pytrack.matching import candidate, mpmatching_utils, mpmatching
import matplotlib.pyplot as plt
df = pd.read_csv("reproduce.csv")
# Extract latitude and longitude directly as a list of tuples
points = [(lat, lon) for lat, lon in zip(df["latitude"], df["longitude"])]
# Calculate the bounding box using Numpy functions
north, east = np.max(points, axis=0)
south, west = np.min(points, axis=0)
# Extract road network graph
G = graph.graph_from_bbox(*distance.enlarge_bbox(north, south, west, east, 500), simplify=True, network_type='drive')
# Visualize the extracted graph on a map centered around the mean location
maps = visualization.Map(location=(np.mean(df["latitude"]), np.mean(df["longitude"])))
maps.add_graph(G, plot_nodes=False)
# Extract and plot candidates
G_interp, candidates = candidate.get_candidates(G, points, interp_dist=20, closest=True, radius=30)
maps.draw_candidates(candidates, 30)
# Extract trellis DAG graph
trellis = mpmatching_utils.create_trellis(candidates)
trellis_draw = visualization.draw_trellis(trellis, figsize=(15, 100), dpi=100)
plt.show()
# Perform the map-matching process
path_prob, predecessor = mpmatching.viterbi_search(G_interp, trellis, "start", "target", beta=3, sigma=5))
# Plot map-matching results
maps.draw_path(G_interp, trellis, predecessor)
maps.save("reproduce.html")
In this way, I obtain the following results:
If it is no problem, remember to leave a star for the repository.