lundi 12 octobre 2015

GDAL and OGR utilities as library functions

It has been often stated that the popular GDAL/OGR C/C++ command line utilities, such as gdal_translate, gdalwarp, ogr2ogr, etc... are mostly examples of how to use the GDAL/OGR API for your own purposes. The truth is that many people just need their functionnality without much additional tuning. Furthermore, over time those utilities have gained a lot of options and tweaks which make it time consuming to understand their working or reinvent them from scratch. Or people repeatedly copied&pasted their source code into their own code. Or simply spawn an external process with the binary of the utility. None of those approaches was optimal.

The recently approved RFC 59.1 "GDAL/OGR utilities as a library", that will be part of GDAL 2.1, aims at solving those problems by moving the code of the utilities into the main GDAL library.

The main advantages are :
  • the utilities can be easily called from any of the supported languages : C, C++, Python, Java (C# and Perl untested at time of writing, but should work with slight changes).
  • in-memory datasets can be used for input and output, avoiding the creation of temporary files on permanent storage.
  • a callback for progress report and cancellation of the processing can be provided.
  • faster execution for repeated calls (in comparison to the case where an external process was spawned)
Here's a 3 line example in Python to reproject a GeoTIFF in WebMercator and export it as a PNG file :
from osgeo import gdal
tmp_ds = gdal.Warp('temp', 'in.tif', format = 'MEM', dstSRS = 'EPSG:3857')
gdal.Translate('out.png', tmp_ds, format = 'PNG')
The utilities currently available from the library are :
gdalbuildvrt will probably be added to this list as well.

This work has been initiated by Faza Mahamood during GSoC 2015 and has been integrated and extended by Even Rouault (Spatialys).

EDIT: as a few people were wondering, the existing command line utilities are kept of course and use the library functions...

4 commentaires:

  1. This is a great addition. It also solves the occasional PATH issues where you are using language bindings but calling the utilities resolves to a different installation somewhere.

    Is it also possible to output to an in-memory VRT? Or at least something which delays processing until the data is actually read. Because i assume your example already performs the warp and fill memory with the entire file. That would limit its use to outputs which fit entirely in memory.

    Maybe using VRT as output format and adding the 'vsimem' prefix in the location?

    1. Of course. Exactly what you can do currently with command line gdalwarp.

      And I've just added an extra convenience. You can used empty name for temporary VRT output :

      ds = gdal.Warp('', input_ds, format = 'VRT')

  2. This is nice! Used to call the modules through ... This is much more elegant. Thanks!

  3. Cool information can be found on your blog. June