Skip to content
Permalink
Browse files

prevent .vrt and .gz files from appearing as both GDAL and OGR items …

…(bug #5636)
  • Loading branch information
etiennesky committed May 28, 2012
1 parent 589460b commit 147911b2c4ee8bbae6ec58f9d44a11bffca6e47b
Showing with 53 additions and 33 deletions.
  1. +23 −16 src/providers/gdal/qgsgdaldataitems.cpp
  2. +30 −17 src/providers/ogr/qgsogrdataitems.cpp
@@ -163,7 +163,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
return 0;

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

// if setting = 2 (Basic scan), return a /vsizip/ item without testing
if ( is_vsizip && scanZipSetting == 2 )
{
QStringList sublayers;
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, thePath, &sublayers );
if ( item )
return item;
}

// if scan items == "Check extension", add item here without trying to open
// unless item is /vsizip
if ( scanItemsSetting == 1 && !is_vsizip )
// return a /vsizip/ item without testing if:
// zipfile and scan zip == "Basic scan"
// not zipfile and scan items == "Check extension"
if (( is_vsizip && scanZipSetting == 2 ) ||
( !is_vsizip && scanItemsSetting == 1 ) )
{
// if this is a VRT file make sure it is raster VRT to avoid duplicates
if ( suffix == "vrt" )
{
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
if ( ! GDALIdentifyDriver( thePath.toLocal8Bit().constData(), 0 ) )
{
QgsDebugMsg( "Skipping VRT file because root is not a GDAL VRT" );
CPLPopErrorHandler();
return 0;
}
CPLPopErrorHandler();
}
// add the item
QStringList sublayers;
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
QgsLayerItem * item = new QgsGdalLayerItem( parentItem, name, thePath, thePath, &sublayers );
@@ -221,10 +228,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
// test that file is valid with GDAL
GDALAllRegister();
// do not print errors, but write to debug
CPLErrorHandler oErrorHandler = CPLSetErrorHandler( CPLQuietErrorHandler );
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
GDALDatasetH hDS = GDALOpen( TO8F( thePath ), GA_ReadOnly );
CPLSetErrorHandler( oErrorHandler );
CPLPopErrorHandler();

if ( ! hDS )
{
@@ -268,7 +268,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )

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

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

// if setting = 2 (Basic scan), return a /vsizip/ item without testing
if ( is_vsizip && scanZipSetting == 2 )
{
QStringList sublayers;
QgsDebugMsg( QString( "adding item name=%1 thePath=%2" ).arg( name ).arg( thePath ) );
QgsLayerItem * item = new QgsOgrLayerItem( parentItem, name, thePath, thePath, QgsLayerItem::Vector );
if ( item )
return item;
}

// if scan items == "Check extension", add item here without trying to open
// unless item is /vsizip
if ( scanItemsSetting == 1 && !is_vsizip && !is_vsigzip )
// return a /vsizip/ item without testing if:
// zipfile and scan zip == "Basic scan"
// not zipfile and scan items == "Check extension"
if (( is_vsizip && scanZipSetting == 2 ) ||
( !is_vsizip && scanItemsSetting == 1 ) )
{
// if this is a VRT file make sure it is vector VRT to avoid duplicates
if ( suffix == "vrt" )
{
OGRSFDriverH hDriver = OGRGetDriverByName( "VRT" );
if ( hDriver )
{
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
OGRDataSourceH hDataSource = OGR_Dr_Open( hDriver, thePath.toLocal8Bit().constData(), 0 );
CPLPopErrorHandler();
if ( ! hDataSource )
{
QgsDebugMsg( "Skipping VRT file because root is not a OGR VRT" );
return 0;
}
OGR_DS_Destroy( hDataSource );
}
}
// add the item
// TODO: how to handle collections?
QgsLayerItem * item = new QgsOgrLayerItem( parentItem, name, thePath, thePath, QgsLayerItem::Vector );
if ( item )
return item;
@@ -333,10 +346,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
OGRRegisterAll();
OGRSFDriverH hDriver;
// do not print errors, but write to debug
CPLErrorHandler oErrorHandler = CPLSetErrorHandler( CPLQuietErrorHandler );
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
OGRDataSourceH hDataSource = OGROpen( TO8F( thePath ), false, &hDriver );
CPLSetErrorHandler( oErrorHandler );
CPLPopErrorHandler();

if ( ! hDataSource )
{

0 comments on commit 147911b

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