GDAL2Tiles

From OpenStreetMap Wiki
Jump to: navigation, search

This page describes a how to use GDAL2Tiles to transform a georeferenced map into a TMS-map which can be used as background in JOSM or published as a web map.

Prerequisites

You need a raster image (bitmap) which is a map. You need to know or guess its projection as well.

Georeferencing with QGis

Use the Georeferencer plugin which uses GDAL.

  1. From the menu, select PluginsManage & Install Plugins.... Install the "Georeferencer GDAL" plugin.
  2. From the menu select RasterGeoreferencerGeoreferencer....
  3. Select Open raster, select an image file (jpg, png, tiff, etc).
  4. Pick a projection. OSM and Google use WGS 84 / Pseudo Mercator EPSG:3857 for their tilemaps.
  5. Select Add Points and click known points on the image and input their corresponding coordinates (at least 2-3 depending on the transformation type).
  6. Open transformation settings. Select output file and change other settings as needed (the defaults should work).
  7. Start the Geo-referencing which will output a GeoTiff file (.tif).
  8. Select Save GCP Points As if you want.

Tiling with GDAL2Tiles

From project page: "The GDAL2Tiles open-source project allows easy publishing of raster maps on the Internet. Your raster file (like TIFF/GeoTIFF, MrSID, ECW, JPEG2000, JPEG, PNG) is converted into a directory structure of small map tiles (TMS compatible), which you can just copy to the webserver" (source [1]). GDAL2Tiles is an easy to use Python-script.

There is also a wizard-like GUI called MapSlicer which leads you through the process step by step.

Installation (Windows)

An easy way to get GDAL2tiles is to use the OSGeo4W installer and install all GDAL-packages and the Python-package. After installation, run the OSGeo4W-shell using the desktop icon or, equivalently, run C:\OSGeo4W\bin\o4w_env.bat, in order to setup the necessary environment variables.

Installation (Linux)

For the latest version, build from source from their website.[2] Or for Ubuntu, you can add a PPA from the UbuntuGIS team such as ubuntugis-unstable.

Otherwise install the gdal-bin package from the repository with the package manager. Eg for Ubuntu, run apt-get install gdal-bin

Tile command

Run the following command to generate a Tile Map Service-compatible directory structure of map tiles from zoomlevel 1 to 8 in outputfolder:

gdal2tiles --profile=mercator -z 1-8 yourmap.tif outputfolder

yourmap.tif could for example be a georeferenced GeoTIFF created by QGIS using its Georeferencer plugin.

Opening in JOSM

JOSM might not support reading a TMS directory structure from the local filesystem. An easy workaround is to use a leigthweigth web-server serving outputfolder from the localhost 127.0.0.1. Use for example Mongoose for this purpose.

Setup a new Imagery source with the following URL

tms:http://127.0.0.1/{zoom}/{x}/{-y}.png

and enjoy your new tiled map. Notice the minus in {-y} which is necessary because JOSM expects tiles same directory structure as Google Maps which is sligthly different from the OSGeo TMS specification (see the gdal2tiles.py source code for details).

This might work to access local files (without a web-server):

tms:file//PATH/{zoom}/{x}/{-y}.png

where PATH is the absolute path for the outputfolder. For example, assuming outputfolder is equal to /home/user/tiles:

tms:file///home/user/tiles/{zoom}/{x}/{-y}.png


Due to copyright and license issues, never use arbitrary maps to develop OpenStreetMap!