从GeoPy地理编码器返回单个地址组件(城市,州等)

lub*_*bar 9 python geocoding geopy google-geocoder

我正在使用GeoPy将地址编码为lat,lng.我还想提取每个地址的逐项地址组件(街道,城市,州,邮政编码).

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

123 Main Street, Los Angeles, CA 90034, USA =>
{street: '123 Main Street', city: 'Los Angeles', state: 'CA', zip: 90034, country: 'USA'}
Run Code Online (Sandbox Code Playgroud)

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

Kro*_*nig 11

以防万一有人仍有同样的问题.您还可以从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)
Run Code Online (Sandbox Code Playgroud)

{'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']}
Run Code Online (Sandbox Code Playgroud)

location.raw['address']
Run Code Online (Sandbox Code Playgroud)

你只得到组件的字典.

查看更多参数的geopy文档或所有地址组件的Nominatim.

希望我能帮助那些仍然遇到麻烦的人.也许赞成其他人可以更容易地找到这个,因为我认为这是这个问题的正确答案.

  • 令我困惑的代码中缺少这一行: ```geolocator = Nominatim(user_agent="specify_your_app_name_here")``` (3认同)

ste*_*nce 5

usaddress由 DataMade使用。这是GitHub 存储库

它像这样工作usaddress.parse('123 Main St. Suite 100 Chicago, IL')并返回这个数组

[('123', 'AddressNumber'), ('Main', 'StreetName'), ('St.', 'StreetNamePostType'), ('Suite', 'OccupancyType'), ('100', 'OccupancyIdentifier'), ('Chicago,', 'PlaceName'), ('IL', 'StateName')]


Mat*_*att 1

不久前我帮助编写了一个名为LiveAddress的程序;它刚刚升级为支持单行(自由格式)地址并实现地理编码功能。

GeoPy 是一个地理编码实用程序,而不是地址解析器/标准化器。然而, LiveAddress API还可以为您验证地址的有效性,填写缺失的信息。您会发现 Google 和 Yahoo 等服务会近似提供地址,而 LiveAddress 等经过 CASS 认证的服务实际上会对其进行验证,并且除非该地址是真实的,否则不会返回结果。

在通过实施 LiveAddress 进行大量研究和开发后,我在 Stack Overflow 帖子中写了一个总结。它记录了一些疯狂但完整的地址格式,并最终导致解析问题(针对美国地址)的解决方案。

要使用 Python 将单行地址解析为组件,只需将整个地址放入“街道”字段即可:

import json
import pprint
import urllib

LOCATION = 'https://api.qualifiedaddress.com/street-address/'
QUERY_STRING = urllib.urlencode({ # entire query sting must be URL-Encoded
    'auth-token': r'YOUR_API_KEY_HERE',
    'street': '1 infinite loop cupertino ca 95014'
})
URL = LOCATION + '?' + QUERY_STRING

response = urllib.urlopen(URL).read()
structure = json.loads(response)
pprint.pprint(structure)
Run Code Online (Sandbox Code Playgroud)

生成的 JSON 对象将包含一个components如下所示的对象:

"components": {
        "primary_number": "1",
        "street_name": "Infinite",
        "street_suffix": "Loop",
        "city_name": "Cupertino",
        "state_abbreviation": "CA",
        "zipcode": "95014",
        "plus4_code": "2083",
        "delivery_point": "01",
        "delivery_point_check_digit": "7"
}
Run Code Online (Sandbox Code Playgroud)

响应还将包括组合的first_line和delivery_line_2,因此您不必在需要时手动连接它们。还提供有关该地址的纬度/经度和其他信息。