I try to merge some multiband rasterfiles (Sentinel 2 data) with Rasterio into a single rasterfile but the result is incorrect. The files are too big to fit in my RAM completely, but I have enough SWAP and I don't get a Error Message. Any ideas, what could have gone wrong?

My code:

import rasterio
from rasterio.merge import merge
import glob

files = glob.glob("../data_multiband/*.tif")
mosaic = []
for file in files:
    tmp = rasterio.open(file)
mosaic, out_trans = merge(mosaic)
out_meta = tmp.meta.copy()
out_meta.update({"driver": "GTiff",
                 "height": mosaic.shape[1],
                 "width": mosaic.shape[2],
                 "transform": out_trans})
with rasterio.open("../data_merged/test.tif", "w", **out_meta) as dest:

That is how the result looks:

enter image description here

As you can see (red marks) some rasterfiles occur two times. The one on the left side are wrong. Thats how the original files look:

enter image description here

  • RAM performance is measured in nanoseconds, swap speed in milliseconds; the three orders of magnitude difference is an indicator that you really don't want to rely on swap for this (1 sec of RAM ~= 16.6 minutes of swap)
    – Vince
    Oct 2, 2020 at 12:40
  • does it work when you use only two input images? If yes try to add ass many images as possible and see where the error occurs.
    – til_b
    Oct 5, 2020 at 22:16

2 Answers 2


I solved the problem. The files have the same crs, but are in different zones. Reprojecting solved it


You may want to use gdalwarp (or gdal.Warp if using Python) for this. This will allow you to merge and reproject in one go. Often a good idea to set x/y resolution when reprojecting in case GDAL ends giving you something like 9.34 m rather than 10 m

gdalwarp  -t_srs "EPSG:XXXX" -tr X Y in1.tif in2.tif out.tif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.