查询Nominatim时如何支持"town"和"city"?

How to support "town" and "city" when querying Nominatim?

使用 Nominatim 进行反向地理编码似乎 return "town" 或 "city",具体取决于位置的大小。

import geojson
from geopy.geocoders import Nominatim

location = "48.84837905, 2.28229522311902"
geolocator = Nominatim(user_agent="my-application",timeout=3)
location = geolocator.reverse(location)
print(location.raw)
#Sometimes "town", sometimes "city"
##print(location.raw['address']['town'])
##print(location.raw['address']['city'])

处理这两种情况的好方法是什么?

谢谢。

这正是 try-except 的用途:

try:
    print(location.raw['address']['town'])
except KeyError:
    print(location.raw['address']['city'])

备选方案

一些注重性能的人会说"but try-except is expensive"。

您可以使用其他一些替代方法:

  • if 'town' in location.raw['address']: ... else: ...
  • location.raw['address'].get('town', location.raw['address'].get('city'))

每种方法都有自己的优点和缺点。 .get,比如,就是不偷懒。 location.raw['address'].get('city') 会 在查找 'town' 之前进行评估,所以事实上它更多 浪费和适得其反。 if-else 方法(取决于它的使用方式)可能需要对其中一个键进行两次哈希处理。

我认为将更常用的密钥放在 try 块中就足够了。

让我们做一些测试:

from timeit import Timer
from random import choice

list_of_dicts = [{choice(('town', 'city')): 1} for _ in range(2000)]

def try_except():
    for d in list_of_dicts:
        try:
            d['town']
        except KeyError:
            d['city']

def if_else():
    for d in list_of_dicts:
        if 'town' in d:
            d['town']
        else:
            d['city']

def get():
    for d in list_of_dicts:
        d.get('town', d.get('city'))


print(min(Timer(try_except).repeat(10, 10)))
print(min(Timer(if_else).repeat(10, 10)))
print(min(Timer(get).repeat(10, 10)))

这输出

0.0053282611981659705
0.0018278721105344786
0.00536558375274554

意味着在这个包含 2000 个词典的示例中,if-else 是最快的(即使它需要对其中一个键进行两次散列),而 try-exceptget 是差不多。