Skip to main content

Simple Python wrapper for MapLibre GL Native using native Rust renderer

Project description

mlnative

PyPI version License

⚠️ Warning: This is an alpha release. The API may change significantly. Not recommended for production use.

Simple Python wrapper for MapLibre GL Native using a native Rust renderer.

A grug-brained library for rendering static map images with minimal complexity.

Features

  • Simple API: One class, minimal methods, zero confusion
  • Native Performance: Rust backend with MapLibre Native C++ core
  • Built-in Defaults: Uses OpenFreeMap Liberty style by default - no configuration needed
  • Address Support: Built-in geocoding with geopy for rendering addresses directly
  • Geometry Support: Shapely integration for bounds fitting and GeoJSON

Installation

pip install mlnative

Platform-specific wheels include the native renderer binary:

  • Linux x86_64, aarch64

Quick Start

from mlnative import Map

# Render a map - uses OpenFreeMap Liberty style by default
with Map(512, 512) as m:
    png_bytes = m.render(center=[-122.4, 37.8], zoom=12)
    
    with open("map.png", "wb") as f:
        f.write(png_bytes)

Rendering from Addresses

from mlnative import Map
from geopy.geocoders import ArcGIS

# Geocode an address and render
geolocator = ArcGIS()
location = geolocator.geocode("Sydney Opera House")

with Map(512, 512) as m:
    png = m.render(
        center=[location.longitude, location.latitude],
        zoom=15  # Good for landmark/building view
    )
    open("sydney.png", "wb").write(png)

Custom Styles

While OpenFreeMap Liberty is the default, you can override it:

# OpenFreeMap styles
m.load_style("https://tiles.openfreemap.org/styles/liberty")
m.load_style("https://tiles.openfreemap.org/styles/positron")
m.load_style("https://tiles.openfreemap.org/styles/dark")

# MapLibre demo tiles (good for testing)
m.load_style("https://demotiles.maplibre.org/style.json")

# Or load from dict
m.load_style({"version": 8, "sources": {...}, "layers": [...]})

API Reference

Map Class

Map(width, height, pixel_ratio=1.0)

Create a new map renderer.

  • width: Image width in pixels (1-4096)
  • height: Image height in pixels (1-4096)
  • pixel_ratio: Pixel ratio for high-DPI rendering

render(center, zoom, bearing=0, pitch=0)

Render the map to PNG bytes. Uses OpenFreeMap Liberty style by default.

  • center: [longitude, latitude] list
  • zoom: Zoom level (0-24)
  • bearing: Rotation in degrees (0-360)
  • pitch: Tilt in degrees (0-85)

render_batch(views)

Render multiple views efficiently.

fit_bounds(bounds, padding=0, max_zoom=24)

Calculate center/zoom to fit bounds. Returns (center, zoom).

bounds = (-122.6, 37.7, -122.3, 37.9)  # (xmin, ymin, xmax, ymax)
center, zoom = m.fit_bounds(bounds)
png = m.render(center=center, zoom=zoom)

set_geojson(source_id, geojson)

Update GeoJSON source data (requires style loaded as dict).

from shapely import Point
m.set_geojson("markers", Point(-122.4, 37.8))

load_style(style)

Load custom style (URL, file path, or dict). Call before render if not using default.

GeoJSON Helpers

from mlnative import point, feature_collection, from_coordinates, from_latlng

# From coordinates (lng, lat)
fc = from_coordinates([(-122.4, 37.8), (-74.0, 40.7)])

# From GPS coordinates (lat, lng)  
fc = from_latlng([(37.8, -122.4), (40.7, -74.0)])

# From shapely
from shapely import MultiPoint, Point
fc = feature_collection(MultiPoint([Point(-122.4, 37.8), Point(-74.0, 40.7)]))

Examples

See examples/ directory:

  • basic.py - Simple usage
  • address_rendering.py - Render from addresses
  • fastapi_server.py - Static maps API

Supported Platforms

  • Linux x86_64, aarch64

Note: macOS and Windows support limited due to upstream MapLibre Native build constraints.

Development

Requires Python 3.12+, Rust 1.70+, and uv.

# Setup
uv pip install -e ".[dev,web]"
cd rust && cargo build --release

# Run tests
just test

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlnative-0.3.2.tar.gz (71.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

mlnative-0.3.2-py3-none-manylinux_2_28_x86_64.whl (20.9 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

mlnative-0.3.2-py3-none-manylinux_2_28_aarch64.whl (20.5 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

File details

Details for the file mlnative-0.3.2.tar.gz.

File metadata

  • Download URL: mlnative-0.3.2.tar.gz
  • Upload date:
  • Size: 71.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mlnative-0.3.2.tar.gz
Algorithm Hash digest
SHA256 21146d951fcbf1532ec6312e2d4d776060c07fbdada086fde96a07b139669e62
MD5 31cbce8d4adcf49311d777853dee9608
BLAKE2b-256 937150c240ef74ac3e5a8a454124eeb9a775cbe4e62dc1a9cd82296a0b827f2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlnative-0.3.2.tar.gz:

Publisher: release.yml on adonm/mlnative

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlnative-0.3.2-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for mlnative-0.3.2-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dbbf298f71810d9fcf15807c847b38a8427f81501038baa0cd050747ec2813b6
MD5 8861434386aeb5927ed9d5a9399a6d1b
BLAKE2b-256 b76b07e9c9d2e6d39600dd3d9463825c85d17573b99bf42f8a2c01a847260339

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlnative-0.3.2-py3-none-manylinux_2_28_x86_64.whl:

Publisher: release.yml on adonm/mlnative

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mlnative-0.3.2-py3-none-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for mlnative-0.3.2-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0f7f390d031e943e44e2aa3a5cf721d5a1598ccd4ad23e513ae98676c1428fd9
MD5 5a2c638dbd3e865413622d9e5246a9c3
BLAKE2b-256 cd6b6ff04e7023973fa7cb6a05213c0c8493c3165035a76e933d1914d7a23870

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlnative-0.3.2-py3-none-manylinux_2_28_aarch64.whl:

Publisher: release.yml on adonm/mlnative

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page