Skip to content

PMTiles

Read PMTiles v3 single-file tile archives: header, directories, tile lookup and tile bytes.

Implements PMTiles. Spec v0.2.0; every port passes the same 81 conformance cases.

Swift 0.2.0Nim v0.2.0Zig v0.2.0

Install

Package.swift
.package(url: "https://github.com/Xenoglyphiq/pmtiles-swift", from: "0.2.0"),
// in your target
dependencies: [.product(name: "PMTiles", package: "pmtiles-swift"), .product(name: "PMTilesIO", package: "pmtiles-swift")]

Examples

The same three examples exist in every port, runnable as-is from each repository's examples/ folder.

Print an archive's header

examples/inspect_archive.swift
// Canonical example `inspect_archive`: decode a local archive's header and print it.
// Usage: swift run inspect_archive [path]
import Foundation
import PMTiles
let path = CommandLine.arguments.dropFirst().first ?? ".spec/conformance/cases/archives/small.pmtiles"
guard let file = FileHandle(forReadingAtPath: path) else {
print("cannot open \(path)")
exit(1)
}
let first127 = [UInt8](file.readData(ofLength: 127))
do {
let h = try decodeHeader(first127)
print("zooms \(h.minZoom)-\(h.maxZoom), tile type \(h.tileType), tile compression \(h.tileCompression)")
print("bounds lon \(h.bounds.minLon)...\(h.bounds.maxLon), lat \(h.bounds.minLat)...\(h.bounds.maxLat)")
print("center (\(h.center.lon), \(h.center.lat)) at zoom \(h.centerZoom)")
} catch {
print("not a PMTiles v3 archive: \(error)")
exit(1)
}

Fetch one tile by z/x/y

examples/fetch_one_tile.swift
// Canonical example `fetch_one_tile`: read one tile by z/x/y from a local archive.
// Usage: swift run fetch_one_tile [path] [z/x/y]
import PMTilesIO
let args = Array(CommandLine.arguments.dropFirst())
let path = args.first ?? ".spec/conformance/cases/archives/small.pmtiles"
let parts = (args.count > 1 ? args[1] : "2/1/3").split(separator: "/").compactMap { UInt32($0) }
let coord = TileCoord(z: UInt8(parts[0]), x: parts[1], y: parts[2])
do {
let source = try FileSource(path: path)
if let tile = try await getTile(source, coord) {
print("\(coord.z)/\(coord.x)/\(coord.y): \(tile.count) bytes")
} else {
print("\(coord.z)/\(coord.x)/\(coord.y): not found")
}
} catch {
print("error: \(error)")
}

Read metadata over HTTP

examples/remote_metadata.swift
// Canonical example `remote_metadata`: read an archive's metadata over HTTP range requests.
// Usage: swift run remote_metadata [url]
import Foundation
import PMTilesIO
let url = URL(string: CommandLine.arguments.dropFirst().first ?? "https://pmtiles.io/protomaps(vector)ODbL_firenze.pmtiles")!
do {
print(try await readMetadata(HTTPSource(url: url)))
} catch {
print("error: \(error)")
}

Ports

LanguagePackageReleaseSpecConformance
SwiftPMTiles0.2.0v0.2.0core ✓ io ✓ full ✓ (81/81)
Nimpmtilesv0.2.0v0.2.0core ✓ io ✓ full ✓ (81/81)
Zigpmtilesv0.2.0v0.2.0core ✓ io ✓ full ✓ (81/81)

Performance

Median time, and ratio to the reference implementation measured in the same session. The spec's target is within 2×.

Portget_tile, PMTilesReaderget_tile, statelessget_tile, Reader
ReferenceRust pmtiles 0.24.1: 165.1 msRust pmtiles 0.24.1: 165.1 msRust pmtiles 0.24.1: 165.1 ms
Swift344.9 ms (2.09×)359.6 ms (2.18×)—
Nim—222.8 ms (1.35×)—
Zig—280.4 ms (1.70×)272.7 ms (1.65×)

Spec and source