TileServer GL

repository·master·Indexed 25 days ago

https://github.com/maptiler/tileserver-gl

A map tile server for JSON GL styles providing vector and server-side generated raster tiles. It uses MapLibre GL Native for rendering and supports clients such as MapLibre GL JS, Android, iOS, Leaflet, and OpenLayers. The server supports MBTiles and PMTiles (including S3-compatible storage) and can be deployed via Docker or as a pure JavaScript version using tileserver-gl-light.

Tokens
14.3K
Snippets
29
Records
78
Agent score
83%

What's inside tileserver-gl

  1. Use tileserver-gl-light for pure JavaScript environments

    master

    If you need a version that does not require native dependencies and can run anywhere, use the tileserver-gl-light npm package.

    Note: This version does not support server-side rasterization via MapLibre GL Native.

  2. Install TileServer GL using Docker

    master

    The easiest way to run TileServer GL is via Docker. The image will be automatically downloaded if it is not present on your system. You can pass additional TileServer GL options by appending them to the end of the command.

    To run with the current directory mounted to /data and port 8080 exposed:

    docker run --rm -it -v $(pwd):/data -p 8080:8080 maptiler/tileserver-gl

    Examples with additional options:

    • Specify a specific MBTiles file: docker run ... maptiler/tileserver-gl --file my-tiles.mbtiles
    • Enable verbose logging: docker run ... maptiler/tileserver-gl --verbose
  3. Install Native Dependencies for TileServer GL

    master

    If installing via npm, you must install the following native dependencies based on your operating system:

    Ubuntu 24.04 (x64/arm64)

    apt install build-essential python3-setuptools pkg-config xvfb libglfw3-dev libuv1-dev libjpeg-turbo8 libicu-dev libcairo2-dev libpango1.0-dev libpng-dev libjpeg-dev libgif-dev librsvg2-dev librsvg2-dev libcurl4-openssl-dev libpixman-1-dev

    macOS 15 (x64/arm64)

    brew install pkg-config cairo pango libpng jpeg giflib librsvg harfbuzz

    Windows (x64)

    Install the Microsoft Visual C++ Redistributable.

  4. Install TileServer GL light via npm

    master

    Install the tileserver-gl-light package globally using npm to serve vector maps with GL styles. This version provides vector tiles for clients like Mapbox Android, iOS, GL JS, Leaflet, and OpenLayers without server-side rendering.

    npm install -g tileserver-gl-light
  5. Configure Cloudflare Cache Rules for TileServer GL

    master

    To optimize performance, you can cache TileServer GL endpoints using Cloudflare Cache Rules. TileServer GL serves various formats including .png, .jpg, .webp (raster), .pbf (vector), and .json (styles).

    To cache vector tiles and styles:

    1. Create a rule matching your hostname (equal).
    2. Create a rule matching URI Path (ends with) for .pbf and .json.
    3. Set Cache status to Eligible for cache.
    4. Overwrite Edge TTL with Browser TTL (e.g., 7 days).

    Warning: Setting a Browser TTL affects client-side expiration. If you update your maps or styles, clients may continue to render old tiles until the cache expires or is manually cleared.

  6. Build TileServer GL light Docker image from source

    master

    To build the tileserver-gl-light image from the repository source, clone the repo, run the publish script without publishing, and build the image from the light directory.

    git clone https://github.com/maptiler/tileserver-gl.git
    cd tileserver-gl
    node publish.js --no-publish
    cd light
    docker build -t tileserver-gl-light .
  7. Run TileServer GL behind a proxy or load-balancer

    master
    When running TileServer GL behind a proxy, ensure the proxy is configured to pass X-Forwarded-* headers to the server. Specifically, X-Forwarded-Host and X-Forwarded-Proto are required so that TileServer GL generates correct URLs (domain and protocol) within TileJSON and other responses.
  8. Run TileServer GL via CLI

    master

    You can start TileServer GL using the tileserver-gl command. You can specify a data file (MBTiles or PMTiles) or a configuration file. If no data file is provided, the server attempts to download a sample file (Zurich area). If no configuration file is provided, a default preview style compatible with OpenMapTiles is used.

    Usage: tileserver-gl [file] [options]
    
    Options:
      --file <file>             MBTiles or PMTiles file (local path, http(s)://, s3://, pmtiles://, or mbtiles:// URL)
      -c, --config <file>       Configuration file [config.json]
      -b, --bind <address>      Bind address
      -p, --port <port>         Port [8080]
      -C|--no-cors             Disable Cross-origin resource sharing headers
      -u|--public_url <url>    Enable exposing the server on subpaths
      --fetch-timeout <ms>      Timeout in ms for fetching remote tiles (default: 15000)
      --ignore-missing-files    Do not exit when referenced data files are missing at startup
      -V, --verbose [level]     More verbose output (level 1-3)
      -s, --silent              Less verbose output
      -l|--log_file <file>      output log file
      -f|--log_format <format>  define the log format
      -v, --version             output the version number
      -h, --help                display help for command
  9. Configure Sprites and Fonts in Style JSON

    master

    Ensure your style JSON contains correct paths for sprites and glyphs. Both can be local or remote.

    Sprites: Use the sprite property. TileServer-GL automatically looks for extensions like .json and .png (including @2x variants). Available placeholders:

    • {style}: The name of the style file (e.g., my-style.json).
    • {styleJsonFolder}: The path to the style file directory.

    Fonts (Glyphs): Use the glyphs property. Available placeholders:

    • {fontstack}: The name of the font and variant.
    • {range}: The range of the glyphs.

    Example: "glyphs": "{fontstack}/{range}.pbf" will resolve to paths like fonts/Open Sans/0-255.pbf.

  10. Manual Publishing

    master

    Use these steps only for special cases like debugging or specific environment needs. Ensure the version number in package.json, the Git tag, and the Docker image tags are identical.

    1. Update Version

    Manually modify the version field in package.json.

    2. Create and Push Git Tag

    git tag vX.X.X
    git push origin --tags

    3. NPM Publishing Options

    Choose one of the following:

    • Publish only full tileserver-gl: npm publish --access public
    • Build and publish both tileserver-gl and tileserver-gl-light: node publish.js
    • Build only tileserver-gl-light (no publish): node publish.js --no-publish

    4. Build and Push Docker Images

    Ensure you are logged in via docker login before proceeding.

    Main Image:

    docker buildx build --platform linux/amd64 -t maptiler/tileserver-gl:latest -t maptiler/tileserver-gl:X.X.X .
    docker push maptiler/tileserver-gl --all-tags

    Light Image:

    cd light
    docker buildx build --platform linux/amd64 -t maptiler/tileserver-gl-light:latest -t maptiler/tileserver-gl-light:X.X.X .
    docker push maptiler/tileserver-gl-light --all-tags
    cd ..
  11. Reference local MBTiles in style JSON

    master

    To use local MBTiles files within a style JSON, use the mbtiles:// prefix. TileServer-GL will look for the file in the root/mbtiles/ directory.

    Alternatively, you can use the syntax mbtiles://{data_id} to reference an existing data object defined in your config.json by its ID.

    "sources": {
      "source1": {
        "url": "mbtiles://source1.mbtiles",
        "type": "vector"
      }
    }
  12. Configure S3 and S3-compatible storage for PMTiles

    master

    PMTiles can be accessed directly from AWS S3 or S3-compatible storage (e.g., MinIO, DigitalOcean Spaces) using s3:// URLs.

    Supported URL Formats:

    1. AWS S3: s3://bucket-name/path/to/file.pmtiles
    2. S3-compatible: s3://endpoint-url/bucket-name/path/to/file.pmtiles

    Authentication: TileServer-GL uses the standard AWS credential chain:

    • Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
    • AWS credentials file: ~/.aws/credentials (Linux/macOS) or C:\Users\USERNAME\.aws\credentials (Windows)
    • IAM roles (on EC2, ECS, or Lambda)

    Configuration in config.json: When defining S3 sources in the data section, use configuration properties for better control. These take precedence over URL query parameters.

    PropertyDescription
    s3ProfileSpecifies the AWS credential profile to use.
    s3RegionSpecifies the AWS region. Default: us-east-1.
    requestPayerEnables "requester pays" buckets (boolean). Default: false.
    s3UrlFormatSpecifies how to interpret S3 URLs (e.g., aws).

    Using S3 in Style JSON: When referencing S3 sources inside a style JSON, you must use the pmtiles:// prefix followed by the S3 URL. You can only use URL query parameters for configuration in style JSON (properties like s3Profile are not supported here).

    // Recommended: Using configuration properties in config.json
    "data": {
      "us-west-tiles": {
        "pmtiles": "s3://prod-bucket/tiles.pmtiles",
        "s3Profile": "production",
        "s3Region": "us-west-2"
      }
    }
    
    // Using S3 in Style JSON (via query parameters)
    "sources": {
      "aws-tiles": {
        "url": "pmtiles://s3://my-bucket/tiles.pmtiles?profile=production",
        "type": "vector"
      }
    }