TileMapBase

0.4.7 · active · verified Fri Apr 17

TileMapBase is a Python library that enables the use of OpenStreetMap and other WMTS tiles as basemaps within Matplotlib figures. It simplifies fetching, caching, and rendering geographical tiles, allowing users to overlay their data on real-world maps. Currently at version 0.4.7, it is actively maintained with periodic updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize TileMapBase, define a geographical extent, create a Matplotlib figure and axes, add OpenStreetMap tiles using PlottingContext, and overlay a data point (London Eye) correctly using the `tilemapbase.crs_geographic` transform.

import matplotlib.pyplot as plt
import tilemapbase

# Crucial initialization step for tilemapbase. Set a user_agent to avoid 403 errors.
# python_accelerated=True can speed up rendering if numba is installed.
tilemapbase.init(python_accelerated=True, user_agent="my-tilemapbase-app")

# Define the map extent [lon_min, lon_max, lat_min, lat_max]
extent = [-0.20, 0.05, 51.45, 51.55] # Example: Central London

fig, ax = plt.subplots(figsize=(10, 10), dpi=100)
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
ax.set_title("London with TileMapBase")

# Create a PlottingContext. This sets up the matplotlib axes for the map projection.
# 'merc' is the standard projection for web maps.
plot_context = tilemapbase.plotting.PlottingContext(
    extent,
    ax=ax,
    proj="merc"
)

# Add OpenStreetMap tiles to the axes
tilemapbase.plotting.add_tile_wmts(
    ax, url=tilemapbase.get_providers_as_dict()["OpenStreetMap"], zoom=13
)

# Set axes limits based on the PlottingContext's projected extent
ax.set_xlim(plot_context.x_min, plot_context.x_max)
ax.set_ylim(plot_context.y_min, plot_context.y_max)
ax.set_aspect(plot_context.get_aspect())

# Example: Overlay a point (London Eye) in geographic coordinates
london_eye_lon, london_eye_lat = -0.1106, 51.5033
ax.plot(
    london_eye_lon, london_eye_lat,
    'o', color='red', markersize=8,
    transform=tilemapbase.crs_geographic, # Crucial: plot in geographic coordinates
    label="London Eye"
)
ax.legend()

plt.show()

view raw JSON →