Skip to content
Permalink
Browse files

[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.
  • Loading branch information
m-kuhn committed Dec 11, 2018
1 parent c21597a commit 74e0f0fdd1df1aa2b301e5de54ee94d0300c2601
Showing with 35 additions and 0 deletions.
  1. +35 −0 src/core/expression/qgsexpressionfunction.cpp
@@ -46,6 +46,7 @@
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsvectorlayerfeatureiterator.h"
#include "qgsproviderregistry.h"

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

static QVariant fcnDecodeUri( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
if ( !layer )
{
parent->setEvalErrorString( QObject::tr( "Cannot find layer %1" ).arg( values.at( 0 ).toString() ) );
return QVariant();
}

if ( !layer->dataProvider() )
{
parent->setEvalErrorString( QObject::tr( "Layer %1 has invalid data provider" ).arg( layer->name() ) );
return QVariant();
}

const QString uriPart = values.at( 1 ).toString();

const QVariantMap decodedUri = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->dataProvider()->dataSourceUri() );

if ( !uriPart.isNull() )
{
return decodedUri.value( values.at( 1 ).toString() );
}
else
{
return decodedUri;
}
}

static QVariant fcnGetRasterBandStat( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QString layerIdOrName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
@@ -4906,6 +4936,11 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
// **General** functions
sFunctions
<< new QgsStaticExpressionFunction( QStringLiteral( "layer_property" ), 2, fcnGetLayerProperty, QStringLiteral( "General" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "decode_uri" ),
QgsExpressionFunction::ParameterList()
<< QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "part" ), true ),
fcnDecodeUri, QStringLiteral( "Map Layers" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "raster_statistic" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "band" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "statistic" ) ), fcnGetRasterBandStat, QStringLiteral( "Rasters" ) );

0 comments on commit 74e0f0f

Please sign in to comment.
You can’t perform that action at this time.