Generating Map tiles at Different Zoom Levels Using Gdal2tiles in Python
By: Sumeet Parekh
Tags: Image Processing, GIS, Python

Generating Map tiles at Different Zoom Levels Using Gdal2tiles in Python Sumeet Parekh Follow Apr 10 · 4 min read

Satellite image of New York City captured by the PlanetScope satellite

In recent years, we have seen massive growth in the use of geospatial data which comprises both satellite and aerial imagery. Not only has this growth led to satellite and aerial imagery becoming a part of the big data revolution but has also provided businesses with new avenues to explore. Earlier, governments and large corporations were the only ones to have access to quality satellite images for military and defense purposes. Today, anyone with a computer and internet connection can obtain satellite images since the prices of acquiring satellite images have fallen significantly. This has opened up various opportunities for data scientists and machine learning engineers to work on different projects.

The images from satellites are only as good as their spatial resolution. Images are sharper if the pixel size is small. Thus, high-resolution images would have a very low value for spatial resolution. Commercial satellites like the WorldView-3 and SkySat have spatial resolution close to 31cm/px and 50cm/px respectively.

There are two major concerns with high-resolution raster data.

If we want to display such high-resolution raster data, a lot of memory is required.

If we are to push it over the internet, there could be bandwidth issues.

To address these concerns, it is best to tile the high-resolution images and gdal2tiles can help us with it.

Data

To walk you through an example using the gdal2tiles library, we will be using a sample geo-referenced image from OpenMapTiles which is in the .tif file format. Images from OpenMapTiles have a spatial resolution of 20m/px.

Sample geo-referenced image from OpenMapTiles

Installation & Usage

Before installing the gdal2tiles library, make sure python and GDAL are installed on your machine. There are some GDAL dependencies required by gdal2tiles. Thus, it would be best to have both python and GDAL installed. If you need help installing them, you can follow this step-by-step guide by Christoph Rieke.

Open up a terminal on your machine and run the following command to install the gdal2tiles python library.

$ pip install gdal2tiles

You can look at the different options available by writing the following command in the terminal.

$ gdal2tiles.py -help

The output for the above command can be seen below:

List of options available for gdal2tiles

Run the command below to get geospatial information of the source image. In our case, the sample image has a file name sample.tif.

$ gdalinfo sample.tif

You should get an output that looks like this:

gdalinfo command output

The output provides us with important geospatial information of the image like the projection or srs value (EPSG:4326), the bounding box coordinates (Upper Left, Lower Left, Upper Right, Lower Right), datum, etc. We need the projection code from this information when using the gdal2tiles library.

Let us write a small python script using the gdal2tiles library to generate tiles using some of the options from above.

import gdal2tiles source_path = "Enter Source Path here"

destination_path = "Enter Source Path here"

def create_tiles(source_path, destination_path): """ Method to generate tiles at different zoom levels using gdal2tiles library. Args: source_path : path for the source file which needs to be tiled destination_path : path for the output directory where the tiles will be generated """ options = {'zoom': 16, 'nb_processes': 4, 'tile_size': 256, 'srs':'EPSG:4326' }

gdal2tiles.generate_tiles(source_path, destination_path,

**options) create_tiles(source_path, destination_path)

As you can see, some of the options from the options list above have been used in the options dictionary. For the above method, we are trying to generate tiles for zoom level 16, tile size for the generated tiles is 256, number of processes used are 4, and the srs (projection) value is EPSG:4326. The options can be modified as per your requirement.

Run the above script using the following command. Replace “gdal2tiles_test” as per the name of your python file.

$ python3 gdal2tiles_test.py

Here is a sample (4 tiles) tiled output after running the above command. It is a step-by-step display of how the tiles are generated at zoom level 16 to form the part of the sample image.

1

2

3

4

Conclusion

This post provides basic knowledge of why gdal2tiles is required and how to use it to generate tiles at different zoom levels in python. You can also do this manually using tools like Adobe Photoshop or GIMP, but it can be prone to errors and can be tedious. Tile generation is a must when it comes to overlaying images onto maps or hosting/serving maps.