Class MapTiles
- All Implemented Interfaces:
HttpServer.Handler
Serves map tiles out of a PMTiles version 3 archive, so an application's own backend is its map tile server and the client needs no third-party tile host.
A PMTiles archive is one file holding a whole tile pyramid: an OpenStreetMap
extract of a city, a country or the planet, as vector tiles (Protomaps
basemaps, OpenMapTiles) or as raster images. Producing one is a build step
outside this class (pmtiles extract cuts a region from a published planet
build; planetiler and tippecanoe write one from OSM data). Serving it is
this handler:
MapTiles tiles = new MapTiles("/srv/maps/region.pmtiles", "/tiles");
server.handle(tiles);
GET /tiles(or/tiles/tiles.json) answers a TileJSON 3.0 document: the tile URL template, zoom range, bounds, center and the archive's own metadata (attribution,vector_layers). Pass that URL to the client'sHttpTileSource/MvtTileSourceand it resolves the template itself.GET /tiles/{z}/{x}/{y}.{ext}answers one tile, with the extension that matches the archive's tile type (mvtorpbf,png,jpg,webp,avif,mlt). A tile inside the archive's zoom range that the archive does not contain answers 204, the convention MapLibre treats as an empty tile; a tile outside the pyramid answers 404.
Tile bytes go out on the same zero-copy path as StaticFiles: a tile is a
byte range of the archive, sent with sendfile() where the platform has it.
A gzip-stored tile is sent as stored, with Content-Encoding: gzip, to a
client that accepts gzip, and inflated here for one that does not. Archives
whose directories or tiles use brotli or zstd are refused when the handler is
constructed, since neither can be decoded here.
The archive can be replaced while the server runs: every request checks the open file's identity (inode, size, modification time) against the one the directories were read from, and a changed file is re-read before it is served from. Replace it by renaming a complete file over it, never by rewriting it in place.
OpenStreetMap data is licensed under the ODbL and must be attributed. The attribution in the archive's metadata is passed through in the TileJSON; the client is responsible for showing it.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncacheControl(String value) Sets theCache-Controlvalue of tile and TileJSON responses; the default is one day.The file extension tiles of this archive are requested with.intThe highest zoom level the archive holds.intThe lowest zoom level the archive holds.handle(HttpServer.Request request) Sets the public base URL written into the TileJSON template, for examplehttps://maps.example.com/tiles.
-
Constructor Details
-
MapTiles
Opens the archive and reads its header, root directory and metadata.
archive: path of the.pmtilesfileprefix: the URL path the handler is mounted at, "" or "/" for the root
- Throws:
IOException- when the file is missing, is not a version 3 archive, or uses a compression this handler cannot decode
-
-
Method Details
-
publicUrl
Sets the public base URL written into the TileJSON template, for examplehttps://maps.example.com/tiles. Without it the template is built from the request's scheme andHostheader, which is wrong behind a proxy that rewrites either.- Parameters:
url- the base URL, without a trailing slash, or null to derive it- Returns:
- this handler
-
cacheControl
-
getMinZoom
public int getMinZoom()The lowest zoom level the archive holds.- Returns:
- the zoom level
-
getMaxZoom
public int getMaxZoom()The highest zoom level the archive holds.- Returns:
- the zoom level
-
getExtension
The file extension tiles of this archive are requested with.- Returns:
mvt,png,jpg,webp,avif,mlt, orbinwhen the archive does not declare a type
-
handle
- Specified by:
handlein interfaceHttpServer.Handler- Throws:
Exception
-