@@ -1697,6 +1697,56 @@ static QVariant fcnRasterValue( const QVariantList &values, const QgsExpressionC
1697
1697
return std::isnan ( value ) ? QVariant () : value;
1698
1698
}
1699
1699
1700
+ static QVariant fcnRasterAttributes ( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
1701
+ {
1702
+ QgsRasterLayer *layer = QgsExpressionUtils::getRasterLayer ( values.at ( 0 ), parent );
1703
+ if ( !layer || !layer->dataProvider () )
1704
+ {
1705
+ parent->setEvalErrorString ( QObject::tr ( " Function `raster_attributes` requires a valid raster layer." ) );
1706
+ return QVariant ();
1707
+ }
1708
+
1709
+ int bandNb = QgsExpressionUtils::getNativeIntValue ( values.at ( 1 ), parent );
1710
+ if ( bandNb < 1 || bandNb > layer->bandCount () )
1711
+ {
1712
+ parent->setEvalErrorString ( QObject::tr ( " Function `raster_attributes` requires a valid raster band number." ) );
1713
+ return QVariant ();
1714
+ }
1715
+
1716
+ const double value = QgsExpressionUtils::getDoubleValue ( values.at ( 2 ), parent );
1717
+ if ( std::isnan ( value ) )
1718
+ {
1719
+ parent->setEvalErrorString ( QObject::tr ( " Function `raster_attributes` requires a valid raster value." ) );
1720
+ return QVariant ();
1721
+ }
1722
+
1723
+ if ( ! layer->dataProvider ()->attributeTable ( bandNb ) )
1724
+ {
1725
+ return QVariant ();
1726
+ }
1727
+
1728
+ const QVariantList data { layer->dataProvider ()->attributeTable ( bandNb )->row ( value ) };
1729
+ if ( data.isEmpty () )
1730
+ {
1731
+ return QVariant ();
1732
+ }
1733
+
1734
+ QVariantMap result;
1735
+ const QList<QgsRasterAttributeTable::Field> fields { layer->dataProvider ()->attributeTable ( bandNb )->fields () };
1736
+ for ( int idx = 0 ; idx < static_cast <int >( fields.count ( ) ) && idx < static_cast <int >( data.count () ); ++idx )
1737
+ {
1738
+ const QgsRasterAttributeTable::Field field { fields.at ( idx ) };
1739
+ if ( field.isColor () || field.isRamp () )
1740
+ {
1741
+ continue ;
1742
+ }
1743
+ result.insert ( fields.at ( idx ).name , data.at ( idx ) );
1744
+ }
1745
+
1746
+ return result;
1747
+ }
1748
+
1749
+
1700
1750
static QVariant fcnFeature ( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * )
1701
1751
{
1702
1752
if ( !context )
@@ -8470,6 +8520,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
8470
8520
<< new QgsStaticExpressionFunction ( QStringLiteral ( " env" ), QgsExpressionFunction::ParameterList () << QgsExpressionFunction::Parameter ( QStringLiteral ( " name" ) ), fcnEnvVar, QStringLiteral ( " General" ), QString () )
8471
8521
<< new QgsWithVariableExpressionFunction ()
8472
8522
<< new QgsStaticExpressionFunction ( QStringLiteral ( " raster_value" ), QgsExpressionFunction::ParameterList () << QgsExpressionFunction::Parameter ( QStringLiteral ( " layer" ) ) << QgsExpressionFunction::Parameter ( QStringLiteral ( " band" ) ) << QgsExpressionFunction::Parameter ( QStringLiteral ( " point" ) ), fcnRasterValue, QStringLiteral ( " Rasters" ) )
8523
+ << new QgsStaticExpressionFunction ( QStringLiteral ( " raster_attributes" ), QgsExpressionFunction::ParameterList () << QgsExpressionFunction::Parameter ( QStringLiteral ( " layer" ) ) << QgsExpressionFunction::Parameter ( QStringLiteral ( " band" ) ) << QgsExpressionFunction::Parameter ( QStringLiteral ( " point" ) ), fcnRasterAttributes, QStringLiteral ( " Rasters" ) )
8473
8524
8474
8525
// functions for arrays
8475
8526
<< new QgsArrayForeachExpressionFunction ()
0 commit comments