tilefetch

Download XYZ, TMS and WMTS map tiles for a bbox or polygon into MBTiles, PMTiles, z/x/y folders or one georeferenced GeoTIFF/PNG mosaic, with provider-policy limits, caching and resume.

Version 0.1.2 · Python >=3.9 · MIT licence
pip install tilefetch
PyPISourceIssuesChangelogCite (DOI)

tilefetch

PyPI version Python versions pipeline status License: MIT DOI Downloads

Download XYZ / TMS / WMTS map tiles for an area and get one georeferenced GeoTIFF/PNG, an MBTiles file, a PMTiles v3 archive or a z/x/y folder - politely, resumably, and within each provider's terms.

tilefetch is for GIS and machine-learning work that needs a basemap or imagery for an area: as one image with correct georeferencing (to drop into QGIS, rasterio or a training pipeline), or as an offline tile package. It has a single required dependency (Pillow) and writes GeoTIFF, MBTiles and PMTiles itself.

Responsible use - read this first

Tile servers are run by people and organisations who pay for the bandwidth. Most free community tile servers forbid bulk or offline downloading; for example the OSMF Tile Usage Policy forbids it for tile.openstreetmap.org. tilefetch is built to keep you inside the rules:

You remain responsible for complying with the terms of every service you use.

Installation

pip install tilefetch                 # Pillow only
pip install "tilefetch[rasterio]"     # optional: write GeoTIFFs through GDAL/rasterio
pip install "tilefetch[dev]"          # pytest, ruff, mypy, pmtiles (reference reader for tests)

Python 3.9+ on Windows, macOS and Linux.

Quickstart

import tilefetch

bbox = (-105.30, 39.95, -105.20, 40.05)   # west, south, east, north (degrees)

# 1. How big is the job?
est = tilefetch.estimate("usgs-imagery", bbox, "12-15")
print(est.total_tiles, est.estimated_bytes, est.within_limit)

# 2. An offline package (MBTiles, PMTiles or a z/x/y folder, chosen by extension)
tilefetch.fetch("usgs-topo", bbox, "12-15", out="boulder.mbtiles")
tilefetch.fetch("usgs-topo", bbox, "12-15", out="boulder.pmtiles")

# 3. One georeferenced image cropped to exactly the bbox
res = tilefetch.mosaic("usgs-imagery", bbox, 15, out="boulder.tif")
print(res.image.size, res.bounds, res.crs)          # EPSG:3857 bounds of the GeoTIFF

# ... or in EPSG:4326, as PNG + world file (.pgw) + .prj
tilefetch.mosaic("usgs-imagery", bbox, 15, out="boulder_4326.png", crs="EPSG:4326")

# 4. Your own tile server (any XYZ template)
tilefetch.fetch(
    "https://tiles.example.org/{z}/{x}/{y}.png",
    bbox, 14, out="area.pmtiles",
    attribution="© My Org", user_agent="myproject/1.0 ([email protected])",
)

The same from the shell:

tilefetch providers
tilefetch estimate -p usgs-imagery --bbox=-105.30,39.95,-105.20,40.05 -z 12-15
tilefetch get -p usgs-topo --bbox=-105.30,39.95,-105.20,40.05 -z 12-15 -o boulder.pmtiles
tilefetch get --url "https://tiles.example.org/{z}/{x}/{y}.png" \
    --bbox=-105.3,39.95,-105.2,40.05 --zoom 12-15 -o out.pmtiles --attribution "© My Org"
tilefetch mosaic -p usgs-imagery --bbox=-105.30,39.95,-105.20,40.05 -z 15 -o out.tif
tilefetch mosaic --input boulder.mbtiles --bbox=-105.25,39.98,-105.22,40.0 -z 15 -o crop.png --crs EPSG:4326
tilefetch info boulder.pmtiles

Use --bbox=... (with =) when the first coordinate is negative, so it is not read as an option.

Built-in providers

Only services whose published terms allow scripted/offline use are included.

Name Data Zoom Limits per job Terms
usgs-topo USGS Topo base map (US & territories) 0-16 50,000 tiles, 8 req/s, 4 threads USGS: public domain, no restrictions
usgs-imagery USGS orthoimagery (US & territories) 0-16 same same
usgs-shaded-relief USGS shaded relief (US & territories) 0-16 same same
nasa-bluemarble NASA Blue Marble Next Generation (global, 500 m) 0-8 100,000 tiles, 16 req/s, 8 threads NASA GIBS bulk-download guidance
nasa-bluemarble-relief Blue Marble with shaded relief and bathymetry 0-8 same same
nasa-modis-terra MODIS Terra daily true colour (params={"time": "YYYY-MM-DD"}) 0-9 same same

Register your own entries with tilefetch.register_provider(TileSource(...)).

Sources

from tilefetch import TileSource, wmts_source

# XYZ / TMS templates
TileSource.from_url("https://t.example.org/{z}/{x}/{y}.png")              # XYZ
TileSource.from_url("https://t.example.org/{z}/{x}/{y}.png", scheme="tms")  # TMS rows
TileSource.from_url("https://t.example.org/{z}/{x}/{-y}.png")             # TMS row placeholder
TileSource.from_url("https://{s}.t.example.org/{z}/{x}/{y}.png", subdomains=("a", "b"))
TileSource.from_url("https://t.example.org/q/{q}.jpeg")                   # Bing-style quadkey
TileSource.from_url("https://wms.example.org/?SERVICE=WMS&REQUEST=GetMap&LAYERS=x"
                    "&SRS=EPSG:3857&BBOX={bbox}&WIDTH=256&HEIGHT=256&FORMAT=image/png")
TileSource.from_url("https://t.example.org/{z}/{x}/{y}.png?key={key}", params={"key": "..."})

# WMTS: REST (ResourceURL) or KVP GetTile, from a GetCapabilities URL, file or XML string
src = wmts_source(
    "https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/1.0.0/WMTSCapabilities.xml",
    "MODIS_Terra_CorrectedReflectance_TrueColor",
    dimensions={"Time": "2024-06-01"},
    attribution="NASA GIBS",
)

wmts_source parses the TileMatrixSets, keeps only Web Mercator ones and maps each TileMatrix to an XYZ zoom from its scale denominator (OGC 0.28 mm pixel), so matrix identifiers such as EPSG:3857:12, 512-px tiles and regional matrix sets whose top-left corner is offset from the world origin all work.

Outputs

Output How Notes
MBTiles out="x.mbtiles" MBTiles 1.3: metadata + tiles tables, rows in TMS order, name, format, bounds, center, minzoom, maxzoom, attribution, description, type, version
PMTiles v3 out="x.pmtiles" 127-byte header, Hilbert tile IDs, varint directories, gzip internal compression, root directory in the first 16 KiB with leaf directories, de-duplicated tiles and run-lengths, clustered. Validated against the reference pmtiles reader
Folder any other path z/x/y.png (extension from the tile bytes) + metadata.json
GeoTIFF .tif / .tiff Pillow-only writer with ModelPixelScale, ModelTiepoint and GeoKeys for EPSG:3857 or EPSG:4326, deflate-compressed; or engine="rasterio"
PNG / JPEG / WebP .png / .jpg / .webp plus world file (.pgw/.jgw/.wld) and .prj (ESRI WKT)

Mosaics (tilefetch.mosaic or an image out= in fetch) are built at one zoom level:

Missing tiles (HTTP 404/204) stay transparent. A mosaic can also be built offline from an existing .mbtiles, .pmtiles or folder: tilefetch.mosaic("area.mbtiles", bbox, 14, out=...).

Downloading behaviour

Tile math

from tilefetch import (tiles_for_bbox, tiles_for_geometry, lonlat_to_tile, tile_bounds_mercator,
                       tile_to_quadkey, quadkey_to_tile, flip_y, zxy_to_tileid)

tiles_for_bbox((28.97, 41.00, 28.99, 41.02), "14-15")   # antimeridian boxes (west > east) work
tiles_for_geometry("area.geojson", 14)                   # exact tile/polygon test, holes included
lonlat_to_tile(13.3777, 52.5163, 12)                     # Tile(z=12, x=2200, y=1343)
tile_to_quadkey((3, 3, 5))                               # '213'
flip_y(3, 5)                                             # 2  (XYZ <-> TMS)
zxy_to_tileid(1, 1, 0)                                   # 4  (PMTiles Hilbert ID)

Development

git clone https://gitlab.com/geophilo1/tilefetch_sdk.git
cd tilefetch_sdk
pip install -e ".[dev,rasterio]"
python -m pytest --cov=tilefetch
python -m ruff check . && python -m ruff format --check src tests && python -m mypy src

The tests never touch the internet: they start an HTTP tile server on 127.0.0.1 that serves generated tiles (and simulates 429/503/timeouts, ETags, WMTS REST/KVP and capabilities). Mosaic tests check georeferencing pixel by pixel; GeoTIFFs are re-read with rasterio when it is installed, and PMTiles archives with the reference pmtiles reader.

License

MIT © 2026 Yusuf Eminoğlu. See LICENSE. Map data and imagery downloaded with this tool remain under their providers' licences and terms.

Academic citation

@software{eminoglu_tilefetch,
  author  = {Eminoğlu, Yusuf},
  title   = {tilefetch},
  year    = {2026},
  version = {0.1.2},
  doi     = {10.5281/zenodo.24063367},
  url     = {https://doi.org/10.5281/zenodo.24063367}
}

See also CITATION.cff.

Related Geophilo SDKs

Other Geophilo SDKs