78 changes: 41 additions & 37 deletions src/providers/gdal/qgsgdaldataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,29 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
if ( thePath.isEmpty() )
return 0;

QgsDebugMsg( "thePath= " + thePath );

QString uri = thePath;
QFileInfo info( thePath );
// zip settings + info
QSettings settings;
//extract basename with extension
QString name = info.fileName();
int scanItemsSetting = settings.value( "/qgis/scanItemsInBrowser", 0 ).toInt();
int scanZipSetting = settings.value( "/qgis/scanZipInBrowser", 1 ).toInt();
bool is_vsizip = ( thePath.startsWith( "/vsizip/" ) ||
thePath.endsWith( ".zip", Qt::CaseInsensitive ) );
bool is_vsigzip = ( thePath.startsWith( "/vsigzip/" ) ||
thePath.endsWith( ".gz", Qt::CaseInsensitive ) );

// get suffix, removing .gz if present
QString tmpPath = thePath; //path used for testing, not for layer creation
if ( is_vsigzip )
tmpPath.chop( 3 );
QFileInfo info( tmpPath );
QString suffix = info.suffix().toLower();
// extract basename with extension
info.setFile( thePath );
QString name = info.fileName();

QgsDebugMsg( "thePath= " + thePath + " tmpPath= " + tmpPath );

// allow normal files or VSIFILE items to pass
if ( !info.isFile() &&
!thePath.startsWith( "/vsizip/" ) &&
!thePath.startsWith( "/vsigzip/" ) )
// allow only normal files or VSIFILE items to continue
if ( !info.isFile() && !is_vsizip && !is_vsigzip )
return 0;

// get supported extensions
Expand All @@ -132,7 +141,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
// skip *.aux.xml files (GDAL auxilary metadata files)
// unless that extension is in the list (*.xml might be though)
if ( thePath.endsWith( ".aux.xml", Qt::CaseInsensitive ) &&
! extensions.contains( "aux.xml" ) )
!extensions.contains( "aux.xml" ) )
return 0;

// skip .tar.gz files
Expand All @@ -156,48 +165,43 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
return 0;
}

// vsifile : depending on options we should just add the item without testing
if ( thePath.startsWith( "/vsizip/" ) )
// add /vsizip/ or /vsigzip/ to path if file extension is .zip or .gz
if ( is_vsigzip )
{
// if this is a /vsigzip/path.zip/file_inside_zip change the name
if ( !thePath.startsWith( "/vsigzip/" ) )
thePath = "/vsigzip/" + thePath;
}
else if ( is_vsizip )
{
if ( !thePath.startsWith( "/vsizip/" ) )
thePath = "/vsizip/" + thePath;
// if this is a /vsigzip/path_to_zip.zip/file_inside_zip remove the full path from the name
if ( thePath != "/vsizip/" + parentItem->path() )
{
name = thePath;
name = name.replace( "/vsizip/" + parentItem->path() + "/", "" );
}

// if setting = 2 (Basic scan), return an item without testing
if ( scanZipSetting == 2 )
{
QStringList sublayers;
QgsDebugMsg( QString( "adding item name=%1 thePath=%2 uri=%3" ).arg( name ).arg( thePath ).arg( uri ) );
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
if ( scanItemsSetting == 1 )
// 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 uri=%3" ).arg( name ).arg( thePath ).arg( uri ) );
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;
}


// try to open using VSIFileHandler
if ( thePath.endsWith( ".zip", Qt::CaseInsensitive ) )
{
if ( !thePath.startsWith( "/vsizip/" ) )
thePath = "/vsizip/" + thePath;
}
else if ( thePath.endsWith( ".gz", Qt::CaseInsensitive ) )
// if scan items == "Check extension", add item here without trying to open
// unless item is /vsizip
if ( scanItemsSetting == 1 && !is_vsizip )
{
if ( !thePath.startsWith( "/vsigzip/" ) )
thePath = "/vsigzip/" + thePath;
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;
}

// test that file is valid with GDAL
Expand Down
4 changes: 4 additions & 0 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1872,13 +1872,17 @@ QGISEXTERN bool isValidRasterFileName( QString const & theFileNameQString, QStri
if ( fileName.endsWith( ".zip", Qt::CaseInsensitive ) )
{
if ( !fileName.startsWith( "/vsizip/" ) )
{
fileName = "/vsizip/" + fileName;
}
QgsDebugMsg( QString( "Trying /vsizip syntax, fileName= %1" ).arg( fileName ) );
}
if ( fileName.endsWith( ".gz", Qt::CaseInsensitive ) )
{
if ( !fileName.startsWith( "/vsigzip/" ) )
{
fileName = "/vsigzip/" + fileName;
}
QgsDebugMsg( QString( "Trying /vsigzip syntax, fileName= %1" ).arg( fileName ) );
}

Expand Down
75 changes: 42 additions & 33 deletions src/providers/ogr/qgsogrdataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,31 +228,42 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )

QgsDebugMsg( "thePath: " + thePath );

QFileInfo info( thePath );
QString name = info.fileName();
// zip settings + info
QSettings settings;
int scanItemsSetting = settings.value( "/qgis/scanItemsInBrowser", 0 ).toInt();
int scanZipSetting = settings.value( "/qgis/scanZipInBrowser", 1 ).toInt();
bool is_vsizip = ( thePath.startsWith( "/vsizip/" ) ||
thePath.endsWith( ".zip", Qt::CaseInsensitive ) );
bool is_vsigzip = ( thePath.startsWith( "/vsigzip/" ) ||
thePath.endsWith( ".gz", Qt::CaseInsensitive ) );

// get suffix, removing .gz if present
QString tmpPath = thePath; //path used for testing, not for layer creation
if ( is_vsigzip )
tmpPath.chop( 3 );
QFileInfo info( tmpPath );
QString suffix = info.suffix().toLower();
// extract basename with extension
info.setFile( thePath );
QString name = info.fileName();

// allow normal files or VSIFILE items to pass
if ( !info.isFile() &&
!thePath.startsWith( "/vsizip/" ) &&
!thePath.startsWith( "/vsigzip/" ) )
// allow only normal files or VSIFILE items to continue
if ( !info.isFile() && !is_vsizip && !is_vsigzip )
return 0;

QStringList myExtensions = fileExtensions();

// skip *.aux.xml files (GDAL auxilary metadata files) and .shp.xml files (ESRI metadata)
// unless that extension is in the list (*.xml might be though)
if ( thePath.right( 8 ).toLower() == ".aux.xml" &&
myExtensions.indexOf( "aux.xml" ) < 0 )
if ( thePath.endsWith( ".aux.xml", Qt::CaseInsensitive ) &&
!myExtensions.contains( "aux.xml" ) )
return 0;
if ( thePath.right( 8 ).toLower() == ".shp.xml" &&
myExtensions.indexOf( "shp.xml" ) < 0 )
if ( thePath.endsWith( ".shp.xml", Qt::CaseInsensitive ) &&
!myExtensions.contains( "shp.xml" ) )
return 0;

// skip .tar.gz files
if ( thePath.right( 7 ) == ".tar.gz" )
if ( thePath.endsWith( ".tar.gz", Qt::CaseInsensitive ) )
return 0;

// We have to filter by extensions, otherwise e.g. all Shapefile files are displayed
Expand Down Expand Up @@ -281,43 +292,41 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
return 0;
}

// vsifile : depending on options we should just add the item without testing
if ( thePath.startsWith( "/vsizip/" ) )
// add /vsizip/ or /vsigzip/ to path if file extension is .zip or .gz
if ( is_vsigzip )
{
if ( !thePath.startsWith( "/vsigzip/" ) )
thePath = "/vsigzip/" + thePath;
}
else if ( is_vsizip )
{
// if this is a /vsigzip/path.zip/file_inside_zip change the name
if ( !thePath.startsWith( "/vsizip/" ) )
thePath = "/vsizip/" + thePath;
// if this is a /vsigzip/path_to_zip.zip/file_inside_zip remove the full path from the name
if ( thePath != "/vsizip/" + parentItem->path() )
{
name = thePath;
name = name.replace( "/vsizip/" + parentItem->path() + "/", "" );
}

// if setting== 2 (Basic scan), return an item without testing
if ( scanZipSetting == 2 )
{
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
if ( scanItemsSetting == 1 )
// 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;
}

// try to open using VSIFileHandler
if ( thePath.endsWith( ".zip", Qt::CaseInsensitive ) )
{
if ( !thePath.startsWith( "/vsizip/" ) )
thePath = "/vsizip/" + thePath;
}
else if ( thePath.endsWith( ".gz", Qt::CaseInsensitive ) )
// if scan items == "Check extension", add item here without trying to open
// unless item is /vsizip
if ( scanItemsSetting == 1 && !is_vsizip && !is_vsigzip )
{
if ( !thePath.startsWith( "/vsigzip/" ) )
thePath = "/vsigzip/" + thePath;
QgsLayerItem * item = new QgsOgrLayerItem( parentItem, name, thePath, thePath, QgsLayerItem::Vector );
if ( item )
return item;
}

// test that file is valid with OGR
Expand Down
6 changes: 6 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,19 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
// cannot be interleaved, so for now just use read-only.
openReadOnly = true;
if ( !mFilePath.startsWith( "/vsizip/" ) )
{
mFilePath = "/vsizip/" + mFilePath;
setDataSourceUri( mFilePath );
}
QgsDebugMsg( QString( "Trying /vsizip syntax, mFilePath= %1" ).arg( mFilePath ) );
}
else if ( mFilePath.endsWith( ".gz", Qt::CaseInsensitive ) )
{
if ( !mFilePath.startsWith( "/vsigzip/" ) )
{
mFilePath = "/vsigzip/" + mFilePath;
setDataSourceUri( mFilePath );
}
QgsDebugMsg( QString( "Trying /vsigzip syntax, mFilePath= %1" ).arg( mFilePath ) );
}

Expand Down