Skip to content
Permalink
Browse files

[API] method to retrieve corresponding GDAL driver from a file

extension
  • Loading branch information
alexbruy committed Aug 1, 2017
1 parent fef93fc commit 0476d4e2daa7cf6c070a39a2bfc23ede2ada9720
@@ -156,6 +156,15 @@ class QgsRasterFileWriter
:rtype: list of str
%End

static QString driverForExtension( const QString &extension );
%Docstring
Returns the GDAL driver name for a specified file ``extension``. E.g. the
driver name for the ".tif" extension is "GTiff".
If no suitable drivers are found then an empty string is returned.
.. versionadded:: 3.0
:rtype: str
%End

};

/************************************************************************
@@ -29,6 +29,9 @@
#include <QTextStream>
#include <QMessageBox>

#include <gdal.h>
#include <cpl_string.h>

QgsRasterDataProvider *QgsRasterFileWriter::createOneBandRaster( Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
{
if ( mTiledMode )
@@ -991,3 +994,37 @@ QString QgsRasterFileWriter::vrtFileName()
QFileInfo outputInfo( mOutputUrl );
return QStringLiteral( "%1.vrt" ).arg( outputInfo.fileName() );
}

QString QgsRasterFileWriter::driverForExtension( const QString &extension )
{
QString ext = extension.trimmed();
if ( ext.isEmpty() )
return QString();

if ( ext.startsWith( '.' ) )
ext.remove( 0, 1 );

GDALAllRegister();
int const drvCount = GDALGetDriverCount();

for ( int i = 0; i < drvCount; ++i )
{
GDALDriverH drv = GDALGetDriver( i );
if ( drv )
{
char **driverMetadata = GDALGetMetadata( drv, nullptr );
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) && CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false ) )
{
QString drvName = GDALGetDriverShortName( drv );
QStringList driverExtensions = QString( GDALGetMetadataItem( drv, GDAL_DMD_EXTENSIONS, nullptr ) ).split( ' ' );

Q_FOREACH ( const QString &driver, driverExtensions )
{
if ( driver.compare( ext, Qt::CaseInsensitive ) == 0 )
return drvName;
}
}
}
}
return QString();
}
@@ -130,6 +130,14 @@ class CORE_EXPORT QgsRasterFileWriter
void setPyramidsConfigOptions( const QStringList &list ) { mPyramidsConfigOptions = list; }
QStringList pyramidsConfigOptions() const { return mPyramidsConfigOptions; }

/**
* Returns the GDAL driver name for a specified file \a extension. E.g. the
* driver name for the ".tif" extension is "GTiff".
* If no suitable drivers are found then an empty string is returned.
* \since QGIS 3.0
*/
static QString driverForExtension( const QString &extension );

private:
QgsRasterFileWriter(); //forbidden
WriterError writeDataRaster( const QgsRasterPipe *pipe, QgsRasterIterator *iter, int nCols, int nRows, const QgsRectangle &outputExtent,
@@ -97,6 +97,15 @@ def testWrite(self):

assert allOk, "Raster file writer test failed"

def testDriverForExtension(self):
self.assertEqual(QgsRasterFileWriter.driverForExtension('tif'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('TIF'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('tIf'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('.tif'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('img'), 'HFA')
self.assertEqual(QgsRasterFileWriter.driverForExtension('.vrt'), 'VRT')
self.assertEqual(QgsRasterFileWriter.driverForExtension('not a format'), '')
self.assertEqual(QgsRasterFileWriter.driverForExtension(''), '')

if __name__ == '__main__':
unittest.main()

0 comments on commit 0476d4e

Please sign in to comment.
You can’t perform that action at this time.