在Python中,我们可以使用函数osmnx.graph_from_place()
和过滤器custom_filter='["waterway"="river"]'
来获得经过过滤的图形。
import osmnx as ox
G = ox.graph_from_place("isle of man", custom_filter='["waterway"="river"]') # download directly.
fig, ax = ox.plot_graph(G, node_color='r')
我想从我的磁盘中从一个OSM格式的XML文件中获得一个过滤过的图形,但是函数xml()不支持参数custom_filter
。如何从*.osm数据中获取经过过滤的图形?
这将只绘制整个*.osm数据集:
import osmnx as ox
G = ox.graph_from_xml("isle-of-man-latest.osm") # from disk.
fig, ax = ox.plot_graph(G, node_color='r')
发布于 2020-08-18 15:42:08
OSMnx的custom_filter
参数允许您使用OverpassQL筛选过路查询,以生成过滤后的原始数据以用于图形构造。如果您正在加载一个.osm文件,那么您将显式地绕过天桥查询步骤,直接导入原始数据的本地文件以供图形构造。OSMnx根据所提供的任何原始数据构造图形。
你有几个选择。首先,您可以直接使用OSMnx的graph_from_place
或graph_from_polygon
函数来获取图形,而不是在可能的情况下从.osm文件加载。其次,如果您需要使用graph_from_xml
并希望对其进行筛选,您可以在构建它之后过滤它:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
# create a graph with more edges than you want
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive', simplify=False)
# filter graph to retain only certain edge types
filtr = ['tertiary', 'tertiary_link', 'secondary', 'unclassified']
e = [(u, v, k) for u, v, k, d in G.edges(keys=True, data=True) if d['highway'] not in filtr]
G.remove_edges_from(e)
# remove any now-disconnected nodes or subcomponents, then simplify toplogy
G = ox.utils_graph.get_largest_component(G)
G = ox.simplify_graph(G)
https://stackoverflow.com/questions/63466207
复制相似问题