Skip to content
Permalink
Browse files

Merge pull request #7984 from rouault/fix_19483

Raster: do not list RasterLite2 as a supported output format, and use a helper for every call site (fixes #19483)
  • Loading branch information
rouault committed Sep 23, 2018
2 parents ac65ee0 + d2a7668 commit 208571a8327094b3b4d6249c667d80dd961b715f
@@ -15,6 +15,7 @@
* *
***************************************************************************/

#include "qgsgdalutils.h"
#include "qgsninecellfilter.h"
#include "qgslogger.h"
#include "cpl_string.h"
@@ -100,8 +101,6 @@ gdal::dataset_unique_ptr QgsNineCellFilter::openInputFile( int &nCellsX, int &nC

GDALDriverH QgsNineCellFilter::openOutputDriver()
{
char **driverMetadata = nullptr;

//open driver
GDALDriverH outputDriver = GDALGetDriverByName( mOutputFormat.toLocal8Bit().data() );

@@ -110,15 +109,15 @@ GDALDriverH QgsNineCellFilter::openOutputDriver()
return outputDriver; //return nullptr, driver does not exist
}

driverMetadata = GDALGetMetadata( outputDriver, nullptr );
if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) )
if ( !QgsGdalUtils::supportsRasterCreate( outputDriver ) )
{
return nullptr; //driver exist, but it does not support the create operation
}

return outputDriver;
}


gdal::dataset_unique_ptr QgsNineCellFilter::openOutputFile( GDALDatasetH inputDataset, GDALDriverH outputDriver )
{
if ( !inputDataset )
@@ -15,6 +15,7 @@
* *
***************************************************************************/

#include "qgsgdalutils.h"
#include "qgsrastercalculator.h"
#include "qgsrastercalcnode.h"
#include "qgsrasterdataprovider.h"
@@ -185,8 +186,6 @@ int QgsRasterCalculator::processCalculation( QgsFeedback *feedback )

GDALDriverH QgsRasterCalculator::openOutputDriver()
{
char **driverMetadata = nullptr;

//open driver
GDALDriverH outputDriver = GDALGetDriverByName( mOutputFormat.toLocal8Bit().data() );

@@ -195,8 +194,7 @@ GDALDriverH QgsRasterCalculator::openOutputDriver()
return outputDriver; //return nullptr, driver does not exist
}

driverMetadata = GDALGetMetadata( outputDriver, nullptr );
if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) )
if ( !QgsGdalUtils::supportsRasterCreate( outputDriver ) )
{
return nullptr; //driver exist, but it does not support the create operation
}
@@ -15,6 +15,7 @@
* *
***************************************************************************/

#include "qgsgdalutils.h"
#include "qgslogger.h"
#include "qgsrelief.h"
#include "qgsaspectfilter.h"
@@ -402,8 +403,6 @@ gdal::dataset_unique_ptr QgsRelief::openInputFile( int &nCellsX, int &nCellsY )

GDALDriverH QgsRelief::openOutputDriver()
{
char **driverMetadata = nullptr;

//open driver
GDALDriverH outputDriver = GDALGetDriverByName( mOutputFormat.toLocal8Bit().data() );

@@ -412,8 +411,7 @@ GDALDriverH QgsRelief::openOutputDriver()
return outputDriver; //return nullptr, driver does not exist
}

driverMetadata = GDALGetMetadata( outputDriver, nullptr );
if ( !CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) )
if ( !QgsGdalUtils::supportsRasterCreate( outputDriver ) )
{
return nullptr; //driver exist, but it does not support the create operation
}
@@ -21,6 +21,7 @@
#include "qgis.h"
#include "qgisapp.h"
#include "qgisappstylesheet.h"
#include "qgsgdalutils.h"
#include "qgshighlight.h"
#include "qgsmapcanvas.h"
#include "qgsprojectionselectiondialog.h"
@@ -2161,7 +2162,7 @@ void QgsOptions::loadGdalDriverList()

// get driver R/W flags, taken from GDALGeneralCmdLineProcessor()
const char *pszRWFlag, *pszVirtualIO;
if ( GDALGetMetadataItem( myGdalDriver, GDAL_DCAP_CREATE, nullptr ) )
if ( QgsGdalUtils::supportsRasterCreate( myGdalDriver ) )
{
myGdalWriteDrivers << myGdalDriverDescription;
pszRWFlag = "rw+";
@@ -15,6 +15,7 @@
* *
***************************************************************************/

#include "qgsgdalutils.h"
#include "qgsrastercalcdialog.h"
#include "qgsproject.h"
#include "qgsrastercalcnode.h"
@@ -202,8 +203,7 @@ void QgsRasterCalcDialog::insertAvailableOutputFormats()
GDALDriverH driver = GDALGetDriver( i );
if ( driver )
{
char **driverMetadata = GDALGetMetadata( driver, nullptr );
if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) )
if ( QgsGdalUtils::supportsRasterCreate( driver ) )
{
QString driverShortName = GDALGetDriverShortName( driver );
QString driverLongName = GDALGetDriverLongName( driver );
@@ -202,6 +202,7 @@ SET(QGIS_CORE_SRCS
qgsfiledownloader.cpp
qgsfileutils.cpp
qgsfontutils.cpp
qgsgdalutils.cpp
qgsgeometrysimplifier.cpp
qgsgeometryvalidator.cpp
qgsgeometryoptions.cpp
@@ -0,0 +1,35 @@
/***************************************************************************
qgsgdalutils.cpp
----------------
begin : September 2018
copyright : (C) 2018 Even Rouault
email : even.rouault at spatialys.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsgdalutils.h"

#define CPL_SUPRESS_CPLUSPLUS //#spellok
#include "gdal.h"
#include "cpl_string.h"

#include <QString>

bool QgsGdalUtils::supportsRasterCreate( GDALDriverH driver )
{
QString driverShortName = GDALGetDriverShortName( driver );
if ( driverShortName == QLatin1String( "SQLite" ) )
{
// it supports Create() but only for vector side
return false;
}
char **driverMetadata = GDALGetMetadata( driver, nullptr );
return CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) &&
CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false );
}
@@ -0,0 +1,44 @@
/***************************************************************************
qgsgdalutils.h
--------------
begin : September 2018
copyright : (C) 2018 Even Rouault
email : even.rouault at spatialys.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSGDALUTILS_H
#define QGSGDALUTILS_H

#define SIP_NO_FILE

#include "qgis_core.h"
#include <gdal.h>

/**
* \ingroup core
* \class QgsGdalUtils
* \brief Utilities for working with GDAL
*
* \note not available in Python bindings
* \since QGIS 3.4
*/
class CORE_EXPORT QgsGdalUtils
{
public:

/**
* Reads whether a driver supports GDALCreate() for raster purposes.
* \param driver GDAL driver
* \returns true if a driver supports GDALCreate() for raster purposes.
*/
static bool supportsRasterCreate( GDALDriverH driver );
};

#endif // QGSGDALUTILS_H
@@ -14,6 +14,7 @@
***************************************************************************/
#include <typeinfo>

#include "qgsgdalutils.h"
#include "qgsrasterfilewriter.h"
#include "qgscoordinatetransform.h"
#include "qgsproviderregistry.h"
@@ -1031,10 +1032,9 @@ QList< QgsRasterFileWriter::FilterFormatDetails > QgsRasterFileWriter::supported
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 ) )
if ( QgsGdalUtils::supportsRasterCreate( drv ) )
{
QString drvName = GDALGetDriverShortName( drv );
QString filterString = filterForDriver( drvName );
if ( filterString.isEmpty() )
continue;
@@ -13,6 +13,7 @@
* *
***************************************************************************/
#include "qgsapplication.h"
#include "qgsgdalutils.h"
#include "qgslogger.h"
#include "qgscoordinatetransform.h"
#include "qgsrasterlayer.h"
@@ -241,9 +242,7 @@ void QgsRasterLayerSaveAsDialog::insertAvailableOutputFormats()
GDALDriverH driver = GDALGetDriver( i );
if ( driver )
{
char **driverMetadata = GDALGetMetadata( driver, nullptr );

if ( CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE, false ) && CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER, false ) )
if ( QgsGdalUtils::supportsRasterCreate( driver ) )
{
QString driverShortName = GDALGetDriverShortName( driver );
QString driverLongName = GDALGetDriverLongName( driver );
@@ -99,6 +99,7 @@ SET(TESTS
testqgsfields.cpp
testqgsfield.cpp
testqgsfilledmarker.cpp
testqgsgdalutils.cpp
testqgsvectorfilewriter.cpp
testqgsfontmarker.cpp
testqgsgeometryimport.cpp
@@ -0,0 +1,78 @@
/***************************************************************************
testqgsgdalutils.cpp
--------------------
begin : September 2018
copyright : (C) 2018 Even Rouault
email : even.rouault at spatialys.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstest.h"
#include <QObject>
#include <QString>
#include <QStringList>
#include <QSettings>

#include <gdal.h>

#include "qgsgdalutils.h"
#include "qgsapplication.h"

class TestQgsGdalUtils: public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void supportsRasterCreate();

private:
};

void TestQgsGdalUtils::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
GDALAllRegister();
}

void TestQgsGdalUtils::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsGdalUtils::init()
{

}

void TestQgsGdalUtils::cleanup()
{

}

void TestQgsGdalUtils::supportsRasterCreate()
{
QVERIFY( QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "GTiff" ) ) );
QVERIFY( QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "GPKG" ) ) );

// special case
QVERIFY( !QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "SQLite" ) ) );

// create-only
QVERIFY( !QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "DTED" ) ) );

// vector-only
QVERIFY( !QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "ESRI Shapefile" ) ) );
}

QGSTEST_MAIN( TestQgsGdalUtils )
#include "testqgsgdalutils.moc"

0 comments on commit 208571a

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