Merge raster data with GDAL in Python
ฝัง
- เผยแพร่เมื่อ 20 ก.ย. 2024
- This tutorial explains how to merge a bunch of raster tiles from the same directory to a single geotiff file using GDAL in Python. There are two options to merge raster data with GDAL: 1) run gdal_merge.py with subprocess 2) build a virtual mosaic of all raster files and convert that to geotiff.
GDAL/OGR Python API: gdal.org/python/
Code:
from osgeo import gdal
import glob
import subprocess
list all files in directory that match pattern
demList = glob.glob("dem[0-9][0-9].tif")
print(demList)
gdal_merge
cmd = "gdal_merge.py -ps 10 -10 -o mergedDEM.tif"
subprocess.call(cmd.split()+demList)
build virtual raster and convert to geotiff
vrt = gdal.BuildVRT("merged.vrt", demList)
gdal.Translate("mergedDEM2.tif", vrt, xRes = 10, yRes = -10)
vrt = None
Thanks for the short tutorial. Here is an alternative: gdal_merge.py -o large_output_raster.tif your/raster/folder/*.tif Make sure you have gdal installed and run it in terminal.
Fantastic, thank you so much. I have spent much longer than I should working out how to loop this through an entire directory of images, which will save so loads of time in future.
You must a good researcher in RS. Thanks for your help
How did you merge python to QGIS to work instantly and show the result? Can you please let me know if there is a video for it. Thank you
I don't understand the subprocess.call function. I understand that you concatenate the cmd with the DMElist, but why is the split needed. Awesome tutorial, thanks!
Thank you very much
Is there a way to add multiple geotiff files( like markers or houses) on an other geotiff file?
Hi, the output file size becomes too large after merging. Could you please explain how to fix this?
Can you make video on custom dataset of raster(geotiff image)dataset.
Hiii, I'm using the virtual raster method but not all the tifs are being merged :( what should I do?
Great video! I've merged a couple of datasets with no issue, but now I get this error:
ERROR 1: ZIPDecode:Decoding error at scanline 0
ERROR 1: TIFFReadEncodedTile() failed.
ERROR 1: \\Path to this file...\DTM_1km_6159_636.tif, band 1: IReadBlock failed at X offset 0, Y offset 0: TIFFReadEncodedTile() failed.
the data is from Denmark and I've got different offset values like x=6, 1 or 8 and y=5 or 6
I've been stuck on this problem for a while and tried all the solutions I've found on the internet, have you encountered this problem before?
Hi, thank you for the tutorial, I am new to python and I am getting the error (OSError: [WinError 193] %1 is not a valid Win32 application) while running the command of subprocess.call(cmd.split()+demlist). Can you please help me out? thanks
I'm having the same issue, did you find the answer to that by any chance ?
@@kmehour No sorry I could not figure it out, but I tried the second method as mentioned in this video and it worked well.
The solution to this problem is subprocess.call(cmd.split()+demlist) write subprocess.call([cmd.split()+demlist], shell=True)
@@kmehour Please read the comment.
@@aryanirvaan Thanks! Helped me solve the problem.
Now I get the error: "TypeError: expected str, bytes or os.PathLike object, not list" did you happen to encounter that as well?
Where did you get these pictures?