Skip to content

Commit 3310343

Browse files
committed
[processing] Add a model-only "load layer to project" algorithm
This can be used to force loading a layer into the current project. The primary use case here is to load a preset layer as part of a model, but it's also useful for loading results from the 'package layers' algorithm into the project.
1 parent bb63a83 commit 3310343

File tree

5 files changed

+188
-1
lines changed

5 files changed

+188
-1
lines changed

src/analysis/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ SET(QGIS_ANALYSIS_SRCS
4141
processing/qgsalgorithmjoinbyattribute.cpp
4242
processing/qgsalgorithmjoinwithlines.cpp
4343
processing/qgsalgorithmlineintersection.cpp
44+
processing/qgsalgorithmloadlayer.cpp
4445
processing/qgsalgorithmmeancoordinates.cpp
4546
processing/qgsalgorithmmergelines.cpp
4647
processing/qgsalgorithmminimumenclosingcircle.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/***************************************************************************
2+
qgsalgorithmloadlayer.cpp
3+
---------------------
4+
begin : November 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsalgorithmloadlayer.h"
19+
20+
///@cond PRIVATE
21+
22+
QString QgsLoadLayerAlgorithm::name() const
23+
{
24+
return QStringLiteral( "loadlayer" );
25+
}
26+
27+
QgsProcessingAlgorithm::Flags QgsLoadLayerAlgorithm::flags() const
28+
{
29+
return FlagHideFromToolbox;
30+
}
31+
32+
QString QgsLoadLayerAlgorithm::displayName() const
33+
{
34+
return QObject::tr( "Load layer into project" );
35+
}
36+
37+
QStringList QgsLoadLayerAlgorithm::tags() const
38+
{
39+
return QObject::tr( "load,open,layer,raster,vector,project" ).split( ',' );
40+
}
41+
42+
QString QgsLoadLayerAlgorithm::group() const
43+
{
44+
return QObject::tr( "Modeler tool" );
45+
}
46+
47+
QString QgsLoadLayerAlgorithm::shortHelpString() const
48+
{
49+
return QObject::tr( "This algorithm loads a layer to the current project." );
50+
}
51+
52+
QgsLoadLayerAlgorithm *QgsLoadLayerAlgorithm::createInstance() const
53+
{
54+
return new QgsLoadLayerAlgorithm();
55+
}
56+
57+
void QgsLoadLayerAlgorithm::initAlgorithm( const QVariantMap & )
58+
{
59+
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
60+
addParameter( new QgsProcessingParameterString( QStringLiteral( "NAME" ), QObject::tr( "Loaded layer name" ) ) );
61+
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Layer" ) ) );
62+
}
63+
64+
QVariantMap QgsLoadLayerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
65+
{
66+
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
67+
QString name = parameterAsString( parameters, QStringLiteral( "NAME" ), context );
68+
69+
if ( !layer )
70+
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
71+
72+
if ( name.isEmpty() )
73+
throw QgsProcessingException( QObject::tr( "Invalid (empty) layer name" ) );
74+
75+
layer->setName( name );
76+
context.addLayerToLoadOnCompletion( layer->id(), QgsProcessingContext::LayerDetails( name, context.project(), name ) );
77+
78+
QVariantMap results;
79+
results.insert( QStringLiteral( "OUTPUT" ), layer->id() );
80+
return results;
81+
}
82+
83+
///@endcond
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***************************************************************************
2+
qgsalgorithmloadlayer.h
3+
---------------------
4+
begin : November 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSALGORITHMLOADLAYER_H
19+
#define QGSALGORITHMLOADLAYER_H
20+
21+
#define SIP_NO_FILE
22+
23+
#include "qgis.h"
24+
#include "qgsprocessingalgorithm.h"
25+
26+
///@cond PRIVATE
27+
28+
/**
29+
* Native rename layer algorithm.
30+
*/
31+
class QgsLoadLayerAlgorithm : public QgsProcessingAlgorithm
32+
{
33+
public:
34+
QgsLoadLayerAlgorithm() = default;
35+
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
36+
Flags flags() const override;
37+
QString name() const override;
38+
QString displayName() const override;
39+
virtual QStringList tags() const override;
40+
QString group() const override;
41+
QString shortHelpString() const override;
42+
QgsLoadLayerAlgorithm *createInstance() const override SIP_FACTORY;
43+
44+
protected:
45+
46+
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
47+
QgsProcessingContext &context, QgsProcessingFeedback * ) override;
48+
49+
};
50+
51+
///@endcond PRIVATE
52+
53+
#endif // QGSALGORITHMLOADLAYER_H

src/analysis/processing/qgsnativealgorithms.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "qgsalgorithmjoinbyattribute.h"
3939
#include "qgsalgorithmjoinwithlines.h"
4040
#include "qgsalgorithmlineintersection.h"
41+
#include "qgsalgorithmloadlayer.h"
4142
#include "qgsalgorithmmeancoordinates.h"
4243
#include "qgsalgorithmmergelines.h"
4344
#include "qgsalgorithmminimumenclosingcircle.h"
@@ -116,6 +117,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
116117
addAlgorithm( new QgsJoinByAttributeAlgorithm() );
117118
addAlgorithm( new QgsJoinWithLinesAlgorithm() );
118119
addAlgorithm( new QgsLineIntersectionAlgorithm() );
120+
addAlgorithm( new QgsLoadLayerAlgorithm() );
119121
addAlgorithm( new QgsMeanCoordinatesAlgorithm() );
120122
addAlgorithm( new QgsMergeLinesAlgorithm() );
121123
addAlgorithm( new QgsMinimumEnclosingCircleAlgorithm() );

tests/src/analysis/testqgsprocessingalgs.cpp

+49-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ class TestQgsProcessingAlgs: public QObject
3535
void cleanup() {} // will be called after every testfunction.
3636
void packageAlg();
3737
void renameLayerAlg();
38+
void loadLayerAlg();
3839

3940
private:
4041

42+
QString mPointLayerPath;
4143
QgsVectorLayer *mPointsLayer = nullptr;
4244
QgsVectorLayer *mPolygonLayer = nullptr;
4345

@@ -59,7 +61,8 @@ void TestQgsProcessingAlgs::initTestCase()
5961

6062
QString pointsFileName = dataDir + "/points.shp";
6163
QFileInfo pointFileInfo( pointsFileName );
62-
mPointsLayer = new QgsVectorLayer( pointFileInfo.filePath(),
64+
mPointLayerPath = pointFileInfo.filePath();
65+
mPointsLayer = new QgsVectorLayer( mPointLayerPath,
6366
QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
6467
QVERIFY( mPointsLayer->isValid() );
6568
// Register the layer with the registry
@@ -169,6 +172,51 @@ void TestQgsProcessingAlgs::renameLayerAlg()
169172
QCOMPARE( results.value( "OUTPUT" ).toString(), QStringLiteral( "new name2" ) );
170173
}
171174

175+
void TestQgsProcessingAlgs::loadLayerAlg()
176+
{
177+
const QgsProcessingAlgorithm *package( QgsApplication::processingRegistry()->algorithmById( QStringLiteral( "native:loadlayer" ) ) );
178+
QVERIFY( package );
179+
180+
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
181+
QgsProject p;
182+
context->setProject( &p );
183+
184+
QgsProcessingFeedback feedback;
185+
186+
QVariantMap parameters;
187+
188+
// bad layer
189+
parameters.insert( QStringLiteral( "INPUT" ), QStringLiteral( "bad layer" ) );
190+
parameters.insert( QStringLiteral( "NAME" ), QStringLiteral( "new name" ) );
191+
bool ok = false;
192+
( void )package->run( parameters, *context, &feedback, &ok );
193+
QVERIFY( !ok );
194+
QVERIFY( context->layersToLoadOnCompletion().empty() );
195+
196+
//invalid name
197+
parameters.insert( QStringLiteral( "INPUT" ), mPointLayerPath );
198+
parameters.insert( QStringLiteral( "NAME" ), QString() );
199+
ok = false;
200+
( void )package->run( parameters, *context, &feedback, &ok );
201+
QVERIFY( !ok );
202+
QVERIFY( context->layersToLoadOnCompletion().empty() );
203+
204+
//good params
205+
parameters.insert( QStringLiteral( "INPUT" ), mPointLayerPath );
206+
parameters.insert( QStringLiteral( "NAME" ), QStringLiteral( "my layer" ) );
207+
ok = false;
208+
QVariantMap results = package->run( parameters, *context, &feedback, &ok );
209+
QVERIFY( ok );
210+
QVERIFY( !context->layersToLoadOnCompletion().empty() );
211+
QString layerId = context->layersToLoadOnCompletion().keys().at( 0 );
212+
QCOMPARE( results.value( QStringLiteral( "OUTPUT" ) ).toString(), layerId );
213+
QVERIFY( !layerId.isEmpty() );
214+
QVERIFY( context->temporaryLayerStore()->mapLayer( layerId ) );
215+
QCOMPARE( context->layersToLoadOnCompletion().value( layerId, QgsProcessingContext::LayerDetails( QString(), nullptr, QString() ) ).name, QStringLiteral( "my layer" ) );
216+
QCOMPARE( context->layersToLoadOnCompletion().value( layerId, QgsProcessingContext::LayerDetails( QString(), nullptr, QString() ) ).project, &p );
217+
QCOMPARE( context->layersToLoadOnCompletion().value( layerId, QgsProcessingContext::LayerDetails( QString(), nullptr, QString() ) ).outputName, QStringLiteral( "my layer" ) );
218+
}
219+
172220

173221
QGSTEST_MAIN( TestQgsProcessingAlgs )
174222
#include "testqgsprocessingalgs.moc"

0 commit comments

Comments
 (0)