从GeoPy geocod返回单个地址组件(城市、州等)

2024-04-20 12:51:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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'}

Google地理编码API确实返回了这些单独的组件。。。有办法从GeoPy拿到这些吗?(或者其他地理编码工具?)


Tags: street编码main地址组件街道地理ca
3条回答

使用DataMade的usaddress。这是GitHub repo

它的工作方式如下usaddress.parse('123 Main St. Suite 100 Chicago, IL'),并返回此数组

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

以防有人仍然有同样的问题。您还可以从Nominatim()geocoder(geopy的标准开源geocoder)获取各个地址组件。

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以获取所有地址组件。

希望我能帮助每一个有困难的人。也许别人更容易找到这个答案,因为我认为这是这个问题的正确答案。

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

GeoPy是一个地理编码实用程序,而不是地址分析器/标准化器。然而,LiveAddress API,它还可以为您验证地址的有效性,填写缺少的信息。你会发现像Google和Yahoo这样的服务大概是地址,而像LiveAddress这样的CASS认证服务实际上是验证的,除非地址是真的,否则不会返回结果。

在完成了很多关于实现LiveAddress的研究和开发之后,我编写了一个summary in this Stack Overflow post。它记录了一些疯狂而完整的格式,地址可以进入并最终导致解析问题的解决方案(对于我们的地址)。

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

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)

生成的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"
}

响应还将包括组合的第一行和第二行,因此如果需要,不必手动连接它们。纬度/经度和其他有关地址的信息也可用。

相关问题 更多 >