2016-11-16 6 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は、それ以上のキャリーオンがなければあなたが望むことをします。結果が「アクセスコードを必要とする、またはこの目的のために意図されていない」と言うのは、通常、キーまたはログインが必要であることを意味します。

関連する問題