python - 在 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, 美国
解决方案
当您打电话时,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))
获取给定地址的所有可用位置的列表,在这种情况下,佛罗里达州似乎是唯一与您的查询字符串匹配的位置
推荐阅读
- python - 如何使用 pyglet 创建 3D?
- python - 根据列值将行拆分为多行
- python - 使用 folium 为特定国家绘制世界等值线图时出错
- php - 如何翻译 symfony 表单错误信息?
- java - 如何让代码等待所有请求完成 Android okhttp
- c++ - 在字符串中输入换行符
- python - How to iterate this n dimensional array?
- iso8583 - difference between iso8583 authorize request and authorize advice request
- ruby-on-rails - Ruby On Rails - "OpenSSL::Cipher::CipherError at ..." when connecting to imported database
- python-3.x - How to improve SpaCy matcher pattern