Introducing a new way to review your PRs.

Read the announcement

maplibre / maplibre-tile-spec

Next generation map vector tiles format

View on GitHub

20 documents · Updated Nov 23, 2026

docs / rust-native-bindings

Rust native bindings

The Rust native bindings make the Rust tile codec usable from systems that do not run Rust. The FFI package serves C, C++, Kotlin, and Java consumers through generated interfaces. The Python package serves Python users through a higher-level API for tile conversion and decoded feature data. See the Rust tile workspace for the larger package map, the Rust core codecs page for shared codec behavior, and the Rust WASM package page for the browser-facing binding.

Native FFI surface

The native FFI surface gives non-Rust programs access to tile conversion without exposing Rust data structures. The mlt-ffi crate defines the shared contract for native libraries and uses the core codec for MLT and MVT work, including the unstable v2 feature set.

The main path through the interface is:

flowchart LR
    A[ C, C++, Kotlin, or Java consumer ] --> B[Generated language bindings]
    B --> C[mlt-ffi interface]
    C --> D[mlt-core]
    D --> E[MLT or MVT bytes]

Tile conversion

MltConverter handles whole-tile conversion between MLT and Mapbox Vector Tile (MVT) bytes. Consumers can decode MLT to MVT, apply a memory limit during decode, or encode MVT with a shared set of encoder options. The result is held in an owned MltBuffer, which remains valid until the consumer releases it.

The conversion API is format-focused. A consumer can convert a tile without understanding the Rust tile model.

References: rust/…/lib.rs• L402-452 rust/…/lib.rs• L89-123 rust/…/Cargo.toml• L14-20

Layer assembly

MltLayerBuilder supports producers that already have feature data and want to write MLT directly. A consumer starts a layer, supplies MVT geometry commands one feature at a time, assigns typed properties, and appends the encoded layer to an output buffer. This path avoids building a complete MVT tile first.

MltEncoderOptions keeps format and size choices separate from layer data. It controls tessellation, feature order, string and integer compression, shared dictionaries, and the choice between the stable v1 wire format and experimental v2 features. The v2 controls cover alternate geometry and value encodings, so callers must select v2 when they need them.

References: rust/…/lib.rs• L137-241 rust/…/lib.rs• L267-340 rust/…/Cargo.toml• L17-20

Tile inspection

MltTileStats provides a light inspection path for tools that need layer names and byte use without fully decoding feature values. It reports the size assigned to geometry, properties, IDs, and remaining layer metadata.

This path lets a consumer inspect tile size before it chooses whether to decode or convert the tile.

References: rust/…/lib.rs• L342-400

Generated language packages

The FFI contract is declared once in the Rust bridge and then presented as language-specific bindings. The generated outputs cover C and C++ headers plus Kotlin and Java access layers, so each consumer uses the same conversion, builder, buffer, and error model.

ConsumerPackage-facing formMain use
CC headers and a shared libraryCall conversion and builder functions from C code.
C++C++ headers and a shared libraryUse the native contract from C++ code.
KotlinKotlin classes backed by JNAConvert tiles and manage native handles from Kotlin.
JavaJava classes over java.lang.foreignConvert tiles and assemble layers from Java.

Binding generation

The generated files are the language-facing form of the mlt-ffi contract. A change to a public Rust type or method must flow into these outputs before downstream consumers can use the new contract. The repository provides one synchronization command and language-specific round-trip checks for this purpose.

The Java output has a raw foreign-function layer as well as higher-level access classes. The raw layer describes native symbols and data layouts. It does not add separate tile features.

The generated C header shows the basic contract: conversion results carry either an owned buffer or an error, and builder calls return success or an error. Java and Kotlin wrappers add their own lifetime and error handling around those same native results.

References: rust/…/Cargo.toml• L14-20 rust/…/MltConverter.h• L16-43 rust/…/MltLayerBuilder.h• L17-99 rust/…/MltConverter.java• L7-73 rust/…/MltConverter.kt• L24-139

Native library packaging

The Java and Kotlin packages publish the native library with platform-specific resource paths. Java loads the library through its foreign-function access layer, while Kotlin relies on JNA to find the same platform resources. The build maps supported targets to the names expected by each loader.

Java consumers can override the packaged library with the mlt.ffi.library system property. Otherwise, the loader extracts the matching library to a private temporary file and loads it once per class loader. The cleanup rules avoid removing a library that another JVM still uses.

The Java package targets the Java foreign-function API available from Java 22. The Kotlin package uses JVM 21 and JNA as its native access layer. Both packages publish versioned Maven artifacts with semantic-version checks and dual MIT or Apache-2.0 license metadata.

References: rust/…/build.gradle• L24-33 rust/…/build.gradle• L61-127 rust/…/NativeLibrary.java• L13-151 rust/…/build.gradle• L17-124

Python package

The Python package turns the Rust codec into the maplibre_tiles module and exposes Python values instead of native handles. It supports tile inspection, structured decode, GeoJSON output, GeoJSON input, and whole-tile MVT input. Its current decode path accepts the known v1 and v2 layer forms and rejects layer tags that the package cannot expose.

The package supports two related user flows:

flowchart LR
    A[MLT bytes] --> B[decode_mlt]
    B --> C[MltLayer and MltFeature objects]
    A --> D[decode_mlt_to_geojson]
    D --> E[GeoJSON text]
    F[GeoJSON FeatureCollection] --> G[encode_geojson]
    H[MVT bytes] --> I[encode_mvt]
    G --> J[MLT bytes]
    I --> J

Decode API

decode_mlt returns layers with names, extents, optional z-grid information, height mode, and feature objects. Each MltFeature exposes an optional ID, a geometry type, WKB geometry bytes, and a property dictionary. The decoder handles both supported v1 and v2 layer forms before it builds these Python values. decode_mlt_to_geojson provides a text form when callers need a standard interchange format, while list_layers returns names without full feature decoding.

The decode API keeps coordinates in tile-local space by default. When callers provide tile z/x/y values, it returns Web Mercator coordinates instead. The tms choice makes the y-axis convention explicit for tile sources that use TMS or XYZ indexing. A layer with z values exposes its grid step and height mode, and its WKB uses a third coordinate.

References: rust/…/lib.rs• L27-53 rust/…/lib.rs• L286-371 rust/…/Cargo.toml• L25-30

Encode API

encode_geojson accepts one GeoJSON FeatureCollection and writes one MLT layer. The layer name and extent supply the MLT metadata that GeoJSON does not carry. encode_mvt accepts a complete MVT protobuf tile and writes its layers as MLT bytes.

GeoJSON input uses integer tile-local coordinates and scalar properties. The encoder checks the input shape and infers one column type from the values seen for each property. A z step enables 3D positions and the experimental v2 wire format. The height mode records whether zero height means mean sea level or the terrain surface.

Both functions share options for tessellation, feature ordering, shared dictionaries, FSST string compression, and FastPFOR integer compression. The sort option presents these ordering choices as none, auto, all, morton, hilbert, or id. none preserves input order, while the other choices let the encoder try spatial or ID orderings. These options affect encoding choices without changing the package-facing input model.

References: rust/…/shared.rs• L12-52 rust/…/geojson.rs• L26-114 rust/…/geojson.rs• L116-214 rust/…/mvt.rs• L12-53 rust/…/Cargo.toml• L21-30

Python distribution

The Python package is published as maplibre-tiles and imports as maplibre_tiles. Maturin builds the Rust extension for CPython with a Python 3.10 compatibility floor, so the package-facing API is the native extension rather than a separate pure-Python layer.

References: rust/…/pyproject.toml• L1-40 rust/…/Cargo.toml• L12-23

Python type contract

The package keeps its public Python contract visible through generated type stubs. The stub lists the decoded classes and module functions, including the supported encode options, so editors and type checkers see the same surface that the extension registers.

The stubs are generated from the PyO3 declarations rather than maintained as a separate API design. When a public function or class changes, its runtime registration and stub output must remain aligned.

References: rust/…/maplibre_tiles.pyi• L1-118 rust/…/lib.rs• L286-385 rust/…/Cargo.toml• L16-29

Consumer boundaries

The bindings protect consumers from Rust ownership rules by defining clear boundaries for bytes, handles, errors, coordinates, and height data. These boundaries are the main compatibility points between the shared codec and each package-facing API.

Ownership and errors

FFI output is owned by an MltBuffer, but its byte view is borrowed from that buffer. A consumer must keep the buffer alive while it reads the bytes and must not clear or release it until the read is complete. Python converts Rust decode and encode failures into ValueError, while FFI callers receive a ConvertError with a failure kind and message.

The shared error model separates bad input, invalid feature data, and an encoding failure. That distinction lets a caller decide whether to reject the source tile, fix the feature stream, or report a codec failure.

References: rust/…/lib.rs• L22-123 rust/…/lib.rs• L22-25 rust/…/MltConverter.h• L16-43

Format and coordinate contracts

The native builder consumes MVT geometry commands and typed scalar properties. The Python GeoJSON encoder consumes a FeatureCollection with non-empty supported geometries and scalar properties. These APIs accept different input shapes because one is a low-level producer path and the other is a Python data interchange path.

Wire version is also a consumer contract. v1 is the stable format, while v2 carries experimental features such as z-grid data and v2-only encoding choices. A caller must select or trigger v2 when it needs those features instead of assuming that a v2-only option changes a v1 layer.

Python z data has a matching input and output contract. A z step requires three-coordinate positions, and three-coordinate positions require a z step. On decode, raw z values remain grid values in tile-local output. A tile transform converts them to metres and applies the selected height interpretation.

References: rust/…/lib.rs• L125-241 rust/…/lib.rs• L286-339 rust/…/geojson.rs• L72-112 rust/…/geojson.rs• L230-240 rust/…/lib.rs• L286-340

Change checklist

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

  • Public types or methods in rust/…/lib.rs require regenerated C, C++, Kotlin, and Java bindings before downstream code sees the same contract.
  • Public PyO3 functions or classes require regenerated rust/mlt-py/maplibre_tiles.pyi output so type checkers match the runtime module.
  • Changes to MLT or MVT conversion rules must be checked in both whole-tile paths and the direct MltLayerBuilder path.
  • Changes to v2 layer support must stay aligned between the FFI core feature set and the Python decoder's supported layer forms.
  • Changes to byte ownership or error kinds must be reflected in every generated language wrapper that unwraps buffers or errors.
  • Changes to coordinate, z-grid, or height-mode rules must be checked against Python decode output, WKB dimensions, and GeoJSON encode validation.
  • Changes to the GeoJSON property contract must stay aligned with column type inference and the public ValueError behavior.
  • Changes to Python encoder sort names or defaults must stay aligned across shared.rs, both encode entry points, and the generated stub literals.
  • Changes to the Python package name, import name, Python version floor, or Maturin module name must stay aligned with the published wheel and user imports.
  • Changes to Java native ABI declarations require regenerated raw Java layouts and downcall bindings as well as the higher-level Java access classes.
  • Changes to native target classifiers must stay aligned with Java resource paths, Kotlin JNA resource paths, and the native files included in each published JAR.
  • Changes to Java library extraction or cleanup must preserve the override path, one-load behavior, and protection against deleting a library used by another JVM.
  • Changes to Java or Kotlin publication versions must pass semantic-version validation and keep the Maven coordinates and license metadata aligned with the FFI release.

Important failure modes

TriggerConsequenceGuard
A consumer reads an FFI byte view after its MltBuffer is cleared or releasedThe result is no longer valid for that readKeep the owning buffer alive until the byte copy or read finishes.
MLT or MVT input cannot be parsedFFI returns an input or encoding error, or Python raises ValueErrorValidate source bytes before using the converted result.
GeoJSON has the wrong top-level type, empty features, unsupported geometry, nested properties, or invalid coordinate shapeencode_geojson rejects the requestSupply a non-empty FeatureCollection with supported scalar data and integer tile-local positions.
z-grid encoding is requested without matching 3D positions, or 3D positions lack a z stepPython encoding fails validationUse matching [x, y, z] positions and a valid z_step.
A height mode is used without a z step or has an unknown valuePython encoding fails validationUse a supported height mode with z_step.
A v2-only option is used while the output remains v1The option has no effect or the required v2 data is absentSelect the v2 wire version or use the Python z-enabled path when the feature needs it.
A Python tile contains a layer tag outside the supported v1 and v2 formsdecode_mlt raises ValueError instead of returning partial layer dataCheck the producer's layer version before sending the tile to the Python package.
A Python sort value is outside the public setEncoding raises ValueError before it reaches the core encoderUse none, auto, all, morton, hilbert, or id.
The Rust FFI surface changes without binding regenerationNative consumers compile against stale declarations or miss new API membersRun the FFI binding synchronization step and the language round-trip checks.
A packaged Java or Kotlin resource does not match the detected platform nameThe JVM cannot find or load the native libraryKeep target classifiers, JAR resource paths, and loader naming rules in sync.
Java cannot extract the packaged library or receives an unsupported platformLoading fails with UnsatisfiedLinkErrorProvide a supported native artifact or set -Dmlt.ffi.library to a loadable library.
Java raw layouts or native symbol declarations do not match the loaded libraryJava downcalls fail or interpret native memory incorrectlyRegenerate the Java raw bindings from the same native header and library contract.

Key files

FileWhy to read it
rust/…/lib.rsDefines the native conversion, layer builder, options, buffer, statistics, and error contract.
rust/…/Cargo.tomlShows the dynamic-library output and the v2-enabled core dependency.
rust/…/README.mdGives the native API map and the binding regeneration and test entry points.
rust/…/MltConverter.hShows the C-facing conversion and result contract.
rust/…/MltLayerBuilder.hShows the C-facing per-feature layer assembly contract.
rust/…/MltConverter.javaShows how Java presents whole-tile conversion and native buffer ownership.
rust/…/NativeLibrary.javaShows Java native-library selection, extraction, loading, and cleanup.
rust/…/build.gradleDefines Java runtime level, native resource packaging, publication, and platform mapping.
rust/…/build.gradleDefines Kotlin's JNA dependency, JVM level, native resource packaging, and publication.
rust/…/pyproject.tomlDefines the published Python package, import module, Python version floor, and Maturin build link.
rust/…/Cargo.tomlDefines the PyO3 extension and stub-generation features.
rust/…/README.mdDescribes the package-facing Python decode and encode flows and their input rules.
rust/…/lib.rsRegisters the Python module and defines decoded layers, features, coordinate transforms, and output forms.
rust/…/shared.rsMaps Python encoding choices to the shared encoder configuration.
rust/…/geojson.rsDefines the GeoJSON-to-MLT package behavior, validation, and property contract.
rust/mlt-py/maplibre_tiles.pyiShows the generated Python-facing API and type contract.
rust/…/test_z.pyVerifies the public contract for z coordinates, WKB output, and height modes.