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

ความคิดเห็น • 19

  • @hubertkanyamahanga2095
    @hubertkanyamahanga2095 2 ปีที่แล้ว +1

    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.

  • @malcolmcoull1240
    @malcolmcoull1240 3 ปีที่แล้ว

    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.

  • @chaoli932
    @chaoli932 2 ปีที่แล้ว

    You must a good researcher in RS. Thanks for your help

  • @hadikarimi2818
    @hadikarimi2818 2 ปีที่แล้ว +1

    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

  • @rubencrespo1720
    @rubencrespo1720 3 ปีที่แล้ว

    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!

  • @saeidshamsaliei2536
    @saeidshamsaliei2536 3 ปีที่แล้ว

    Thank you very much

  • @MrBreazyyy
    @MrBreazyyy 3 ปีที่แล้ว +1

    Is there a way to add multiple geotiff files( like markers or houses) on an other geotiff file?

  • @sarahtulip4714
    @sarahtulip4714 2 ปีที่แล้ว

    Hi, the output file size becomes too large after merging. Could you please explain how to fix this?

  • @shubhamkedar508
    @shubhamkedar508 3 ปีที่แล้ว +1

    Can you make video on custom dataset of raster(geotiff image)dataset.

  • @isabelahorta3063
    @isabelahorta3063 2 ปีที่แล้ว

    Hiii, I'm using the virtual raster method but not all the tifs are being merged :( what should I do?

  • @livmaritharstad1629
    @livmaritharstad1629 ปีที่แล้ว

    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?

  • @sabazehra1412
    @sabazehra1412 3 ปีที่แล้ว +1

    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

    • @kmehour
      @kmehour 2 ปีที่แล้ว

      I'm having the same issue, did you find the answer to that by any chance ?

    • @sabazehra1412
      @sabazehra1412 2 ปีที่แล้ว

      @@kmehour No sorry I could not figure it out, but I tried the second method as mentioned in this video and it worked well.

    • @aryanirvaan
      @aryanirvaan 2 ปีที่แล้ว

      The solution to this problem is subprocess.call(cmd.split()+demlist) write subprocess.call([cmd.split()+demlist], shell=True)

    • @aryanirvaan
      @aryanirvaan 2 ปีที่แล้ว

      @@kmehour Please read the comment.

    • @Maze160
      @Maze160 ปีที่แล้ว +1

      @@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?

  • @yergenabdrakhmanov8243
    @yergenabdrakhmanov8243 3 ปีที่แล้ว

    Where did you get these pictures?