Skip to content Skip to sidebar Skip to footer

Contextily Add_basemap Uses Wrong Extent And Zoom Level

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 correctl

Solution 1:

The GML file of Linz is based on Gauss-Krüger system M31-5Mio (EPSG:31255). Here is runnable code that demonstrates all the steps to produce a plot of the GML with basemap requested from webmap tiles' provider of choice.

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

The output plot:

linz-map

Post a Comment for "Contextily Add_basemap Uses Wrong Extent And Zoom Level"