首页 > 解决方案 > 在 Jupyter 笔记本中使用 Geopy - 我的 lambda 查询不起作用

问题描述

我在 Jupyter Notebook 和 Python 中使用 Geopy。我想检索街道地址的纬度和经度。我想将查询限制在德克萨斯州,而 lambda 代码不起作用……有人可以帮忙吗?即使使用“Houston, Tx”也行不通。街道地址是“1931 Santa Rosa, Houston, Tx 77023”。

#pip install geopy   #https://geopy.readthedocs.io/en/latest/
from geopy.geocoders import Nominatim
from functools import partial
import urllib.parse

geocode = lambda query: geolocator.geocode("%s, Houston, TX" % query)

geolocator = Nominatim(user_agent="team5")
location = geolocator.geocode("1931 Santa Rosa 77023")

print((location.latitude, location.longitude))
print(location.address)

上面的结果是:

(30.8757886, -87.2426917) 1931, Brownsdale Loop Road, Santa Rosa County, Florida, 32565, 美国

标签: pythonjupyter-notebookgeopy

解决方案


当您打电话时,location = geolocator.geocode("1931 Santa Rosa 77023")您将覆盖顶部的查询分配

geolocator = Nominatim(user_agent="team5")
geocode = lambda query: geolocator.geocode("%s, Houston, TX" % query)
print(geocode("1931 Santa Rosa 77023")) # CUSTOM QUERY
print(geolocator.geocode("1931 Santa Rosa 77023")) # WITHOUT CUSTOM QUERY

查看输出的差异

None
1931, Brownsdale Loop Road, Santa Rosa County, Florida, 32565, United States

但是请注意,您始终可以运行

print(geolocator.geocode("1931 Santa Rosa 77023", exactly_one=False))

获取给定地址的所有可用位置的列表,在这种情况下,佛罗里达州似乎是唯一与您的查询字符串匹配的位置

如果您使用openmaps自己运行手动搜索,则可以确认这一点


推荐阅读