【问题标题】:Find the county for a city, state查找城市、州的县
【发布时间】:2017-10-27 18:33:06
【问题描述】:

我有一个城市 + 州的信息,我正在尝试查找该县。 我尝试了几件事。我得到的最接近的是使用geopy。这是一个例子:

from geopy.geocoders import Nominatim
geolocator = Nominatim()
loc = geolocator.geocode('Chicago Illinois')
print(loc.address)
# u'Chicago, Cook County, Illinois, United States of America'
loc = geolocator.geocode('San Francisco California')
print(loc.address) 
# u'SF, California, United States of America'

问题在于,不清楚县是否是输出的一部分,以及是否是如何使用代码提取它(字符串格式似乎发生了变化)。

获取城市+州的县信息的最佳方式是什么?

【问题讨论】:

  • 所以第一个任务的“县”应该是“库克县”?第二个,我不知道。 OSM 数据是随意的,我不确定你能做到这一点。 print loc.raw 会告诉你什么是可用的。
  • 你可以试试pygeocoder。

标签: python geocoding geopy


【解决方案1】:

这是一个geocoder solution,可以帮助您获取县级数据。如果您还没有安装它,您可以pip install geocoder

import geocoder

results = geocoder.google("Chicago, IL")

print(results.current_result.county)

【讨论】:

    【解决方案2】:

    它也可以在“address_components”列表中使用 Google API [Country -> State -> County -> ...]:

    request(
        'http://maps.googleapis.com/maps/api/geocode/json', 'GET', {
            'address': 'Chicago Illinois'
            'language': 'en',
            'sensor': 'true'
        }
    )
    

    如果第一个列表中的变化太多,您必须定义正确的长/纬度:

    request(
        'http://maps.googleapis.com/maps/api/geocode/json', 'GET', {
            'latlng': "${Latitude},${Longitude}",
            'language': 'en',
            'sensor': 'true'
        }
    )
    

    另一个示例,Yahoo API:

    request('https://query.yahooapis.com/v1/public/yql', 'GET', {
        'q': "select * from geo.places.parent where child_woeid in (select woeid from geo.places where text='Chicago, Illinois')",
        'market': 'en-gb',
        'format': 'json',
        'env': 'store%3A%2F%2Fdatatables.org%2Falltableswithkeys',
        'callback': ''
    })
    

    假设,可以在任何 GEO API 中直接使用相同的方式。

    【讨论】:

    • “国家”和“县”不同。要么是问题中的拼写错误,要么是您误解了。
    • @roganjosh,抱歉,注意力不集中,但它适用于县作为“address_components”的一部分developers.google.com/maps/documentation/geocoding/intro?hl=en
    • 好的,这是一项不同的服务。如果这可以回答两个示例的“县”问题,那么我认为这是一个不错的答案,只要您包含任何 API 限制(每单位时间的请求数)。我不确定 OSM 是否可靠地携带这些数据。
    • geopy 也有:nominatim.openstreetmap.org/search/Chicago/…<county>Cook County</county>
    • 第二个例子呢? OP 已经在问题中显示“库克县”。
    【解决方案3】:

    这是一个弱点,因为geopy 不保证县的回应。如果您对其他 API 持开放态度,U.S. Small Business Administration 有一个带有“特定城市的所有数据”端点的 U.S. City & County Web Data API,其中包含城市和县的县,例如 City and County of San Francisco

    API 具有xmljson 响应格式。 您的示例所需的唯一更改是使用州缩写:

    import requests
    import json
    
    locations = ['Chicago, IL', 'San Francisco, CA']
    
    city_data_url = 'http://api.sba.gov/geodata/primary_links_for_city_of/%s/%s.json'
    
    for l in locations:
        split_name = l.split(', ')
        response = requests.get(city_data_url % tuple(split_name))
    
        resp_json = json.loads(response.text)
        print resp_json[0]['full_county_name']
    

    输出:

    Cook County
    San Francisco County
    

    【讨论】:

    【解决方案4】:

    请使用以下代码提取国家名称的最后一个单词。

    from geopy.geocoders import Nominatim
    
    geolocator = Nominatim(timeout=3)
    
    location = geolocator.geocode('Chicago Illinois',language='en')
    
    loc_dict = location.raw
    
    print(loc_dict['display_name'])
    
    print (loc_dict['display_name'].rsplit(',' , 1)[1])
    
    
    location = geolocator.geocode('San Francisco California')
    
    loc_dict = location.raw
    
    print(loc_dict['display_name'])
    
    print (loc_dict['display_name'].rsplit(',' , 1)[1])
    

    【讨论】:

      【解决方案5】:

      还有geocoder project 可以非常轻松地进行地理编码查找,包括按城市或按城市和州。它支持多个提供商,请查看this table

      使用“Google 地图地理编码”提供程序的示例:

      In [1]: import geocoder
      
      In [2]: GOOGLE_GEOCODE_API_KEY = "your_api_key"
      
      In [3]: result = geocoder.google("Chicago, IL", key=GOOGLE_GEOCODE_API_KEY)
      
      In [4]: result.country_long
      Out[4]: 'United States'
      
      In [5]: result.country
      Out[5]: 'US'
      

      此处的 Google Maps Geocoding API 键是可选的,但允许更好地使用 API rate limits。当然,还有高级 API 选项。

      【讨论】:

      • Google 地图现在需要密钥。
      猜你喜欢
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2010-09-30
      • 2018-07-31
      • 2022-12-13
      相关资源
      最近更新 更多