TransWikia.com

Contextily add_basemap uses wrong extent and zoom level

Geographic Information Systems Asked by tuesday on April 29, 2021

I’m trying to add a contextily basemap to a Matplotlib figure containing a GeoPandas data frame. When I just plot the data frame using df.plot the map extent is calculated correctly.

However, when I try adding a contextily basemap the map extent (and zoom level) is calculated wrongly and the following warning is shown:

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

I’m trying to execute the following code:

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)

The output of df.tail() can be seen here:

enter image description here

The gml file is from data.gv.at

One Answer

As pointed out in a comment above, the GeoDataFrame needs to be plotted on the axis first before adding the basemap with contextily.

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()
df.plot(color='none',edgecolor='green', ax = ax)
ctx.add_basemap(ax = ax, source=ctx.providers.Stamen.Toner)

Correct answer by tuesday on April 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP