`我输入了以下代码来生成一个pickle文件,该文件是在运行代码后自行创建的,但在对文件进行depickle之后,所有数据都是“无”,而不是获取地点和道路的名称
colab文件链接:[文本] (https://colab.research.google.com/drive/1o-YxFhpSJA5eDDsG7sXQ9qO76CNSO8oU?usp=sharing)
OUTPUT :
{'County': [None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
'Road': [None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None],
'Neighbourhood': []}
问题在于您传递给 geolocator.reverse() 的坐标字符串,如笔记本中所示:
##geolocator.reverse( "37.85 "+" , "+"-122.24" ).raw['address']
多余的空格可能会破坏它。 幸运的是,geopy 提供了如何将查询坐标提交给反向查找功能的多个选项:
:type query: :class:`geopy.point.Point`, list or tuple of ``(latitude,
longitude)``, or string as ``"%(latitude)s, %(longitude)s"``.
我建议将其作为元组传递,而不是尝试构造字符串。我不清楚
cord
变量的传入格式是什么,但根据可用的示例,它似乎是一个列表[str, str]。我会剥离每一块并将其转换为浮点数。我添加了一个 try/ except 块,以便在转换引发异常时将值设置为 None,但您可能更愿意退出而不设置位置数据(也许返回成功或失败标志?)
def location(cord):
try:
Latitude = float(cord[0].strip())
Longitude = float(cord[1].strip())
except ValueError:
print(f"Invalid location inputs {cord}") #Logging would be better if possible
loc = {"road": None, "county": None}
else:
# Technically its ok, but I renamed this to loc to avoid conflict with function name
# Pass coordinates as a tuple
loc = geolocator.reverse((Latitude, Longitude)).raw['address']
#if the values are missing replace by empty string
if loc.get('road') is None:
loc['road'] = None
if loc.get('county') is None:
loc['county'] = None
finally:
loc_update['county'].append(loc['county'])
loc_update['road'].append(loc['road'])