Skip to content

Commit b37f5f4

Browse files
committedNov 9, 2022
Raster attributes and raster maptips
1 parent 5fe715a commit b37f5f4

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
 
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "raster_attributes",
3+
"type": "function",
4+
"groups": ["Rasters"],
5+
"description": "Returns a map with the fields names as keys and the raster attribute table values as values from the attribute table entry that matches the given raster value .",
6+
"arguments": [{
7+
"arg": "layer",
8+
"description": "the name or id of a raster layer"
9+
}, {
10+
"arg": "band",
11+
"description": "the band number to sample the value from and for the associated attribute table lookup."
12+
}, {
13+
"arg": "value",
14+
"description": "raster value"
15+
}],
16+
"examples": [{
17+
"expression": "raster_attributes('vegetation', 1, raster_value('vegetation', 1, make_point(1,1)))",
18+
"returns": "{'class': 'Vegetated', 'subclass': 'Trees'}"
19+
}],
20+
"tags": ["provider", "point", "raster", "found", "attributes"]
21+
}

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,56 @@ static QVariant fcnRasterValue( const QVariantList &values, const QgsExpressionC
16971697
return std::isnan( value ) ? QVariant() : value;
16981698
}
16991699

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+
17001750
static QVariant fcnFeature( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * )
17011751
{
17021752
if ( !context )
@@ -8470,6 +8520,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
84708520
<< new QgsStaticExpressionFunction( QStringLiteral( "env" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "name" ) ), fcnEnvVar, QStringLiteral( "General" ), QString() )
84718521
<< new QgsWithVariableExpressionFunction()
84728522
<< 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" ) )
84738524

84748525
// functions for arrays
84758526
<< new QgsArrayForeachExpressionFunction()

‎src/gui/qgsmaptip.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "qgsmapcanvas.h"
1818
#include "qgsmaptool.h"
1919
#include "qgsvectorlayer.h"
20+
#include "qgsrasterlayer.h"
2021
#include "qgsexpression.h"
2122
#include "qgslogger.h"
2223
#include "qgssettings.h"
@@ -313,6 +314,31 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, Qg
313314
return tipString;
314315
}
315316

317+
QString QgsMapTip::fetchRaster( QgsMapLayer *layer, QgsPointXY &mapPosition, QgsMapCanvas *mapCanvas )
318+
{
319+
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
320+
if ( !rlayer )
321+
return QString();
322+
323+
if ( !layer->isInScaleRange( mapCanvas->mapSettings().scale() ) )
324+
{
325+
return QString();
326+
}
327+
328+
const QgsPointXY mappedPosition { mapCanvas->mapSettings().mapToLayerCoordinates( layer, mapPosition ) };
329+
330+
if ( ! layer->extent().contains( mappedPosition ) )
331+
{
332+
return QString( );
333+
}
334+
335+
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) );
336+
context.appendScope( QgsExpressionContextUtils::mapSettingsScope( mapCanvas->mapSettings() ) );
337+
338+
const QString mapTip = rlayer->mapTipTemplate();
339+
340+
}
341+
316342
void QgsMapTip::applyFontSettings()
317343
{
318344
const QgsSettings settings;

‎src/gui/qgsmaptip.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ class GUI_EXPORT QgsMapTip : public QWidget
9797
QgsPointXY &mapPosition,
9898
QgsMapCanvas *mapCanvas );
9999

100+
// Sample the raster and get the maptip text
101+
QString fetchRaster( QgsMapLayer *layer,
102+
QgsPointXY &mapPosition,
103+
QgsMapCanvas *mapCanvas );
104+
100105
QString replaceText(
101106
QString displayText, QgsVectorLayer *layer, QgsFeature &feat );
102107

0 commit comments

Comments
 (0)
Please sign in to comment.