在 OSMNx 中查找到最近边缘的距离时发现错误的距离 [英] Wrong distance found when finding distance to nearest edge in OSMNx

查看:56
本文介绍了在 OSMNx 中查找到最近边缘的距离时发现错误的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用 OSMNx (OpenStreetMap + NetworkX) 包计算到最近边的距离(以米为单位).这是我的代码:

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 main():
lat = 51.217309
lon = 4.418449
get_d_to_nearest_edge(lat, lon, 'node_area.graphml')

请注意,node_area.graphml"是包含此特定位置的离线地图.get_d_to_nearest_edge() 函数如下:

Note that 'node_area.graphml' is a offline map containing this specific location. The get_d_to_nearest_edge() function is given below:

def get_d_to_nearest_edge(latitude, longitude, file_name):
print("-Now the nearest edge will be found-")
G = ox.load_graphml(filename=file_name, folder='BAPenvironment')
_, _, _, distance = get_nearest_edge(G, (latitude, longitude))
print('The distance to the nearest edge is ' + to_str(distance) + ' m')

在上面的代码中,访问了函数 get_nearest_edge().该函数的代码如下:

In the code above, the function get_nearest_edge() is accessed. The code of this function 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(distance)
return geometry, u, v, distance

这与 OSMNx 函数中内置的原始函数几乎相同,但已被修改以返回距离.最后一个函数 to_str() 在这里:

This is almost the same function as the original, build-in in OSMNx function, but has been modified to return the distance too. The last function to_str() is here:

 # convert everything into the same type (np.array) and then convert it to a string 
   def to_str(var):
       return str(list(np.reshape(np.asarray(var), (1, np.size(var)))[0]))[1:-1]

输出

但是输出似乎不正确.当我在 Google 地图中验证到最近边缘的距离时,我发现这是 18.57 米.

Output

However the output seems to be incorrect. When I verify the distance to the nearest edge in Google Maps I found that this is 18.57m.

> Now the nearest edge will be found Converting node and edge attribute
> data types Loaded graph with 36 nodes and 62 edges in 0.01 seconds
> from "BAPenvironment\node_area.graphml" Created GeoDataFrame
> "unnamed_edges" from graph in 0.00 seconds Found nearest edge
> ((247670339, 247527633)) to point (51.217309, 4.418449) in 0.00 seconds
> 0.00018056587349098678 
> The distance to the nearest edge is 0.00018056587349098678 m

我刚刚添加了m",因为我认为这是以米为单位.有谁知道如何以米为单位?还是以米为单位,我的代码有错吗?

I just added the 'm' because I think this is in meters. Anyone knows how to get it in meters? Or is it in meters, and is my code wrong?

推荐答案

OSMnx 的 get_nearest_edge 确实返回了距离.请参阅文档,其中说可选返回点和最近边之间以图形坐标单位表示的距离."如果您不想以度为单位工作,只需投影您的图表:

OSMnx's get_nearest_edge does return the distance. See the docs, which say "Optionally return the distance in graph’s coordinates’ units between the point and the nearest edge." If you don't want to work in degrees as your units, just project your graph:

import osmnx as ox
from shapely.geometry import Point
ox.config(use_cache=True, log_console=True)

# lat-long point
point = 34.081076, -118.351811
G = ox.graph_from_point(point, network_type='drive')

# project the graph (and point) to a meter projection
Gp = ox.project_graph(G)
point_geom_proj, crs = ox.projection.project_geometry(Point(reversed(point)), to_crs=Gp.graph['crs'])
x, y = point_geom_proj.x, point_geom_proj.y

# find nearest edge as (u, v, key) and distance to it
u, v, key, dist = ox.get_nearest_edge(Gp, (y, x), return_dist=True)
dist # 40.2 meters

这篇关于在 OSMNx 中查找到最近边缘的距离时发现错误的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆