如何使用OSMnx从OSM格式的XML文件创建过滤图?

问题描述

在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文件中获取经过过滤的图形,但是函数osmnx.graph_from_xml()不支持参数custom_filter。如何从* .osm数据获取过滤的图形?

这只会绘制整个* .osm数据集:

import osmnx as ox
G = ox.graph_from_xml("isle-of-man-latest.osm") # from disk.
fig,node_color='r')

解决方法

OSMnx的custom_filter参数使您可以使用OverpassQL过滤Overpass查询,以生成过滤后的原始数据以进行图形构建。如果要加载.osm文件,那么您将显式地绕过Overpass查询步骤,而直接导入原始数据的本地文件以进行图形构建。 OSMnx从您提供的任何原始数据构建图形。

您有两种选择。首先,可以直接使用OSMnx的graph_from_placegraph_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,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)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...