使用Interpolate Points函数细分街道的技术疑问与实现求助
问题解答:按最大距离细分街道及Interpolate Points函数说明
为什么interpolate_points返回generator?
ox.utils_geo.interpolate_points返回的是生成器对象,这是为了在处理大规模路网时节省内存,避免一次性生成所有点占用过多资源。要得到可直接使用的点列表,只需将生成器转换为列表,例如:
# 对单个线段插值,得到点列表 point_list = list(ox.utils_geo.interpolate_points(linestring_geometry, 10))
完整街道细分与图重建实现
以下是实现按指定距离细分街道并重建路网图的完整代码:
# 安装依赖 !pip install osmnx !pip install momepy # 导入库 import osmnx as ox import pandas as pd import geopandas as gpd import momepy import networkx as nx import matplotlib.pyplot as plt # 目标区域 place_name = "Macon, Macon County, Illinois, United States" # 获取步行路网图 graph = ox.graph_from_place(place_name, network_type="walk") fig, ax = ox.plot_graph(graph) # 提取路网的节点和边GeoDataFrame nodes, edges = ox.graph_to_gdfs(graph) # 获取所有边的几何对象列表 geometry_list = edges['geometry'].tolist() # 生成细分后的边线段 new_edges = [] for geom in geometry_list: # 对每条边按指定距离插值(这里用0.0005,对应约50米,可根据需求调整) interpolated_points = list(ox.utils_geo.interpolate_points(geom, 0.0005)) # 用相邻插值点构建新线段 for i in range(len(interpolated_points)-1): new_edge = ox.utils_geo.LineString([interpolated_points[i], interpolated_points[i+1]]) new_edges.append(new_edge) # 将新线段转为GeoDataFrame gdf_new_edges = gpd.GeoDataFrame({'geometry': new_edges}, geometry='geometry', crs=3528) # 将GeoDataFrame转为NetworkX图 G_primal = momepy.gdf_to_nx(gdf_new_edges, approach="primal") # 可视化对比 f, ax = plt.subplots(1, 2, figsize=(12, 6), sharex=True, sharey=True) gdf_new_edges.plot(color="k", ax=ax[0]) for i, facet in enumerate(ax): facet.set_title(("细分后街道", "重建路网图")[i]) facet.axis("off") nx.draw(G_primal, {n: [n[0], n[1]] for n in list(G_primal.nodes)}, ax=ax[1], node_size=50) plt.show()
关键说明
- 插值距离的单位:若输入的几何对象用WGS84坐标系(EPSG:4326),距离参数的单位是度;若已转换为米制坐标系(如UTM),则单位是米。
- 用
momepy.gdf_to_nx可以将线段GeoDataFrame转为NetworkX图,方便后续路网分析。
内容的提问来源于stack exchange,提问作者Avery Fischer
相关产品推荐
相关产品推荐

