geopandas:将事故点与最近的道路相匹配
我有以下地理数据框: 事故_c = 奥地利所有事故的集合作为点, 街道 = 奥地利的道路作为线串(开放街道地图数据) 两者都有 crs 类型 epsg:3310
现在我想将每起事故与最近的道路相匹配。我的第一次尝试是这样的:
def nearest_street(accident_point, streets):
row_canditates=streets.copy()
nearest_road = None
min_distance = None
row_canditates["distance_road"] = row_canditates.apply(lambda row: accident_point["geometry"].distance(row.geometry),axis=1)
min_distance = row_canditates["distance_road"].min()
min_road = row_canditates.loc[row_canditates["distance_road"] == min_distance]
nearest_road = min_road["osm_id"].values[0]
return nearest_road, min_distance
accidents_c["nearest_road"], accidents_c["distance_road"] = zip(*accidents_c.apply(nearest_street, streets=roads, axis=1))
这有效,但需要很长时间。所以我就在想一个办法,只包括距离事故点不超过3000米的道路,让速度更快。 为此我使用了缓冲方法。并这样做了:
def nearest_street(accident_point, streets):
row_canditates=streets.copy()
nearest_road = None
min_distance = None
buffered_accident = accident_point["geometry"].buffer(2000)
bounds = buffered_accident.bounds
x_min, x_max, y_min, y_max = buffered_accident.bounds
row_canditates=row_canditates.cx[x_min:x_max, y_min:y_max]
row_canditates["distance_road"] = row_canditates.apply(lambda row: accident_point["geometry"].distance(row.geometry),axis=1)
min_distance = row_canditates["distance_road"].min()
min_road = row_canditates.loc[row_canditates["distance_road"] == min_distance]
nearest_road = min_road["osm_id"].values[0]
return nearest_road, min_distance
accidents_c["nearest_road"], accidents_c["distance_road"] = zip(*accidents_c.apply(nearest_street, streets=roads, axis=1))
这工作得更快,但结果更糟。代码 a 和代码 b 之间的差异有时会超过 1000 米。您认为代码中的问题出在哪里? 您知道有什么更好的方法来限制对最近环境的搜索吗?
I have the following GeoDataFrames:
accidents_c = collection of all accidents in austria as points,
streets = roads in austria as a linestrings (open street map data)
Both have the crs type epsg:3310
Now i want to match every accident to the nearest road. My first attempt was this:
def nearest_street(accident_point, streets):
row_canditates=streets.copy()
nearest_road = None
min_distance = None
row_canditates["distance_road"] = row_canditates.apply(lambda row: accident_point["geometry"].distance(row.geometry),axis=1)
min_distance = row_canditates["distance_road"].min()
min_road = row_canditates.loc[row_canditates["distance_road"] == min_distance]
nearest_road = min_road["osm_id"].values[0]
return nearest_road, min_distance
accidents_c["nearest_road"], accidents_c["distance_road"] = zip(*accidents_c.apply(nearest_street, streets=roads, axis=1))
This works but takes forever. So i was thinking about a way to make it faster by only including roads which are not more than 3000 meters away from the accident point.
For this i used the buffer method. And did this:
def nearest_street(accident_point, streets):
row_canditates=streets.copy()
nearest_road = None
min_distance = None
buffered_accident = accident_point["geometry"].buffer(2000)
bounds = buffered_accident.bounds
x_min, x_max, y_min, y_max = buffered_accident.bounds
row_canditates=row_canditates.cx[x_min:x_max, y_min:y_max]
row_canditates["distance_road"] = row_canditates.apply(lambda row: accident_point["geometry"].distance(row.geometry),axis=1)
min_distance = row_canditates["distance_road"].min()
min_road = row_canditates.loc[row_canditates["distance_road"] == min_distance]
nearest_road = min_road["osm_id"].values[0]
return nearest_road, min_distance
accidents_c["nearest_road"], accidents_c["distance_road"] = zip(*accidents_c.apply(nearest_street, streets=roads, axis=1))
This works much faster but the results are worse. Differences are sometimes over 1000 meters between code a and code b. Where do you think is the problem in the code?
Do you know any better method to limit the search for the nearest environment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望 https://osmnx.readthedocs.io/en /stable/osmnx.html#osmnx.distance.nearest_edges 会表现得更好。下面显示了使用英国事故数据和英国城市的完整示例。同样的方法也适用于奥地利。只需参考事故数据并选择您要使用的区域的多边形。
I would expect https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.distance.nearest_edges would perform better. Below shows full example of using UK accident data and a UK city. Same approach would work for Austria. Just need reference to accident data and choose a polygon that is area you want to use.
我想我发现了错误。
必须是这个
这就是结果如此糟糕的原因。
但谢谢你的帮助,罗布·雷蒙德。
i think i found the mistake.
needs to be this one
This was the reason why results were so bad.
But thanks for your help Rob Raymond.