I want to check if a shapely Point is part of a route obtained by the shortest_path() function of networkx for the city of Aachen, Germany.
I want to go from Node 7192074686 to node 32885719. In 2022, an accident occured at the pedestrian crossing at Jülicher Straße / Lombardenstraße / Dennewartstraße, POINT (10.77901 53.90290) (WSG84).
How can I check if the given point is part of the route?
So far, my solution would be to check the nearest node of the point and calculate the distance to each node returned by the nx.shortest_path() function of networkx. However, as the nodes are not exactly the same, I would have to work with threshholds, and this leads to more problems: a) What should the treshhold be? This depends on how dense the nodes in the network are, and b) 2 or more nodes in the route could be linked to one accident
Any help is greatly appreciated!
Below is the code for obtaining the network and calculating the shortest path:
import networkx as nx
import osmnx as ox
# get original network
network = ox.graph_from_place("Aachen, Germany", network_type="walk")
# set start and end nodes
start_node = 7192074686
end_node = 32885719
# calculate the path
path_nodes = nx.shortest_path(G=network, source=start_node, end=end_node, weight="length")
I want to check if a shapely Point is part of a route
If you want to determine whether a point is part of a route, you want to intersect the point geometry with the (buffered) route geometry:
import osmnx as ox
from shapely.geometry import Point
G = ox.graph_from_place("Aachen, Germany", network_type="walk")
# solve shortest path
weight = "length"
route = ox.shortest_path(G, 7192074686, 32885719, weight)
# get the route geometry, buffered out to 10 meters (an arbitrary threshold)
gdf = ox.utils_graph.route_to_gdf(G, route, weight)
route_geom_proj, crs = ox.projection.project_geometry(gdf.unary_union)
route_geom_proj_buff = route_geom_proj.buffer(10)
route_geom_buff, _ = ox.projection.project_geometry(route_geom_proj_buff, crs=crs, to_latlong=True)
# does the point geometry intersect the route geometry?
Point(10.77901, 53.90290).intersects(route_geom_buff) # False
That said, your x-y point (10.77901, 53.90290)
is out near Lübeck and nowhere near Aachen.