1 导入库
import osmnx as ox
import time
from shapely.geometry import Polygon
import os
import numpy as np
2 提取Openstreetmap 的graph
G=ox.graph_from_place('Huangpu,Shanghai,China',network_type='drive',simplify=True)
ox.plot_graph(G)
3 提取graph中的点和边
gdf_nodes, gdf_edges = ox.utils_graph.graph_to_gdfs(G)
4 将点和边的geoDataFrame中非数值的部分转化成字符串
gdf_nodes = ox.io._stringify_nonnumeric_cols(gdf_nodes)
gdf_nodes
gdf_edges = ox.io._stringify_nonnumeric_cols(gdf_edges)
gdf_edges
5 为每一个边赋予一个id
gdf_edges["fid"] = np.arange(0, gdf_edges.shape[0], dtype='int')
gdf_edges
6 保存至文件
gdf_nodes.to_file("nodes.shp")
gdf_edges.to_file("edges.shp")