使用OSMnx加载OSM设施显示异常,请求技术排查
问题分析与解决方法
核心问题原因
你遇到的情况主要来自三个方面:
- 边界匹配偏差:
features_from_place依赖OSM的行政边界数据,"Centrum, Rotterdam"对应的边界可能和你预期的区域不完全一致,导致部分设施不在查询范围内。 - 数据类型过滤:
features_from_place默认只返回**多边形(way/relation)类型的要素,但大量餐厅在OSM中是以点(node)**形式存在的,默认不会被抓取。 - 查询参数限制:未明确指定要获取的要素类型,导致漏抓数据。
解决步骤
明确要素类型
在调用features_from_place时,通过element_type参数指定同时获取点、线、多边形类型的数据:amenity = ox.features.features_from_place( 'centrum, Rotterdam,Netherlands', tags={'amenity': 'restaurant'}, element_type=['node', 'way', 'relation'] )这样就能包含OSM中所有标记为餐厅的点和多边形要素。
验证并调整边界范围
如果行政边界不符合预期,可以先获取边界的经纬度范围,再用features_from_bbox进行精确查询:# 先获取区域边界的bbox gdf_place = ox.geocode_to_gdf('centrum, Rotterdam,Netherlands') bbox = gdf_place.total_bounds # 格式为(min_lon, min_lat, max_lon, max_lat) # 用bbox查询设施 amenity = ox.features.features_from_bbox( north=bbox[3], south=bbox[1], east=bbox[2], west=bbox[0], tags={'amenity': 'restaurant'}, element_type=['node', 'way', 'relation'] )检查OSM数据一致性
部分区域的OSM数据可能存在标记不规范的情况,比如餐厅未正确标记amenity=restaurant,而是用了其他标签,但这种情况较少见。可以直接在OSM官网的对应区域手动验证标签情况。
修改后的完整代码示例
import osmnx as ox import matplotlib.pyplot as plt # 获取区域边界并查询所有类型的餐厅要素 gdf_place = ox.geocode_to_gdf('centrum, Rotterdam,Netherlands') bbox = gdf_place.total_bounds amenity = ox.features.features_from_bbox( north=bbox[3], south=bbox[1], east=bbox[2], west=bbox[0], tags={'amenity': 'restaurant'}, element_type=['node', 'way', 'relation'] ) # 获取道路数据 road = ox.graph.graph_from_address('Centrum, Rotterdam') # 绘图 fig, ax = plt.subplots(figsize=(10,10),dpi=120) ox.plot.plot_footprints(amenity, ax=ax, color='red', edge_color='green', edge_linewidth=1, show=False, close=False) # 单独绘制点类型的餐厅要素 amenity_nodes = amenity[amenity.geometry.type == 'Point'] amenity_nodes.plot(ax=ax, color='blue', markersize=10) ox.plot.plot_graph(road, ax=ax, node_color='#696969', node_size=5, edge_color='#A9A9A9', show=False, close=False) plt.show()
内容的提问来源于stack exchange,提问作者user203586
相关产品推荐
相关产品推荐

