发生异常:将带有 Contextily 的底图添加到 GeoPandas 地图时出现 SSLError

Exception has occurred: SSLError when adding basemap with Contextily to GeoPandas map

我正在尝试使用 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 行中设置 verify=False 来关闭 SSL 验证,这样它就变成了:

request = requests.get(tile_url, headers={"user-agent": USER_AGENT}, verify=False)

此解决方案不太理想,但在找到永久解决方案之前可以完成工作。我仍然希望有一个灵魂知道如何彻底和永久地解决这个问题。