简体   繁体   中英

How to find the distance to the nearest edge on a MultiDiGraph using OSMNx

I'm trying to find the distance in a MultiDiGraph from a given location to the closest edge and closest node. For example the specified location with latitude = 51.217220 and longitude = 4.418535 . Firstly we compose a MultiDiGraph at that location with a radius of 350m. Here is the code:

import osmnx as ox
lat = 51.217220
lon = 4.418535
G = ox.graph_from_point((lat, lon), network_type='drive', distance=350, simplify=True)

The OSMNx package has 2 valuable functions for my project: get_nearest_edge() and get_nearest_node() . get_nearest_node() finds the nearest node and returns it's ID. It also has an built-in option to return the distance to this nearest node:

ox.get_nearest_node(G, (lat,lon), method='haversine', return_dist=True) 

get_nearest_edge finds the nearest edge and returns the ID's of the 2 nodes that define the edge (each edge consists of 2 nodes being connected). However there's is no built-in option to get the distance to this edge.

How can I calculate the distance from the given location to the nearest edge?

You can create a copy of the original get_nearest_edge method and in your local copy of the method, you only need to replace line 247 with:

closet_edge_to_point, distance = edges_with_distances[0]

and the return statement with

return geometry, u, v, distance

You could also create an issue for this at the GitHub page of osmnx and propose this as new feature.

Yes so, my head function is get_d_to_nearest_edge(). This code is given below:

def get_d_to_nearest_edge(latitude, longitude, file_name):
    # get nearest node incident to nearest edge to reference point
    G = ox.load_graphml(filename=file_name, folder='BAPenvironment')
    distance = get_nearest_edge(G, (latitude, longitude))
    print(type(distance))
    print(distance)

In this function I call the method get_nearest_edge(), however it's rewritten as @Sparky05 told me before. The code is below:

def get_nearest_edge(G, point):
    """
    Return the nearest edge to a pair of coordinates. Pass in a graph and a tuple
    with the coordinates. We first get all the edges in the graph. Secondly we compute
    the euclidean distance from the coordinates to the segments determined by each edge.
    The last step is to sort the edge segments in ascending order based on the distance
    from the coordinates to the edge. In the end, the first element in the list of edges
    will be the closest edge that we will return as a tuple containing the shapely
    geometry and the u, v nodes.
    Parameters
    ----------
    G : networkx multidigraph
    point : tuple
        The (lat, lng) or (y, x) point for which we will find the nearest edge
        in the graph
    Returns
    -------
    closest_edge_to_point : tuple (shapely.geometry, u, v)
        A geometry object representing the segment and the coordinates of the two
        nodes that determine the edge section, u and v, the OSM ids of the nodes.
    """
    start_time = time.time()

    gdf = ox.graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
    graph_edges = gdf[["geometry", "u", "v"]].values.tolist()

    edges_with_distances = [
        (
            graph_edge,
            ox.Point(tuple(reversed(point))).distance(graph_edge[0])
        )
        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

    ox.log('Found nearest edge ({}) to point {} in {:,.2f} seconds'.format((u, v), point, time.time() - start_time))
    print(type(distance))
    print(distance)
    return geometry, u, v, distance

The thing is, when I check the type within the get_nearest_edge() function, the type is float, just like @Sparky05 said and the distance is found. But however when I return this values to my head function get_d_to_nearest_edge() the value is a tuple, and can't be printed as a float.

However what's even more important is that the distance that I find is totally incorrect. The output: 0.00025593968794827235 and the real distance should be 26.13 meters, any help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM