Skip to content

Commit

Permalink
Add utility function QgsMapLayerUtils::layerSourceMatchesPath
Browse files Browse the repository at this point in the history
Tests whether a layer has a source which matches a specified
file path
  • Loading branch information
nyalldawson committed Jul 28, 2021
1 parent 8285a15 commit 216925a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgsmaplayerutils.sip.in
Expand Up @@ -34,6 +34,15 @@ The ``crs`` argument specifies the desired coordinate reference system for the c
Creates and returns the (possibly ``None``) database connection for a ``layer``.
Ownership is transferred to the caller.

.. versionadded:: 3.22
%End

static bool layerSourceMatchesPath( const QgsMapLayer *layer, const QString &path );
%Docstring
Returns ``True`` if the source of the specified ``layer`` matches the given ``path``.

This method can be used to test whether a layer is associated with a file path.

.. versionadded:: 3.22
%End

Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsmaplayerutils.cpp
Expand Up @@ -112,3 +112,12 @@ QgsAbstractDatabaseProviderConnection *QgsMapLayerUtils::databaseConnection( con
return nullptr;
}
}

bool QgsMapLayerUtils::layerSourceMatchesPath( const QgsMapLayer *layer, const QString &path )
{
if ( !layer || path.isEmpty() )
return false;

const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->source() );
return parts.value( QStringLiteral( "path" ) ).toString() == path;
}
9 changes: 9 additions & 0 deletions src/core/qgsmaplayerutils.h
Expand Up @@ -51,6 +51,15 @@ class CORE_EXPORT QgsMapLayerUtils
*/
static QgsAbstractDatabaseProviderConnection *databaseConnection( const QgsMapLayer *layer ) SIP_FACTORY;

/**
* Returns TRUE if the source of the specified \a layer matches the given \a path.
*
* This method can be used to test whether a layer is associated with a file path.
*
* \since QGIS 3.22
*/
static bool layerSourceMatchesPath( const QgsMapLayer *layer, const QString &path );

};

#endif // QGSMAPLAYERUTILS_H
Expand Down
27 changes: 27 additions & 0 deletions tests/src/python/test_qgsmaplayerutils.py
Expand Up @@ -64,6 +64,33 @@ def testCombinedExtent(self):
QgsCoordinateTransformContext())
self.assertEqual(extent.toString(0), '-13234651,2607875 : 2008833,5921203')

def test_layerSourceMatchesPath(self):
"""
Test QgsMapLayerUtils.layerSourceMatchesPath()
"""
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(None, ''))
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(None, 'aaaaa'))

# shapefile
layer1 = QgsVectorLayer(unitTestDataPath() + '/points.shp', 'l1')
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(layer1, ''))
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(layer1, 'aaaaa'))
self.assertTrue(QgsMapLayerUtils.layerSourceMatchesPath(layer1, unitTestDataPath() + '/points.shp'))

# geopackage with layers
layer1 = QgsVectorLayer(unitTestDataPath() + '/mixed_layers.gpkg|layername=lines', 'l1')
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(layer1, ''))
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(layer1, 'aaaaa'))
self.assertTrue(QgsMapLayerUtils.layerSourceMatchesPath(layer1, unitTestDataPath() + '/mixed_layers.gpkg'))
layer2 = QgsVectorLayer(unitTestDataPath() + '/mixed_layers.gpkg|layername=points', 'l1')
self.assertTrue(QgsMapLayerUtils.layerSourceMatchesPath(layer2, unitTestDataPath() + '/mixed_layers.gpkg'))

# raster layer from gpkg
rl = QgsRasterLayer(f'GPKG:{unitTestDataPath()}/mixed_layers.gpkg:band1')
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(rl, ''))
self.assertFalse(QgsMapLayerUtils.layerSourceMatchesPath(rl, 'aaaaa'))
self.assertTrue(QgsMapLayerUtils.layerSourceMatchesPath(rl, unitTestDataPath() + '/mixed_layers.gpkg'))


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

0 comments on commit 216925a

Please sign in to comment.