I have a list of many many rasters. I want to have a simple and easy to understand script that takes this and merges it into one big mosaic. I found a similar question but the accepted answer adivices against using R for this purpose. I could also use R but are very inclined towards Python. I really need a script as I am going to simply share it with some people that want it in this form. I am inclined to using gdal_merge.py but I wanted some thoughts on the problem first as I am potentially going to handle massive amounts of images. Why merge so many images? I also found it impractical but was something I was asked to do.


Have you looked at using FWTools? There is a python script called gdal_merge that is available within FWTools. You can use a list as input. The command with usage would be:

gdal_merge -o c:\temp\output_image.tif -q -v --optfile c:\temp\rasterlist.txt
  • Sure, this was exactly what I was going to use. Does it make sense to use it for a big big amount of images? – JEquihua Apr 2 '13 at 19:18
  • I have used it for hundreds of images. The question you need to know is the output size. Certain file types have file size limitations. But yes, you can use it on large raster datasets. – Ryan Garnett Apr 2 '13 at 19:26
  • I would want the script to search in a folder form a list with all image file names: path="C://Julian//EarthEnginedownload//L5_ANNUAL_EVI_2002" dirList=os.listdir(path) and feed that to gdal_merge, how can I properly do this? @Ryan Garnett – JEquihua Apr 2 '13 at 23:34
  • You are going to want to output that search to a text file. When I have done this in command prompt, I use the > option. Ie: >c:\temp\imagelist.txt This outlist will be used in the --optfile option. – Ryan Garnett Apr 3 '13 at 14:56
  • Ok, so I did something like this: ### discard files that are not geotiffs dirList = [item for item in dirList if item[(len(item)-3):(len(item))]=="tif"] ### print to text file thefile = open('filelist.txt', 'w') for item in dirList: print>>thefile, item Ideally I would want to call gdal_merge.py from another script as I find it rather cumbersome to create the *.txt and then gdal_merge -o ... As I mentioned before I'm interested in sharing this, it's not really for me. I would like to use something like gdal_merge.raster_copy( etc ) but I have not got this to work. – JEquihua Apr 3 '13 at 15:53

Another approach that doesn't generate a new image - specially in a situation where you have a plethora of large raster datasets - but uses the existing ones could be using the gdalbuildvrt command line: it gives you more control for the output through its options and you don't need to provide the txt file with the raster list if all of your images are in a single directory.

If your application doesn't support GDAL virtual formats (but most of OSGeo projects does, like QGIS and MapServer), you can still convert the generated VRT file (it is just an xml) to an effective image format using gdal_translate.

  • 1
    please note that if you make a vrt (Virtual Raster) you need the source files to be there somewhere, since internally the vrt links to those files. – nickves Apr 3 '13 at 12:11
  • of course you need the files, but this way you do not duplicate the needed storage as with gdal_merge – capooti Apr 3 '13 at 13:36
  • Alas I dont think that the application supports GDAL virtual formats. We are working on the google earth engine and it's in very early stages of development but thank you, I like the idea a lot. – JEquihua Apr 3 '13 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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