Morecantile
Morecantile is a Python library for constructing and using map tile grids, also known as TileMatrixSet (TMS). It extends the functionality of libraries like mercantile by supporting a wider range of TileMatrixSet grids beyond just Web Mercator. The library is currently at version 7.0.3 and maintains an active release cadence, providing tools to work with OGC TMS specifications (currently TMS 2.0).
Warnings
- breaking Morecantile 7.0.0 dropped support for Python versions 3.8, 3.9, and 3.10. It now requires Python 3.11 or newer.
- breaking Starting with version 7.0.0, the default CRS for GeoJSON outputs from methods like `TileMatrixSet.feature` is now WGS84 (EPSG:4326) as per specification. Additionally, property names like `grid_name` and `grid_crs` were renamed to `tms` and `tms_crs` respectively within GeoJSON responses.
- gotcha The `TileMatrixSet.is_valid` method became strict by default in 7.0.0. This means it will raise an error if a requested tile's zoom level is beyond the TMS's `maxzoom` and a corresponding `TileMatrix` is not explicitly defined, whereas previous versions might have issued a `UserWarning`.
- breaking Morecantile versions 4.0.0 and above adopted the OGC TMS 2.0 specification, which introduced several breaking changes to the TMS JSON document structure. Key field renames include `identifier` to `id`, `supportedCRS` to `crs`, `tileMatrix` to `tileMatrices`, and `topLeftCorner` to `pointOfOrigin`.
Install
-
pip install morecantile
Imports
- tms
import morecantile tms = morecantile.tms.get('WebMercatorQuad') - Tile
from morecantile import Tile
- TileMatrixSet
from morecantile.models import TileMatrixSet
Quickstart
import morecantile
# Get a default TileMatrixSet (TMS) like WebMercatorQuad
tms = morecantile.tms.get("WebMercatorQuad")
# Define a tile (Z, X, Y)
tile = morecantile.Tile(x=10, y=10, z=4)
# Get the bounding box of the tile in the TMS's CRS (e.g., EPSG:3857 for WebMercatorQuad)
xy_bounds = tms.xy_bounds(tile)
print(f"Tile {tile} XY Bounds: {xy_bounds}")
# Get the bounding box of the tile in Geographic CRS (e.g., EPSG:4326)
geographic_bounds = tms.bounds(tile)
print(f"Tile {tile} Geographic Bounds: {geographic_bounds}")
# List available default TileMatrixSets
print(f"Available TMS grids: {morecantile.tms.list()[:3]}...")