Rapidly Mosaic Multiple Rasters using GDAL

Shubham Pawar
3 min readAug 1, 2022

--

An efficient way to use available computation resources

Mosaic of Landsat 8 tiles of India (Satellite imagery from USGS Earth Explorer)

Satellites in orbit provide invaluable information related to the earth’s surface. The satellite spatial resolution varies from a few centimeters to kilometers. With the free-of-cost availability of high-resolution satellite data, its application in various domains is increasing. The high-resolution data are rich in spatial information but occupy more space on disk. Most of the satellite data are in tiled format. So, if any regional, country or continental-scale analysis requires a seamless mosaicing of tiled data, it will be a tedious task to mosaic the tiles in one go. Although, the google earth engine (GEE) platform provides the functionality for processing large-scale geospatial data. But while exporting the data, GEE limits the number of pixels that can be exported at a time. If the number of pixels exceeds the threshold limit the platform saves it in tiles. The traditional software and tools are not optimized for handling large-scale data and mosaicing them. The Geospatial Data Abstraction Library (GDAL) library developed by the open source geospatial foundation (OSGEO) has the ability to process large-scale geospatial data in an efficient way.

Steps Involved:

  1. Installation of GDAL
  2. Create VRT of tiled raster data and Mosaic the rasters

Installation of GDAL

https://download.gisinternals.com/

Find the suitable version of GDAL core and GDAL python bindings. Install them on the system. If you face any problems while installing please follow this video.

After installation make sure that the GDAL installation path is added to the path variable of the system environment variable. Usually, the installation path is C:\Program Files\GDAL.

Also, create the following new variables in the system environment variable along with corresponding values.

     +-------------------+-----------------------------------+
| Variable Name | Value |
+===================+===================================+
| GDAL_DATA | C:\Program Files\GDAL\gdal-data |
| GDAL_DRIVER_PATH | C:\Program Files\GDAL\gdalplugins |
+-------------------+-----------------------------------+

Check installation

To check whether the installation is working properly or not by typing the following command in Command Prompt window

gdalinfo --version 

If it shows various options then the GDAL is successfully installed on the system.

Create VRT of tiled raster data

The VRT format is a native GDAL format that allows a virtual GDAL dataset to be composed from other GDAL datasets. The VRT file contains the datasets and the algorithms in XML structure. The tiled rasters will be the input data in our case. To mosaic the tiles we will create a VRT of the rasters tiles. This will help GDAL to analyze the data extent and scale.

gdalbuildvrt mosaic.vrt C:/path/to/rasters/*.tif

mosaic.vrt: Output virtual raster tile
C:/path/to/rasters/*.tif: Lists all the tiff files in a folder (keep all the files to be mosaiced in one folder)

Mosaic the rasters using VRT

The next step is applying the mosaicing algorithm. To mosaic the images we will use the gdalwarp utility. It is an image mosaicing, projection and warping utility. To mosaic the data type following command on command prompt

gdalwarp -srcnodata 0.0 -dstnodata 0.0 -r near -ot UInt16 -multi -wo "NUM_THREADS=ALL_CPUS" -co "COMPRESS=DEFLATE" -co "BIGTIFF=YES" mosaic.vrt mosaic.tif

The command has various flags. We will go through them one by one.

The -srcnodata flag sets no data value for input images. The -dstnodata flag sets no data value for the output image. -r specifies the image resampling method which we are using near which corresponds to nearest neighborhood resampling. -ot specifies the output data type which we are keeping as uin16 it depends on the input data range. The important flags are -multi and -woNUM_THREADS=ALL_CPUS’ which uses multithreaded warping with all CPU cores. With the multithreading option GDAL process chunks of image and perform input/output operation simultaneously. The last flag is -co which passes the file creation options. The input and output images are at end of the flags.

Here we go !!! Just wait for a few minutes your output is getting ready. It will not give any memory error.

--

--