How to Mosaic/Merge Raster Data in Python

A simple process to merge different Raster files

Abdishakur
Spatial Data Science

--

Satellite Image scenes might not coincide with your area of interest. You might need to clip into a boundary (make it smaller) or create a mosaic of different scenes to get the desired result (merge different raster files).

This short blog post will show you how to merge different raster tiles in Python using Rasterio.

Mosaic Example — Image by the author.

Raster Mosaic with Python

Let us first import the libraries and create the output folder with Python path lib.

from rasterio.plot import show
from rasterio.merge import merge
import rasterio as rio
from pathlib import Path
path = Path('data/')
Path('output').mkdir(parents=True, exist_ok=True)
output_path = 'output/mosaic_output.tif'

Now we iterate over the available .tif files in the data folder. We will merge all files in this data folder and create a mosaic from them. We also create an empty list to hold the files in the data folder.

raster_files = list(path.iterdir())raster_to_mosiac = []

--

--

Abdishakur
Spatial Data Science

Writing about Geospatial Data Science, AI, ML, DL, Python, SQL, GIS | Top writer | 1m views.