Get Network From osmnx(笔记而已,以备翻阅)
import matplotlib.pyplot as plt
from osmnx import settings, config
from osmnx import geocoder, graph_from_point, graph_from_place, graph_from_address, graph_from_polygon, graph_from_xml, plot_graph
from osmnx import save_graph_shapefile, save_graphml, save_graph_xml, geometries_from_xml
from shapely.geometry import Polygon, LinearRing
# import osmnx as ox
# city = ox.geometries_from_place(query='东城区,北京,中国', tags={'building': True})
# city.plot()
# plt.show()
# # geocode the address string to a (lat, lng) point
# point = geocoder.geocode(query='景山街道,东城区,北京,中国')
# print('(lat, lng) -->', point)
# address = 'Ceramstraat, Delft, Netherlands'
# address = 'Ceramstraat'
# address = '逸群道路'
# M = graph_from_address(address=address,
# dist=1000,
# dist_type='network',
# network_type='drive',# network_type : string {"all_private", "all", "bike", "drive", "drive_service", "walk"}
# simplify=False,
# truncate_by_edge=True)# 按边截断
center_point = (39.790973, 116.5113173)
distance = 400
M = graph_from_point(
center_point,
dist=distance,# bounding box distance in meters from the center point
dist_type="bbox",# string {"bbox", "network"} if dist_type is network, find node in graph nearest to center point then truncate graph by network dist from it
network_type="all",# string {"all_private", "all", "bike", "drive", "drive_service", "walk"}
simplify=False,# if True, simplify graph topology with the `simplify_graph` function
retain_all=False,# False: retain only the largest weakly connected component.
truncate_by_edge=False,# if True, retain nodes outside boundary polygon if at least one of node's neighbors is within the polygon
clean_periphery=False,# if True, buffer 500m to get a graph larger than requested, then simplify, then truncate it to requested spatial boundaries
custom_filter=None)
plot_graph(M,
figsize=(32, 32),
bgcolor="none",# "#E1E1E1",
node_size=120,
node_color="#FF0000",
node_alpha=None,# float
node_edgecolor="none",
node_zorder=1,
edge_color="#000000",
edge_linewidth=5,
edge_alpha=None,# float
dpi=600,
save=True,
filepath='./images/graph_all.png')
save_graph_xml(M, filepath='./osm/graph_all.osm')
save_graph_shapefile(M, 'shpdata_all')
# save_graphml(M, 'graph_simp.graphml', gephi=True, encoding="utf-8")
# M = graph_from_xml('./osm/graph_simp.osm', simplify=False)
# plot_graph(M, figsize=(32, 32), node_size=20, dpi=600, save=True, filepath='./images/graph_all.png')
print('number_of_nodes:', M.number_of_nodes(), 'number_of_edges:', M.number_of_edges())
# 计算机\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settingss
M = graph_from_point(
center_point,
dist=distance,# bounding box distance in meters from the center point
dist_type="bbox",# string {"bbox", "network"} if dist_type is network, find node in graph nearest to center point then truncate graph by network dist from it
network_type="all",# string {"all_private", "all", "bike", "drive", "drive_service", "walk"}
simplify=True,# if True, simplify graph topology with the `simplify_graph` function
retain_all=False,# False: retain only the largest weakly connected component.
truncate_by_edge=False,# if True, retain nodes outside boundary polygon if at least one of node's neighbors is within the polygon
clean_periphery=False,# if True, buffer 500m to get a graph larger than requested, then simplify, then truncate it to requested spatial boundaries
custom_filter=None)
plot_graph(M,
figsize=(32, 32),
bgcolor="none",# "#E1E1E1",
node_size=120,
node_color="#FF0000",
node_alpha=None,# float
node_edgecolor="none",
node_zorder=1,
edge_color="#000000",
edge_linewidth=5,
edge_alpha=None,# float
dpi=600,
save=True,
filepath='./images/graph_simp.png')
save_graph_xml(M, filepath='./osm/graph_simp.osm')
save_graph_shapefile(M, 'shpdata_simp')
print('number_of_nodes:', M.number_of_nodes(), 'number_of_edges:', M.number_of_edges())
个人学习记录

浙公网安备 33010602011771号