[gdal-dev] Calling gdal_merge from a Python script
Even Rouault
even.rouault at spatialys.com
Fri Aug 26 10:36:02 PDT 2016
On Friday 26 August 2016 12:01:15 Robin Wilson wrote:
> Hi,
>
> I've was very happy to see the RFC 59.1 code merged in GDAL 2.1, allowing
> the use of some of the GDAL command-line utilities from within Python
> without having to 'shell out' using something like the subprocess module.
>
> I notice that gdal_merge isn't in the list of command-line utilities that
> have been wrapped to be accessed programmatically - and that, in fact, as
> far as I can see none of the Python-based command-line utilities have been
> wrapped.
>
> Are there any plans to provide programmatic access to gdal_merge.py and the
> other Python-based utilities?
>
> If not, what is the best suggested way to use this from a Python script?
> Using subprocess can be quite brittle (I've had lots of issues with paths
> etc, particularly on Windows machines - and being able to `import gdal` in
> Python is in no way related to being able to run gdal_merge.py from the
> shell), but the only other way seems to be to manipulate the path somehow
> to try and import gdal_merge.py, and then manipulate sys.argv to 'fake' the
> command-line arguments to gdal_merge.py - which also seems quite brittle.
Robin,
Yes, most Python based utilities are already usable as functions. You should
be able to "import gdal_merge" (either by tweaking the path or just copying it
next to your code) and call gdal_merge.main( argv = argv ) where argv is an
array of the splitted arguments.
That's more or less the approach that has been taken for exposing the C++
utilities. Perhaps not the most elegant one, but very easily extensible.
Even
--
Spatialys - Geospatial professional services
http://www.spatialys.com
More information about the gdal-dev
mailing list