首页 > 解决方案 > 发生异常:将带有上下文的底图添加到 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()

我尝试了不同的地图提供者无济于事,并看到几个链接提供了与参考包相关的建议,我不确定这是否适用于此。提前致谢!

标签: pythonsslgisgeopandascontextily

解决方案


我找到了一个不太理想且不安全的临时解决方案,即通过在 C:\path_to_python_libraries\contextily\tile.py 的第 443 行中设置来关闭 SSL 验证verify=False,使其变为:

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

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


推荐阅读