2

I am trying to batch mosaic a set of rasters using gdal in OSGeo4w shell command line. When I try to use gdal_merge I am getting error as below.

    run o-help for a list of available commands
    C:\>gdal_merge
    'gdal_merge' is not recognized as an internal or external command,
    operable program or batch file. 

I tried to see the available libraries in OSGeo. I found gdal_merge is not installed while running
o-help

C:\>o-help
                   -={ OSGeo4W Shell Commands }=-
  avcexport                               avcimport
  cct                                     cs2cs
  curl                                    derb
  dllupdate                               fax2ps
  fax2tiff                                fftw-wisdom
  gdaladdo                                gdalbuildvrt
  gdaldem                                 gdalenhance
  gdalinfo                                gdallocationinfo
  gdalmanage                              gdalserver
  gdalsrsinfo                             gdaltindex
  gdaltransform                           gdalwarp
  gdal_contour                            gdal_grid
  gdal_rasterize                          gdal_translate
  genbrk                                  genccode
  gencfu                                  gencmn
  gencnval                                gendict
  gennorm2                                genrb
  gensprep                                geod
  geotifcp                                gie
  gif2h5                                  gnmanalyse
  gnmmanage                               gpsbabel`

But I am seeing gdal_merge in other system. Also, I am able to find the gdal_merge.py in the below directory.

C:\OSGeo4W64\apps\Python37\Scripts

Why is it not showing in my system, and how do I fix this issue?

10
  • 2
    It's gdal_merge.py gdal.org/programs/gdal_merge.html, it's only compiled (.com), batch (.bat) and executables (.exe) that get to be called without their extension... assuming Windows. Are you running this from an OSGeo or GDAL shell? If you're going straight from CMD window you need to ensure you %PATH% contains your python install. Commented Feb 12, 2020 at 7:22
  • I am running straight from OSGeo shell which I already mentioned. Commented Feb 12, 2020 at 7:26
  • 1
    There should be files "gdal_merge.bat" and "gdal_merge.py" in your C:\OSGeo4W64\bin directory. If they are missing then OSGeo4W installer has not placed them for you. You can try to re-install or run the script directly with something like python "%OSGEO4W_ROOT%\apps\Python37\Scripts\gdal_merge.py" %*. BTW for most use cases gdalbuildvrt is better than gdal_merge.py.
    – user30184
    Commented Feb 12, 2020 at 7:40
  • 1
    @MichaelStimson is partly right. However, if OSGeo4W installation succeeds there is a gdal_merge.bat file that is found from the %PATH% and it starts python and launches the script with plain "gdal_merge". Contents of the gdal_merge.bat file are @echo off call "%OSGEO4W_ROOT%\bin\o4w_env.bat" python "%OSGEO4W_ROOT%\bin\gdal_merge.py" %*
    – user30184
    Commented Feb 12, 2020 at 8:11
  • 2
    Uninstall and re-install OSGeo4W.
    – user30184
    Commented Feb 12, 2020 at 14:08

2 Answers 2

2

Even gdal_merge.py is a Python script, with OSGeo4W installation command "gdal_merge" is supposed to work as well because there exists a "gdal_merge.bat" launch script that Windows fires with "gdal_merge" command.

C:\OSGeo4W64\bin>dir gdal_merge*
 Volume in drive C is Windows
 Volume Serial Number is BBBB-BBBB

 Directory of C:\OSGeo4W64\bin

12.02.2020  13.54                99 gdal_merge.bat
03.02.2020  23.59            19 848 gdal_merge.py

enter image description here

What the batch file does is to set the environment right and then ask Python to run the gdal_merge.py script

@echo off 
call "%OSGEO4W_ROOT%\bin\o4w_env.bat" 
python "%OSGEO4W_ROOT%\bin\gdal_merge.py" %* 

If gdal_merge.py and gdal_merge.bat are missing from the OSGeo4W\bin directory then something has gone wrong with the installation.

2
  • 2
    Thank you for sharing your knowledge. My answer was wrong. Something doesn't look OK in the OSGeo4W Shell of the question post. Commented Feb 12, 2020 at 18:41
  • Thanks for pointing that out, I've always used the python script directly from an OSGeo shell, I was not aware of the batch file method, though I haven't used it for quite a long time preferring GDALBuildVRT to pseudo-mosiac rasters. In irregular image sets (like road corridors or disjoint areas) it saves large chunks of NoData blocks required to make the raster rectangular. Commented Feb 12, 2020 at 23:58
0

For other gdal tools, if you have QGIS you can run:

cd C:\Program Files\QGIS 3.16\bin

then run your code, e.g

gdalwarp -of GTiff...

This is because the above directory contains gdalwarp.exe

However gdal_merge is located elsewhere, in C:\Program Files\QGIS 3.16\apps\Python37\Scripts

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.