返回单个地址部分(城市、州等)来自GeoPy地理编码器 [英] Return individual address components (city, state, etc.) from GeoPy geocoder

查看:36
本文介绍了返回单个地址部分(城市、州等)来自GeoPy地理编码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GeoPy对地址进行地理编码,以将其转换为LNG。我还想提取每个地址的详细地址组成部分(街道、城市、州、邮政编码)。

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()地理编码器(这是来自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)

给予

{'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']}

location.raw['address']

您获得的词典仅包含组件。

查看geopy documentation了解更多参数,或查看Nominatim了解所有地址组件。

这篇关于返回单个地址部分(城市、州等)来自GeoPy地理编码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆