Skip to content

Commit

Permalink
[FEATURE] Add decode_uri expression function
Browse files Browse the repository at this point in the history
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 74e0f0f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "qgsfieldformatterregistry.h" #include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h" #include "qgsfieldformatter.h"
#include "qgsvectorlayerfeatureiterator.h" #include "qgsvectorlayerfeatureiterator.h"
#include "qgsproviderregistry.h"


const QString QgsExpressionFunction::helpText() const const QString QgsExpressionFunction::helpText() const
{ {
Expand Down Expand Up @@ -4030,6 +4031,35 @@ static QVariant fcnGetLayerProperty( const QVariantList &values, const QgsExpres
return QVariant(); 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 * ) static QVariant fcnGetRasterBandStat( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{ {
QString layerIdOrName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent ); QString layerIdOrName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
Expand Down Expand Up @@ -4906,6 +4936,11 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
// **General** functions // **General** functions
sFunctions sFunctions
<< new QgsStaticExpressionFunction( QStringLiteral( "layer_property" ), 2, fcnGetLayerProperty, QStringLiteral( "General" ) ) << 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" ) ) << new QgsStaticExpressionFunction( QStringLiteral( "raster_statistic" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "layer" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "band" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "band" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "statistic" ) ), fcnGetRasterBandStat, QStringLiteral( "Rasters" ) ); << QgsExpressionFunction::Parameter( QStringLiteral( "statistic" ) ), fcnGetRasterBandStat, QStringLiteral( "Rasters" ) );
Expand Down

0 comments on commit 74e0f0f

Please sign in to comment.