Hello I\'m trying to find the distance to the nearest edge, in meters, using the OSMNx (OpenStreetMap + NetworkX) package. This is my code:
def m
I was able to solve this by changing the Ox.Point function. I wrote a new function to calculate the haversine distance to each coordinate in the LineString object.
from haversine import haversine, Unit
def closest_point_on_haversine(point,edge):
closest_point = None
for c in edge[0].coords:
if closest_point == None:
closest_point = haversine(tuple(reversed(point)), c, unit='m')
else:
if haversine(tuple(reversed(point)), c, unit='m') < closest_point:
closest_point = haversine(tuple(reversed(point)), c, unit='m')
return closest_point
def get_nearest_edge_with_dist(G, point):
start_time = time.time()
gdf = graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
graph_edges = gdf[["geometry", "u", "v"]].values.tolist()
edges_with_distances = [
(
graph_edge,
# Point(tuple(reversed(point))).distance(graph_edge[0])
closest_point_on_haversine(point,graph_edge)
)
for graph_edge in graph_edges
]
edges_with_distances = sorted(edges_with_distances, key=lambda x: x[1])
closest_edge_to_point, distance = edges_with_distances[0]
geometry, u, v = closest_edge_to_point
return geometry, u, v, distance