Class MapTiles

java.lang.Object
com.codename1.backend.MapTiles
All Implemented Interfaces:
HttpServer.Handler

public final class MapTiles extends Object implements 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's HttpTileSource / MvtTileSource and it resolves the template itself.
  • GET /tiles/{z}/{x}/{y}.{ext} answers one tile, with the extension that matches the archive's tile type (mvt or pbf, 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 Details

    • MapTiles

      public MapTiles(String archive, String prefix) throws IOException

      Opens the archive and reads its header, root directory and metadata.

      • archive: path of the .pmtiles file
      • prefix: 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

      public MapTiles publicUrl(String url)
      Sets the public base URL written into the TileJSON template, for example https://maps.example.com/tiles. Without it the template is built from the request's scheme and Host header, 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

      public MapTiles cacheControl(String value)
      Sets the Cache-Control value of tile and TileJSON responses; the default is one day. Null omits the header.
      Parameters:
      value - the header value
      Returns:
      this handler
    • 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

      public String getExtension()
      The file extension tiles of this archive are requested with.
      Returns:
      mvt, png, jpg, webp, avif, mlt, or bin when the archive does not declare a type
    • handle

      public HttpServer.Response handle(HttpServer.Request request) throws Exception
      Specified by:
      handle in interface HttpServer.Handler
      Throws:
      Exception