【问题标题】:How to find the distance to the nearest edge on a MultiDiGraph using OSMNx如何使用 OSMNx 查找到 MultiDiGraph 上最近边缘的距离
【发布时间】:2020-04-16 23:22:58
【问题描述】:

我试图在 MultiDiGraph 中找到从给定位置到最近边和最近节点的距离。 例如用latitude = 51.217220longitude = 4.418535 指定的位置。首先,我们在半径为 350m 的那个位置合成一个MultiDiGraph。代码如下:

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

OSMNx 包对我的项目有 2 个有价值的功能:get_nearest_edge()get_nearest_node()get_nearest_node() 找到最近的节点并返回它的 ID。它还有一个内置选项可以返回到这个最近节点的距离:

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

get_nearest_edge 找到最近的边并返回定义边的 2 个节点的 ID(每条边由 2 个连接的节点组成)。但是没有内置选项可以获取到此边缘的距离。

如何计算从给定位置到最近边缘的距离?

【问题讨论】:

    标签: python networkx openstreetmap osmnx


    【解决方案1】:

    是的,我的 head 函数是 get_d_to_nearest_edge()。这段代码如下:

    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)
    

    在这个函数中,我调用了 get_nearest_edge() 方法,但是它被重写为 @Sparky05 之前告诉我的。代码如下:

    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
    

    问题是,当我在 get_nearest_edge() 函数中检查类型时,类型是浮点数,就像@Sparky05 所说的那样,找到了距离。但是,当我将此值返回给我的头函数 get_d_to_nearest_edge() 时,该值是一个元组,不能打印为浮点数。

    但更重要的是,我发现的距离完全不正确。输出:0.00025593968794827235,实际距离应该是26.13米,有帮助吗?

    【讨论】:

    • 您不应该使用答案来添加此新代码,而是修改了您的问题(或打开了一个新问题):在您的 get_d_to_nearest_edge 方法中出现错误。如果您只对距离感兴趣,这应该是_, _, _, distance = get_nearest_edge(...)。对于你的第二个点的距离值,记住返回的值是 GPS 位置(经度、纬度)之间的距离,可能你需要搜索转换。
    • 对不起,我是这个论坛的新手,不知道如何使用它。我将使用您的反馈来解决我未来的问题。现在回到代码......好吧,我只需要找到从给定位置到最近边缘的距离,所以我猜距离垂直于边缘。我已经在使用 (lon,lat) 了,为什么还需要另一个转换?
    • 你需要检查ox.Point(..).distance计算出的距离,然后你就知道返回值是什么意思了。如果您已经尝试过但仍未找到答案,请在此处使用新问题打开一个新问题。 Stackoverflow 试图针对每个问题专注于思考一个问题,以便其他有类似问题的人(而不是很多类似的问题;))找到答案。
    • @Sparky05 我在这里开了一个新问题:stackoverflow.com/questions/61344467
    【解决方案2】:

    您可以创建原始get_nearest_edge 方法的副本,并在该方法的本地副本中,只需将line 247 替换为:

    closet_edge_to_point, distance = edges_with_distances[0]
    

    和返回语句

    return geometry, u, v, distance
    

    您也可以在osmnx 的 GitHub 页面上为此创建一个问题,并将其作为新功能提出。

    【讨论】:

    • 感谢您的回复。但是,在执行此操作时,在打印距离时,屏幕上不会出现任何内容。任何想法为什么?
    • 检查结果的类型 (print(type(distance)) - 它应该是浮点数。或者尝试将打印添加到整个结果列表中 (print(edges_with_distances))
    猜你喜欢
    • 2020-08-04
    • 2020-01-28
    • 2020-05-10
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2021-09-16
    • 2021-12-06
    相关资源
    最近更新 更多