2016-11-16 73 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做你沒有進一步攜帶的東西。如果結果顯示「需要訪問代碼,或者不需要訪問代碼」,通常這意味着您需要密鑰或登錄信息。

相關問題