从 GeoPy 地理编码器返回各个地址组成部分(城市、州等)

2024-01-23

我正在使用 GeoPy 将地址地理编码为经纬度。我还想提取每个地址的逐项地址组成部分(街道、城市、州、邮政编码)。

GeoPy 返回一个带有地址的字符串——但我找不到可靠的方法来分离每个组件。例如:

123 Main Street, Los Angeles, CA 90034, USA =>
{street: '123 Main Street', city: 'Los Angeles', state: 'CA', zip: 90034, country: 'USA'}

Google 地理编码 API 确实返回这些单独的组件...有没有办法从 GeoPy 获取这些组件? (或者不同的地理编码工具?)


您还可以从以下位置获取各个地址部分:Nominatim()geocoder(这是 geopy 的标准开源地理编码器)。

from geopy.geocoders import Nominatim

# address is a String e.g. 'Berlin, Germany'
# addressdetails=True does the magic and gives you also the details
location = geolocator.geocode(address, addressdetails=True)

print(location.raw)

gives

{'type': 'house',
 'class': 'place',
 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright',
 'display_name': '2, Stralauer Allee, Fhain, Friedrichshain-Kreuzberg, Berlin, 10245, Deutschland',
 'place_id': '35120946',
 'osm_id': '2825035484',
 'lon': '13.4489063',
 'osm_type': 'node',
 'address': {'country_code': 'de',
             'road': 'Stralauer Allee',
             'postcode': '10245',
             'house_number': '2',
             'state': 'Berlin',
             'country': 'Deutschland',
             'suburb': 'Fhain',
             'city_district': 'Friedrichshain-Kreuzberg'},
 'lat': '52.5018003',
 'importance': 0.421,
 'boundingbox': ['52.5017503', '52.5018503', '13.4488563', '13.4489563']}

with

location.raw['address']

您将获得仅包含组件的字典。

看一眼地理文档 http://geopy.readthedocs.io/en/1.10.0/#geopy.geocoders.Nominatim如需更多参数或提名 http://wiki.openstreetmap.org/wiki/Nominatim对于所有地址组件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 GeoPy 地理编码器返回各个地址组成部分(城市、州等) 的相关文章

随机推荐