contextily.
add_basemap
Add a (web/local) basemap to ax.
Matplotlib axis with x_lim and y_lim set in Web Mercator (EPSG=3857)
[Optional. Default=’auto’] Level of detail for the basemap. If ‘auto’, if calculates it automatically. Ignored if url is a local file.
[Optional. Default: ‘http://tile.stamen.com/terrain/tileZ/tileX/tileY.png’] Source url for web tiles, or path to local file. If local, the file is read with rasterio and all bands are loaded into the basemap.
[Optional. Default=’bilinear’] Interpolation algorithm to be passed to imshow. See matplotlib.pyplot.imshow for further details.
[Optional. Defaults to attribution specified by the url] Text to be added at the bottom of the axis. This defaults to the attribution of the provider specified in url if available. Specify False to not automatically add an attribution, or a string to pass a custom attribution.
[Optional. Defaults to ATTRIBUTION_SIZE]. Font size to render attribution text with.
[Optional. Default=True] If True, the extent of the basemap added is reset to the original extent (xlim, ylim) of ax
[Optional. Default=None] CRS, expressed in any format permitted by rasterio, to use for the resulting basemap. If None (default), no warping is performed and the original Web Mercator (EPSG:3857, {‘init’ :’epsg:3857’}) is used.
[Optional. Default=Resampling.bilinear] Resampling method for executing warping, expressed as a rasterio.enums.Resampling method
Other parameters to be passed to imshow.
Examples
>>> db = gpd.read_file(ps.examples.get_path('virginia.shp')) .to_crs(epsg=3857)
Add a web basemap:
>>> ax = db.plot(alpha=0.5, color='k', figsize=(6, 6)) >>> ctx.add_basemap(ax, url=url) >>> plt.show()
Or download a basemap to a local file and then plot it:
>>> url = 'virginia.tiff' >>> _ = ctx.bounds2raster(*db.total_bounds, zoom=6, path=url) >>> ax = db.plot(alpha=0.5, color='k', figsize=(6, 6)) >>> ctx.add_basemap(ax, url=url) >>> plt.show()
add_attribution
Utility to add attribution text.
Text to be added at the bottom of the axis.
[Optional. Defaults to 8] Font size in which to render the attribution text.
method.
Matplotlib Text object added to the plot.
bounds2raster
Take bounding box and zoom, and write tiles into a raster file in the Spherical Mercator CRS (EPSG:3857)
West edge
South edge
East edge
North edge
Level of detail
Path to raster file to be written
[Optional. Default: ‘http://tile.stamen.com/terrain/tileZ/tileX/tileY.png’] URL for tile provider. The placeholders for the XYZ need to be tileX, tileY, tileZ, respectively. IMPORTANT: tiles are assumed to be in the Spherical Mercator projection (EPSG:3857).
[Optional. Default: False] If True, w, s, e, n are assumed to be lon/lat as opposed to Spherical Mercator.
[Optional. Default: 0] if the tile API is rate-limited, the number of seconds to wait between a failed request and the next try
[Optional. Default: 2] total number of rejected requests allowed before contextily will stop trying to fetch more tiles from a rate-limited API.
[Optional. Default: ‘http://tile.stamen.com/terrain/tileZ/tileX/tileY.png’] URL for tile provider. The placeholders for the XYZ need to be tileX, tileY, tileZ, respectively. See cx.sources.
Image as a 3D array of RGB values
Bounding box [minX, maxX, minY, maxY] of the returned image
bounds2img
Take bounding box and zoom and return an image with all the tiles that compose the map and its Spherical Mercator extent.
warp_tiles
Reproject (warp) a Web Mercator basemap into any CRS on-the-fly
raster dimensions (h, w, b)
Image as a 3D array (h, w, b) of RGB values (e.g. as returned from contextily.bounds2img)
Bounding box [minX, maxX, minY, maxY] of the returned image, expressed in Web Mercator (EPSG:3857)
[Optional. Default=’EPSG:4326’] Target CRS, expressed in any format permitted by rasterio. Defaults to WGS84 (lon/lat)
Bounding box [minX, maxX, minY, maxY] of the returned (warped) image
warp_img_transform
Reproject (warp) an img with a given transform and s_crs into a different t_crs
NOTE: this method works well with rasterio’s .read() approach to raster’s dimensions (b, h, w)
Image as a 3D array (b, h, w) of RGB values (e.g. as returned from rasterio’s .read() method)
Transform of the input image as expressed by rasterio and the affine package
Source CRS in which img is passed, expressed in any format permitted by rasterio.
Target CRS, expressed in any format permitted by rasterio.
Warped image as a 3D array (b, h, w) of RGB values (e.g. as returned from rasterio’s .read() method)
howmany
Number of tiles required for a given bounding box and a zoom level
[Optional. Default=True] If True, print short message with number of tiles and zoom.
Place
Geocode a place by name and get its map.
This allows you to search for a name (e.g., city, street, country) and grab map and location data from the internet.
The location to be searched.
The level of detail to include in the map. Higher levels mean more tiles and thus longer download time. If None, the zoom level will be automatically determined.
Path to a raster file that will be created after getting the place map. If None, no raster file will be downloaded.
The amount to adjust a chosen zoom level if it is chosen automatically.
The URL to use for downloading map tiles. See the cx.tile_providers module for some options, as well as cx.bounds2image for guidance.
cx.tile_providers
cx.bounds2image
The result of calling geopy.geocoders.Nominatim with search as input.
geopy.geocoders.Nominatim
search
The southern bbox edge.
The northern bbox edge.
The eastern bbox edge.
The western bbox edge.
The image corresponding to the map of search.
The bounding box of the returned image, expressed in lon/lat, with the following order: [minX, minY, maxX, maxY]
The bounding box of the returned image, expressed in Web Mercator, with the following order: [minX, minY, maxX, maxY]
Methods
plot(self[, ax, zoom, interpolation, …])
plot
Plot a Place object
Place.
Plot a Place object …
Matplotlib axis with x_lim and y_lim set in Web Mercator (EPSG=3857). If not provided, a new 12x12 figure will be set and the name of the place will be added as title
Matplotlib axis with x_lim and y_lim set in Web Mercator (EPSG=3857) containing the basemap
>>> lvl = ctx.Place('Liverpool') >>> lvl.plot()
plot_map
Plot a map of the given place.
The map to plot. If an ndarray, this must be an image corresponding to a map. If an instance of Place, the extent of the image and name will be inferred from the bounding box.
The axis on which to plot. If None, one will be created.
Whether to turn off the axis border and ticks before plotting.
[Optional. Default to standard ATTRIBUTION] Text to be added at the bottom of the axis.
The axis on the map is plotted.