Tiles in a Tile Map Server (TMS) context are basically raster map data that’s broken into tiny pre-rendered tiles for maximum web client loading efficiency. GDAL, with Python, can chop up your input raster into the folder/file name and numbering structures that TMS compliant clients expect.
This is an excerpt from the book: Geospatial Power Tools – Open Source GDAL/OGR Command Line Tools by me, Tyler Mitchell. The book is a comprehensive manual as well as a guide to typical data processing workflows, such as the following short sample…
The bonus with this utility is that it also creates a basic web mapping application that you can start using right away.
The script is designed to use georeferenced rasters, however, any raster should also work with the right options. The (georeferenced) Natural Earth raster dataset is used in the first examples, with a non-georeferenced raster at the end.
There are many options to tweak the output and setup of the map services; see the complete gdal2tiles chapter for more information.
Minimal TMS Generation
At the bare minimum an input file is needed:
gdal2tiles.py NE1_50M_SR_W.tif Generating Base Tiles: 0...10...20...30...40...50...60...70...80...90...100 - done. Generating Overview Tiles: 0...10...20...30...40...50...60...70...80...90...100 - done.
The output created is the same name as the input file, and include an array of sub-folders and sample web pages:
NE1_50M_SR_W NE1_50M_SR_W/0 NE1_50M_SR_W/0/0 NE1_50M_SR_W/0/0/0.png NE1_50M_SR_W/1 ... NE1_50M_SR_W/4/9/7.png NE1_50M_SR_W/4/9/8.png NE1_50M_SR_W/4/9/9.png NE1_50M_SR_W/googlemaps.html NE1_50M_SR_W/openlayers.html NE1_50M_SR_W/tilemapresource.xml
Open the openlayers.html file in a web browser to see the results.
The default map loads a Google Maps layer, it will complain that you do not have an appropriate API key setup in the file, ignore it and switch to the OpenStreetMap layer in the right hand layer listing. |
The resulting map should show your nicely coloured world map image from the Natural Earth dataset. The TMS Overlay option will show in the layer listing, so you can toggle it on/off to see that it truly is loading. Figure 5.2 (above) shows the result of our gdal2tiles command.
Geospatial Power Tools is 350+ pages long – 100 of those pages cover these kinds of workflow topic examples. Each copy includes a complete (edited!) set of the GDAL/OGR command line documentation as well as the following topics/examples:
Workflow Table of Contents
- Report Raster Information – gdalinfo 23
- Web Services – Retrieving Rasters (WMS) 29
- Report Vector Information – ogrinfo 35
- Web Services – Retrieving Vectors (WFS) 45
- Translate Rasters – gdal_translate 49
- Translate Vectors – ogr2ogr 63
- Transform Rasters – gdalwarp 71
- Create Raster Overviews – gdaladdo 75
- Create Tile Map Structure – gdal2tiles 79
- MapServer Raster Tileindex – gdaltindex 85
- MapServer Vector Tileindex – ogrtindex 89
- Virtual Raster Format – gdalbuildvrt 93
- Virtual Vector Format – ogr2vrt 97
- Raster Mosaics – gdal_merge 107