Stitch images using gdal command utility

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Stitch images using gdal command utility

#1 Post by _unknown_ » 23 Oct 2014 21:44

I have 4 images in one day downloaded from a website

How will I merge all these 4 images and save to a new directory leaving only the first 8 character as the file name for each day.

Code for merging files:

set in_path=path_to_in_file
set out_path=path_to_out_file

md %out_path%

cd /d %in_path%

FORFILES /C "cmd /c gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o @path %out_path%\@fname.tif"
Last edited by _unknown_ on 06 Feb 2015 00:46, edited 2 times in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to merge images per day using gdalmerge and cmd(.bat

#2 Post by Squashman » 24 Oct 2014 09:42

_unknown_ wrote:FORFILES /C "cmd /c gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o @path %out_path%\@fname.tif"

You said merging files is easy but I am not understanding how you are getting this code to merge one day of data with the correct output file name.
Could you show a better example of how you are merging one day of data.

_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Re: Stitch images using gdal command utility

#3 Post by _unknown_ » 26 Oct 2014 21:20

Example of merging images for one day:

gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o merged.tif
Last edited by _unknown_ on 06 Feb 2015 00:50, edited 3 times in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to merge images per day using gdalmerge and cmd(.bat

#4 Post by Squashman » 27 Oct 2014 07:27

_unknown_ wrote:Merging images:

gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o merge.tif input1.tif input2.tif input3.tif input4.tif/input5.tif

Please show an example with the file names you specified above.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to merge images per day using gdalmerge and cmd(.bat

#5 Post by Aacini » 27 Oct 2014 09:51

I don't like to try to solve problems when the data is incomplete, but here we go.

Assume that in the input path there are a certain number of files with this naming scheme:

Code: Select all

TYYYYDDDxxxxxx.L2.Tera.tif

Where YYYYDDD is a YearDay. The Batch file below is an attempt to solve the request. The data used in each step is explained in the code:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Initialize variables
set "in_path=path_to_in_file"
set "out_path=path_to_out_file"
set "yearDay="
set "fileList="

rem Process all *.tif files in input path
cd "%in_path%"
for %%a in (*.tif) do (
   set "fileName=%%a"
   rem If the YearDay in this file is the same of previous one
   if "!fileName:~1,7!" equ "!yearDay!" (
      rem Join this filename to previous list
      set "fileList=!fileList! !fileName!"
   ) else (
      rem Merge the files in the list, if any, in the out_path leaving only TYYYYDDD.tif
      if defined fileList gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o %out_path%\T!yearDay!.tif !fileList!
      rem And start a new YearDay and fileList
      set "yearDay=!fileName:~1,7!"
      set "fileList=!fileName!"
   )
)
rem Merge the files in the last list
gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o %out_path%\T!yearDay!.tif !fileList!


Antonio

_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Re: Stitch images using gdal command utility

#6 Post by _unknown_ » 27 Oct 2014 20:37

Do I really need to set the yearday?Because the files that I'm going to process is not just for one year but from year 2000 up to present and so on. That would be lots of years and days.
Last edited by _unknown_ on 06 Feb 2015 00:51, edited 1 time in total.

_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Re: Stitch images using gdal command utility

#7 Post by _unknown_ » 27 Oct 2014 21:11

Files added to the input_directory everyday are always delayed one day. How will I consider these things in merging these files? Thank you in advance!
Last edited by _unknown_ on 06 Feb 2015 00:53, edited 2 times in total.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to merge images per day using gdalmerge and cmd(.bat

#8 Post by Aacini » 28 Oct 2014 00:42

_unknown_ wrote:Sorry if you wasted your time on this. I'm just new to this batch things. Do I really need to set the yearday?Because the files that I'm going to process is not just for one year but from year 2000 up to present and so on. That would be lots of years and days.


Excuse me. I think there is a confusion here. Your original request have several missing points, so I tried to guess some details in order to attempt to generate a solution. To be clear in the assumptions, I included the specifications used in the solution as comments in the program.

For example, in your original request you said "I have all the projected images for 3 days... How will I merge all the images that are for day 1, for day 2 and for day 3", but I assumed that you want to process more than just 3 days so the program indicate: "Process all *.tif files in input path". This means that all files will be processed when the program run, so you don't need to set any yearday.

The easiest way to realize what the program do is just running it. You may include an ECHO command before the gdal_merge.py commands that "Merge the files in the list", so the resulting commands be displayed in the screen instead of be executed. For example:

Code: Select all

if defined fileList ECHO gdal_merge.py -n 0 -a_nodata -32767 -of GTiff ...

ECHO gdal_merge.py -n 0 -a_nodata -32767 -of GTiff ...

If the input_directory would contain the files you show in the first post of this topic, the displayed commands would be these ones:

Code: Select all

gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o T2010232.tif T2010232034000.L2.Tera.tif T2010232035000.L2.Tera.tif T2010232036000.L2.Tera.tif T2010232037000.L2.Tera.tif
gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o T2010233.tif T2010233045000.L2.Tera.tif T2010233046000.L2.Tera.tif T2010233047000.L2.Tera.tif T2010233048000.L2.Tera.tif T2010233049000.L2.Tera.tif
gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o T2010234.tif T2010234050000.L2.Tera.tif T2010234051000.L2.Tera.tif T2010234052000.L2.Tera.tif T2010234053000.L2.Tera.tif T2010234054000.L2.Tera.tif

If you want to request some modifications in this solution please list the file names in the input_directory, the results displayed by previous program with those files (with the ECHO commands), and clearly describe the changes you want.

Antonio

_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Re: Stitch images using gdal command utility

#9 Post by _unknown_ » 28 Oct 2014 01:07

I just set the following:
set "in_path=D:\input\"
set "out_path=D:\output\"
set "yearDay=1999123" and just leave the filelist blank

I also include an ECHO command before the gdal_merge.py.

And here is the output:

C:\path\to\bat\file>merged.bat
gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o D:\merged\\T201023.tif
Last edited by _unknown_ on 06 Feb 2015 00:49, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to merge images per day using gdalmerge and cmd(.bat

#10 Post by Squashman » 28 Oct 2014 06:43

You do not need to set the YEARDAY variable. It has been clearly stated.

_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Re: How to merge images per day using gdalmerge and cmd(.bat

#11 Post by _unknown_ » 28 Oct 2014 23:20

I didn't set the yearday and the filelist and here's the output:

C:\path\to\bat\file>merged.bat
No input files selected.
Usage: gdal_merge.py [-o out_filename] [-of out_format] [-co NAME=VALUE]*
[-ps pixelsize_x pixelsize_y] [-tap] [-separate] [-q] [-v] [-pct]
[-ul_lr ulx uly lrx lry] [-init "value [value...]"]
[-n nodata_value] [-a_nodata output_nodata_value]
[-ot datatype] [-createonly] input_files
[--help-general]

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to merge images per day using gdalmerge and cmd(.bat

#12 Post by Squashman » 29 Oct 2014 06:36

Using Antonio's batch code and the input file names you gave us in your first post, this is the output I get.
The forum is just wrapping the output.

Code: Select all

C:\BatchFiles\gdal_merge>aacini_code.bat
gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o C:\BatchFiles\gdal_merge\Output\T2010232.tif T2010232034000.L2_LAC.Tera.tif T2010232035000.L2_LAC.Tera.tif T2010232036000.L2_LAC.Tera.tif T2010232037000.L2_LAC.Tera.tif

gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o C:\BatchFiles\gdal_merge\Output\T2010233.tif T2010233045000.L2_LAC.Tera.tif T2010233046000.L2_LAC.Tera.tif T2010233047000.L2_LAC.Tera.tif T2010233048000.L2_LAC.Tera.tif T2010233049000.L2_LAC.Tera.tif

gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o C:\BatchFiles\gdal_merge\Output\T2010234.tif T2010234050000.L2_LAC.Tera.tif T2010234051000.L2_LAC.Tera.tif T2010234052000.L2_LAC.Tera.tif T2010234053000.L2_LAC.Tera.tif T2010234054000.L2_LAC.Tera.tif

C:\BatchFiles\gdal_merge>

_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Re: How to merge images per day using gdalmerge and cmd(.bat

#13 Post by _unknown_ » 29 Oct 2014 19:47

Sorry if I'm still confused. I'm really not good at it. I really can't figure it out. What I did was fill the in_path and the out_path only, and still showing a "No input files selected error".

_unknown_
Posts: 25
Joined: 23 Oct 2014 21:04

Re: How to merge images per day using gdalmerge and cmd(.bat

#14 Post by _unknown_ » 30 Oct 2014 02:57

I called the merge.bat from the desktop and not on where the input directory is. Because I want it to call from everywhere.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to merge images per day using gdalmerge and cmd(.bat

#15 Post by Squashman » 30 Oct 2014 06:37

_unknown_ wrote:Sorry if I'm still confused. I'm really not good at it. I really can't figure it out. What I did was fill the in_path and the out_path only, and still showing a "No input files selected error".

Correct. That is all you need to change in the batch file.

Post Reply