Skip to main content

Server to on-the-fly extract and serve vector tiles from an mbtiles file on S3

Project description

mbtiles-s3-server CircleCI Test Coverage

Python server to on-the-fly extract and serve vector tiles from mbtiles files on S3. Javascript, maps styles, fonts, and sprites are included so you can get setup quickly, especially with OpenMapTiles mbtiles files, but these are not required to be used.

Versioning must be enabled on the underlying S3 bucket

Work in progress. Not all features described are implemented. This document serves as a rough design spec.

Installation

pip install mbtiles-s3-server

The libsqlite3 binary library is also required, but this is typically already installed on most systems. The earliest version of libsqlite3 known to work is 2012-12-12 (3.7.15).

Example usage

  1. Create or obtain an mbtiles file, for example from https://openmaptiles.org/.

  2. Upload this file to S3, for example to https://my-bucket.s3.eu-west-2.amazonaws.com/tiles.mbtiles

  3. Ensure to have a IAM user that has s3:GetObject and s3:GetObjectVersion permissions on this S3 object, for example

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "",
                "Effect": "Allow",
                "Principal": "*",
                "Action": [
                    "s3:GetObject",
                    "s3:GetObjectVersion"
                ],
                "Resource": [
                    "arn:aws:s3:::my-bucket/tiles.mbtiles"
                ]
            }
        ]
    }   
    
  4. Start this server, configured with the location of this object and credentials for this user - it's configured using environment variables. You can assign the tiles file any version you like, in this case, 1.0.

    PORT=8080 \
    MBTILES__1__URL=https://my-bucket.s3.eu-west-2.amazonaws.com/tiles.mbtiles \
    MBTILES__1__IDENTIFIER=mytiles \
    MBTILES__1__VERSION=1.0 \
    AWS_REGION=eu-west-2 \
    AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE \
    AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY \
    AWS_SESSION_TOKEN="Only needed for temporary credentials" \
    HTTP_ACCESS_CONTROL_ALLOW_ORIGIN="*" \
        python -m mbtiles_s3_server
    
  5. On your user-facing site, include HTML that loads these tiles from this server, for example to load maps from a server started as above running locally

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Example map</title>
        <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
        <script src="http://localhost:8080/v1/static/maplibre-gl@2.1.9/maplibre-gl.js"></script>
        <link href="http://localhost:8080/v1/static/maplibre-gl@2.1.9/maplibre-gl.css" rel="stylesheet">
      </head>
      <body>
        <h1>Example map</h1>
        <div id='map' style='width: 400px; height: 300px;'></div>
        <script>
        var map = new maplibregl.Map({
            container: 'map',
            style: 'http://localhost:8080/v1/styles/positron-gl-style@1.8/style.json?tiles=mytiles@1.0',
            center: [-74.5, 40],
            zoom: 9
        });
        </script>
      </body>
    </html>
    

    This HTML is included in this repository in example.html. A simple server can be started to view it by

    python -m http.server 8081 --bind 127.0.0.1
    

    and going to http://localhost:8081/example.html

For the curious, advanced, or developers of this server itself

Hosting your own vector map tiles to show them in a browser requires quite a few components:

  1. JavaScript

    A library such as MapLibre GL, and your own code to run the library, and point it to a style file

  2. Style file

    A JSON file that defines how the library should style the map tiles, and where it should find the map tiles, glyphs (fonts), and the sprite

  3. Glyphs (fonts)

    Sets of fonts; different fonts can be used for different labels and zoom levels, as defined in the Style file

  4. Sprite

    A single JSON index file and a single PNG file; the JSON file contains the offsets and sizes of images within the single PNG file

  5. Vector map tiles

    A set of often hundreds of thousands of tiles each covering a different location and different zoom level. These can be distributed as a single mbtiles file, but this is not the format that the Javascript library accepts. This on-the-fly conversion from the mbtiles file to tiles is the main feature of this server.

Licenses

The code of the server itself is released under the MIT license. However, several components included are released under separate licenses.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mbtiles-s3-server-0.0.2.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

mbtiles_s3_server-0.0.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file mbtiles-s3-server-0.0.2.tar.gz.

File metadata

  • Download URL: mbtiles-s3-server-0.0.2.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.3 CPython/3.9.7

File hashes

Hashes for mbtiles-s3-server-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1222b858049f913ec24026e28bd08de4e92e307c081ca7f1afc31dc9b1bbb66b
MD5 d004f51b3572bccb85ab5c08e16040a0
BLAKE2b-256 a0f2b0026f885fa2489438dd96ed0dc6f0e5b2bbcc01a3ae8902669e8bf38533

See more details on using hashes here.

File details

Details for the file mbtiles_s3_server-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: mbtiles_s3_server-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.5 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.3 CPython/3.9.7

File hashes

Hashes for mbtiles_s3_server-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 775490442b5553aea35739faa8f6cb9f6753530580c9695e69559e3628887f51
MD5 03d36e9ccd74501801f17e4fb4d57c2b
BLAKE2b-256 e741f4a1a2a05ec2c59914042d516abc22239f169ed2d0f5d7c931f2c5a5ae18

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page