从Python中的地理位置确定国家/地区

时间:2016-11-16 14:21:44

标签: python gis

我的纬度和经度有500k +地理点。我使用以下函数来确定各自的国家。

 def findCountry(lat, lon):
    data = json.load(urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false' % (lat, lon)))
    for result in data['results']:
        for component in result['address_components']:
             if 'country' in component['types']:
                  return component['long_name']
    return None

对findCountry()的函数调用如下所示:

df3['country'] = df3.apply(lambda row: lookup(row['StartLat'],row['StartLong']), axis = 1)

但对于500K +积分,它需要花费无限长的时间才能完成。只是想知道我是否可以优化此功能或使用其他内置功能来快速完成。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可能已经非常了解地理编码器(请参阅http://geopy.readthedocs.io/en/latest/#module-geopy.geocoders)。它提供对与此相关的各种服务的统一访问。文档(https://media.readthedocs.org/pdf/geopy/latest/geopy.pdf)在第47页列出了它们。我不知道哪些文件可能比您使用的文件更快。但是,鉴于您的任务规模,他们可能值得调查。此代码旨在首先提供所提供的内容。

import geopy.geocoders
import inspect
import string

serviceNames = [_[0] for _ in inspect.getmembers(geopy.geocoders) \
    if not _[0].startswith('__')
    and  _[0][0] in string.ascii_uppercase
    ]
print (serviceNames)

for serviceName in serviceNames:
    try:
        exec('geolocator = geopy.geocoders.%s()' % serviceName)
    except:
        print (serviceName, 'requires access code, or not intended for this purpose')
        continue
    try:
        result = geolocator.reverse('43.2,65.4')
        print (serviceName, str(result).encode('ascii', 'ignore'))
    except:
        print (serviceName, 'reverse unsupported?')
        continue

我的假设是只有以大写字母开头的成员代表服务。这是输出。

['ArcGIS', 'Baidu', 'Bing', 'DataBC', 'GeoNames', 'GeocodeFarm', 'GeocoderDotUS', 'GeocoderNotFound', 'GoogleV3', 'IGNFrance', 'LiveAddress', 'NaviData', 'Nominatim', 'OpenCage', 'OpenMapQuest', 'Photon', 'SERVICE_TO_GEOCODER', 'What3Words', 'YahooPlaceFinder', 'Yandex']
ArcGIS reverse unsupported?
Baidu requires access code, or not intended for this purpose
Bing requires access code, or not intended for this purpose
DataBC reverse unsupported?
GeoNames requires access code, or not intended for this purpose
GeocodeFarm reverse unsupported?
GeocoderDotUS reverse unsupported?
GeocoderNotFound reverse unsupported?
GoogleV3 b'[Location(Tamdy District, Uzbekistan, (42.54183, 65.2488422, 0.0)), Location(Navoiy Province, Uzbekistan, (42.6988575, 64.6337685, 0.0)), Location(Uzbekistan, (41.377491, 64.585262, 0.0))]'
IGNFrance requires access code, or not intended for this purpose
LiveAddress requires access code, or not intended for this purpose
NaviData reverse unsupported?
Nominatim b'Tomdi Tumani, Navoiy Viloyati, Ozbekiston'
OpenCage requires access code, or not intended for this purpose
OpenMapQuest reverse unsupported?
Photon reverse unsupported?
SERVICE_TO_GEOCODER requires access code, or not intended for this purpose
What3Words requires access code, or not intended for this purpose
YahooPlaceFinder requires access code, or not intended for this purpose
Yandex b'[Location( , , (42.052267, 65.243642, 0.0)), Location(, (42.004874, 64.330882, 0.0)), Location(None, (41.765066, 63.150118, 0.0))]'

GoogleV3和Nominatim无需进一步随身携带即可满足您的需求。如果结果显示需要访问代码,或者不是为了此目的而使用访问代码。通常这意味着你需要一把钥匙或登录。

相关问题