terramask
Geographic masks for any area on Earth: the sea, lakes and rivers, the land, forests, glaciers, parks, or any layer of OpenStreetMap, as a coverage grid, outline polylines, a signed distance field or GeoJSON polygons. Areas can also be cut into depth or height bands from terrain tiles.
Everything comes from tiles fetched on demand, so there is nothing to download in advance: only the tiles covering the area, at the zoom that matches the output resolution, and only the layers asked for. Terrain is fetched only when bands are asked for.
One Rust core, published for Rust, Python, Node.js and the browser (WebAssembly). terramask grew out of watermask, which did the water only; see Coming from watermask.
Martha's Vineyard. The sea from OpenStreetMap, cut into zones at 5, 10, 20 and
30 m deep by terrain tiles; the land cut at 15, 30 and 50 m; the woods from the
landcover layer. The shoreline stays OpenStreetMap's: the terrain only decides
where one zone meets the next.
import terramask
vineyard = (-70.85, 41.3, -70.45, 41.55) # west, south, east, north
water = terramask.fetch(vineyard, width=1200) # tiles are cached after the first run
mask = water.mask(vineyard, 1200)
mask.coverage # (1000, 1200) float32, fraction of each pixel that is water
mask.outlines() # shoreline: [(n, 2) float32] in pixels, water on the left
mask.distance() # pixels to the shore, positive in water, negative on land
water.geojson() # the water as GeoJSON in lon/lat, one MultiPolygon per class
land = terramask.fetch(vineyard, width=1200, select="land")
woods = terramask.fetch(vineyard, width=1200, select=["forest", "parks"])
Water
Water is the default: the sea, lakes, rivers, canals and docks.
Cape Cod and the islands. The shoreline is mask.outlines(); the lines at sea are
outlines of mask.distance() at growing distances, fading out.
Amsterdam, Venice, Stockholm: mask.coverage in grey, the shoreline in black,
waterway centre lines in grey. cargo run --release --example readme redraws
every image here from live tiles.
Two layers of the tiles are read:
| layer | kind | classes (default in bold) |
|---|---|---|
water |
areas | ocean, lake, river, dock, pond, swimming_pool |
waterway |
lines | river, canal, stream, ditch, drain |
river areas are the water surface of wide rivers and canals; river lines
are centre lines, of every river, however narrow. Seasonal water
(intermittent) and water underground (tunnels) are left out unless asked
for.
Deriving water from elevation (everything at or below 0 m) misses lakes and rivers, which sit above sea level, and floods land that lies below it: the Netherlands, deltas, reclaimed coasts. Complete coastline and water datasets exist (the OSM water polygons), but they are gigabytes and have to be tiled and stored before use. Vector tile services already serve that data cut into tiles, for free, one small tile at a time.
Land, forests, parks and other layers
select picks other layers by preset or by rule; everything else (masks,
outlines, distance, GeoJSON) works the same:
import terramask
vineyard = (-70.85, 41.3, -70.45, 41.55)
land = terramask.fetch(vineyard, width=1200, select="land") # what the sea leaves
green = terramask.fetch(vineyard, width=1200, select=["forest", "parks"])
green.subset("forest") # one of the two
terramask.fetch(vineyard, width=1200, select=["landuse:cemetery", "park:*"]) # layer:class rules
| preset | rules |
|---|---|
water |
water:ocean,lake,river,dock waterway:river,canal,stream (the default) |
ocean |
water:ocean |
lakes |
water:lake |
rivers |
water:river waterway:river,stream |
land |
each tile less its ocean |
forest, glacier, wetland, sand, rock, grass, farmland |
landcover:wood, :ice, :wetland… |
parks |
park:* (national parks, nature reserves, protected areas) |
urban |
landuse:residential,commercial,industrial,retail |
buildings |
building:* |
roads |
transportation:motorway,trunk,primary,secondary,tertiary,minor,service,busway,raceway |
paths |
transportation:path,track |
road_names |
transportation_name:* (centrelines with name, ref, for labels) |
rail |
transportation:rail,transit |
Any layer and class of the OpenMapTiles schema
can be named as layer:class,class, or layer:* for all of it. A name picks
a class or a subclass: city parks are landcover:park (subclass park of
class grass), footways transportation:footway; landcover:grass/grass is
the lawns alone, not the rest of class grass. Only the layers asked for
are downloaded and cached, each on its own, so asking for forests after water
fetches the tiles once more, and only their landcover. GeoJSON features
carry layer, class, subclass and the other attributes of the tile
(brunnel, render_height…; names in other languages are left out).
Tiles cut every feature at their edges. merged joins the pieces of each
class into one shape, which suits masks and coastlines; joined mends each
feature on its own, so two buildings sharing a wall stay two, and strings a
road's pieces back into one line across the tile edges.
Depth and height bands
Terrain tiles give heights, the sea floor below zero. They are fetched only by
fetch_elevation; then split cuts every area at the levels given, and the
bands are areas like any other:
import terramask
vineyard = (-70.85, 41.3, -70.45, 41.55)
sea = terramask.fetch(vineyard, width=1200, select="ocean")
terrain = terramask.fetch_elevation(vineyard, width=1200)
zones = sea.split(terrain, [-30, -20, -10, -5]) # metres, depths negative
zones.areas # [AreaInfo(layer='water', cls='ocean', low=None, high=-30.0), …]
deep = zones.within(high=-20).mask(vineyard, 1200) # deeper than 20 m
zones.geojson() # properties: layer, class, min, max (null at the open ends)
terrain.grid(vineyard, 1200) # (1000, 1200) float32 metres
Each area is joined into one polygon, cut to the extent of the terrain tiles, then into bands: below the lowest level, between each pair, above the highest. The bands come from contours of the terrain (marching squares, closed at the edge of the tiles) and are intersected with the area, so together they cover it exactly and do not overlap. Where the terrain puts sea above sea level, as it can near a shore, those bits fall in the highest band.
By default the zoom stops at 10 (about 150 m per pixel at the equator), as
the sea floor has no more detail deeper. For detailed land heights pass
max_zoom up to 15. From 11 on, some coasts, much of the US for one, come
from land surveys that flatten the sea to 0 m; the fetcher puts the sea floor
back there from the zoom-10 tiles (in Rust, Elevation::fill_sea does it for
tiles you read yourself). For terrain magnified past its pixels,
Elevation::cubic(true) samples with Catmull-Rom instead of bilinear, so
slopes have no creases at pixel edges.
Where the data comes from
Map layers. By default OpenFreeMap: the whole
planet in the OpenMapTiles schema, rebuilt
weekly from OpenStreetMap, free with no key and no request limits. Any source
in that schema works (MapTiler, a self-hosted copy of OpenFreeMap's planet
file): pass its TileJSON URL or a {z}/{x}/{y} URL template.
At zoom 14, the deepest, a tile is about 2.4 km across at the equator and its coordinates are quantised to about 0.6 m; OpenMapTiles simplifies geometry there by less than that, so this is full OpenStreetMap detail. At lower zooms tiles are simplified and small features dropped, in step with the pixel size.
Terrain. Terrain Tiles
on AWS Open Data (Terrarium PNG, no key, CORS open), from many sources: see
their attribution.
Any Terrarium source works: pass a {z}/{x}/{y} URL template.
Attribution. Maps made with the map layers must credit "© OpenMapTiles © OpenStreetMap contributors"; maps with terrain bands, the terrain sources too.
How it works
- Zoom. The shallowest zoom whose tiles (drawn 256 px wide) have pixels
no larger than the output's, capped at the source's deepest zoom, then
lowered while the area needs more than
max_tilestiles (default 256). - Tiles. Fetched in parallel and cached on disk, only the layers asked
for, one file each (for water, a fifth of the bytes or less). The cache is
$TERRAMASK_CACHE, or else~/Library/Caches/terramaskon macOS,%LOCALAPPDATA%\terramaskon Windows,~/.cache/terramaskon Linux. A cached tile is used for 30 days, across OpenFreeMap's weekly builds, then downloaded again; when offline, older tiles are used anyway. Tiles past 30 days that nobody asks for are deleted, checked once a day. The TileJSON is read each run, since OpenFreeMap's tile URLs change with every build, and the last good copy is used when offline. - Decoding. A small protobuf reader pulls the layers out of each tile and cuts every feature to its own tile, so the buffer tiles share with their neighbours is not counted twice. Land is each tile's square less its sea; a tile with nothing in it is all land, since tile sources leave empty tiles out and the sea is always something.
- Mask. Polygons are filled with the nonzero winding rule, so the many pieces of one sea join without seams and islands stay holes. Each pixel gets its exact covered fraction along the row and four sub-rows down it.
- Outlines. Marching squares at coverage ½, joined into polylines with the covered side on the left.
- Distance. An exact Euclidean distance transform, signed.
- Polygons. For GeoJSON, the pieces of each layer and class are joined with a polygon union (i_overlay), same nonzero rule as the mask, so the polygons cover what the mask covers. The output is OGC-valid (shapely and GEOS take it as is), exteriors counter-clockwise and holes clockwise, and can be cut to a box.
- Bands. On request only: terrain tiles are decoded into one height field, contoured at each level, and the contours intersected with the joined areas (see Depth and height bands).
Masks are on a north-up Web Mercator grid. In Rust, Features::mask_with takes
any projection instead.
Rust
[dependencies]
terramask = "0.2" # default-features = false drops the HTTP client and terrain
Features: fetch (download and cache tiles) and dem (terrain, elevation
bands), both on by default.
use terramask::{Fetcher, Filter, GeoJsonOptions, Grid, MaskOptions, ZoomLimits};
let bounds = [-70.85, 41.3, -70.45, 41.55];
let grid = Grid::with_width(bounds, 1200);
let water = Fetcher::new().features_for(&grid, &Filter::default(), &ZoomLimits::default(), |done, total| {
eprintln!("{done}/{total} tiles");
})?;
let mask = water.mask(&grid, &MaskOptions::default());
let shore = mask.outlines();
let dist = mask.distance();
// Any projection: lon, lat → pixel.
let tm = water.mask_with(800, 600, &MaskOptions::default(), |lon, lat| my_projection(lon, lat));
// Polygons from anywhere, already in pixels: the same fill, outlines and distance.
let park = terramask::Mask::from_rings(800, 600, &[terramask::Ring { exterior: true, points: park_px }], 4);
let to_edge = park.distance();
// Polygons: joined per class and cut to the box (Features::merged gives the same as structs).
let json = water.to_geojson(&GeoJsonOptions { bounds: Some(bounds), ..Default::default() });
// Buildings and roads one by one, mended across tile edges, with their attributes.
let town = Fetcher::new().water(bounds, 14, &Filter::parse(&["buildings", "roads"])?, |_, _| {})?.joined(Some(bounds));
let bridges = town.lines.iter().filter(|l| l.tag("brunnel") == Some("bridge"));
// Other layers, and depth bands from terrain fetched only here.
let fetcher = Fetcher::new();
let woods = fetcher.features_for(&grid, &Filter::parse(&["forest", "parks"])?, &ZoomLimits::default(), |_, _| {})?;
let sea = water.subset(&Filter::parse(&["ocean"])?);
let terrain = fetcher.elevation_for(&grid, &ZoomLimits::default(), |_, _| {})?;
let zones = sea.split(&terrain, &[-30.0, -20.0, -10.0, -5.0])?; // Area::elevation holds each band
let deep = zones.within(f64::NEG_INFINITY, -20.0).mask(&grid, &MaskOptions::default());
Without the fetch feature, bring tiles from anywhere:
let mut water = terramask::Features::new();
for id in terramask::tiles_for(&bounds, 13) {
water.add_tile(id, &bytes_of(id), &Filter::default())?;
}
cargo run --release --example render -- -70.85,41.3,-70.45,41.55 1200 out.png
draws the mask, shoreline and waterways of an area.
Python
pip install terramask
The examples above cover most uses. Also:
terramask.fetch(bounds, width, areas=["ocean"], lines=[], source=..., log=print),
water.lines(bounds, width) for waterway centre lines as (class, (n, 2) array),
water.mask(..., line_width=1.5) to burn them into the mask,
Features().add_tile(z, x, y, data, select=...) and Elevation().add_tile(z, x, y, png)
for your own tiles, terrain.at(lon, lat), terramask.PRESETS,
zoom_for(bounds, width) and tiles_for(bounds, zoom).
water.geojson(bounds=vineyard) cuts the polygons and lines to the box;
water.geojson(pieces=True) keeps the areas as the tiles cut them. Into
shapely:
import terramask
from shapely.geometry import shape
vineyard = (-70.85, 41.3, -70.45, 41.55)
water = terramask.fetch(vineyard, width=1200)
sea = next(shape(f["geometry"]) for f in water.geojson(bounds=vineyard)["features"]
if f["properties"]["class"] == "ocean")
Node.js
npm install @pileoni/terramask
const tm = require('@pileoni/terramask')
const vineyard = [-70.85, 41.3, -70.45, 41.55]
const water = tm.fetch(vineyard, { width: 1200 }) // blocks until the tiles are in
const mask = water.mask(vineyard, 1200)
mask.coverage() // Float32Array, row 0 at the top
const [pts, ends] = mask.outlines() // x,y pairs; ends[i] = end of line i, in points
mask.distance() // Float32Array
water.geojson({ bounds: vineyard }) // string; also { pieces: true }
const green = tm.fetch(vineyard, { width: 1200, select: ['forest', 'parks'] })
const sea = water.subset(['ocean'])
const terrain = tm.fetchElevation(vineyard, { width: 1200 }) // maxZoom: up to 15 for land
const zones = sea.split(terrain, [-30, -20, -10, -5])
zones.areas() // [{ layer, class, low, high }, …]
zones.within(undefined, -20).mask(vineyard, 1200) // deeper than 20 m
Browser (WebAssembly)
npm install @pileoni/terramask-wasm
The page fetches the tiles; terramask reads them.
import init, { Features, zoomFor, tilesFor, tileUrl } from '@pileoni/terramask-wasm'
await init()
const [w, s, e, n] = [-70.85, 41.3, -70.45, 41.55]
const tilejson = await (await fetch('https://tiles.openfreemap.org/planet')).json()
const z = zoomFor(w, s, e, n, 1200, 256, tilejson.maxzoom)
const ids = tilesFor(w, s, e, n, z) // z, x, y, z, x, y, …
const water = new Features()
await Promise.all(Array.from({ length: ids.length / 3 }, async (_, i) => {
const [tz, tx, ty] = ids.slice(3 * i, 3 * i + 3)
const r = await fetch(tileUrl(tilejson.tiles[0], tz, tx, ty))
if (r.ok) water.addTile(tz, tx, ty, new Uint8Array(await r.arrayBuffer()))
}))
const mask = water.mask(w, s, e, n, 1200)
const json = water.geojson(false, [w, s, e, n]) // pieces?, bounds?
Other layers: Features.select(['forest', 'land']) instead of new Features().
Terrain: the same loop over tilesFor(w, s, e, n, zoomFor(w, s, e, n, 1200, 256, terrariumZoom()))
with tileUrl(terrarium(), …) into new Elevation()'s addTile, then
water.split(elevation, [-30, -20, -10]).
demo/index.html does this on a canvas: build the package into demo/pkg
(below), then serve the folder (python3 -m http.server -d demo).
Building
cargo test # core, offline (real tiles in tests/fixtures)
maturin develop --release && pytest # Python, in a virtualenv
cd node && npm install && npm run build && npm test
wasm-pack build crates/terramask-wasm --release --target web --out-dir ../../demo/pkg
Limits
- Masks are Web Mercator unless you project yourself (
mask_with, Rust only). - Waterway lines are cut to the box but not joined: a river is one line per tile it crosses.
- The union snaps coordinates to a grid of about 2³⁰ steps across the area: under a millimetre for a city, a few centimetres for the whole world.
- Water covered by something else (
covered=yes) is not in the tiles. - Across the 180° meridian, tiles are fetched correctly; GeoJSON longitudes on the far side run past 180.
- Landcover and parks at low zooms are generalised by OpenMapTiles much more than water; at a few kilometres per pixel small woods drop out.
- The
boundarylayer holds lines only, so countries and regions are not areas here. - Elevation bands are only as good as the terrain: the sea floor is about a kilometre per sample offshore, and terrain tiles of one zoom are read at a time.
Coming from watermask
terramask is watermask 0.2 plus other layers, land and elevation bands, under a new name. With the default selection it returns the same water. To move over, change the package name, then:
| watermask | terramask |
|---|---|
Water (Rust, Python, Node, wasm) |
Features |
Fetcher::water_for (Rust) |
Fetcher::features_for |
water_fraction on masks (Rust, Python) |
fraction |
Filter { areas, lines, .. } (Rust) |
Filter::water(&areas, &lines), or Filter::parse |
$WATERMASK_CACHE, …/watermask cache folder |
$TERRAMASK_CACHE, …/terramask |
The areas and lines options of the other languages are unchanged. The
cache is a new folder, so the first run downloads the tiles again.
License
MIT.
Metadata
Release files for terramask 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| terramask-0.2.0.tar.gz | 175.3 kB | Without provenance | Details for terramask-0.2.0.tar.gz |
Built distributions (wheels)
Total release size: 11.1 MB
Release files / terramask-0.2.0.tar.gz
| Download URL | terramask-0.2.0.tar.gz |
|---|---|
| Size | 175.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f4336884efba16f8ccbf51d86bfec107f2e70f44e4356d5170b89fad1a2a467d
|
|
BLAKE2b-256 checksum How to use checksums |
f4bc0fcfacadbffe95cf8f4a335ea9fd7d678d33c4cca2e595d6f3e8e5ce9558
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / terramask-0.2.0-cp310-abi3-win_amd64.whl
| Download URL | terramask-0.2.0-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
0573ae80ccffa1555e62a66ee700fc567d9def9d6f9ee591b06c4dd0f421729a
|
|
BLAKE2b-256 checksum How to use checksums |
66a5e82f0f7f1896cfaf0b19777ee10abb828d0d18652904f9df83eca69aa3de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / terramask-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl
| Download URL | terramask-0.2.0-cp310-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.8 MB |
| Tags | CPython 3.10 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
f266b1df0c902e84e41fe32073d3b3ee80e7141db9fc4dd28353069a41ecfe02
|
|
BLAKE2b-256 checksum How to use checksums |
6514516e652982a9de4de18c3ba8537717d8ade9b604dc17fcf0e54426556274
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / terramask-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl
| Download URL | terramask-0.2.0-cp310-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 1.7 MB |
| Tags | CPython 3.10 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
dddbd23440735b2dae90fac1ec27acdb011b1f1af3eb26e4acaba6e007a993db
|
|
BLAKE2b-256 checksum How to use checksums |
26db0ee5d72dacdf97c3580ff6768e3772e7c6b2fd883ef3e430ab6d07acafcf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / terramask-0.2.0-cp310-abi3-manylinux_2_28_aarch64.whl
| Download URL | terramask-0.2.0-cp310-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
ce97c7e5ecc59fd1d399c44c4faf027f662d5873a8272e2a8c5f94f49ba2418d
|
|
BLAKE2b-256 checksum How to use checksums |
2152d51848fdbaa596a7b7632786613f76e67e7c7a826964a902f5e735657358
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / terramask-0.2.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | terramask-0.2.0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.6 MB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
0960a377dfd6e081b5316e28b7d8630eabf0f19eccdbf774edf086181104af00
|
|
BLAKE2b-256 checksum How to use checksums |
59812125d2561a6bb86cb428a7021514e608d9182aa74d8471498055a366c16c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / terramask-0.2.0-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | terramask-0.2.0-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
db66a9319f1c7e079f957f7ced2b14722ba4c1e4391b986b75fb03447df3c63a
|
|
BLAKE2b-256 checksum How to use checksums |
e19722f71631ae4de59448709b61f7023750878835e1d5c3ecb2fa200fbe7e54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Release files / terramask-0.2.0-cp310-abi3-macosx_10_13_x86_64.whl
| Download URL | terramask-0.2.0-cp310-abi3-macosx_10_13_x86_64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.10 abi3 macOS 10.13+ x86-64 |
|
SHA-256 checksum How to use checksums |
e7cf683cb274f991c57eed8b2c86c8d7665e163907e62e7ffe8da78372b8460b
|
|
BLAKE2b-256 checksum How to use checksums |
7bcbe28092ed2eb9f3292afd7f7610d916c552d5d7fdfd79b61e0b66f5d448f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.13
|
Security for terramask 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.