Skip to content

Commit 47fe506

Browse files
committed
[FEATURE] Add decode_uri expression function
This function takes a layer parameter and an optional uri part parameter. It will return details about the data source of the layer. The available details depend on the data provider. If called with only a layer parameter, a map will be returned with all the available information. If the part is also specified, only the value for the requested part will be extracted from the map.
1 parent a12bc5f commit 47fe506

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/core/expression/qgsexpressionfunction.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "qgsfieldformatterregistry.h"
4747
#include "qgsfieldformatter.h"
4848
#include "qgsvectorlayerfeatureiterator.h"
49+
#include "qgsproviderregistry.h"
4950

5051
const QString QgsExpressionFunction::helpText() const
5152
{
@@ -4030,6 +4031,35 @@ static QVariant fcnGetLayerProperty( const QVariantList &values, const QgsExpres
40304031
return QVariant();
40314032
}
40324033

4034+
static QVariant fcnDecodeUri( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
4035+
{
4036+
QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
4037+
if ( !layer )
4038+
{
4039+
parent->setEvalErrorString( QObject::tr( "Cannot find layer %1" ).arg( values.at( 0 ).toString() ) );
4040+
return QVariant();
4041+
}
4042+
4043+
if ( !layer->dataProvider() )
4044+
{
4045+
parent->setEvalErrorString( QObject::tr( "Layer %1 has invalid data provider" ).arg( layer->name() ) );
4046+
return QVariant();
4047+
}
4048+
4049+
const QString uriPart = values.at( 1 ).toString();
4050+
4051+
const QVariantMap decodedUri = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->dataProvider()->dataSourceUri() );
4052+
4053+
if ( !uriPart.isNull() )
4054+
{
4055+
return decodedUri.value( values.at( 1 ).toString() );
4056+
}
4057+
else
4058+
{
4059+
return decodedUri;
4060+
}
4061+
}
4062+
40334063
static QVariant fcnGetRasterBandStat( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
40344064
{
40354065
QString layerIdOrName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
@@ -4906,6 +4936,11 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
49064936
// **General** functions
49074937
sFunctions
49084938
<< new QgsStaticExpressionFunction( QStringLiteral( "layer_property" ), 2, fcnGetLayerProperty, QStringLiteral( "General" ) )
4939+
<< new QgsStaticExpressionFunction( QStringLiteral( "decode_uri" ),
4940+
QgsExpressionFunction::ParameterList()
4941+
<< QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
4942+
<< QgsExpressionFunction::Parameter( QStringLiteral( "part" ), true ),
4943+
fcnDecodeUri, QStringLiteral( "Map Layers" ) )
49094944
<< new QgsStaticExpressionFunction( QStringLiteral( "raster_statistic" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
49104945
<< QgsExpressionFunction::Parameter( QStringLiteral( "band" ) )
49114946
<< QgsExpressionFunction::Parameter( QStringLiteral( "statistic" ) ), fcnGetRasterBandStat, QStringLiteral( "Rasters" ) );

0 commit comments

Comments
 (0)