使用python地理编码器进行反向地理编码

使用python地理编码器进行反向地理编码,python,python-2.7,geocoding,Python,Python 2.7,Geocoding,我正在尝试用python和geocoder模块进行反向地理编码 我创建了这个脚本 #!/Users/admin/anaconda/bin/python import geocoder import unicodecsv import logging with open('locs2.csv', 'rb') as f: reader = unicodecsv.DictReader(f, encoding='iso-8859-1') for line in reader:

我正在尝试用python和geocoder模块进行反向地理编码

我创建了这个脚本

#!/Users/admin/anaconda/bin/python

import geocoder
import unicodecsv
import logging


with open('locs2.csv', 'rb') as f:
    reader = unicodecsv.DictReader(f, encoding='iso-8859-1')
    for line in reader:
        lat = line['lat']
        lon = line['lon']
        g = geocoder.google(['lat','lon'], method=reverse)
        if g.ok:
          g.postal
          logging.info('Geocoding SUCCESS: ' + address)
        else:
          logging.warning('Geocoding ERROR: ' + address)
根据文件,我们可以做相反的事情。但是,当我运行脚本时,出现了以下错误
namererror:name“reverse”未定义

为什么?

短暂性脑缺血发作

这是我的文件中的一个示例

lat, lon
48.7082,2.2797
48.7577,2.2188
47.8333,2.2500
48.9833,1.7333
47.9333,1.9181
46.2735,4.2586
**编辑**:我对脚本做了一点修改(见下文),出现了这个错误

WARNING:root:Geocoding ERROR:
修改后的脚本

#!/Users/admin/anaconda/bin/python

import geocoder
import unicodecsv
import logging

pcode=[] 
lat=[]
lon=[]


with open('locs2.csv', 'rb') as f:
    reader = unicodecsv.DictReader(f, encoding='iso-8859-1')
    for line in reader:
        lat = line['lat']
        lon = line['lon']
        g = geocoder.google([lat,lon], method='reverse')
        if g.ok:
          pcode.extend(g.postal)
          logging.info('Geocoding SUCCESS: '+
str(lat)+','+str(lon)+','+str(pcode))
        else:
          logging.warning('Geocoding ERROR: ' + str(lat)+','+str(lon))

fields= 'lat', 'lon', 'pcode'
rows=zip(lat,lon,pcode) 

with open('/Users/admin/python/myfile.csv', 'wb') as outfile: 
    w =  unicodecsv.writer(outfile, encoding='iso-8859-1') 
    w.writerow(fields) 
    for i in rows: 
        w.writerow(i)

问题是,您忘记在选项
reverse
周围添加引号:

g = geocoder.google(['lat','lon'], method='reverse')
我认为这是他们文件中的一个错误。您可以向作者发送一条消息,通知他/她这一点,他/她可能想要更新他们的文档

此外,您需要使用实际参数
lat
lon
来调用它,而不是字符串。否则你会得到一个错误。 所以


更新:如果您运行的反向查找给出了
g.ok==False
,那么问题与API提供者限制您的请求有关(在上面的示例中,这是Google)。例如,不应以过快的速度轮询和。调用
g.debug()
时,此信息非常清楚。其中包括:

## Provider's Attributes
* status: OVER_QUERY_LIMIT
* error_message: You have exceeded your rate-limit for this API.
要限制您的请求,您可以执行以下操作:

import geocoder
import unicodecsv
import logging
import time
import csv

pcode=[]

with open('locs2.csv', 'rb') as f:
    reader = csv.DictReader(f)
    for line in reader:            
        lat = float(line['lat'])
        lon = float(line['lon'])
        g = geocoder.google([lat,lon], method='reverse')
        attempts = 1  # number of lookups
        while not(g.ok) and attempts < 4:
            logging.warning('Geocoding ERROR: {}'.format(g.debug()))
            time.sleep(2)  # 2 seconds are specified in the API. If you still get errors, it's because you've reached the daily quota.
            g = geocoder.google([lat,lon], method='reverse')
            attempts += 1
        if attempts > 3:
            logging.warning('Daily quota of google lookups exceeded.')
            break
        pcode.extend(g.postal)
        logging.info('Geocoding SUCCESS: ({},{},{})'.format(lat,lon,pcode))
导入地理编码器
进口独角兽
导入日志记录
导入时间
导入csv
pcode=[]
以open('locs2.csv','rb')作为f:
reader=csv.DictReader(f)
对于行内读取器:
lat=浮动(行['lat'])
lon=浮动(直线['lon'])
g=geocoder.google([lat,lon],method='reverse')
尝试次数=1#查找次数
而不是(g.ok)和尝试次数<4次:
logging.warning('地理编码错误:{}'。格式(g.debug())
时间。睡眠(2)#2秒在API中指定。如果仍然出现错误,那是因为您已达到每日配额。
g=geocoder.google([lat,lon],method='reverse')
尝试次数+=1
如果尝试次数>3:
logging.warning('超出了google每日查找配额')
打破
pcode.extend(g.postal)
logging.info('地理编码成功:({},{},{},{})'.format(lat,lon,pcode))
一些提供程序,例如Mapquest,目前不实施限制


geocoder版本0.9.1中存在的一个缺陷是
lat
lon
应与
0.0
不同。如果你有这样的坐标(沿赤道或格林威治子午线),那么查找将生成一个
类型错误

地理编码器已经取得了长足的进步,最新版本是1.6.1,有望修复你提到的一些错误

请查看最新的文档以了解更多信息

以下是支持反向地理编码的提供商列表:


你好,奥利弗,谢谢你的帮助。我对脚本做了一些修改,出现了一个新错误。这对你有什么影响吗?TIA@AndyK您得到的“错误”只是调用日志模块生成的警告:
logging.warning('Geocoding error:')
。这意味着对于某些特定的反向查找,表达式
g.ok
的计算结果为
False
。如果没有您的源数据,我们无法说明发生这种情况的原因。您可以使用
logging.warning('Geocoding ERROR:({},{})').format(lat,lon))
来知道它失败的坐标。奥利弗,我刚刚意识到。该死,我还不熟悉Python!!!这是我的源文件。你能帮我一点忙吗?我又修改了我的剧本(见帖子)。我可以看到第9行的地理编码错误为
45.8167,5.9167
。奇怪的是,没有显示成功结果……对这些坐标进行反向查找对我来说很有效:
g.ok
计算结果为
True
。如果您想调试它,我们应该将此讨论移至。
import geocoder
import unicodecsv
import logging
import time
import csv

pcode=[]

with open('locs2.csv', 'rb') as f:
    reader = csv.DictReader(f)
    for line in reader:            
        lat = float(line['lat'])
        lon = float(line['lon'])
        g = geocoder.google([lat,lon], method='reverse')
        attempts = 1  # number of lookups
        while not(g.ok) and attempts < 4:
            logging.warning('Geocoding ERROR: {}'.format(g.debug()))
            time.sleep(2)  # 2 seconds are specified in the API. If you still get errors, it's because you've reached the daily quota.
            g = geocoder.google([lat,lon], method='reverse')
            attempts += 1
        if attempts > 3:
            logging.warning('Daily quota of google lookups exceeded.')
            break
        pcode.extend(g.postal)
        logging.info('Geocoding SUCCESS: ({},{},{})'.format(lat,lon,pcode))
import geocoder

g = geocoder.google([45.15, -75.14], method='reverse')
g.city
g.state
g.country
g.postal