Skip to content

Commit

Permalink
case insensitive vsizip/vsigzip support
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Apr 23, 2012
1 parent c56491b commit 04abf76
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
24 changes: 12 additions & 12 deletions src/providers/gdal/qgsgdaldataitems.cpp
Expand Up @@ -116,9 +116,9 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
int scanZipSetting = settings.value( "/qgis/scanZipInBrowser", 1 ).toInt();

// allow normal files or VSIFILE items to pass
if ( ! info.isFile() &&
thePath.left( 8 ) != "/vsizip/" &&
thePath.left( 9 ) != "/vsigzip/" )
if ( !info.isFile() &&
!thePath.startsWith( "/vsizip/" ) &&
!thePath.startsWith( "/vsigzip/" ) )
return 0;

// get supported extensions
Expand All @@ -131,16 +131,16 @@ 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.right( 8 ).toLower() == ".aux.xml" &&
extensions.indexOf( "aux.xml" ) < 0 )
if ( thePath.endsWith( ".aux.xml", Qt::CaseInsensitive ) &&
! extensions.contains( "aux.xml" ) )
return 0;

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

// Filter files by extension
if ( extensions.indexOf( info.suffix().toLower() ) < 0 )
if ( !extensions.contains( info.suffix().toLower() ) )
{
bool matches = false;
foreach( QString wildcard, wildcards )
Expand All @@ -157,7 +157,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
}

// vsifile : depending on options we should just add the item without testing
if ( thePath.left( 8 ) == "/vsizip/" )
if ( thePath.startsWith( "/vsizip/" ) )
{
// if this is a /vsigzip/path.zip/file_inside_zip change the name
if ( thePath != "/vsizip/" + parentItem->path() )
Expand Down Expand Up @@ -189,14 +189,14 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )


// try to open using VSIFileHandler
if ( thePath.right( 4 ) == ".zip" )
if ( thePath.endsWith( ".zip", Qt::CaseInsensitive ) )
{
if ( thePath.left( 8 ) != "/vsizip/" )
if ( !thePath.startsWith( "/vsizip/" ) )
thePath = "/vsizip/" + thePath;
}
else if ( thePath.right( 3 ) == ".gz" )
else if ( thePath.endsWith( ".gz", Qt::CaseInsensitive ) )
{
if ( thePath.left( 9 ) != "/vsigzip/" )
if ( !thePath.startsWith( "/vsigzip/" ) )
thePath = "/vsigzip/" + thePath;
}

Expand Down
22 changes: 11 additions & 11 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -109,15 +109,15 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
// Try to open using VSIFileHandler (see qgsogrprovider.cpp)
// TODO suppress error messages and report in debug, like in OGR provider
// TODO use the file name of the file inside the zip, needs unzip.h
if ( uri.right( 4 ) == ".zip" )
if ( uri.endsWith( ".zip", Qt::CaseInsensitive ) )
{
if ( uri.left( 8 ) != "/vsizip/" )
if ( !uri.startsWith( "/vsizip/" ) )
setDataSourceUri( "/vsizip/" + uri );
QgsDebugMsg( QString( "Trying /vsizip syntax, uri= %1" ).arg( dataSourceUri() ) );
}
else if ( uri.right( 3 ) == ".gz" )
else if ( uri.endsWith( ".gz", Qt::CaseInsensitive ) )
{
if ( uri.left( 9 ) != "/vsigzip/" )
if ( !uri.startsWith( "/vsigzip/" ) )
setDataSourceUri( "/vsigzip/" + uri );
QgsDebugMsg( QString( "Trying /vsigzip syntax, uri= %1" ).arg( dataSourceUri() ) );
}
Expand All @@ -126,7 +126,7 @@ QgsGdalProvider::QgsGdalProvider( QString const & uri )
mGdalBaseDataset = GDALOpen( TO8F( dataSourceUri() ), GA_ReadOnly );

CPLErrorReset();
if ( mGdalBaseDataset == NULL )
if ( !mGdalBaseDataset )
{
QgsDebugMsg( QString( "Cannot open GDAL dataset %1: %2" ).arg( dataSourceUri() ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
return;
Expand Down Expand Up @@ -575,7 +575,7 @@ void QgsGdalProvider::readBlock( int theBandNo, QgsRectangle const & theExtent,
double xRes = theExtent.width() / thePixelWidth;
double yRes = theExtent.height() / thePixelHeight;

// Find top, bottom rows and left, right column the raster extent covers
// Find top, bottom rows and left, right column the raster extent covers
// These are limits in target grid space
int top = 0;
int bottom = thePixelHeight - 1;
Expand Down Expand Up @@ -1869,23 +1869,23 @@ QGISEXTERN bool isValidRasterFileName( QString const & theFileNameQString, QStri

// Try to open using VSIFileHandler (see qgsogrprovider.cpp)
// TODO suppress error messages and report in debug, like in OGR provider
if ( fileName.right( 4 ) == ".zip" )
if ( fileName.endsWith( ".zip", Qt::CaseInsensitive ) )
{
if ( fileName.left( 8 ) != "/vsizip/" )
if ( !fileName.startsWith( "/vsizip/" ) )
fileName = "/vsizip/" + fileName;
QgsDebugMsg( QString( "Trying /vsizip syntax, fileName= %1" ).arg( fileName ) );
}
if ( fileName.right( 3 ) == ".gz" )
if ( fileName.endsWith( ".gz", Qt::CaseInsensitive ) )
{
if ( fileName.left( 9 ) != "/vsigzip/" )
if ( !fileName.startsWith( "/vsigzip/" ) )
fileName = "/vsigzip/" + fileName;
QgsDebugMsg( QString( "Trying /vsigzip syntax, fileName= %1" ).arg( fileName ) );
}

//open the file using gdal making sure we have handled locale properly
//myDataset = GDALOpen( QFile::encodeName( theFileNameQString ).constData(), GA_ReadOnly );
myDataset = GDALOpen( TO8F( fileName ), GA_ReadOnly );
if ( myDataset == NULL )
if ( !myDataset )
{
if ( CPLGetLastErrorNo() != CPLE_OpenFailed )
retErrMsg = QString::fromUtf8( CPLGetLastErrorMsg() );
Expand Down
19 changes: 9 additions & 10 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -235,9 +235,9 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
int scanZipSetting = settings.value( "/qgis/scanZipInBrowser", 1 ).toInt();

// allow normal files or VSIFILE items to pass
if ( ! info.isFile() &&
thePath.left( 8 ) != "/vsizip/" &&
thePath.left( 9 ) != "/vsigzip/" )
if ( !info.isFile() &&
!thePath.startsWith( "/vsizip/" ) &&
!thePath.startsWith( "/vsigzip/" ) )
return 0;

QStringList myExtensions = fileExtensions();
Expand Down Expand Up @@ -282,11 +282,10 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
}

// vsifile : depending on options we should just add the item without testing
if ( thePath.left( 8 ) == "/vsizip/" )
if ( thePath.startsWith( "/vsizip/" ) )
{
// if this is a /vsigzip/path.zip/file_inside_zip change the name
if ( thePath.left( 8 ) == "/vsizip/" &&
thePath != "/vsizip/" + parentItem->path() )
if ( thePath != "/vsizip/" + parentItem->path() )
{
name = thePath;
name = name.replace( "/vsizip/" + parentItem->path() + "/", "" );
Expand All @@ -310,14 +309,14 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
}

// try to open using VSIFileHandler
if ( thePath.right( 4 ) == ".zip" )
if ( thePath.endsWith( ".zip", Qt::CaseInsensitive ) )
{
if ( thePath.left( 8 ) != "/vsizip/" )
if ( !thePath.startsWith( "/vsizip/" ) )
thePath = "/vsizip/" + thePath;
}
else if ( thePath.right( 3 ) == ".gz" )
else if ( thePath.endsWith( ".gz", Qt::CaseInsensitive ) )
{
if ( thePath.left( 9 ) != "/vsigzip/" )
if ( !thePath.startsWith( "/vsigzip/" ) )
thePath = "/vsigzip/" + thePath;
}

Expand Down
12 changes: 5 additions & 7 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -264,18 +264,18 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )

// Try to open using VSIFileHandler
// see http://trac.osgeo.org/gdal/wiki/UserDocs/ReadInZip
if ( mFilePath.right( 4 ) == ".zip" )
if ( mFilePath.endsWith( ".zip", Qt::CaseInsensitive ) )
{
// GDAL>=1.8.0 has write support for zip, but read and write operations
// cannot be interleaved, so for now just use read-only.
openReadOnly = true;
if ( mFilePath.left( 8 ) != "/vsizip/" )
if ( !mFilePath.startsWith( "/vsizip/" ) )
mFilePath = "/vsizip/" + mFilePath;
QgsDebugMsg( QString( "Trying /vsizip syntax, mFilePath= %1" ).arg( mFilePath ) );
}
else if ( mFilePath.right( 3 ) == ".gz" )
else if ( mFilePath.endsWith( ".gz", Qt::CaseInsensitive ) )
{
if ( mFilePath.left( 9 ) != "/vsigzip/" )
if ( !mFilePath.startsWith( "/vsigzip/" ) )
mFilePath = "/vsigzip/" + mFilePath;
QgsDebugMsg( QString( "Trying /vsigzip syntax, mFilePath= %1" ).arg( mFilePath ) );
}
Expand All @@ -287,12 +287,11 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
CPLSetConfigOption( "OGR_ORGANIZE_POLYGONS", "ONLY_CCW" ); // "SKIP" returns MULTIPOLYGONs for multiringed POLYGONs

// first try to open in update mode (unless specified otherwise)
if ( ! openReadOnly )
if ( !openReadOnly )
ogrDataSource = OGROpen( TO8F( mFilePath ), true, &ogrDriver );

if ( !ogrDataSource )
{

QgsDebugMsg( "OGR failed to opened in update mode, trying in read-only mode" );

// try to open read-only
Expand All @@ -305,7 +304,6 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )

if ( ogrDataSource )
{

QgsDebugMsg( "OGR opened using Driver " + QString( OGR_Dr_GetName( ogrDriver ) ) );

ogrDriverName = OGR_Dr_GetName( ogrDriver );
Expand Down

4 comments on commit 04abf76

@etiennesky
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goof thinking ! have you looked at my improvements and tests in pull request 130 ?

@timlinux
Copy link
Member

@timlinux timlinux commented on 04abf76 Apr 24, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etiennesky
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think github notice emails are not always working...

If you want I can patch that pull request with these changes by Jef

@etiennesky
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't apply cleanly after Jurgen's mods - will create a new pull request

Please sign in to comment.