如何从 OpenStreetMap 中的 osm.pbf 文件获取街道名称
How to get street name from osm.pbf file in OpenStreetMap
您可以从这里下载任何数据集https://download.geofabrik.de/australia-oceania.html
这是我的代码
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("/DATA/user/nabih/pitcairn-islands-latest.osm.pbf")
# 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
街道名称是 highway
元素的 name
键的值(有关所有可能的公路类型,请参阅 https://wiki.openstreetmap.org/wiki/Map_features#Highway,您可能希望在查询中进一步过滤它)。然后,您可以将所有 highway
行与它们在 id
:
上的 name
行自行连接
df_osm.loc[df_osm.tagkey=='highway', ['id', 'tagvalue']].merge(
df_osm.loc[df_osm.tagkey=='name', ['id', 'tagvalue']],
on='id', suffixes=['_kind', '_name'])
pitcairn-islands-latest.osm.pbf
的结果:
id tagvalue_kind tagvalue_name
0 1034153953 residential Main Road
1 1034161481 residential Hill of Difficulty Road
如果您还想包括国家名称,可以将 df_osm.tagkey=='name'
替换为 df_osm.tagkey.str.startswith('name')
。有关详细信息和其他可能的名称,请参阅 https://wiki.openstreetmap.org/wiki/Key:name。
您可以从这里下载任何数据集https://download.geofabrik.de/australia-oceania.html
这是我的代码
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("/DATA/user/nabih/pitcairn-islands-latest.osm.pbf")
# 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
街道名称是 highway
元素的 name
键的值(有关所有可能的公路类型,请参阅 https://wiki.openstreetmap.org/wiki/Map_features#Highway,您可能希望在查询中进一步过滤它)。然后,您可以将所有 highway
行与它们在 id
:
name
行自行连接
df_osm.loc[df_osm.tagkey=='highway', ['id', 'tagvalue']].merge(
df_osm.loc[df_osm.tagkey=='name', ['id', 'tagvalue']],
on='id', suffixes=['_kind', '_name'])
pitcairn-islands-latest.osm.pbf
的结果:
id tagvalue_kind tagvalue_name
0 1034153953 residential Main Road
1 1034161481 residential Hill of Difficulty Road
如果您还想包括国家名称,可以将 df_osm.tagkey=='name'
替换为 df_osm.tagkey.str.startswith('name')
。有关详细信息和其他可能的名称,请参阅 https://wiki.openstreetmap.org/wiki/Key:name。