Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def draw(self):
ax = plot.ax
if len(self.df.geometry) == 0:
return ax
basemap, extent = ctx.bounds2img(
*self._webmap_extent, zoom=self.zoom,
url=getattr(ctx.sources, provider), ll=True
)
extent = (extent[0], extent[1], extent[3], extent[2])
ax.imshow(basemap, extent=extent, interpolation='bilinear')
return ax
def add_basemap(ax, zoom, url='http://tile.stamen.com/terrain/tileZ/tileX/tileY.png'):
xmin, xmax, ymin, ymax = ax.axis()
basemap, extent = ctx.bounds2img(xmin, ymin, xmax, ymax, zoom=zoom, url=url)
ax.imshow(basemap, extent=extent, interpolation='bilinear')
# restore original x/y limits
ax.axis((xmin, xmax, ymin, ymax))
ctx.add_basemap(ax)
###############################################################################
# We can control the detail of the map tiles using the optional `zoom` keyword
# (be careful to not specify a too high `zoom` level,
# as this can result in a large download).:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, zoom=12)
###############################################################################
# By default, contextily uses the Stamen Terrain style. We can specify a
# different style using ``ctx.providers``:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, url=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()
# Add background tiles to plot
# ============================
#
# We can use `add_basemap` function of contextily to easily add a background
# map to our plot. :
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax)
###############################################################################
# We can control the detail of the map tiles using the optional `zoom` keyword
# (be careful to not specify a too high `zoom` level,
# as this can result in a large download).:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, zoom=12)
###############################################################################
# By default, contextily uses the Stamen Terrain style. We can specify a
# different style using ``ctx.providers``:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, url=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()
df = df.to_crs(epsg=3857)
###############################################################################
import contextily as ctx
###############################################################################
# Add background tiles to plot
# ============================
#
# We can use `add_basemap` function of contextily to easily add a background
# map to our plot. :
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax)
###############################################################################
# We can control the detail of the map tiles using the optional `zoom` keyword
# (be careful to not specify a too high `zoom` level,
# as this can result in a large download).:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, zoom=12)
###############################################################################
# By default, contextily uses the Stamen Terrain style. We can specify a
# different style using ``ctx.providers``:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
ctx.add_basemap(ax, url=ctx.providers.Stamen.TonerLite)
ax.set_axis_off()
zoom = kwargs.pop('zoom', None)
# The plot extent is a well-defined function of plot data geometry and user input to
# the "extent" parameter, except in the case of numerical instability or invalid user
# input, in which case the default plot extent for the given projection is used. But
# the default extent is not well-exposed inside of the Cartopy API, so in edge cases
# where we are forced to fall back to default extent we don't actually know the true
# plot extent.
#
# For this reason we (1) recalculate "good case" plot extent here, instead of saving
# the value to an init variable and (2) accept that this calculation is potentially
# incorrect in edge cases.
extent = relax_bounds(*self.df.total_bounds) if self.extent is None else self.extent
if zoom is None:
zoom = ctx.tile._calculate_zoom(*extent)
else:
howmany = ctx.tile.howmany(*extent, zoom, ll=True, verbose=False)
if howmany > 100:
better_zoom_level = ctx.tile._calculate_zoom(*extent)
warnings.warn(
f'Generating a webmap at zoom level {zoom} for the given plot extent '
f'requires downloading {howmany} individual tiles. This slows down '
f'plot generation and places additional pressure on the tile '
f'provider\'s server, which many deny your request when placed under '
f'high load or high request volume. Consider setting "zoom" to '
f'{better_zoom_level} instead. This is the recommended zoom level for '
f'the given plot extent.'
)
self.zoom = zoom
self._webmap_extent = extent
# input, in which case the default plot extent for the given projection is used. But
# the default extent is not well-exposed inside of the Cartopy API, so in edge cases
# where we are forced to fall back to default extent we don't actually know the true
# plot extent.
#
# For this reason we (1) recalculate "good case" plot extent here, instead of saving
# the value to an init variable and (2) accept that this calculation is potentially
# incorrect in edge cases.
extent = relax_bounds(*self.df.total_bounds) if self.extent is None else self.extent
if zoom is None:
zoom = ctx.tile._calculate_zoom(*extent)
else:
howmany = ctx.tile.howmany(*extent, zoom, ll=True, verbose=False)
if howmany > 100:
better_zoom_level = ctx.tile._calculate_zoom(*extent)
warnings.warn(
f'Generating a webmap at zoom level {zoom} for the given plot extent '
f'requires downloading {howmany} individual tiles. This slows down '
f'plot generation and places additional pressure on the tile '
f'provider\'s server, which many deny your request when placed under '
f'high load or high request volume. Consider setting "zoom" to '
f'{better_zoom_level} instead. This is the recommended zoom level for '
f'the given plot extent.'
)
self.zoom = zoom
self._webmap_extent = extent
def draw(self):
ax = plot.ax
if len(self.df.geometry) == 0:
return ax
basemap, extent = ctx.bounds2img(
*self._webmap_extent, zoom=self.zoom,
url=getattr(ctx.sources, provider), ll=True
)
extent = (extent[0], extent[1], extent[3], extent[2])
ax.imshow(basemap, extent=extent, interpolation='bilinear')
return ax
# ============================
#
# Now we can use the above function to easily add a background map to our
# plot. The `zoom` keyword is required and let's you specify the detail of the
# map tiles (be careful to not specify a too high `zoom` level, as this can
# result in a large download):
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
add_basemap(ax, zoom=10)
###############################################################################
# By default, contextily uses the Stamen Terrain style. We can specify a
# different style using ``ctx.sources``:
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
add_basemap(ax, zoom=11, url=ctx.sources.ST_TONER_LITE)
ax.set_axis_off()
# The plot extent is a well-defined function of plot data geometry and user input to
# the "extent" parameter, except in the case of numerical instability or invalid user
# input, in which case the default plot extent for the given projection is used. But
# the default extent is not well-exposed inside of the Cartopy API, so in edge cases
# where we are forced to fall back to default extent we don't actually know the true
# plot extent.
#
# For this reason we (1) recalculate "good case" plot extent here, instead of saving
# the value to an init variable and (2) accept that this calculation is potentially
# incorrect in edge cases.
extent = relax_bounds(*self.df.total_bounds) if self.extent is None else self.extent
if zoom is None:
zoom = ctx.tile._calculate_zoom(*extent)
else:
howmany = ctx.tile.howmany(*extent, zoom, ll=True, verbose=False)
if howmany > 100:
better_zoom_level = ctx.tile._calculate_zoom(*extent)
warnings.warn(
f'Generating a webmap at zoom level {zoom} for the given plot extent '
f'requires downloading {howmany} individual tiles. This slows down '
f'plot generation and places additional pressure on the tile '
f'provider\'s server, which many deny your request when placed under '
f'high load or high request volume. Consider setting "zoom" to '
f'{better_zoom_level} instead. This is the recommended zoom level for '
f'the given plot extent.'
)
self.zoom = zoom
self._webmap_extent = extent