pythonsslgisgeopandascontextily

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


I'm trying to use Contextily to add a basemap to a map created with GeoPandas, but am getting the following error:

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)

Here is my code:

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()

I have tried different map providers to no avail and saw several links giving suggestions related to a reference package, which I'm not sure are applicable here. Thanks in advance!


Solution

  • I found a less than ideal and not secure makeshift solution, which is to turn off the SSL verification by setting verify=False in C:\path_to_python_libraries\contextily\tile.py, line 443, so that it becomes:

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

    This solution is less than ideal, but will do the job until a permanent solution is found. I'm still hoping there's a soul out there that knows how to fix this issue cleanly and permanently.