2016-11-16 6 views
0

위도와 경도가 500k + 지리적 인 지점이 있습니다. 아래의 함수를 사용하여 해당 국가를 결정합니다. Python의 지리적 위치에서 국가를 결정하십시오.

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

그러나 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에 함수 호출()는 다음과 같다. 그냥이 함수를 최적화 할 수 있는지 또는 다른 내장 된 함수를 사용하여 신속하게 처리 할 수 ​​있는지 궁금합니다.

도움을 주시면 감사하겠습니다.

+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은 더 이상 수행하지 않고도 원하는대로 수행합니다. 결과에 '액세스 코드가 필요하거나이 용도로 사용하지 않아야합니다.'라고 말하면 일반적으로 키 또는 로그인이 필요합니다.