【问题标题】:geocoding with Python reverse_geocoder使用 Python reverse_geocoder 进行地理编码
【发布时间】:2018-03-27 03:27:53
【问题描述】:

我有这个电子表格包含日本的城市名称(不是 100% 正确)、城市坐标、纬度和经度是正确的。我的目标是使用地理编码器数据库中的名称验证我们表中的所有城市名称。

import reverse_geocoder as rg 
import pandas as pd
import numpy as np

df=pd.read_excel('desktop/japan.xlsx') #read in the spreadsheet
df['coord']=list(zip(df.lat,df.long)) # zip the lat and lng to make a new series
df.head() 

for x,y in df['coord']:   # use a for loop to find all the records in reverse_geocoder
coordinates=(x,y)
results=rg.search(coordinates)
print (results)

我从 Jupyter Notebook 得到一些结果,然后出现错误,IndexError: list index out of range。我该如何解决?我用谷歌搜索,没有找到任何有用的解决方案。如何打印所有城市名称?谢谢你一百万。这是电子表格的链接。 Japan city names and coordinates

      IndexError  Traceback (most recent call=last)
    <ipython-input-6-2a5e602f55a8> in <module>()
     1 for x,y in df['coord']:
     2     coordinates=(x,y)
 ----> 3     results=rg.search(coordinates)
     4     print (results)

      ~/anaconda3/lib/python3.6/site-     packages/reverse_geocoder/__init__.py in search(geo_coords, mode, verbose)
      291 
      292     _rg = RGeocoder(mode=mode, verbose=verbose)
      --> 293     return _rg.query(geo_coords)
      294 
      295 if __name__ == '__main__':

     ~/anaconda3/lib/python3.6/site-packages/reverse_geocoder/__init__.py in query(self, coordinates)
      126         else:
      127             _, indices = self.tree.pquery(coordinates, k=1)
      --> 128         return [self.locations[index] for index in indices]
       129 
       130     def load(self, stream):

    ~/anaconda3/lib/python3.6/site-packages/reverse_geocoder/__init__.py in <listcomp>(.0)
      126         else:
      127             _, indices = self.tree.pquery(coordinates, k=1)
       --> 128         return [self.locations[index] for index in indices]
      129 
      130     def load(self, stream):

     IndexError: list index out of range

【问题讨论】:

    标签: python jupyter-notebook geocoding reverse-geocoding


    【解决方案1】:

    这是因为您的文件中缺少纬度和经度。

    df = df.loc[df.lat.notna(), :]
    df = df.loc[df.long.notna(), :]
    

    在反向地理编码之前添加这两行,这将省略所有缺失值。

    【讨论】:

    • 成功了!我想我必须放弃所有的 NA,谢谢!
    猜你喜欢
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多