3

How to fetch the available GDAL raster drivers in Python? (The equivalent of gdal_translate --formats using the command line utilities.)

2 Answers 2

8

A one liner to get all drivers (GDAL + OGR) (more or less 210 drivers)

python -c "from osgeo import gdal;print('\n'.join(sorted([gdal.GetDriver(i).GetDescription() for i in range(gdal.GetDriverCount())])))"

You can also filter drivers to get only GDAL one using the following (or you will not get the same number of drivers output as gdal_translate --formats e.g 136 on my machine)

I also display if driver support only raster or both raster and vector, I also display the other capabilities (read/write, support vsi,...). The logic is a port of the equivalent C++ code

from osgeo import gdal 

all_driver_names = [gdal.GetDriver(i).GetDescription() for i in range(gdal.GetDriverCount())] 

gdal_driver_names = [driver_name for driver_name in all_driver_names if gdal.GetDriverByName(driver_name).GetMetadataItem('DCAP_RASTER') == 'YES']

driver_type = {
    'DCAP_RASTER': 'raster',
    'DCAP_MULTIDIM_RASTER': 'multidimensional raster',
    'DCAP_VECTOR': 'vector',
    'DCAP_GNM': 'geography network'
}

for driver_name in gdal_driver_names:
    driver = gdal.GetDriverByName(driver_name)
    driver_shortname = driver.ShortName # Get short name
    driver_types = ','.join([driver_type[t] for t in driver_type.keys() if driver.GetMetadataItem(t) == 'YES']) # Get if driver support raster and vector or only raster

    pszRFlag = ""
    if driver.GetMetadataItem('DCAP_OPEN') == 'YES':
        pszRFlag = "r";
    
    pszWFlag = ""
    if driver.GetMetadataItem('DCAP_CREATE') == 'YES':
        pszWFlag = "w+";
    elif driver.GetMetadataItem('DCAP_CREATECOPY') == 'YES':
        pszWFlag = "w";
    else:
        pszWFlag = "o";

    pszVirtualIO = ""
    if driver.GetMetadataItem('DCAP_VIRTUALIO') == 'YES':
        pszVirtualIO = "v";
    
    pszSubdatasets = ""
    if driver.GetMetadataItem('DMD_SUBDATASETS') == 'YES':
        pszSubdatasets = "s";

    driver_longname = driver.LongName # Get Long name
    print(f"{driver_shortname} -{driver_types}- ({pszRFlag}{pszWFlag}{pszVirtualIO}{pszSubdatasets}): {driver_longname}")
3
  • Mike gets the nod for a more comprehensive answer. I like the one liner though, so +1. Commented Jun 24, 2021 at 22:29
  • Edited to get all the rules e.g not only short and long name but also if raster only or raster and vector but also how to get driver capabilities (in parenthesis in gdal_translate --formats output )
    – ThomasG77
    Commented Jun 24, 2021 at 23:15
  • 2
    Sorting alphabetically is ok when you're looking for a specific driver, but do note that the order that gdalinfo --formats outputs (and presumably the python API) is intended as that is the order that GDAL tries each driver when attempting to open a file. Can be important with certain formats when trying to figure out why a file is getting opened by one driver instead of the one you really want (e.g. some hdf and jp2 based files).
    – user2856
    Commented Jun 25, 2021 at 8:44
6

Checking the Python docs I see:

  • gdal.GetDriverCount(), returns an int
  • gdal.GetDriver(int i), returns a Driver()
  • gdal.Driver(), has ShortName and LongName attributes

So chaining them together:

import gdal


def list_drivers():
    n = gdal.GetDriverCount()
    for i in range(n):
        driver = gdal.GetDriver(i)
        print('{:03d}: {} - {}'.format(i, driver.ShortName, driver.LongName))


if __name__ == '__main__':
    list_drivers()

2
  • Thanks! I have to learn to navigate the API docs better. I had looked and searched up and down several times and still managed to miss GetDriver (tunnel vision on the keyword 'format' I guess). Commented Jun 24, 2021 at 22:27
  • 2
    If you don't filter with if driver.GetMetadataItem('DCAP_RASTER') == 'YES', you don't get the "right" number of drivers (vector and raster instead of only raster) like in gdal_translate --formats
    – ThomasG77
    Commented Jun 24, 2021 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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