2016-11-16 71 views
0

我有经纬度为500k +的地理点。我使用下面的函数来确定它们各自的国家。从Python的地理位置确定国家

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 +点,其采取的无限长的时间才能完成。只是想知道我是否可以优化此功能或使用其他内置功能来快速完成此功能。

任何帮助,将不胜感激。

+0

http://stackoverflow.com/questions/16497384/google-maps-api-geocoding-handling-multiple-requests –

回答

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做你没有进一步携带的东西。如果结果显示“需要访问代码,或者不需要访问代码”,通常这意味着您需要密钥或登录信息。

相关问题