我正在尝试用python和模块Geo编码器进行逆向地理编码
我建造了这个脚本
#!/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)根据这里医生的说法,我们可以做相反的事情。然而,当我运行这个脚本时,我有一个错误NameError: name 'reverse' is not defined
为什么?
提亚
这是我的文件中的一个示例
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)发布于 2014-11-13 23:25:04
问题是,您忘记在选项reverse周围添加引号。
g = geocoder.google(['lat','lon'], method='reverse')我认为这是他们文件中的一个错误。您可以向作者发送一条消息,通知他/她,他/她可能想更新他们的文档。
此外,您需要使用实际参数lat和lon,而不是字符串来调用它。否则你会出错的。所以,
g = geocoder.google([lat, lon], method='reverse') # lat and lon you've extracted already, but maybe you'll still need to cast them to float.更新:如果您遇到了提供g.ok == False的反向查找,那么问题与API提供程序对请求的节流有关(在上面的示例中,这是Google)。例如,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))一些供应商,如Mapquest,目前不强制执行节流。
地理编码版本0.9.1中存在的一个缺陷是,lat和lon都应该与0.0不同。如果有这样的坐标(沿赤道或沿格林尼治子午线),则查找将生成一个TypeError。
发布于 2015-09-15 02:44:29
Geo编码器已经走了很长一段路,最近的版本是1.6.1,希望能修复您提到的几个bug。
查看Python地理编码器中的逆向地理编码的最新文档。
import geocoder
g = geocoder.google([45.15, -75.14], method='reverse')
g.city
g.state
g.country
g.postal以下是支持反向地理编码的提供者列表:
https://stackoverflow.com/questions/26914900
复制相似问题