Map-MatchingΒΆ
[1]:
# Import libraries
import numpy as np
import pandas as pd
from pytrack.graph import graph, distance
from pytrack.analytics import visualization
from pytrack.matching import candidate, mpmatching_utils, mpmatching
[2]:
df = pd.read_excel("dataset.xlsx")
latitude = df["latitude"].to_list()
longitude = df["longitude"].to_list()
points = [(lat, lon) for lat, lon in zip(latitude[:30], longitude[:30])]
[3]:
# Create BBOX
north, east = np.max(np.array([*points]), 0)
south, west = np.min(np.array([*points]), 0)
# Extract road graph
G = graph.graph_from_bbox(*distance.enlarge_bbox(north, south, west, east, 500), simplify=True, network_type='drive')
Downloaded 448.27kB
[4]:
# Initialize maps
loc = (np.mean(latitude[:30]), np.mean(longitude[:30]))
maps = visualization.Map(location=loc, zoom_start=15)
#maps
[5]:
# Show extracted graph
maps.add_graph(G, plot_nodes=True)
maps
[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
[6]:
# Extract candidates
G_interp, candidates = candidate.get_candidates(G, points, interp_dist=5, closest=True, radius=30)
# Plot results
maps.draw_candidates(candidates, 30)
maps
[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
[7]:
# Extract trellis DAG graph
trellis = mpmatching_utils.create_trellis(candidates)
# Plot trellis graph
trellis_draw = visualization.draw_trellis(trellis, figsize=(15, 100), dpi=200)
trellis_draw
[8]:
# Perform the map-matching process
path_prob, predecessor = mpmatching.viterbi_search(G_interp, trellis, "start", "target")
[9]:
# Plot map-matching results
maps.draw_path(G_interp, trellis, predecessor)
maps
[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook