Skip to content

Commit c58576e

Browse files
committed
[FEATURE] implement ability to skip specific drivers (fixes #182)
1 parent f1d7062 commit c58576e

File tree

9 files changed

+267
-16
lines changed

9 files changed

+267
-16
lines changed

python/core/qgsapplication.sip

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,33 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
260260
@note added in 2.0 */
261261
static QString buildOutputPath();
262262

263+
/** Sets the GDAL_SKIP environment variable to include the specified driver
264+
* and then calls GDALDriverManager::AutoSkipDrivers() to unregister it. The
265+
* driver name should be the short format of the Gdal driver name e.g. GTiff.
266+
* @note added in 2.0
267+
*/
268+
static void skipGdalDriver( QString theDriver );
269+
270+
/** Sets the GDAL_SKIP environment variable to exclude the specified driver
271+
* and then calls GDALDriverManager::AutoSkipDrivers() to unregister it. The
272+
* driver name should be the short format of the Gdal driver name e.g. GTiff.
273+
* @note added in 2.0
274+
*/
275+
static void restoreGdalDriver( QString theDriver );
276+
277+
278+
/** Returns the list of gdal drivers that should be skipped (based on
279+
* GDAL_SKIP environment variable)
280+
* @note added in 2.0
281+
*/
282+
static QStringList skippedGdalDrivers( ) const ;
283+
284+
/** Apply the skipped drivers list to gdal
285+
* @see skipGdalDriver
286+
* @see restoreGdalDriver
287+
* @see skippedGdalDrivers
288+
* @note added in 2.0 */
289+
static void applyGdalSkippedDrivers();
290+
263291
};
264292

src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ TARGET_LINK_LIBRARIES(${QGIS_APP_NAME}
422422
#should only be needed for win
423423
${QT_QTMAIN_LIBRARY}
424424
${QWTPOLAR_LIBRARY}
425+
${GDAL_LIBRARY}
425426
qgis_core
426427
qgis_gui
427428
qgis_analysis

src/app/qgisapp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4822,6 +4822,9 @@ void QgisApp::options()
48224822

48234823
//do we need this? TS
48244824
mMapCanvas->refresh();
4825+
4826+
mRasterFileFilter.clear();
4827+
QgsRasterLayer::buildSupportedRasterFileFilter( mRasterFileFilter );
48254828
}
48264829

48274830
delete optionsDialog;

src/app/qgsoptions.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#define ELLIPS_FLAT "NONE"
4242
#define ELLIPS_FLAT_DESC "None / Planimetric"
4343

44+
#define CPL_SUPRESS_CPLUSPLUS
45+
#include <gdal.h>
4446
/**
4547
* \class QgsOptions - Set user options and preferences
4648
* Constructor
@@ -431,6 +433,9 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
431433

432434
restoreGeometry( settings.value( "/Windows/Options/geometry" ).toByteArray() );
433435
tabWidget->setCurrentIndex( settings.value( "/Windows/Options/row" ).toInt() );
436+
437+
loadGdalDriverList();
438+
434439
}
435440

436441
//! Destructor
@@ -744,6 +749,10 @@ void QgsOptions::saveOptions()
744749
//
745750
settings.setValue( "locale/userLocale", cboLocale->currentText() );
746751
settings.setValue( "locale/overrideFlag", grpLocale->isChecked() );
752+
753+
754+
// Gdal skip driver list
755+
saveGdalDriverList();
747756
}
748757

749758

@@ -1010,3 +1019,57 @@ void QgsOptions::on_mClearCache_clicked()
10101019
QgsNetworkAccessManager::instance()->cache()->clear();
10111020
#endif
10121021
}
1022+
1023+
void QgsOptions::loadGdalDriverList()
1024+
{
1025+
QStringList mySkippedDrivers = QgsApplication::skippedGdalDrivers();
1026+
GDALDriverH myGdalDriver; // current driver
1027+
QString myGdalDriverDescription;
1028+
QStringList myDrivers;
1029+
for ( int i = 0; i < GDALGetDriverCount(); ++i )
1030+
{
1031+
myGdalDriver = GDALGetDriver( i );
1032+
1033+
Q_CHECK_PTR( myGdalDriver );
1034+
1035+
if ( !myGdalDriver )
1036+
{
1037+
QgsLogger::warning( "unable to get driver " + QString::number( i ) );
1038+
continue;
1039+
}
1040+
myGdalDriverDescription = GDALGetDescription( myGdalDriver );
1041+
myDrivers << myGdalDriverDescription;
1042+
}
1043+
QStringListIterator myIterator( myDrivers );
1044+
myDrivers.sort();
1045+
while (myIterator.hasNext())
1046+
{
1047+
QString myName = myIterator.next();
1048+
QListWidgetItem * mypItem = new QListWidgetItem( myName );
1049+
if ( mySkippedDrivers.contains( myName ) )
1050+
{
1051+
mypItem->setCheckState( Qt::Unchecked );
1052+
}
1053+
else
1054+
{
1055+
mypItem->setCheckState( Qt::Checked );
1056+
}
1057+
lstGdalDrivers->addItem( mypItem );
1058+
}
1059+
}
1060+
1061+
void QgsOptions::saveGdalDriverList()
1062+
{
1063+
for ( int i=0; i < lstGdalDrivers->count(); i++ )
1064+
{
1065+
QListWidgetItem * mypItem = lstGdalDrivers->item( i );
1066+
if ( mypItem->checkState() == Qt::Unchecked )
1067+
{
1068+
QgsApplication::skipGdalDriver( mypItem->text() );
1069+
}
1070+
else
1071+
{
1072+
QgsApplication::restoreGdalDriver( mypItem->text() );
1073+
}
1074+
}
1075+
}

src/app/qgsoptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ class QgsOptions : public QDialog, private Ui::QgsOptionsBase
120120
void on_mBrowseCacheDirectory_clicked();
121121
void on_mClearCache_clicked();
122122

123+
/* Load the list of drivers available in GDAL
124+
* @note added in 2.0
125+
*/
126+
void loadGdalDriverList();
127+
128+
/* Save the list of which gdal drivers should be used.
129+
* @note added in 2.0
130+
*/
131+
void saveGdalDriverList();
132+
123133
protected:
124134
//! Populates combo box with ellipsoids
125135
void getEllipsoidList();

src/core/qgsapplication.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "qgsconfig.h"
3535

3636
#include <ogr_api.h>
37+
#include <gdal_priv.h>
3738
#include <cpl_conv.h> // for setting gdal options
3839

3940
QObject * QgsApplication::mFileOpenEventReceiver;
@@ -49,6 +50,7 @@ QString QgsApplication::mConfigPath = QDir::homePath() + QString( "/.qgis/" );
4950
bool QgsApplication::mRunningFromBuildDir = false;
5051
QString QgsApplication::mBuildSourcePath;
5152
QString QgsApplication::mBuildOutputPath;
53+
QStringList QgsApplication::mGdalSkipList;
5254

5355
/*!
5456
\class QgsApplication
@@ -711,3 +713,43 @@ QString QgsApplication::relativePathToAbsolutePath( QString rpath, QString targe
711713

712714
return targetElems.join( "/" );
713715
}
716+
717+
void QgsApplication::skipGdalDriver( QString theDriver )
718+
{
719+
if ( mGdalSkipList.contains( theDriver ) || theDriver.isEmpty() )
720+
{
721+
return;
722+
}
723+
mGdalSkipList << theDriver;
724+
applyGdalSkippedDrivers();
725+
}
726+
727+
void QgsApplication::restoreGdalDriver( QString theDriver )
728+
{
729+
if ( !mGdalSkipList.contains( theDriver ) )
730+
{
731+
return;
732+
}
733+
int myPos = mGdalSkipList.indexOf( theDriver );
734+
if ( myPos >= 0 )
735+
{
736+
mGdalSkipList.removeAt( myPos );
737+
}
738+
applyGdalSkippedDrivers();
739+
}
740+
741+
void QgsApplication::applyGdalSkippedDrivers()
742+
{
743+
mGdalSkipList.removeDuplicates();
744+
QString myDriverList = mGdalSkipList.join(" ");
745+
QgsDebugMsg( "Gdal Skipped driver list set to:" );
746+
QgsDebugMsg( myDriverList );
747+
#if defined(Q_WS_WIN32) || defined(WIN32)
748+
CPLSetConfigOption("GDAL_SKIP", myDriverList.toUtf8());
749+
#else
750+
int myChangeFlag = 1; //whether we want to force the env var to change
751+
setenv( "GDAL_SKIP", myDriverList.toUtf8(), myChangeFlag );
752+
#endif
753+
GDALDriverManager myDriverManager;
754+
myDriverManager.AutoLoadDrivers();
755+
}

src/core/qgsapplication.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <QApplication>
1919
#include <QEvent>
20+
#include <QStringList>
2021

2122
#include <qgis.h>
2223

@@ -223,6 +224,33 @@ class CORE_EXPORT QgsApplication: public QApplication
223224
@note added in 2.0 */
224225
static QString buildOutputPath() { return mBuildOutputPath; }
225226

227+
/** Sets the GDAL_SKIP environment variable to include the specified driver
228+
* and then calls GDALDriverManager::AutoSkipDrivers() to unregister it. The
229+
* driver name should be the short format of the Gdal driver name e.g. GTIFF.
230+
* @note added in 2.0
231+
*/
232+
static void skipGdalDriver( QString theDriver );
233+
234+
/** Sets the GDAL_SKIP environment variable to exclude the specified driver
235+
* and then calls GDALDriverManager::AutoSkipDrivers() to unregister it. The
236+
* driver name should be the short format of the Gdal driver name e.g. GTIFF.
237+
* @note added in 2.0
238+
*/
239+
static void restoreGdalDriver( QString theDriver );
240+
241+
/** Returns the list of gdal drivers that should be skipped (based on
242+
* GDAL_SKIP environment variable)
243+
* @note added in 2.0
244+
*/
245+
static QStringList skippedGdalDrivers( ){ return mGdalSkipList; };
246+
247+
/** Apply the skipped drivers list to gdal
248+
* @see skipGdalDriver
249+
* @see restoreGdalDriver
250+
* @see skippedGdalDrivers
251+
* @note added in 2.0 */
252+
static void applyGdalSkippedDrivers();
253+
226254
signals:
227255
void preNotify( QObject * receiver, QEvent * event, bool * done );
228256

@@ -246,6 +274,10 @@ class CORE_EXPORT QgsApplication: public QApplication
246274
static QString mBuildSourcePath;
247275
/** path to the output directory of the build. valid only when running from build directory */
248276
static QString mBuildOutputPath;
277+
/** List of gdal drivers to be skipped. Uses GDAL_SKIP to exclude them.
278+
* @see skipGdalDriver, restoreGdalDriver
279+
* @note added in 2.0 */
280+
static QStringList mGdalSkipList;
249281
};
250282

251283
#endif

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,11 @@ int QgsGdalProvider::colorInterpretation( int theBandNo ) const
11821182
void QgsGdalProvider::registerGdalDrivers()
11831183
{
11841184
if ( GDALGetDriverCount() == 0 )
1185+
{
11851186
GDALAllRegister();
1187+
}
1188+
//call regardless of above
1189+
QgsApplication::applyGdalSkippedDrivers();
11861190
}
11871191

11881192

0 commit comments

Comments
 (0)