pandas 如何在 Python 中从 OSM 文件中提取和可视化数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45771809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract and visualize data from OSM file in Python
提问by Kinjal Kachi
I have downloaded an OpenStreetMap file on my desktop , and I have used my OSM file in the jupyter notebook.
我在桌面上下载了一个 OpenStreetMap 文件,并且在 jupyter notebook 中使用了我的 OSM 文件。
My code:
我的代码:
import xml.etree.cElementTree as ET
osm_file = "ahmedabad_india.osm"
for event, elem in ET.iterparse(osm_file, events=("start",)):
print(elem)
# prints the Element 'osm' at 0x03A7DC08>
#<Element 'bounds' at 0x03A7DDA0>
#<Element 'node' at 0x03A7DE90>
#<Element 'tag' at 0x03A7DF08> and so on ...
I'd like to see the contents of all the tags
i.e.
<'node', 'id', 'name', ...>
and so on.
我想看看所有tags
ie
<'node', 'id', 'name', ...>
等的内容。
I tried using elem
tag but, it prints nothing.
我尝试使用elem
标签,但它什么也没打印。
Can anyone help me to figure out, who to get the contents of tags like node, ways etc.
谁能帮我弄清楚,谁来获取节点、方式等标签的内容?
回答by mforez
You can extract all the data from an .osm
file through PyOsmium(A fast and flexible C++ library for working with OpenStreetMap data) and then handle it with Pandas:
您可以.osm
通过PyOsmium(用于处理 OpenStreetMap 数据的快速灵活的 C++ 库)从文件中提取所有数据,然后使用Pandas处理它:
Code:
代码:
import osmium as osm
import pandas as pd
class OSMHandler(osm.SimpleHandler):
def __init__(self):
osm.SimpleHandler.__init__(self)
self.osm_data = []
def tag_inventory(self, elem, elem_type):
for tag in elem.tags:
self.osm_data.append([elem_type,
elem.id,
elem.version,
elem.visible,
pd.Timestamp(elem.timestamp),
elem.uid,
elem.user,
elem.changeset,
len(elem.tags),
tag.k,
tag.v])
def node(self, n):
self.tag_inventory(n, "node")
def way(self, w):
self.tag_inventory(w, "way")
def relation(self, r):
self.tag_inventory(r, "relation")
osmhandler = OSMHandler()
# scan the input file and fills the handler list accordingly
osmhandler.apply_file("muenchen.osm")
# transform the list into a pandas DataFrame
data_colnames = ['type', 'id', 'version', 'visible', 'ts', 'uid',
'user', 'chgset', 'ntags', 'tagkey', 'tagvalue']
df_osm = pd.DataFrame(osmhandler.osm_data, columns=data_colnames)
df_osm = tag_genome.sort_values(by=['type', 'id', 'ts'])
Output:
输出: