How to convert a GeoTIFF file to raster map tiles

There are several options available if you want to convert a GeoTIFF file to raster map tiles. Here I will show three ways of achieving this.

I am using docker here because it is a really easy way platform-independent way to use the latest tools. If you are not familiar with docker I highly recommend learning about it. Docker is a really powerful piece of software. However, If you prefer to can use locally installed executables. The tools used here are:


For these examples, I am using raster data from natural earth but the techniques used here should work with any GeoTIFF file.

Option 1 - Create an mbtiles file using gdal_translate and gdaladdo


cd to the directory where your tif file is located and run these commands:

docker run --rm -v "$PWD":/work -w /work -it osgeo/gdal:ubuntu-full-latest gdal_translate HYP_HR_SR_OB_DR.tif HYP_HR_SR_OB_DR.mbtiles -of MBTILES
docker run --rm -v "$PWD":/work -w /work -it osgeo/gdal:ubuntu-full-latest gdaladdo HYP_HR_SR_OB_DR.mbtiles 2 4 8 16

The first command creates an mbtiles file up to the maximum zoom possible with the image quality. This file will only have one zoom level available. The second command adds lower zoom levels.

You can view the created tiles using tileserver-gl:

docker run -it --rm --name tileserver-gl -v $PWD:/data -p 8080:80 klokantech/tileserver-gl

Advantages


  • Uses offical gdal tools including the official docker image.

Disadvantages


  • Server needs mbtiles support or tiles need to be extracted.

Option 2 - Create TMS tiles with gdal2tiles


cd to the directory where your tif file is located. You then need to create a directory for your tiles. For the command below I used tiles. You can then create your tiles using a command like this:

docker run --rm -v "$PWD":/work -w /work pchynoweth/gdal:ubuntu-full-latest gdal2tiles.py --zoom=1-6 SR_HR.tif tiles

Note: I used an alternative gdal docker image because of issues with the latest package. At time of writing the python dependencies were not part of the official docker image. This may have changed since. If it works I recommend using the official docker image instead: `osgeo/gdal:ubuntu-full-latest.

This command will place all your tiles in the tiles directory along with some sample html pages. To demo the tiles launch any simple http server. I used python3:

python -m http.server --bind localhost 3000

Note: You may need to use python3 instead of python for some linux distributions.

Advantages


  • Does not need any special server side support for serving tiles. This means tiles can be served from any basic http server or something like Amazon S3.
  • Uses offical gdal tools.
  • Example all work and are easy to run.

Disadvantages


  • gdal2tiles.py dependencies can be problematic. It doesn't work with the official docker image.

Option 3 - Create an mbtiles file with gdal2mbtiles


gdal2mbtiles uses gdal2tiles and mbutil to create an mbtiles file of raster images. It can be invoked like this:

docker run --rm -v $PWD:/work -w /work pchynoweth/gdal2mbtiles:latest gdal2mbtiles SR_HR.tif -z 0-6 -w none mymap.mbtiles

You can view the created tiles using tileserver-gl as we did for option 1:

docker run -it --rm --name tileserver-gl -v $PWD:/data -p 8080:80 klokantech/tileserver-gl

If you run with -w all you will get a bunch of examples showing how to use the data. To use these you will need to run a server that can both serve tiles and html pages. If you just want to quickly preview these you can run tileserver-gl and python. You just need to modify the index.html to use tileserver-gl for tile requests like this:

- ./{z}/{x}/{y}.png
+ http://[::]:8080/data/SR_HR/{z}/{x}/{y}.png

If you are looking for an all-in-one solution I suggest looking at tobinbradley/mbtiles-server.

Advantages


  • Easy to create zoom levels greater than the resolution should support.
  • Creates good examples showing how to use the map with different clients.

Disadvantages


  • Slower than the other methods.
  • Not an official gdal tool.
  • Server needs mbtiles support or tiles need to be extracted.
  • Some of the examples do not work. OpenLayers 3 works.

Comments

Popular Posts