I want to use gdal_merge.py
to merge a series of .tif files prior to clipping the merged file to a shapefile boundary, but want to do so as part of another larger script that will execute a number of other processes. I am a python/GDAL newbie and am not sure how to go about calling gdal_merge.py
into another script.
I cannot merely run gdal_merge.py
on its own as it is but one step in a script that will hopefully execute a number of processes. Any thoughts on the best way to do this?
-
Welcome to the site Mike. As this question stands, I see four questions: How to generate a list of tiff files?; How to reproject a list of .tiff files? How to clip a list of tiff files?; How to call a script from within a script?. GIS SE is a focused Q&A site that is best suited for single questions. Please consider focusing this post into a single question. For more information on GIS SE, you can take the tour (gis.stackexchange.com/tour) or access our help center (gis.stackexchange.com/help).Aaron– Aaron ♦2014年10月20日 15:35:30 +00:00Commented Oct 20, 2014 at 15:35
2 Answers 2
The easiest way to do this is by importing the path where gdal_merge.py
is located, in my case, /usr/bin/
-- substitute with the path to gdal_merge
on your system, which, obviously, could be a Windows path too.
import sys
sys.path.append('/usr/bin/')
import gdal_merge as gm
You will now have to build up an array for sys.argv
, as if you were calling gdal_merge
directly, e.g.,
sys.argv = ['-o','outputfile.tiff','inputfile1.png', 'inputfile2.png', ....'inputfile10.png']
gm.main()
There is more information on this Stack Overflow post
There is also the __init__.py
mechanism, but this requires the file you are importing to be in a sub-directory of wherever you are running your python file from.
-
Thanks a lot for the response John, I'll give it a try and let you know how it goes.Mike– Mike2014年10月21日 00:34:05 +00:00Commented Oct 21, 2014 at 0:34
-
Are you in a position to use gdalwarp instead (depends what you are doing, I guess, but often recommended over gdal_merge). If so, you could use the suprocess module as it is C and not Python. Look particularly at Popen.John Powell– John Powell2014年10月21日 09:05:28 +00:00Commented Oct 21, 2014 at 9:05
-
So here's the situation. I have to write a script that will reproject a series of tifs, merge them, and then clip them to a boundary. I have already written a script using gdalwarp to reproject (using the subprocess module per another poster). That said, I now have to pull off the other two steps in the same script and am not sure how to use subprocess for multiple processes. Furthermore, I wasn't aware that gdalwarp could merge a series of files. I'm sure this is fairly simple, but this is all brand new to me.Mike– Mike2014年10月21日 12:32:28 +00:00Commented Oct 21, 2014 at 12:32
-
gdalwarp input_tiles output_tile will definitely mosaic tiles and you could call it using subprocess also. Unfortunately, we just had a power cut at work and my laptop battery is a bit low, so can't do any testing :(John Powell– John Powell2014年10月21日 12:42:51 +00:00Commented Oct 21, 2014 at 12:42
-
Awesome John, thanks. Will gdalwarp need to be called in 3 different subprocesses given that it will be running three different processes (reprojecting 4 tiles, mosaicing the four, and then clipping them) or can all these features be contained in a single line?Mike– Mike2014年10月21日 16:52:33 +00:00Commented Oct 21, 2014 at 16:52
Edit:
Just read that this option was suggested in the comments already. Anyway, for completeness sake. Maybe someone can merge the answers?
Of course it also possible to use python's subprocess
, e.g.
import subprocess
def merge(first, second, out_file):
"""
This utility will automatically mosaic a set of images.
All the images must be in the same coordinate system and
have a matching number of bands, but they may be overlapping,
and at different resolutions. In areas of overlap,
the last image will be copied over earlier ones.
:param first:
:param second:
:param out_file:
:return:
"""
ps = subprocess.Popen(
['gdal_merge.py', '-o', out_file,
'-of', unipath.Path(out_file).ext,
'-n', NO_DATA_VALUE,
first, second],
stdout=subprocess.PIPE
)
output = ps.communicate()[0]
for line in output.splitlines():
logger.debug("[*] {0}".format(line))