Skip to content
Permalink
Browse files

add supportedFiltersAndFormats() method to QgsRasterFileWriter

  • Loading branch information
alexbruy committed Dec 7, 2017
1 parent 7037c29 commit 52b58643357c9c19aed3cfd85e33256690a950ac
@@ -35,6 +35,13 @@ class QgsRasterFileWriter
WriteCanceled,
};

enum RasterFormatOption
{
SortRecommended,
};
typedef QFlags<QgsRasterFileWriter::RasterFormatOption> RasterFormatOptions;


QgsRasterFileWriter( const QString &outputUrl );

QgsRasterDataProvider *createOneBandRaster( Qgis::DataType dataType,
@@ -156,6 +163,37 @@ class QgsRasterFileWriter
:rtype: list of str
%End

static QString filterForDriver( const QString &driverName );
%Docstring
Creates a filter for an GDAL driver key
:rtype: str
%End

struct FilterFormatDetails
{
QString driverName;
%Docstring
Unique driver name
%End

QString filterString;
%Docstring
Filter string for file picker dialogs
%End
};

static QList< QgsRasterFileWriter::FilterFormatDetails > supportedFiltersAndFormats( RasterFormatOptions options = SortRecommended );
%Docstring
Returns a list or pairs, with format filter string as first element and GDAL format key as second element.
Relies on GDAL_DMD_EXTENSIONS metadata, if it is empty corresponding driver will be skipped even if supported.

The ``options`` argument can be used to control the sorting and filtering of
returned formats.

.. seealso:: :py:func:`supportedOutputRasterLayerExtensions()`
:rtype: list of QgsRasterFileWriter.FilterFormatDetails
%End

static QString driverForExtension( const QString &extension );
%Docstring
Returns the GDAL driver name for a specified file ``extension``. E.g. the
@@ -1023,3 +1023,83 @@ QStringList QgsRasterFileWriter::extensionsForFormat( const QString &format )
}
return QStringList();
}

QString QgsRasterFileWriter::filterForDriver( const QString &driverName )
{
GDALDriverH drv = GDALGetDriverByName( driverName.toLocal8Bit().data() );
if ( drv )
{
QString drvName = GDALGetDriverLongName( drv );
QString extensionsString = QString( GDALGetMetadataItem( drv, GDAL_DMD_EXTENSIONS, nullptr ) );
if ( extensionsString.isEmpty() )
{
return QString();
}
QStringList extensions = extensionsString.split( ' ' );
QString filter = drvName + " (";
for ( const QString &ext : extensions )
{
filter.append( QStringLiteral( "*.%1 *.%2" ).arg( ext.toLower(), ext.toUpper() ) );
}
filter.append( QStringLiteral( ")" ) );
return filter;
}

return QString();
}

QList< QgsRasterFileWriter::FilterFormatDetails > QgsRasterFileWriter::supportedFiltersAndFormats( RasterFormatOptions options )
{
QList< FilterFormatDetails > results;

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

FilterFormatDetails tifFormat;

for ( int i = 0; i < drvCount; ++i )
{
GDALDriverH drv = GDALGetDriver( i );
if ( drv )
{
QString drvName = GDALGetDriverShortName( drv );
char **driverMetadata = GDALGetMetadata( drv, nullptr );
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) && CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false ) )
{
QString filterString = filterForDriver( drvName );
if ( filterString.isEmpty() )
continue;

FilterFormatDetails details;
details.driverName = drvName;
details.filterString = filterString;

if ( options & SortRecommended )
{
if ( drvName == QLatin1String( "GTiff" ) )
{
tifFormat = details;
continue;
}
}

results << details;
}
}
}

std::sort( results.begin(), results.end(), []( const FilterFormatDetails & a, const FilterFormatDetails & b ) -> bool
{
return a.driverName < b.driverName;
} );

if ( options & SortRecommended )
{
if ( !tifFormat.filterString.isEmpty() )
{
results.insert( 0, tifFormat );
}
}

return results;
}
@@ -54,6 +54,16 @@ class CORE_EXPORT QgsRasterFileWriter
WriteCanceled = 6, //!< Writing was manually canceled
};

/**
* Options for sorting and filtering raster formats.
* \since QGIS 3.0
*/
enum RasterFormatOption
{
SortRecommended = 1 << 1, //!< Use recommended sort order, with extremely commonly used formats listed first
};
Q_DECLARE_FLAGS( RasterFormatOptions, RasterFormatOption )

QgsRasterFileWriter( const QString &outputUrl );

/**
@@ -134,6 +144,33 @@ class CORE_EXPORT QgsRasterFileWriter
void setPyramidsConfigOptions( const QStringList &list ) { mPyramidsConfigOptions = list; }
QStringList pyramidsConfigOptions() const { return mPyramidsConfigOptions; }

//! Creates a filter for an GDAL driver key
static QString filterForDriver( const QString &driverName );

/**
* Details of available filters and formats.
* \since QGIS 3.0
*/
struct FilterFormatDetails
{
//! Unique driver name
QString driverName;

//! Filter string for file picker dialogs
QString filterString;
};

/**
* Returns a list or pairs, with format filter string as first element and GDAL format key as second element.
* Relies on GDAL_DMD_EXTENSIONS metadata, if it is empty corresponding driver will be skipped even if supported.
*
* The \a options argument can be used to control the sorting and filtering of
* returned formats.
*
* \see supportedOutputRasterLayerExtensions()
*/
static QList< QgsRasterFileWriter::FilterFormatDetails > supportedFiltersAndFormats( RasterFormatOptions options = SortRecommended );

/**
* Returns the GDAL driver name for a specified file \a extension. E.g. the
* driver name for the ".tif" extension is "GTiff".

0 comments on commit 52b5864

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