2

I'm wondering if there is any way of retrieving the correct file extension for GDAL/OGR drivers. For example, if I do something like

from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')

I would like to know what file extension an ESRI Shapefile requires. Of course I could create my own mapping, e.g. using a dictionary, but I would need to fill it manually. Aren't there any other possibilities?

1 Answer 1

5

Get a handle to the driver, then check its metadata (driver.GetMetadataItem(gdal.DMD_EXTENSIONS)):

from osgeo import gdal

raster_drivers = []
vector_drivers = []

for i in range(gdal.GetDriverCount()):

    drv = gdal.GetDriver(i)
    md = drv.GetMetadata_Dict()
        
    d = [drv.ShortName, drv.LongName, drv.GetMetadataItem(gdal.DMD_EXTENSIONS)]

    if 'DCAP_RASTER' in md:
        raster_drivers.append(d)
    if 'DCAP_VECTOR' in md:  # note "if" not "elif" or "else" as some drivers can handle both raster and vector
        vector_drivers.append(d)
2
  • Works nicely for GDAL, thanks. Could you extend your answer to OGR, too? Based on your answer, I found that it works similarly, with meta = driver.GetMetadata() and meta['DMD_EXTENSION'].
    – s6hebern
    Jul 20, 2021 at 8:33
  • 1
    My answer includes OGR (as of GDAL v2.0 released in 2015). Notice the checks for DCAP_VECTOR? If you're still using the old ogr, get with the times and use gdal.OpenEx to open vector data ;).
    – user2856
    Jul 20, 2021 at 9:38

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.