Skip to main content

Your own tiles and services

Besides named basemaps and MapLibre styles, style takes tile data: a tile URL template, TileJSON, a PMTiles archive (URL or local file), or an ArcGIS service. basemapper detects whether the tiles are raster or vector and builds a style for them; tile_style() builds one with your overrides. OGC services (WMS, WMTS) have their own style builders.

import basemapper as bm

A tile URL

An XYZ template, as raster tiles:

bm.basemap("https://tile.opentopomap.org/{z}/{x}/{y}.png", 720, 405,
           center=(-111.65, 40.60), zoom=11)

PMTiles

A PMTiles archive is read with HTTP range requests, or from a local file. Raster archives draw as they are; here a USGS topographic map of Mount Whitney, fitted to the archive’s extent:

bm.basemap("https://pmtiles.io/usgs-mt-whitney-8-15-webp-512.pmtiles", 720, 405,
           bounds=[-118.3198, 36.5611, -118.2607, 36.5930])

Vector archives get a default style over every source layer:

firenze = "https://pmtiles.io/protomaps(vector)ODbL_firenze.pmtiles"
bm.basemap(firenze, 720, 405, center=(11.255, 43.773), zoom=13.5)

tile_style() sets which source layers it draws, and its paint:

print(sorted({l["source_layer"] for l in bm.list_layers(firenze)}))

styled = bm.tile_style(
    firenze,
    source_layers=["earth", "water", "landuse", "roads", "buildings"],
    paint={"fill-color": "#f2efe9", "line-color": "#8a8a8a"},
)
bm.basemap(styled, 720, 405, center=(11.255, 43.773), zoom=13.5)
['boundaries', 'buildings', 'earth', 'landcover', 'landuse', 'natural', 'physical_line', 'physical_point', 'places', 'pois', 'roads', 'transit', 'water']

With a basemap style

A vector archive can also be drawn with a full basemap style whose source points at it. protomaps-light.json and protomaps-dark.json are the Protomaps basemap styles with the Florence archive as their source ("url": "pmtiles://https://pmtiles.io/protomaps(vector)ODbL_firenze.pmtiles"), and a style file’s path works as style:

bm.basemap("styles/protomaps-light.json", 720, 405, center=(11.2551, 43.7710), zoom=15.2)

bm.basemap("styles/protomaps-dark.json", 720, 405, center=(11.2551, 43.7710), zoom=15.2)

For your own Protomaps archive, change the source’s url to pmtiles:// and the archive’s URL or path. These styles are for Protomaps tiles v3; archives built from late 2024 on use tiles v4, with the styles of @protomaps/basemaps 5 (how the styles were made).

TileJSON

A TileJSON URL works the same way; this is OpenFreeMap’s planet tileset, with the default vector style:

bm.basemap("https://tiles.openfreemap.org/planet", 720, 405,
           center=(-111.8887, 40.7556), zoom=12)

Your tiles in a basemap’s style

basemap_provider(name, tiles=) keeps a basemap’s style and swaps in your own tiles, which must use the same schema (OpenFreeMap’s styles use OpenMapTiles): self-hosted tiles, or an extract for offline use.

style = bm.basemap_provider("OpenFreeMap.Positron", tiles="https://tiles.openfreemap.org/planet")
bm.basemap(style, 720, 405, center=(-111.8887, 40.7556), zoom=12)

OGC services

wms_style() and wmts_style() build styles for OGC services. NASA’s Blue Marble, from GIBS:

gibs = bm.wms_style("https://gibs.earthdata.nasa.gov/wms/epsg3857/best/wms.cgi",
                    layers="BlueMarble_ShadedRelief_Bathymetry", format="image/jpeg")
bm.basemap(gibs, 720, 360, center=(0, 20), zoom=0.6)

In plots

Any of these goes in add_basemap() and geom_basemap():

import geodatasets
import geopandas as gpd
import matplotlib.pyplot as plt

nc = gpd.read_file(geodatasets.get_path("geoda.sids"))
fig, ax = plt.subplots(figsize=(8, 4.4))
nc.boundary.plot(ax=ax, color="black", linewidth=0.4)
ax.set_xlim(-84.4, -81.2)
ax.set_ylim(34.9, 36.6)
bm.add_basemap(ax, "https://tile.opentopomap.org/{z}/{x}/{y}.png", alpha=0.8, crs=nc.crs)
ax.set_axis_off()
Downloading file 'sids.zip' from 'https://geodacenter.github.io/data-and-lab//data/sids.zip' to '/home/runner/.cache/geodatasets'.
Extracting 'sids/sids.gpkg' from '/home/runner/.cache/geodatasets/sids.zip' to '/home/runner/.cache/geodatasets/sids.zip.unzip'

Tile cache

Tiles are cached on disk, for as long as the servers’ caching headers allow. tile_cache() shows where; tile_cache(clear=True) empties it, and tile_cache(False) turns it off for the session.