使用OSMnx查询布雷西亚OpenStreetMap数据时脚本无响应求助
解决OSMnx获取布雷西亚OpenStreetMap数据无响应的问题
以下是针对你的问题的几种实用解决方案:
1. 用OSM关系ID精准定位查询
你已经拿到了布雷西亚的OSM关系ID(45144),直接使用该ID可以避免模糊地名匹配的不确定性,精准锁定目标区域:
import osmnx as ox # 用OSM关系ID指定区域,格式为"R{relation_id}" place = "R45144" osmnx_buffer = 10000 # 单位:米 g = ox.graph.graph_from_place(place, buffer_dist=osmnx_buffer) g = ox.projection.project_graph(g) ox.plot_graph(g)
2. 启用进度反馈,确认程序运行状态
OSMnx默认不显示下载/处理进度,容易让你误以为程序无响应。启用日志和进度条,就能看到后台的工作状态:
import osmnx as ox # 配置日志输出和缓存,避免重复下载 ox.config(log_console=True, use_cache=True) place = "Brescia, Italy" osmnx_buffer = 10000 # 开启进度条显示下载进度 g = ox.graph.graph_from_place(place, buffer_dist=osmnx_buffer, progress=True) g = ox.projection.project_graph(g) ox.plot_graph(g)
3. 先测试小范围数据,排查问题
10000米的缓冲范围可能导致数据量过大,处理耗时极长。先缩小缓冲距离测试程序是否能正常运行:
import osmnx as ox place = "Brescia, Italy" osmnx_buffer = 2000 # 先测试小范围 g = ox.graph.graph_from_place(place, buffer_dist=osmnx_buffer) g = ox.projection.project_graph(g) ox.plot_graph(g)
4. 升级OSMnx及依赖库
旧版本OSMnx可能存在地理编码或查询逻辑的bug,升级到最新版本可以解决不少兼容性问题:
pip install --upgrade osmnx
5. 先获取边界多边形,再下载路网
如果graph_from_place仍有问题,可以先获取布雷西亚的边界,再基于多边形下载路网:
import osmnx as ox # 获取布雷西亚的边界多边形 place_poly = ox.geocode_to_gdf("Brescia, Italy").iloc[0]["geometry"] # 给边界添加缓冲 buffered_poly = place_poly.buffer(osmnx_buffer) # 基于多边形下载路网 g = ox.graph.graph_from_polygon(buffered_poly) g = ox.projection.project_graph(g) ox.plot_graph(g)
内容的提问来源于stack exchange,提问作者hellomynameisA
相关产品推荐
相关产品推荐

