API reference¶
Basemaps for matplotlib and GeoPandas rendered from MapLibre styles.
The parameters are those of contextily's add_basemap, but the basemap is a MapLibre style (vector
tiles, labels, icons) rendered with ml2drender: for the extent of the axes, at their resolution and
directly in their CRS, instead of raster tiles that are merged and warped.
STYLES
module-attribute
¶
Named MapLibre styles that load without an API key, for source="OpenFreeMap.Positron" and others.
Each is the URL of a style JSON, or the style: maps of the world, then of national mapping agencies
(their country only), terrain and imagery. Mapterhorn.Hillshade is transparent, to draw over
another basemap. Check the terms of each service before heavy use.
add_attribution
¶
add_attribution(ax: Axes, text: str, font_size: float = ATTRIBUTION_SIZE, **kwargs: Any) -> Annotation
Write a text in the bottom right corner of the axes, as MapLibre's attribution control does.
The text is on a translucent white box. Its words are wrapped to the width of the axes whenever the figure is drawn, and what still does not fit (a word wider than the axes) is clipped. The font is matplotlib's default, backed up by an installed font of Chinese, Japanese and Korean (Hiragino Sans, Yu Gothic, Noto Sans CJK JP, ...) for attributions in those.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
Axes to write on. |
required |
text
|
str
|
Text to write. |
required |
font_size
|
float
|
Size of the text in points. Defaults to 8. |
ATTRIBUTION_SIZE
|
**kwargs
|
Any
|
Passed to matplotlib's |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Annotation |
Annotation
|
The text. |
Examples:
>>> import basemaplibre as bml
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> bml.add_attribution(ax, "© OpenStreetMap contributors").get_text()
'© OpenStreetMap contributors'
Source code in basemaplibre/_plotting.py
add_basemap
¶
add_basemap(ax: Axes, zoom: Zoom = 'auto', source: Source | None = None, headers: dict[str, str] | None = None, interpolation: str = 'bilinear', attribution: str | bool | None = None, attribution_size: float = ATTRIBUTION_SIZE, reset_extent: bool = True, crs: Any = None, resampling: Any = None, zoom_adjust: float | None = None, timeout: Timeout = None, *, labels: Labels = True, pixel_ratio: float | None = None, load: Loader | None = None, **extra_imshow_args: Any) -> None
Draw a basemap rendered from a MapLibre style under the data of the axes.
The parameters are those of contextily's add_basemap; labels, pixel_ratio and load are
new. The style is drawn directly in crs, if ml2drender supports its projection. Axes that
stretch the latitudes of a geographic CRS (as GeoPandas does) get the equidistant cylindrical
projection they show, so labels keep their shape.
The image covers the extent of the axes exactly, at the resolution of the figure. It is rendered
here, so that errors are raised, and again whenever the axes are drawn with other limits, at
another size or at another resolution: after zooming, panning or fig.tight_layout(), or by
fig.savefig(dpi=300). That may download tiles, and raises its errors where the figure is
drawn.
Styles of OpenHistoricalMap's tiles are drawn as of today, as on its website.
Failed requests (network errors, timeouts, HTTP 429 and 5xx) are tried twice more, after 1 and
2 seconds. Downloads are cached on disk, by URL and headers (see set_cache_dir), but for the
style and its TileJSON, which name the current tiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
Axes to draw on, with their extent in |
required |
zoom
|
float | auto
|
Level of detail as in contextily: the zoom of 256-pixel XYZ
tiles of the same scale, which may be fractional (the style is evaluated at MapLibre
zoom |
'auto'
|
source
|
Source
|
MapLibre style: a style dict, the URL of a style JSON, a name of
|
None
|
headers
|
dict[str, str]
|
HTTP headers sent with every request. Defaults to None. |
None
|
interpolation
|
str
|
Interpolation of |
'bilinear'
|
attribution
|
str | bool
|
Text in the corner, added to the one of a basemap drawn before. None and True are the attributions of the style and the sources it draws, False is none. Defaults to None. |
None
|
attribution_size
|
float
|
Size of the attribution in points. Defaults to 8. |
ATTRIBUTION_SIZE
|
reset_extent
|
bool
|
Whether to keep the limits of the axes. Defaults to True. |
True
|
crs
|
Any
|
CRS of the axes: anything |
None
|
resampling
|
Any
|
Ignored with a |
None
|
zoom_adjust
|
float
|
Added to the zoom, "auto" too; may be fractional. Defaults to None. |
None
|
timeout
|
float | tuple[float, float]
|
Limit of each request in seconds, or of connecting and reading. Defaults to None, which is no limit as in contextily. |
None
|
labels
|
bool | only | above
|
Whether to draw the style's symbol layers
(labels and icons). "only" draws nothing else, as an overlay above the data ( |
True
|
pixel_ratio
|
float
|
Image pixels per CSS pixel, whatever the resolution of the
figure; with |
None
|
load
|
Loader
|
Async |
None
|
**extra_imshow_args
|
Any
|
Passed to |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ImportError
|
If |
Examples:
>>> import basemaplibre as bml
>>> ax = gdf.plot(alpha=0.5)
>>> bml.add_basemap(ax, crs=gdf.crs, source="OpenFreeMap.Positron")
Source code in basemaplibre/_plotting.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
set_cache_dir
¶
Keep the resources add_basemap downloads in a directory, as contextily's set_cache_dir.
By default they are kept in a temporary directory removed when Python exits. A directory set here is kept, so a later session that sets it again reuses them; delete it to download them again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
The directory, made if missing. |
required |