简体   繁体   English

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

[英]How to create a filtered graph from an OSM-formatted XML file using OSMnx?

In Python, we can use a function osmnx.graph_from_place() and a filter custom_filter='["waterway"="river"]' to get a filtered graph.在 Python 中,我们可以使用一个 function 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')

I want to get a filtered graph from an OSM-formatted XML file from my disk, but the function osmnx.graph_from_xml() does not support the parameter custom_filter .我想从磁盘中的 OSM 格式的 XML 文件中获取过滤图,但 function osmnx.graph_from_xml()不支持参数custom_filter How to get a filtered graph from *.osm data?如何从 *.osm 数据中获取过滤图?

This will just plot the whole *.osm dataset:这将只是 plot 整个 *.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')

OSMnx's custom_filter parameter lets you filter an Overpass query with OverpassQL to generate filtered raw data for graph construction. OSMnx 的custom_filter参数允许您使用 OverpassQL 过滤 Overpass 查询,以生成用于图形构建的过滤原始数据。 If you are loading a.osm file, then you are explicitly bypassing the Overpass query step to instead directly import a local file of raw data for graph construction.如果您正在加载 a.osm 文件,那么您将明确绕过 Overpass 查询步骤,而是直接导入原始数据的本地文件以构建图形。 OSMnx constructs the graph from whatever raw data you provide it. OSMnx 根据您提供的任何原始数据构建图表。

You have a couple options.你有几个选择。 First, you can use OSMnx's graph_from_place or graph_from_polygon functions directly to get your graph, instead of loading from a.osm file, if possible.首先,如果可能,您可以直接使用 OSMnx 的graph_from_placegraph_from_polygon函数来获取图形,而不是从 .osm 文件加载。 Second, if you need to use graph_from_xml and want to filter it, you can filter it after you've constructed it:其次,如果你需要使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM