Skip to content

Commit

Permalink
layer_property expression function should use proper layer metadata
Browse files Browse the repository at this point in the history
for return values where available

...instead of the older, server-specific properties

This affects:

- layer_property(...,'title')
- layer_property(...,'abstract')
- layer_property(...,'keywords')
- layer_property(...,'attribution')
  • Loading branch information
nyalldawson committed Feb 19, 2019
1 parent b06f00d commit 68b4602
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4215,21 +4215,34 @@ static QVariant fcnGetLayerProperty( const QVariantList &values, const QgsExpres
if ( !layer )
return QVariant();

// here, we always prefer the layer metadata values over the older server-specific published values
QString layerProperty = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
if ( QString::compare( layerProperty, QStringLiteral( "name" ), Qt::CaseInsensitive ) == 0 )
return layer->name();
else if ( QString::compare( layerProperty, QStringLiteral( "id" ), Qt::CaseInsensitive ) == 0 )
return layer->id();
else if ( QString::compare( layerProperty, QStringLiteral( "title" ), Qt::CaseInsensitive ) == 0 )
return layer->title();
return !layer->metadata().title().isEmpty() ? layer->metadata().title() : layer->title();
else if ( QString::compare( layerProperty, QStringLiteral( "abstract" ), Qt::CaseInsensitive ) == 0 )
return layer->abstract();
return !layer->metadata().abstract().isEmpty() ? layer->metadata().abstract() : layer->abstract();
else if ( QString::compare( layerProperty, QStringLiteral( "keywords" ), Qt::CaseInsensitive ) == 0 )
{
QStringList keywords;
const QgsAbstractMetadataBase::KeywordMap keywordMap = layer->metadata().keywords();
for ( auto it = keywordMap.constBegin(); it != keywordMap.constEnd(); ++it )
{
keywords.append( it.value() );
}
if ( !keywords.isEmpty() )
return keywords;
return layer->keywordList();
}
else if ( QString::compare( layerProperty, QStringLiteral( "data_url" ), Qt::CaseInsensitive ) == 0 )
return layer->dataUrl();
else if ( QString::compare( layerProperty, QStringLiteral( "attribution" ), Qt::CaseInsensitive ) == 0 )
return layer->attribution();
{
return !layer->metadata().rights().isEmpty() ? QVariant( layer->metadata().rights() ) : QVariant( layer->attribution() );
}
else if ( QString::compare( layerProperty, QStringLiteral( "attribution_url" ), Qt::CaseInsensitive ) == 0 )
return layer->attributionUrl();
else if ( QString::compare( layerProperty, QStringLiteral( "source" ), Qt::CaseInsensitive ) == 0 )
Expand Down
25 changes: 25 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TestQgsExpression: public QObject
private:

QgsVectorLayer *mPointsLayer = nullptr;
QgsVectorLayer *mPointsLayerMetadata = nullptr;
QgsVectorLayer *mMemoryLayer = nullptr;
QgsVectorLayer *mAggregatesLayer = nullptr;
QgsVectorLayer *mChildLayer = nullptr;
Expand Down Expand Up @@ -92,6 +93,25 @@ class TestQgsExpression: public QObject
mPointsLayer->setMinimumScale( 500 );
mPointsLayer->setMaximumScale( 1000 );

mPointsLayerMetadata = new QgsVectorLayer( pointFileInfo.filePath(),
pointFileInfo.completeBaseName() + "_metadata", QStringLiteral( "ogr" ) );
QgsProject::instance()->addMapLayer( mPointsLayerMetadata );
QgsLayerMetadata metadata;
metadata.setTitle( QStringLiteral( "metadata title" ) );
metadata.setAbstract( QStringLiteral( "metadata abstract" ) );
QMap<QString, QStringList> keywords;
keywords.insert( QStringLiteral( "key1" ), QStringList() << QStringLiteral( "val1" ) << QStringLiteral( "val2" ) );
keywords.insert( QStringLiteral( "key2" ), QStringList() << QStringLiteral( "val3" ) );
metadata.setKeywords( keywords );
metadata.setRights( QStringList() << QStringLiteral( "right1" ) << QStringLiteral( "right2" ) );
mPointsLayerMetadata->setMetadata( metadata );
mPointsLayerMetadata->setTitle( QStringLiteral( "layer title" ) );
mPointsLayerMetadata->setAbstract( QStringLiteral( "layer abstract" ) );
mPointsLayerMetadata->setKeywordList( QStringLiteral( "layer,keywords" ) );
mPointsLayerMetadata->setDataUrl( QStringLiteral( "data url" ) );
mPointsLayerMetadata->setAttribution( QStringLiteral( "layer attribution" ) );
mPointsLayerMetadata->setAttributionUrl( QStringLiteral( "attribution url" ) );

QString rasterFileName = testDataDir + "tenbytenraster.asc";
QFileInfo rasterFileInfo( rasterFileName );
mRasterLayer = new QgsRasterLayer( rasterFileInfo.filePath(),
Expand Down Expand Up @@ -1288,6 +1308,11 @@ class TestQgsExpression: public QObject
QTest::newRow( "layer_property storage_type" ) << QStringLiteral( "layer_property('%1','storage_type')" ).arg( mPointsLayer->name() ) << false << QVariant( "ESRI Shapefile" );
QTest::newRow( "layer_property geometry_type" ) << QStringLiteral( "layer_property('%1','geometry_type')" ).arg( mPointsLayer->name() ) << false << QVariant( "Point" );

QTest::newRow( "layer_property title with metadata" ) << QStringLiteral( "layer_property('%1','title')" ).arg( mPointsLayerMetadata->name() ) << false << QVariant( "metadata title" );
QTest::newRow( "layer_property abstract with metadata" ) << QStringLiteral( "layer_property('%1','abstract')" ).arg( mPointsLayerMetadata->name() ) << false << QVariant( "metadata abstract" );
QTest::newRow( "layer_property keywords with metadata" ) << QStringLiteral( "array_to_string(layer_property('%1','keywords'),',')" ).arg( mPointsLayerMetadata->name() ) << false << QVariant( "val1,val2,val3" );
QTest::newRow( "layer_property attribution with metadata" ) << QStringLiteral( "array_to_string(layer_property('%1','attribution'))" ).arg( mPointsLayerMetadata->name() ) << false << QVariant( "right1,right2" );

QTest::newRow( "decode_uri shp path" ) << QStringLiteral( "array_last(string_to_array(replace(decode_uri('%1', 'path'), '\\\\', '/'), '/'))" ).arg( mPointsLayer->name() ) << false << QVariant( "points.shp" );

// raster_statistic tests
Expand Down

0 comments on commit 68b4602

Please sign in to comment.