mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
Add unit tests for QgsProcessingUtils::mapLayerFromString
This commit is contained in:
parent
11fb72eef7
commit
600518de72
@ -68,9 +68,11 @@ class QgsProcessingUtils
|
|||||||
|
|
||||||
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
|
static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
|
||||||
%Docstring
|
%Docstring
|
||||||
Interprets a string as a map layer within the supplied ``context``. The method will attempt to
|
Interprets a string as a map layer within the supplied ``context``.
|
||||||
|
|
||||||
|
The method will attempt to
|
||||||
load a layer matching the passed ``string``. E.g. if the string matches a layer ID or name
|
load a layer matching the passed ``string``. E.g. if the string matches a layer ID or name
|
||||||
within the current project this layer will be returned.
|
within the context's project or temporary layer store then this layer will be returned.
|
||||||
If the string is a file path and ``allowLoadingNewLayers`` is true, then the layer at this
|
If the string is a file path and ``allowLoadingNewLayers`` is true, then the layer at this
|
||||||
file path will be loaded and added to the context's temporary layer store.
|
file path will be loaded and added to the context's temporary layer store.
|
||||||
Ownership of the layer remains with the ``context`` or the context's current project.
|
Ownership of the layer remains with the ``context`` or the context's current project.
|
||||||
|
@ -80,9 +80,11 @@ class CORE_EXPORT QgsProcessingUtils
|
|||||||
static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
|
static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interprets a string as a map layer within the supplied \a context. The method will attempt to
|
* Interprets a string as a map layer within the supplied \a context.
|
||||||
|
*
|
||||||
|
* The method will attempt to
|
||||||
* load a layer matching the passed \a string. E.g. if the string matches a layer ID or name
|
* load a layer matching the passed \a string. E.g. if the string matches a layer ID or name
|
||||||
* within the current project this layer will be returned.
|
* within the context's project or temporary layer store then this layer will be returned.
|
||||||
* If the string is a file path and \a allowLoadingNewLayers is true, then the layer at this
|
* If the string is a file path and \a allowLoadingNewLayers is true, then the layer at this
|
||||||
* file path will be loaded and added to the context's temporary layer store.
|
* file path will be loaded and added to the context's temporary layer store.
|
||||||
* Ownership of the layer remains with the \a context or the context's current project.
|
* Ownership of the layer remains with the \a context or the context's current project.
|
||||||
|
@ -104,6 +104,7 @@ class TestQgsProcessing: public QObject
|
|||||||
void removeProvider();
|
void removeProvider();
|
||||||
void compatibleLayers();
|
void compatibleLayers();
|
||||||
void normalizeLayerSource();
|
void normalizeLayerSource();
|
||||||
|
void mapLayers();
|
||||||
void mapLayerFromString();
|
void mapLayerFromString();
|
||||||
void algorithm();
|
void algorithm();
|
||||||
void features();
|
void features();
|
||||||
@ -331,7 +332,7 @@ void TestQgsProcessing::normalizeLayerSource()
|
|||||||
QCOMPARE( QgsProcessingUtils::normalizeLayerSource( "data\\layers \"new\"\\test.shp" ), QString( "data/layers 'new'/test.shp" ) );
|
QCOMPARE( QgsProcessingUtils::normalizeLayerSource( "data\\layers \"new\"\\test.shp" ), QString( "data/layers 'new'/test.shp" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestQgsProcessing::mapLayerFromString()
|
void TestQgsProcessing::mapLayers()
|
||||||
{
|
{
|
||||||
// test mapLayerFromProject
|
// test mapLayerFromProject
|
||||||
|
|
||||||
@ -364,7 +365,7 @@ void TestQgsProcessing::mapLayerFromString()
|
|||||||
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( r1->id(), &p ), r1 );
|
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( r1->id(), &p ), r1 );
|
||||||
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( v1->id(), &p ), v1 );
|
QCOMPARE( QgsProcessingUtils::mapLayerFromProject( v1->id(), &p ), v1 );
|
||||||
|
|
||||||
// test mapLayerFromString
|
// test loadMapLayerFromString
|
||||||
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster2 );
|
QgsMapLayer *l = QgsProcessingUtils::loadMapLayerFromString( raster2 );
|
||||||
QVERIFY( l->isValid() );
|
QVERIFY( l->isValid() );
|
||||||
QCOMPARE( l->type(), QgsMapLayer::RasterLayer );
|
QCOMPARE( l->type(), QgsMapLayer::RasterLayer );
|
||||||
@ -379,6 +380,71 @@ void TestQgsProcessing::mapLayerFromString()
|
|||||||
delete l;
|
delete l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestQgsProcessing::mapLayerFromString()
|
||||||
|
{
|
||||||
|
// test mapLayerFromString
|
||||||
|
|
||||||
|
QgsProcessingContext c;
|
||||||
|
QgsProject p;
|
||||||
|
|
||||||
|
// add a bunch of layers to a project
|
||||||
|
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
|
||||||
|
QString raster1 = testDataDir + "tenbytenraster.asc";
|
||||||
|
QString raster2 = testDataDir + "landsat.tif";
|
||||||
|
QFileInfo fi1( raster1 );
|
||||||
|
QgsRasterLayer *r1 = new QgsRasterLayer( fi1.filePath(), "R1" );
|
||||||
|
QVERIFY( r1->isValid() );
|
||||||
|
QFileInfo fi2( raster2 );
|
||||||
|
QgsRasterLayer *r2 = new QgsRasterLayer( fi2.filePath(), "ar2" );
|
||||||
|
QVERIFY( r2->isValid() );
|
||||||
|
|
||||||
|
QgsVectorLayer *v1 = new QgsVectorLayer( "Polygon", "V4", "memory" );
|
||||||
|
QgsVectorLayer *v2 = new QgsVectorLayer( "Point", "v1", "memory" );
|
||||||
|
p.addMapLayers( QList<QgsMapLayer *>() << r1 << r2 << v1 << v2 );
|
||||||
|
|
||||||
|
// no project set yet
|
||||||
|
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QString(), c ) );
|
||||||
|
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QStringLiteral( "v1" ), c ) );
|
||||||
|
|
||||||
|
c.setProject( &p );
|
||||||
|
|
||||||
|
// layers from current project
|
||||||
|
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( QString(), c ) );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster1, c ), r1 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( raster2, c ), r2 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "R1", c ), r1 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "ar2", c ), r2 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V4", c ), v1 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v1", c ), v2 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( r1->id(), c ), r1 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v1->id(), c ), v1 );
|
||||||
|
|
||||||
|
// check that layers in context temporary store are used
|
||||||
|
QgsVectorLayer *v5 = new QgsVectorLayer( "Polygon", "V5", "memory" );
|
||||||
|
QgsVectorLayer *v6 = new QgsVectorLayer( "Point", "v6", "memory" );
|
||||||
|
c.temporaryLayerStore().addMapLayers( QList<QgsMapLayer *>() << v5 << v6 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "V5", c ), v5 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( "v6", c ), v6 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v5->id(), c ), v5 );
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( v6->id(), c ), v6 );
|
||||||
|
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( "aaaaa", c ) );
|
||||||
|
|
||||||
|
// if specified, check that layers can be loaded
|
||||||
|
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( "aaaaa", c ) );
|
||||||
|
QString newRaster = testDataDir + "requires_warped_vrt.tif";
|
||||||
|
// don't allow loading
|
||||||
|
QVERIFY( ! QgsProcessingUtils::mapLayerFromString( newRaster, c, false ) );
|
||||||
|
// allow loading
|
||||||
|
QgsMapLayer *loadedLayer = QgsProcessingUtils::mapLayerFromString( newRaster, c, true );
|
||||||
|
QVERIFY( loadedLayer->isValid() );
|
||||||
|
QCOMPARE( loadedLayer->type(), QgsMapLayer::RasterLayer );
|
||||||
|
// should now be in temporary store
|
||||||
|
QCOMPARE( c.temporaryLayerStore().mapLayer( loadedLayer->id() ), loadedLayer );
|
||||||
|
|
||||||
|
// since it's now in temporary store, should be accessible even if we deny loading new layers
|
||||||
|
QCOMPARE( QgsProcessingUtils::mapLayerFromString( newRaster, c, false ), loadedLayer );
|
||||||
|
}
|
||||||
|
|
||||||
void TestQgsProcessing::algorithm()
|
void TestQgsProcessing::algorithm()
|
||||||
{
|
{
|
||||||
DummyAlgorithm alg( "test" );
|
DummyAlgorithm alg( "test" );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user