Skip to content
Permalink
Browse files

Fix QgsRasterFileWriter::driverForExtension and extensionsForFormat

were skipping non-creatable datasets

And indicate in the docs that read-only datasets are also considered
  • Loading branch information
nyalldawson committed Jan 30, 2018
1 parent f37c2bf commit eba7e7b8b443d13ca22817a16116ec9533abf06b
@@ -175,6 +175,9 @@ 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.

Note that this method works for all GDAL drivers, including those without create support
(and which are not supported by QgsRasterFileWriter).

.. versionadded:: 3.0
%End

@@ -185,6 +188,9 @@ E.g. returns "tif", "tiff" for the format "GTiff".

If no matching format driver is found an empty list will be returned.

Note that this method works for all GDAL drivers, including those without create support
(and which are not supported by QgsRasterFileWriter).

.. versionadded:: 3.0
%End

@@ -996,7 +996,7 @@ QString QgsRasterFileWriter::driverForExtension( const QString &extension )
if ( drv )
{
char **driverMetadata = GDALGetMetadata( drv, nullptr );
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) && CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false ) )
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false ) )
{
QString drvName = GDALGetDriverShortName( drv );
QStringList driverExtensions = QString( GDALGetMetadataItem( drv, GDAL_DMD_EXTENSIONS, nullptr ) ).split( ' ' );
@@ -1018,7 +1018,7 @@ QStringList QgsRasterFileWriter::extensionsForFormat( const QString &format )
if ( drv )
{
char **driverMetadata = GDALGetMetadata( drv, nullptr );
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) && CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false ) )
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false ) )
{
return QString( GDALGetMetadataItem( drv, GDAL_DMD_EXTENSIONS, nullptr ) ).split( ' ' );
}
@@ -186,6 +186,10 @@ class CORE_EXPORT QgsRasterFileWriter
* 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.
*
* Note that this method works for all GDAL drivers, including those without create support
* (and which are not supported by QgsRasterFileWriter).
*
* \since QGIS 3.0
*/
static QString driverForExtension( const QString &extension );
@@ -196,6 +200,9 @@ class CORE_EXPORT QgsRasterFileWriter
*
* If no matching format driver is found an empty list will be returned.
*
* Note that this method works for all GDAL drivers, including those without create support
* (and which are not supported by QgsRasterFileWriter).
*
* \since QGIS 3.0
*/
static QStringList extensionsForFormat( const QString &format );
@@ -106,13 +106,17 @@ def testDriverForExtension(self):
self.assertEqual(QgsRasterFileWriter.driverForExtension('.tif'), 'GTiff')
self.assertEqual(QgsRasterFileWriter.driverForExtension('img'), 'HFA')
self.assertEqual(QgsRasterFileWriter.driverForExtension('.vrt'), 'VRT')
self.assertEqual(QgsRasterFileWriter.driverForExtension('.jpg'), 'JPEG')
self.assertEqual(QgsRasterFileWriter.driverForExtension('asc'), 'AAIGrid')
self.assertEqual(QgsRasterFileWriter.driverForExtension('not a format'), '')
self.assertEqual(QgsRasterFileWriter.driverForExtension(''), '')

def testExtensionsForFormat(self):
self.assertCountEqual(QgsRasterFileWriter.extensionsForFormat('not format'), [])
self.assertCountEqual(QgsRasterFileWriter.extensionsForFormat('GTiff'), ['tiff', 'tif'])
self.assertCountEqual(QgsRasterFileWriter.extensionsForFormat('GPKG'), ['gpkg'])
self.assertCountEqual(QgsRasterFileWriter.extensionsForFormat('JPEG'), ['jpg', 'jpeg'])
self.assertCountEqual(QgsRasterFileWriter.extensionsForFormat('AAIGrid'), ['asc'])

def testSupportedFiltersAndFormat(self):
# test with formats in recommended order

0 comments on commit eba7e7b

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