上下文 add_basemap 使用了错误的范围和缩放级别

Contextily add_basemap uses wrong extent and zoom level

我正在尝试将上下文底图添加到包含 GeoPandas 数据框的 Matplotlib 图。当我使用 df.plot 绘制数据框时,地图范围计算正确。

但是,当我尝试添加上下文底图时,地图范围(和缩放级别)计算错误并显示以下警告:

UserWarning: The inferred zoom level of 27 is not valid for the current tile provider (valid zooms: 0 - 20).

我正在尝试执行以下代码:

df = gpd.read_file('linz/StatBez_Linz_EPSG_4326.gml')
df = df.to_crs(epsg=3857)

fig = plt.figure(figsize=(16,9))
ax = plt.subplot()
ctx.add_basemap(ax = ax, source=ctx.providers.Stamen.Toner, crs=df.crs.to_string())
df.plot(color='none',edgecolor='green', ax = ax)

df.tail()的输出可以在这里看到:

gml 文件来自 data.gv.at

林茨的 GML 文件基于 Gauss-Krüger 系统 M31-5Mio (EPSG:31255)。这是可运行的代码,演示了生成 GML 图的所有步骤,底图是从 webmap tiles 的选择提供商请求的。

import contextily as ctx
import geopandas
import matplotlib.pyplot as plt

# Read GML
linz_districts = geopandas.read_file('./data/StatBez_Linz.gml')

# The coordinates are in the Gauss-Krüger system M31-5Mio. 
# CRS is EPSG:31255
# Set proper coordinate system to the geoDataFrame
linz_31255 = linz_districts.set_crs(31255)

# Convert CRS to Web-Mercator to match basemap layer
linz_3857 = linz_31255.to_crs('epsg:3857')

# plot Linz
ax = linz_3857.plot(figsize=(9, 16), zorder=10, ec='gray', alpha=0.4)

# plot basemap (it uses 'epsg:3857')
src_basemap = ctx.providers.Stamen.Terrain
ctx.add_basemap( ax, source=src_basemap, alpha=0.6, zorder=8 )

# Also possible with
#ctx.add_basemap( ax, source='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png' )

# manipulate xticks, use format
ax.set_xticklabels(['{:,.0f}'.format(x) for x in ax.get_xticks()]);
ax.set_yticklabels(['{:,.0f}'.format(y) for y in ax.get_yticks()]);

输出图: