【发布时间】:2021-10-24 22:00:23
【问题描述】:
当使用 NOminatim 和 GeoPy 对给定的地址字符串进行编码时,如何获取国家信息?
是否有任何结构化的方法可以在输出中获取国家/地区名称?一种方法是在最后一个“,”之后使用字符串来获取国家/地区,但这似乎不是一种可靠的方法。
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent='myapplication')
x = 'PA - Cranberry Township'
location = geolocator.geocode(x, exactly_one=True,language="english", namedetails=True)
# how to get country name from response
location.raw
{'place_id': 259010359,
'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
'osm_type': 'relation',
'osm_id': 4131949,
'boundingbox': ['40.673669', '40.7453357', '-80.1525583', '-80.0559393'],
'lat': '40.7099343',
'lon': '-80.1060506',
'display_name': 'Cranberry Township, Butler County, Pennsylvania, 16066, United States',
'class': 'boundary',
'type': 'administrative',
'importance': 0.6463910141518243,
'icon': 'https://nominatim.openstreetmap.org/ui/mapicons//poi_boundary_administrative.p.20.png',
'namedetails': {'name': 'Cranberry Township',
'name:en': 'Cranberry Township',
'official_name': 'Cranberry Township'}}
【问题讨论】:
-
为什么用逗号分割 display_name 并从中获取任何你需要的东西不健壮?
-
我不确定最后一个字符串是否总是国家
标签: python openstreetmap geocoding geopy