Python geopy地理编码器中的超时错误

时间:2015-05-13 14:50:15

标签: python timeout geopy

我是一个相对较新的Python用户,我正在尝试使用函数来使用“geopy”模块返回城市和国家的纬度和经度。我有错误,因为我的城市拼写错误,我已设法抓住。我现在遇到的麻烦是我遇到了超时错误。我已阅读此问题Geopy: catch timeout error并相应地调整了我的超时参数。     但是,在我收到超时错误之前,它现在运行了不同的时间长度。我试过在更快的网络上运行它,它在某种程度上起作用。问题是我需要为100k行执行此操作,并且在超时之前迭代的最大行数为20k。任何关于如何解决这个问题的帮助/建议都非常感谢。

import os
from geopy.geocoders import Nominatim
os.getcwd() #check current working directory
os.chdir("C:\Users\Philip\Documents\HDSDA1\Project\Global Terrorism Database")

#import file as a csv
import csv
gtd=open("gtd_original.csv","r")
csv_f=csv.reader(gtd)
outf=open("r_ready.csv","wb")
writer=csv.writer(outf,dialect='excel')
for row in csv_f:
    if row[13] in ("","NA") or row[14] in ("","NA"):   
        lookup = row[12] + "," + row[8]  # creates a city,country
        geolocator = Nominatim()
        location = geolocator.geocode(lookup, timeout = None) #looks up the city/country on maps
        try:
            location.latitude
        except:
            lookup = row[8]
            location = geolocator.geocode(lookup) 
        row[13] = location.latitude
        row[14] = location.longitude
    writer.writerow(row)      
gtd.close()
outf.close()

2 个答案:

答案 0 :(得分:5)

我希望您延长Nominatim服务的使用政策(http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy)。尝试在请求之间进行1秒的休眠并缓存结果,可能有很多重复。

睡觉部分:

from time import sleep
### your code
row[14] = location.longitude
sleep(1) # after last line in if

缓存:

coords = {}
if coords.has_key([row[8], row[12] ]):
    row[13] , row[14] = coords[ [ row[8], row[12] ] ]
else:
    #geolocate

<强>更新

性能:1个请求/秒 - &gt; 3600 reqs / hour - &gt; 36K请求/ 10h

import os
from time import sleep
from geopy.geocoders import Nominatim
os.getcwd() #check current working directory
os.chdir("C:\Users\Philip\Documents\HDSDA1\Project\Global Terrorism Database")

#import file as a csv
import csv
gtd=open("gtd_original.csv","r")
csv_f=csv.reader(gtd)
outf=open("r_ready.csv","wb")
writer=csv.writer(outf,dialect='excel')
coords = {}
for row in csv_f:
    if row[13] in ("","NA") or row[14] in ("","NA"):   
        lookup = row[12] + "," + row[8]  # creates a city,country

        if coords.has_key( (row[8], row[12]) ):   ## test if result is already cached
            row[13] , row[14] = coords[ (row[8], row[12]) ]
        else:    
            geolocator = Nominatim()
            location = geolocator.geocode(lookup, timeout = None) #looks up the city/country on maps
            try:
                location.latitude
            except:
                lookup = row[8]
                location = geolocator.geocode(lookup) 
            row[13] = location.latitude
            row[14] = location.longitude
            coords[ (row[8], row[12]) ] = (row[13] , row[14])  # cache the new coords
            sleep(1) # sleep for 1 sec (required by Nominatim usage policy)

    writer.writerow(row)      
gtd.close()
outf.close()

答案 1 :(得分:2)

您可以使用GeocoderTimedOut

这是一个可以帮助你的示例函数

import geopy
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut

def do_geocode(address):
    geopy = Nominatim()
    try:
        return geopy.geocode(address)
    except GeocoderTimedOut:
        return do_geocode(address)

如果发生超时则非常简单,然后重试。希望它有所帮助