python - 发生异常:将带有上下文的底图添加到 GeoPandas 地图时出现 SSLError
问题描述
我正在尝试使用 Contextily 将底图添加到使用 GeoPandas 创建的地图中,但出现以下错误:
HTTPSConnectionPool(host='a.tile.openstreetmap.org', port=443): Max retries exceeded with url: /14/8452/7869.png (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))
File "D:\generate_udf_maps.py", line 42, in generate_map
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik)
File "D:\generate_udf_maps.py", line 60, in <module>
generate_map(gdb_path, None, operations_file)
这是我的代码:
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon
import contextily as ctx
fig, ax = plt.subplots()
p1 = Point(640864.516, 784724.566)
p2 = Point(638594.804, 788492.544)
p3 = Point(644076.197, 788141.637)
p4 = Point(641089.953, 789539.343)
df = gpd.GeoDataFrame(geometry=[p1, p2, p3, p4])
df.plot(ax=ax)
ctx.add_basemap(ax, source=ctx.providers.OpenStreetMap.Mapnik) # Error happens here. If this line is not present I get a map with just the points.
plt.show()
我尝试了不同的地图提供者无济于事,并看到几个链接提供了与参考包相关的建议,我不确定这是否适用于此。提前致谢!
解决方案
我找到了一个不太理想且不安全的临时解决方案,即通过在 C:\path_to_python_libraries\contextily\tile.py 的第 443 行中设置来关闭 SSL 验证verify=False
,使其变为:
request = requests.get(tile_url, headers={"user-agent": USER_AGENT}, verify=False)
该解决方案不太理想,但会在找到永久解决方案之前完成工作。我仍然希望有一个灵魂知道如何干净和永久地解决这个问题。
推荐阅读
- f# - 如何使用 HtmlDocument 的 TryGetHtml
- python - 使用 Python 用下限标准化
- java - RecyclerView Adapter with TextView
- sql - 如何在 DB2 查询中打印“TEST PASSED”
- ios - 使用 fastlane 运行 xcov 时找不到任何 .xccoverage 文件
- c++ - 在 C++ 中按值迭代?
- php - 如何将一个输入项值传递给另一个输入项?
- python - How to get the average value after grouping by columns?
- c++ - Why does std::sort fail to find the appropriate (static member) function overload?
- python - 如何通过 http 和 https 为 conda/anaconda 配置代理?