Skip to content

Commit

Permalink
Implement sidecarFilesForUri for gdal provider
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 8, 2021
1 parent 52dff0f commit 28b18bd
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/core/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -3702,6 +3702,89 @@ QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const
}
}

QStringList QgsGdalProviderMetadata::sidecarFilesForUri( const QString &uri ) const
{
const QVariantMap uriParts = decodeUri( uri );
const QString path = uriParts.value( QStringLiteral( "path" ) ).toString();

if ( path.isEmpty() )
return {};

// all gdal uris may have a .aux.xml sidecar
const QFileInfo fileInfo( path );
const QString suffix = fileInfo.suffix();

static QMap< QString, QStringList > sExtensions
{
{
QStringLiteral( "jpg" ), {
QStringLiteral( "jpw" ),
QStringLiteral( "jgw" ),
QStringLiteral( "jpgw" ),
QStringLiteral( "jpegw" ),
}
},
{
QStringLiteral( "img" ), {
QStringLiteral( "ige" ),
}
},
{
QStringLiteral( "sid" ), {
QStringLiteral( "j2w" ),
}
},
{
QStringLiteral( "tif" ), {
QStringLiteral( "tifw" ),
QStringLiteral( "tfw" ),
}
},
{
QStringLiteral( "bil" ), {
QStringLiteral( "bilw" ),
QStringLiteral( "blw" ),
}
},
{
QStringLiteral( "raster" ), {
QStringLiteral( "rasterw" ),
}
},
{
QStringLiteral( "bt" ), {
QStringLiteral( "btw" ),
}
}
};


QStringList res;
// format specific sidecars
for ( auto it = sExtensions.constBegin(); it != sExtensions.constEnd(); ++it )
{
if ( suffix.compare( it.key(), Qt::CaseInsensitive ) == 0 )
{
for ( const QString &ext : it.value() )
{
res.append( fileInfo.dir().filePath( fileInfo.completeBaseName() + '.' + ext ) );
}
}
}

// sidecars which could be present for any file
for ( const QString &ext :
{
QStringLiteral( "aux.xml" ),
QStringLiteral( "ovr" ),
QStringLiteral( "wld" )
} )
{
res.append( fileInfo.dir().filePath( fileInfo.completeBaseName() + '.' + ext ) );
}
return res;
}

QgsGdalProviderMetadata::QgsGdalProviderMetadata():
QgsProviderMetadata( PROVIDER_KEY, PROVIDER_DESCRIPTION )
{
Expand Down
1 change: 1 addition & 0 deletions src/core/providers/gdal/qgsgdalprovider.h
Expand Up @@ -385,6 +385,7 @@ class QgsGdalProviderMetadata final: public QgsProviderMetadata
QgsProviderMetadata::ProviderMetadataCapabilities capabilities() const override;
ProviderCapabilities providerCapabilities() const override;
QList< QgsProviderSublayerDetails > querySublayers( const QString &uri, Qgis::SublayerQueryFlags flags = Qgis::SublayerQueryFlags(), QgsFeedback *feedback = nullptr ) const override;
QStringList sidecarFilesForUri( const QString &uri ) const override;
};

///@endcond
Expand Down
22 changes: 22 additions & 0 deletions tests/src/python/test_provider_gdal.py
Expand Up @@ -115,6 +115,28 @@ def testDecodeEncodeUriVsizip(self):
encodedUri = QgsProviderRegistry.instance().encodeUri('gdal', parts)
self.assertEqual(encodedUri, uri)

def test_provider_sidecar_files_for_uri(self):
"""
Test retrieving sidecar files for uris
"""
metadata = QgsProviderRegistry.instance().providerMetadata('gdal')

self.assertEqual(metadata.sidecarFilesForUri(''), [])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/some_file.asc'), ['/home/me/some_file.aux.xml', '/home/me/some_file.ovr', '/home/me/some_file.wld'])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/special.jpg'), ['/home/me/special.jpw', '/home/me/special.jgw', '/home/me/special.jpgw', '/home/me/special.jpegw', '/home/me/special.aux.xml', '/home/me/special.ovr', '/home/me/special.wld'])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/special.img'),
['/home/me/special.ige', '/home/me/special.aux.xml', '/home/me/special.ovr',
'/home/me/special.wld'])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/special.sid'),
['/home/me/special.j2w', '/home/me/special.aux.xml', '/home/me/special.ovr', '/home/me/special.wld'])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/special.tif'), ['/home/me/special.tifw', '/home/me/special.tfw', '/home/me/special.aux.xml', '/home/me/special.ovr', '/home/me/special.wld'])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/special.bil'),
['/home/me/special.bilw', '/home/me/special.blw', '/home/me/special.aux.xml', '/home/me/special.ovr', '/home/me/special.wld'])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/special.raster'),
['/home/me/special.rasterw', '/home/me/special.aux.xml', '/home/me/special.ovr', '/home/me/special.wld'])
self.assertEqual(metadata.sidecarFilesForUri('/home/me/special.bt'),
['/home/me/special.btw', '/home/me/special.aux.xml', '/home/me/special.ovr', '/home/me/special.wld'])


if __name__ == '__main__':
unittest.main()

2 comments on commit 28b18bd

@rouault
Copy link
Contributor

@rouault rouault commented on 28b18bd Aug 9, 2021

Choose a reason for hiding this comment

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

@nyalldawson using GDALGetFileList() could avoid hardcoding that logic. Should also work at least for a few OGR drivers.

@nyalldawson
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@rouault thanks for the heads up -- I'll upstream the missing bits from this pr and then will adapt accordingly!

Please sign in to comment.