【问题标题】:Retrieve the country from the geographical locations in Python从 Python 中的地理位置中检索国家/地区
【发布时间】:2016-11-15 18:56:33
【问题描述】:

我正在尝试从我的 pandas 数据框中的纬度和经度点获取国家/地区名称。 目前我已经使用 geolocator.reverse(latitude,longitude) 来获取地理位置的完整地址。但是没有选项可以从完整地址中检索国家名称,因为它返回一个列表。

使用方法:

 def get_country(row):
     pos = str(row['StartLat']) + ', ' + str(row['StartLong'])
     locations = geolocator.reverse(pos)
     return locations

通过传递数据框调用 get_country:

df4['country'] = df4.apply(lambda row: get_country(row), axis = 1)

当前输出:

StartLat                  StartLong           Address          
52.509669              13.376294          Potsdamer Platz, Mitte, Berlin, Deutschland, Europe

只是想知道当我们通过地理点时是否有一些 Python 库来检索国家。

任何帮助将不胜感激。

【问题讨论】:

  • 你不能拆分地址并从列表中获取元素 - 即。 "Potsdamer Platz, Mitte, Berlin, Deutschland, Europ".split(", ")[-2]
  • 你用什么模块?
  • @furas:我目前使用 geolocator.reverse()
  • 我没有要求函数名,而是模块名 - geopygeocoder,其他。但我看到你得到了答案。
  • 你能把初始化geolocator的代码贴出来吗?

标签: python geocoding


【解决方案1】:

我的代码,希望对您有所帮助:

from geopy.geocoders import Nominatim 
nm = Nominatim()

place, (lat, lng) = nm.geocode("3995 23rd st, San Francisco,CA 94114")
print('Country' + ": " + place.split()[-1])

【讨论】:

    【解决方案2】:

    在您的get_country 函数中,您的返回值location 将有一个属性raw,它是一个看起来像这样的字典:

    {
      'address': {
         'attraction': 'Potsdamer Platz',
         'city': 'Berlin',
         'city_district': 'Mitte',
         'country': 'Deutschland',
         'country_code': 'de',
         'postcode': '10117',
         'road': 'Potsdamer Platz',
         'state': 'Berlin'
       },
       'boundingbox': ['52.5093982', '52.5095982', '13.3764983', '13.3766983'],
       'display_name': 'Potsdamer Platz, Mitte, Berlin, 10117, Deutschland',
       ... and so one ...
    }
    

    所以location.raw['address']['country'] 给了'Deutschland'

    如果我正确阅读了您的问题,可能的解决方案是:

    def get_country(row):
        pos = str(row['StartLat']) + ', ' + str(row['StartLong'])
        locations = geolocator.reverse(pos)
        return location.raw['address']['country']
    

    编辑:location.raw 对象的格式将根据您使用的地理定位器服务而有所不同。我的示例使用来自geopy's documentation site 示例的 geopy.geocoders.Nominatim,因此您的结果可能会有所不同。

    【讨论】:

    • 我相信 API 不再在返回位置的 raw 属性中返回 address
    • 感谢您的回答。如果我们尝试从 70k 唯一地理位置中提取地址,我会超时并出现 HTTP 错误 502:网关错误。你有什么解决办法吗?
    【解决方案3】:

    我不确定您使用 geopy 的服务是什么,但作为一个我可能偏向于的小插件,我认为这对您来说可能是一个更简单的解决方案。

    https://github.com/Ziptastic/ziptastic-python

    from ziptastic import Ziptastic
    
    # Set API key.
    api = Ziptastic('<your api key>')
    result = api.get_from_coordinates('42.9934', '-84.1595')
    

    这将返回一个字典列表,如下所示:

    [
        {
            "city": "Owosso",
            "geohash": "dpshsfsytw8k",
            "country": "US",
            "county": "Shiawassee",
            "state": "Michigan",
            "state_short": "MI",
            "postal_code": "48867",
            "latitude": 42.9934,
            "longitude": -84.1595,
            "timezone": "America/Detroit"
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多