Crates.io | pmtiles |
lib.rs | pmtiles |
version | 0.15.0 |
created_at | 2022-10-21 21:56:32.651552+00 |
updated_at | 2025-07-02 19:36:46.531301+00 |
description | Implementation of the PMTiles v3 spec with multiple sync and async backends. |
homepage | |
repository | https://github.com/stadiamaps/pmtiles-rs |
max_upload_size | |
id | 693954 |
size | 4,892,778 |
PMTiles
(for Rust)This crate implements the PMTiles v3 spec, originally created by Brandon Liu for Protomaps.
PMTile
archivesmmap
(Tokio) for local fileshttp
and https
(Reqwest + Tokio) for URLss3
(Rust-S3 + Tokio) for S3-compatible bucketsPMTile
archivesMBTiles
+ x/y/z
mmap
and http
at least)async-std
)PRs welcome!
PMTiles
fileuse bytes::Bytes;
use pmtiles::{AsyncPmTilesReader, TileCoord};
async fn get_tile(z: u8, x: u32, y: u32) -> Option<Bytes> {
let file = "example.pmtiles";
// Use `new_with_cached_path` for better performance
let reader = AsyncPmTilesReader::new_with_path(file).await.unwrap();
let coord = TileCoord::new(z, x, y).unwrap();
reader.get_tile(coord).await.unwrap()
}
This example uses a simple hashmap-based cache to optimize reads from a PMTiles
source. The same caching is available for all other methods. Note that HashMapCache
is a rudimentary cache without eviction. You may want to build a more sophisticated cache for production use by implementing the DirectoryCache
trait.
use bytes::Bytes;
use pmtiles::{AsyncPmTilesReader, HashMapCache, TileCoord};
use pmtiles::reqwest::Client; // Re-exported Reqwest crate
async fn get_tile(z: u8, x: u32, y: u32) -> Option<Bytes> {
let cache = HashMapCache::default();
let client = Client::builder().use_rustls_tls().build().unwrap();
let url = "https://protomaps.github.io/PMTiles/protomaps(vector)ODbL_firenze.pmtiles";
let reader = AsyncPmTilesReader::new_with_cached_url(cache, client, url).await.unwrap();
let coord = TileCoord::new(z, x, y).unwrap();
reader.get_tile(coord).await.unwrap()
}
AWS client configuration is fairly none-trivial to document here. See AWS SDK documentation for more details.
use bytes::Bytes;
use pmtiles::{AsyncPmTilesReader, HashMapCache, TileCoord};
use pmtiles::aws_sdk_s3::Client; // Re-exported AWS SDK S3 client
async fn get_tile(client: Client, z: u8, x: u32, y: u32) -> Option<Bytes> {
let cache = HashMapCache::default();
let bucket = "https://s3.example.com".to_string();
let key = "example.pmtiles".to_string();
let reader = AsyncPmTilesReader::new_with_cached_client_bucket_and_path(cache, client, bucket, key).await.unwrap();
let coord = TileCoord::new(z, x, y).unwrap();
reader.get_tile(coord).await.unwrap()
}
PMTiles
fileuse pmtiles::{PmTilesWriter, TileType, TileCoord};
use std::fs::File;
let file = File::create("example.pmtiles").unwrap();
let mut writer = PmTilesWriter::new(TileType::Mvt).create(file).unwrap();
let coord = TileCoord::new(0, 0, 0).unwrap();
writer.add_tile(coord, &[/*...*/]).unwrap();
writer.finalize().unwrap();
make
.
Install it with cargo install just
.just
.just test
.Licensed under either of
Some PMTile
fixtures copied from official PMTiles repository.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.