Introducing a new way to review your PRs.

Read the announcement

maplibre / martin

Blazing fast and lightweight PostGIS, MBtiles and PMtiles tile server, tile generation, and mbtiles tooling.

View on GitHub

22 documents · Updated Nov 23, 2026

docs / geospatial-tooling

Geospatial tooling

Geospatial tooling turns source locations and shapes into a shared tile coordinate model. It gives tile production, raster access, and vector processing the same rules for projection, tile lookup, geometry rounding, and per-tile output.

Coordinate model

The coordinate model links geographic input to tile-local output. Geographic values are projected into a world grid first. Vector work then scales that grid into integer units where each tile has a fixed side and a top-left origin.

The main coordinate stages are:

  • Geographic coordinates: longitude and latitude in WGS84.
  • Projected coordinates: Web Mercator meters or another tile grid's CRS units.
  • World coordinates: normalized values with x east and y south for vector tile production.
  • Integer tile coordinates: a zoom-wide grid used by geometry slicing and quantization.
  • Tile-local coordinates: values relative to one tile's origin. A small shared border buffer lets neighboring tiles keep matching geometry.

martin-tile-utils provides Web Mercator conversion, tile size, resolution, and XYZ bounds. martin-tilegen uses these values to project and scale features before it passes them to the integer geometry tools. TileGrid extends the same model to custom CRS and OGC tile matrix definitions.

This diagram shows how source coordinates become tile-local geometry and then tile records.

flowchart LR
    A[Longitude and latitude] --> B[CRS projection]
    B --> C[World coordinates]
    C --> D[Zoom grid scaling]
    D --> E[Integer rounding]
    E --> F[Geometry slicing]
    F --> G[Tile-local vector or raster result]

References: martin-tile-utils/…/lib.rs• L419-496 martin-tile-utils/…/grid.rs• L18-70 martin-tilegen/…/project.rs• L1-27 martin-tilegen/…/render.rs• L498-655

Tile grids

A tile grid defines how a zoom level divides a coordinate reference system into addressable tiles. It supplies the grid name, CRS, top-left origin, zoom-zero size, starting matrix shape, and whether columns wrap around the world.

Built-in grids cover common global and regional cases:

Grid familyPurpose
Web MercatorDefault slippy-map grid for global map tiles.
WGS84 geographicLongitude and latitude tiles with two columns at zoom zero.
Regional projected gridsLocal metric coverage such as Europe, New Zealand, and the polar regions.
Simple CRSPlanar tile space for maps without geographic meaning.

The grid validates tile addresses and converts a CRS bounding box into a rectangular tile range. Wrapping is explicit. World grids may continue past the west or east edge while local grids stop at their defined bounds. This keeps tile selection separate from the geometry slicer's integer tile identifiers.

References: martin-tile-utils/…/grid.rs• L123-170 martin-tile-utils/…/grid.rs• L183-220 martin-tile-utils/…/grid.rs• L318-385

Tile addressing

Tile addressing gives every tile a stable zoom, column, and row identity. TileCoord validates the public XYZ address, while TileRect represents an inclusive rectangle of tiles for bounds, windows, and work limits.

The shared address rules are:

  • Zoom levels define a power-of-two tile matrix.
  • x counts columns from the west.
  • y counts rows from the north.
  • A tile rectangle can be split into non-overlapping pieces when work areas overlap.
  • The integer vector slicer also supports signed TileId values so geometry can remain unwrapped before the tile server applies world wrapping.

The distinction matters at the antimeridian. Geometry can stay continuous in the signed grid, then the tile-generation layer maps columns back into the valid zoom range. A public XYZ address must still pass the grid's range check.

References: martin-tile-utils/…/coordinate.rs• L7-79 martin-tile-utils/…/rectangle.rs• L9-35 martin-tile-utils/…/rectangle.rs• L127-193 map-tile-toolkit/…/tile.rs• L1-56 martin-tilegen/…/render.rs• L205-243 martin-tilegen/…/render.rs• L832-847

Vector geometry processing

The vector geometry toolkit prepares integer lines, points, and polygons for tile output. It keeps original vertices where possible, uses a buffer to make neighboring pieces agree, and returns lazy per-tile results so work follows the tiles that the geometry reaches.

Geometry normalization

Geometry normalization makes floating-point shapes safe for integer tile work. The quantizer rounds coordinates to the nearest grid point with the same tie rule used by the tile renderer. If direct rounding could make a valid polygon invalid, it rebuilds the affected area so rings remain simple and holes keep their meaning.

The quantizer supports three high-level goals:

  • Preserve a valid single polygon after rounding.
  • Merge multipolygon parts that meet after rounding.
  • Build a valid result from a polygon when the caller explicitly asks for repair.

Ring orientation remains part of the contract. Exterior and hole winding is normalized before slicing when the output format needs a fixed convention.

References: map-tile-toolkit/…/quantize.rs• L25-52 map-tile-toolkit/…/quantize.rs• L261-385 map-tile-toolkit/…/quantize.rs• L442-500 map-tile-toolkit/…/quantize.rs• L1034-1090

Geometry slicing

Geometry slicing maps one integer geometry to the tiles whose buffered boxes it touches. Lines keep original vertices and can produce several runs in one tile. Points can appear in neighboring tiles when they lie inside a shared buffer. Polygons produce edge pieces and compact fill runs for areas fully covered by the polygon.

The slicer uses two output modes:

ModeTile content
ClippedOnly the geometry that reaches the tile. Polygon rings may include synthetic corners outside the buffered box to stay closed.
UnclippedThe whole geometry for every tile it reaches. The consumer performs the final clipping.

The slicer validates bounds before iteration. This makes coordinate overflow a call-level error for normal line and point work. Polygon pieces can also fail if their output is too large to index.

References: map-tile-toolkit/…/slicer.rs• L56-107 map-tile-toolkit/…/slicer.rs• L109-155 map-tile-toolkit/…/slicer.rs• L157-264 map-tile-toolkit/…/slicer.rs• L266-334

Polygon coverage

Polygon processing separates tiles touched by ring edges from tiles covered by the polygon interior. Edge tiles receive polygon pieces. Interior tiles are represented as row spans, which avoids creating one full geometry object for every covered tile.

The polygon path keeps the input ring winding and closes gaps outside the tile with synthetic geometry. The synthetic edges stay outside the buffered box, so a later mosaic can discard them and recover the shared original edges. A row-based sweep makes the cost follow the polygon boundary and the emitted tile rows instead of the full covered area.

References: map-tile-toolkit/…/polygon.rs• L1-34 map-tile-toolkit/…/polygon.rs• L62-114 map-tile-toolkit/…/polygon.rs• L567-611 map-tile-toolkit/…/polygon.rs• L802-892

Raster tile alignment

Raster tooling uses the same Web Mercator tile geometry to read Cloud Optimized GeoTIFF data as tiles. It validates the source CRS and pixel shape, finds the zoom that matches a raster level's resolution, and accepts a level only when its origin aligns with tile corners within a small tolerance.

An untagged square raster can use a requested tile address as its georeference. A tagged raster must describe Web Mercator with square pixels and an unrotated north-up layout. These rules prevent a raster window from being assigned to a tile matrix with a different scale or orientation.

References: cog-tiles/…/geo.rs• L1-34 cog-tiles/…/geo.rs• L36-101 cog-tiles/…/geo.rs• L103-145 cog-tiles/…/tile.rs• L16-67 cog-tiles/…/tile.rs• L94-124

Tile payload utilities

Tile payload utilities keep tile bytes and tile addresses consistent across vector and raster paths. They describe the data format and external encoding together, detect common raster, JSON, MLT, and compressed payloads, and provide matching compression and decompression operations.

This layer is about the tile boundary rather than geometry. Tile generation can produce a tile body, select its format, apply the requested encoding, and pass the resulting bytes to MBTiles, PMTiles, or an HTTP response without each sink defining its own content rules.

References: martin-tile-utils/…/lib.rs• L37-130 martin-tile-utils/…/lib.rs• L158-228 martin-tile-utils/…/lib.rs• L231-325 martin-tile-utils/…/decoders.rs• L19-87

The detailed geometry operations and common tile primitives are documented in Geometry and tile primitives.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the world-coordinate axis direction aligned between martin-tilegen/…/project.rs and the XYZ y direction used by TileGrid and TileCoord.
  • Keep the tile extent and buffer contract aligned between RenderLayer, Slicer, polygon fill runs, and the tile-local geometry record.
  • Keep signed, unwrapped geometry columns separate from validated public XYZ columns when changing antimeridian handling.
  • Keep raster zoom matching and tile-corner alignment consistent with the Web Mercator constants shared by tile utilities and COG readers.
  • Keep tile format, content type, and external encoding mappings aligned with the payload bytes written by tile sinks.

Important failure modes

TriggerConsequenceGuard
A tile grid uses an invalid CRS, origin, extent, or zoom-zero matrix.Tile ranges and tile addresses do not describe the intended space.TileGrid validates these values when the grid is created.
A vector feature or buffered tile reaches the i32 limit.The feature cannot be represented in the integer tile grid and is skipped or reported as a geometry error.Bounds are checked before slicing and layer setup checks the zoom grid.
A buffer is at least half the tile extent.Neighbor ownership overlaps too widely for stable routing.Slicer and RenderLayer reject the buffer.
A tagged COG is not Web Mercator, north-up, or square-pixel.Raster levels cannot be mapped safely to the XYZ tile matrix.COG georeferencing rejects unsupported metadata.
Independent polygon parts meet after rounding.Separate rounded parts can overlap or form invalid rings.Quantizer::multi_polygon groups nearby parts and rebuilds their union when needed.

Key files

FileWhy to read it
map-tile-toolkit/…/slicer.rsPublic contract for integer lines, points, polygons, buffers, and clip modes.
map-tile-toolkit/…/polygon.rsHigh-level polygon coverage, edge pieces, and fill runs.
map-tile-toolkit/…/quantize.rsFloat-to-integer polygon rounding and validity repair.
map-tile-toolkit/…/tile.rsSigned tile identifiers and global-to-local tile origins.
map-tile-toolkit/…/geom.rsExact orientation and area rules used by geometry operations.
martin-tile-utils/…/grid.rsCRS-aware tile matrix definitions and tile ranges.
martin-tile-utils/…/coordinate.rsValidated public XYZ tile addresses.
martin-tile-utils/…/lib.rsShared Web Mercator, XYZ, payload format, and encoding utilities.
martin-tilegen/…/project.rsSource projection into the renderer's world coordinate model.
martin-tilegen/…/render.rsEnd-to-end use of quantization and slicing during tile production.
cog-tiles/…/geo.rsRaster georeferencing, zoom matching, and tile alignment.
cog-tiles/…/tile.rsCOG Web Mercator tile bounds and resolution.