Skip to content

Commit 147911b

Browse files
committed
prevent .vrt and .gz files from appearing as both GDAL and OGR items (bug #5636)
1 parent 589460b commit 147911b

File tree

2 files changed

+53
-33
lines changed

2 files changed

+53
-33
lines changed

src/providers/gdal/qgsgdaldataitems.cpp

+23-16
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
163163
return 0;
164164

165165
// Filter files by extension
166-
if ( !extensions.contains( info.suffix().toLower() ) )
166+
if ( !extensions.contains( suffix ) )
167167
{
168168
bool matches = false;
169169
foreach( QString wildcard, wildcards )
@@ -197,20 +197,27 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
197197
}
198198
}
199199

200-
// if setting = 2 (Basic scan), return a /vsizip/ item without testing
201-
if ( is_vsizip && scanZipSetting == 2 )
202-
{
203-
QStringList sublayers;
204-
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
205-
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, thePath, &sublayers );
206-
if ( item )
207-
return item;
208-
}
209-
210-
// if scan items == "Check extension", add item here without trying to open
211-
// unless item is /vsizip
212-
if ( scanItemsSetting == 1 && !is_vsizip )
200+
// return a /vsizip/ item without testing if:
201+
// zipfile and scan zip == "Basic scan"
202+
// not zipfile and scan items == "Check extension"
203+
if (( is_vsizip && scanZipSetting == 2 ) ||
204+
( !is_vsizip && scanItemsSetting == 1 ) )
213205
{
206+
// if this is a VRT file make sure it is raster VRT to avoid duplicates
207+
if ( suffix == "vrt" )
208+
{
209+
// do not print errors, but write to debug
210+
CPLPushErrorHandler( CPLQuietErrorHandler );
211+
CPLErrorReset();
212+
if ( ! GDALIdentifyDriver( thePath.toLocal8Bit().constData(), 0 ) )
213+
{
214+
QgsDebugMsg( "Skipping VRT file because root is not a GDAL VRT" );
215+
CPLPopErrorHandler();
216+
return 0;
217+
}
218+
CPLPopErrorHandler();
219+
}
220+
// add the item
214221
QStringList sublayers;
215222
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
216223
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, thePath, &sublayers );
@@ -221,10 +228,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
221228
// test that file is valid with GDAL
222229
GDALAllRegister();
223230
// do not print errors, but write to debug
224-
CPLErrorHandler oErrorHandler = CPLSetErrorHandler( CPLQuietErrorHandler );
231+
CPLPushErrorHandler( CPLQuietErrorHandler );
225232
CPLErrorReset();
226233
GDALDatasetH hDS = GDALOpen( TO8F( thePath ), GA_ReadOnly );
227-
CPLSetErrorHandler( oErrorHandler );
234+
CPLPopErrorHandler();
228235

229236
if ( ! hDS )
230237
{

src/providers/ogr/qgsogrdataitems.cpp

+30-17
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
268268

269269
// We have to filter by extensions, otherwise e.g. all Shapefile files are displayed
270270
// because OGR drive can open also .dbf, .shx.
271-
if ( myExtensions.indexOf( info.suffix().toLower() ) < 0 )
271+
if ( myExtensions.indexOf( suffix ) < 0 )
272272
{
273273
bool matches = false;
274274
foreach( QString wildcard, wildcards() )
@@ -285,7 +285,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
285285
}
286286

287287
// .dbf should probably appear if .shp is not present
288-
if ( info.suffix().toLower() == "dbf" )
288+
if ( suffix == "dbf" )
289289
{
290290
QString pathShp = thePath.left( thePath.count() - 4 ) + ".shp";
291291
if ( QFileInfo( pathShp ).exists() )
@@ -310,20 +310,33 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
310310
}
311311
}
312312

313-
// if setting = 2 (Basic scan), return a /vsizip/ item without testing
314-
if ( is_vsizip && scanZipSetting == 2 )
315-
{
316-
QStringList sublayers;
317-
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
318-
QgsLayerItem * item = new QgsOgrLayerItem( parentItem, name, thePath, thePath, QgsLayerItem::Vector );
319-
if ( item )
320-
return item;
321-
}
322-
323-
// if scan items == "Check extension", add item here without trying to open
324-
// unless item is /vsizip
325-
if ( scanItemsSetting == 1 && !is_vsizip && !is_vsigzip )
313+
// return a /vsizip/ item without testing if:
314+
// zipfile and scan zip == "Basic scan"
315+
// not zipfile and scan items == "Check extension"
316+
if (( is_vsizip && scanZipSetting == 2 ) ||
317+
( !is_vsizip && scanItemsSetting == 1 ) )
326318
{
319+
// if this is a VRT file make sure it is vector VRT to avoid duplicates
320+
if ( suffix == "vrt" )
321+
{
322+
OGRSFDriverH hDriver = OGRGetDriverByName( "VRT" );
323+
if ( hDriver )
324+
{
325+
// do not print errors, but write to debug
326+
CPLPushErrorHandler( CPLQuietErrorHandler );
327+
CPLErrorReset();
328+
OGRDataSourceH hDataSource = OGR_Dr_Open( hDriver, thePath.toLocal8Bit().constData(), 0 );
329+
CPLPopErrorHandler();
330+
if ( ! hDataSource )
331+
{
332+
QgsDebugMsg( "Skipping VRT file because root is not a OGR VRT" );
333+
return 0;
334+
}
335+
OGR_DS_Destroy( hDataSource );
336+
}
337+
}
338+
// add the item
339+
// TODO: how to handle collections?
327340
QgsLayerItem * item = new QgsOgrLayerItem( parentItem, name, thePath, thePath, QgsLayerItem::Vector );
328341
if ( item )
329342
return item;
@@ -333,10 +346,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
333346
OGRRegisterAll();
334347
OGRSFDriverH hDriver;
335348
// do not print errors, but write to debug
336-
CPLErrorHandler oErrorHandler = CPLSetErrorHandler( CPLQuietErrorHandler );
349+
CPLPushErrorHandler( CPLQuietErrorHandler );
337350
CPLErrorReset();
338351
OGRDataSourceH hDataSource = OGROpen( TO8F( thePath ), false, &hDriver );
339-
CPLSetErrorHandler( oErrorHandler );
352+
CPLPopErrorHandler();
340353

341354
if ( ! hDataSource )
342355
{

0 commit comments

Comments
 (0)