Generating Map tiles at Different Zoom Levels Using Gdal2tiles in Python

Sumeet Parekh
Apr 10 · 4 min read
Satellite image for New York City taken from the PlanetScope satellite
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

Sample geo-referenced image from OpenMapTiles

Installation & Usage

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 gdal2tilessource_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 tileddestination_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

Geek Culture

Proud to geek out.

Sign up for Geek Culture Hits

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Sumeet Parekh

Written by

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store