4 releases
Uses new Rust 2024
new 0.1.3 | May 8, 2025 |
---|---|
0.1.2 | May 7, 2025 |
0.1.1 | May 7, 2025 |
0.1.0 | May 6, 2025 |
#89 in Visualization
237 downloads per month
29KB
630 lines
maplibre-legend
A Rust crate to generate SVG legends from a MapLibre GL style JSON.
Given a MapLibre style (e.g. style.json
), this library parses all layers and produces standalone SVG symbols or a combined legend, complete with optional labels and raster entries.
This project is a work in progress. It doesn't yet support all layer types, but it does cover the most common cases.
By using each layer's metadata
attribute, you can flexibly customize how the legend is generated—especially the labels.
Features
- Parse MapLibre GL style (v8) JSON into a structured
Style
model. - Render individual layer legends (fill, line, circle) as SVG snippets.
- Optionally include raster layers.
- Render labels and customizable dimensions (
default_width
/default_height
). - Stack all layers into one combined SVG with separators.
- For finer details, the metadata attribute of the layer is used.
Installation
Add this crate to your Cargo.toml
:
[dependencies]
maplibre-legend = "0.1.0" # replace with the latest version
Then in your code:
use maplibre_legend::MapLibreLegend;
Usage
use maplibre_legend::MapLibreLegend;
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let style_json = fs::read_to_string("style.json")?;
let legend = MapLibreLegend::new(&style_json, 250,40, true, true)?;
/// Render a single layer
if let Some(svg) = legend.render_layer("vs2023", Some(true)) {
fs::write("vs2023.svg", svg)?;
}
/// Render all layers
let combined = legend.render_all(true);
fs::write("combined.svg", combined)?;
Ok(())
}
Examples
Given the included style.json
(which defines various raster, fill, line and circle layers with metadata.legend.label
), calling:
let svg = legend.render_all(true);
produces a full-page legend similar to:
style.json | style2.json | style3.json |
---|---|---|
Crate Modules
circle
: renders circle layersline
: renders line layersfill
: renders polygon (fill) layersraster
: renders raster (tile) layersdefault
: renders a gray polygoncommon
: shared types (Style
,Layer
, etc.)
Contributing
- Fork the repo and create a feature branch.
- Run
cargo fmt
andcargo clippy
to keep code clean. - Submit a pull request—feedback is welcome!
License
This project is licensed under the MIT License. See the LICENSE file for details.
Dependencies
~0.7–1.6MB
~34K SLoC