Skip to main content

Converts a GDAL-readable dataset into an MBTiles file.This is used to generate web maps.

Project description

Convert GDAL-readable datasets into an MBTiles file

gdal2mbtiles helps you generate web mapping tiles that can be shown through a browser-based mapping library on your website.

GDAL-readable files are images that are georeference, that means that they are positioned and projected on to the world. In order to display a dynamic map on the web, you don’t want to serve the whole image at once, so it must be sliced into tiles that are hosted by a tile server.

The MBTiles file format was developed by MapBox to make tile storage easier. You can upload the final file to their service, or run your own tile server. MapBox provides one called TileStream.

Later versions of GDAL (>= 2) allow generation of mbtiles files via the gdal_translate and gdaladdo commands. However, gdal2mbtiles offers some advantages:

  • allows you to specify an upper resolution/zoom level. GDAL always uses the native resolution of the input raster to determine the highest zoom level of the mbtiles output, whereas gdal2mbtiles can also upsample to create zoom levels at a higher resolution than your original file.

  • the gdal_translate command only converts the geotiff at the native resolution, so the lower resolutions are added to the file via overviews (gdaladdo)

  • gdaladdo can only add overviews down to the zoom level corresponding to the size of the tile/block size (256x256). gdal2mbtiles can always create images down to zoom level 1.

  • performance: gdal2mbtiles uses pyvips for image processing, which is parallel and quick. Compared to the equivalent processing with GDAL, gdal2mbtiles is typically 2-4 times quicker. For example:

    • a resolution 14 file, 13000x11000 pixels, min resolution 0, max resolution 14: ~5 minutes with gdal2mbtiles and ~8 minutes with GDAL commands.

    • a resoluton 11 file, 200,000x200,000, zoom level 11 only: ~30min with gdal2mbtiles and ~133min with GDAL (with GDAL_CACHE_MAX and GDAL_NUM_THREADS options)

Installation

PyPI package page: https://pypi.python.org/pypi/gdal2mbtiles/

Using pip:

$ pip install gdal2mbtiles

From source:

$ git clone https://github.com/ecometrica/gdal2mbtiles.git
$ cd gdal2mbtiles
$ python setup.py install

External Dependencies

We rely on GDAL to read georeferenced datasets. Under Debian or Ubuntu, you can install the GDAL library & binary via apt.

Default GDAL versions in Ubuntu LTS:

  • Xenial: 1.11

  • Bionic: 2.2

  • Focal: 3.0

We recommend using the UbuntuGIS PPA to get more recent versions of GDAL, if needed, as is the case for Xenial.

sudo add-apt-repository ppa:ubuntugis/ppa && sudo apt-get update
sudo apt-get install gdal-bin libgdal-dev

The ubuntugis PPA also usually includes python-gdal or python3-gdal that will install the python bindings at the system level. Installing that may be enough for you if you aren’t planning to use a non-default python or a virtual environment.

Otherwise, you will also need to install the GDAL python bindings package from PyPI. Make sure to install the version that matches the installed GDAL library. You can double-check that version with gdal-config --version.

pip install \
  --global-option=build_ext \
  --global-option=--gdal-config=/usr/bin/gdal-config \
  --global-option=--include-dirs=/usr/include/gdal/ \
  GDAL=="$(gdal-config --version)"

We also rely on VIPS (version 8.2+) to do fast image processing.

Under Debian or Ubuntu, run the following to install it without the GUI nip2:

$ sudo apt-get install --no-install-recommends libvips libvips-dev

You’ll also need a few other libraries to deal with large TIFF files and to optimize the resulting PNG tiles.

Under Debian or Ubuntu, run the following to install them:

$ sudo apt-get install libtiff5 optipng pngquant

Command Line Interface

$ gdal2mbtiles --help
usage: gdal2mbtiles [-h] [-v] [--name NAME] [--description DESCRIPTION]
                    [--layer-type {baselayer,overlay}] [--version VERSION]
                    [--format {jpg,png}]
                    [--spatial-reference SPATIAL_REFERENCE]
                    [--resampling {near,bilinear,cubic,cubicspline,lanczos}]
                    [--min-resolution MIN_RESOLUTION]
                    [--max-resolution MAX_RESOLUTION] [--fill-borders]
                    [--no-fill-borders] [--zoom-offset N]
                    [--coloring {gradient,palette,exact}]
                    [--color BAND-VALUE:HTML-COLOR]
                    [--colorize-band COLORIZE-BAND]
                    [--png8 PNG8]
                    [INPUT] [OUTPUT]

Converts a GDAL-readable into an MBTiles file

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         explain what is being done

Positional arguments:
  INPUT                 GDAL-readable file.
  OUTPUT                Output filename. Defaults to INPUT.mbtiles

MBTiles metadata arguments:
  --name NAME           Human-readable name of the tileset. Defaults to INPUT
  --description DESCRIPTION
                        Description of the layer. Defaults to ""
  --layer-type {baselayer,overlay}
                        Type of layer. Defaults to "overlay"
  --version VERSION     Version of the tileset. Defaults to "1.0.0"
  --format {jpg,png}    Tile image format. Defaults to "png"

GDAL warp arguments:
  --spatial-reference SPATIAL_REFERENCE
                        Destination EPSG spatial reference. Defaults to 3857
  --resampling {near,bilinear,cubic,cubicspline,lanczos}
                        Resampling algorithm for warping. Defaults to "near"
                        (nearest-neighbour)

Rendering arguments:
  --min-resolution MIN_RESOLUTION
                        Minimum resolution/zoom level to render and slice.
                        Defaults to None (do not downsample)
  --max-resolution MAX_RESOLUTION
                        Maximum resolution/zoom level to render and slice.
                        Defaults to None (do not upsample)
  --fill-borders        Fill image to whole world with empty tiles. Default.
  --no-fill-borders     Do not add borders to fill image.
  --zoom-offset N       Offset zoom level by N to fit unprojected images to
                        square maps. Defaults to 0.
  --png8                Quantizes 32-bit RGBA to 8-bit RGBA paletted PNGs.
                        value range from 2 to 256. Default to False.

Coloring arguments:
  --coloring {gradient,palette,exact}
                        Coloring algorithm.
  --color BAND-VALUE:HTML-COLOR
                        Examples: --color="0:#ff00ff" --color=255:red
  --colorize-band COLORIZE-BAND
                        Raster band to colorize. Defaults to 1

Contributing

Reporting bugs and submitting patches

Please check our issue tracker for known bugs and feature requests.

We accept pull requests for fixes and new features.

Development and Testing

We use Tox and Pytest to test locally and CircleCI for remote testing.

  1. Clone the repo

  2. Install whichever External Dependencies are suitable for your OS/VM.

  3. Create and activate a virtual environment

  4. Install tox: pip install tox

  5. Set the GDAL_CONFIG env var for tox via the venv activations script.

    If using virtualenv: echo 'export GDAL_VERSION=$(gdal-config --version)' >> $VIRTUAL_ENV/bin/postactivate

    If using venv: echo 'export GDAL_VERSION=$(gdal-config --version)' >> $VIRTUAL_ENV/bin/activate

  6. Run tests to confirm all is working: tox

  7. Do some development:

    • Make some changes

    • Run the tests

    • Fix any errors

    • Run the tests again

    • Update CHANGELOG.rst with a line about the change in the UNRELEASED section

    • Add yourself to AUTHORS.rst if not already there

    • Write a nice commit message

    • Repeat

  8. Make a PR

You don’t need to worry initially about testing in every combination of GDAL and Ubuntu, leave that to the remote CI build matrix when you make a PR and let the reviewers figure out if it needs more work from that.

Credits

Maxime Dupuis and Simon Law wrote this program, with the generous support of Ecometrica.

See AUTHORS.rst for the full list of contributors.

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

gdal2mbtiles-2.1.5.tar.gz (47.8 kB view details)

Uploaded Source

Built Distribution

gdal2mbtiles-2.1.5-py3-none-any.whl (53.3 kB view details)

Uploaded Python 3

File details

Details for the file gdal2mbtiles-2.1.5.tar.gz.

File metadata

  • Download URL: gdal2mbtiles-2.1.5.tar.gz
  • Upload date:
  • Size: 47.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for gdal2mbtiles-2.1.5.tar.gz
Algorithm Hash digest
SHA256 ef3d0aa44f97a4dd5e3c7be3a001ef7928c73645c199f03447ae46522490fd1c
MD5 e62e69946e230079f98c9e328b1d646a
BLAKE2b-256 e291c5912e8195d135d8d96b67c0dfa6be666d69f10d832b84510c85be57a154

See more details on using hashes here.

File details

Details for the file gdal2mbtiles-2.1.5-py3-none-any.whl.

File metadata

  • Download URL: gdal2mbtiles-2.1.5-py3-none-any.whl
  • Upload date:
  • Size: 53.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for gdal2mbtiles-2.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c678a846e23db31fdfda03835054a1ba34fc916163316573f46885988463e838
MD5 c285f8ae913aefad46df42010708d164
BLAKE2b-256 61dab94b069a43172a7c74848990ac46fd7ed2b0fcb7f37b6277481e5a00db17

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