List of GDAL raster file extensions
To get a list of format names and common extensions use:
from osgeo import gdal
for i in range(gdal.GetDriverCount()):
drv = gdal.GetDriver(i)
if drv.GetMetadataItem(gdal.DCAP_RASTER):
print(drv.GetMetadataItem(gdal.DMD_LONGNAME), drv.GetMetadataItem(gdal.DMD_EXTENSIONS))
Output snippet:
('Virtual Raster', 'vrt')
('GeoTIFF', 'tif tiff')
('National Imagery Transmission Format', 'ntf')
('Raster Product Format TOC format', 'toc')
('ECRG TOC format', 'xml')
('Erdas Imagine Images (.img)', 'img')
('CEOS SAR Image', None)
('CEOS Image', None)
('JAXA PALSAR Product Reader (Level 1.1/1.5)', None)
('Ground-based SAR Applications Testbed File Format (.gff)', 'gff')
('ELAS', None)
('Arc/Info Binary Grid', None)
That is assuming you only want raster formats, and are using GDAL 2.0+. If you are using 1.x you can omit the DCAP_RASTER test. This is commonly used in 'Open...' or 'Save As...' dialogs for recommended names. If you are doing a 'Save As...', you have to check for the appropriate metadata option for Create() or CreateCopy()(DCAP_CREATE/DCAP_CREATECOPY).
Apologies if my code is non-pythonic, I rarely use it. If you'd like a specific answer for a specific version, please post your gdal version.
File extensions are mostly meaningless. Sure, they might indicate the file's format but you can easily rename a .exe file to .txt and the file would not change itself. File extensions are mostly used for ease of usage and on some operating systems to decide what to do (which program to launch) if a user tries to "open" a file. Opening an .exe file that was renamed to .txt will lead to confusion because the file format itself is not text.
GDAL supports formats, not file extensions. Some formats have various common extensions, others might even not have any standards.
Instead of checking file extensions, use something like a try/except
statement to determine if gdal can open the input file.
If you are trying to answer the question "Will the user be able to open the dataset with GDAL?" by comparing it to a precompiled list of file extensions (or even datasets for that matter) you will be in big trouble for two reasons:
As @bugmenot123 pointed out the file extension might differ from the raster filetype and even more importantly a lot of extensions overlap / are used by different formats (e.g. .hdf, .bil).
The amount of formats GDAL can handle depends largely on how it was compiled. Lots of drivers require external libraries to work and if GDAL wasn't compiled with them it won't be able to open the dataset (again .hdf HDF5 is a prominent example). There is no one definitive GDAL installation but it very likely differs from user to user.
One way to solve this is by trying to open the dataset and see if GDAL can handle it (as @bugmenot123 pointed out with try/except
).
from osgeo import gdal
gdal.UseExceptions()
try:
gdal.Open("/user/provided/path/Geotiff.tif")
except:
print "not a supported dataset"