Skip to content

Commit

Permalink
Identify
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Nov 8, 2022
1 parent 5a88611 commit be9b864
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
Expand Up @@ -286,9 +286,14 @@ Sets the ``value`` for ``row`` and ``column``.
:return: ``True`` on success.
%End

QVariant value( const int row, const int column );
QVariant value( const int row, const int column ) const;
%Docstring
Returns the ``value`` for ``row`` and ``column``.
%End

QVariantList row( const double matchValue ) const;
%Docstring
Returns a row of data for the given ``matchValue`` or and empty row if there is not match.
%End

static Qgis::RasterAttributeTableFieldUsage guessFieldUsage( const QString &name, const QVariant::Type type );
Expand Down
42 changes: 41 additions & 1 deletion src/core/raster/qgsrasterattributetable.cpp
Expand Up @@ -640,7 +640,7 @@ bool QgsRasterAttributeTable::setValue( const int row, const int column, const Q
return true;
}

QVariant QgsRasterAttributeTable::value( const int row, const int column )
QVariant QgsRasterAttributeTable::value( const int row, const int column ) const
{
if ( row < 0 || row >= mData.count( ) || column < 0 || column >= mData[ row ].count( ) )
{
Expand All @@ -649,6 +649,46 @@ QVariant QgsRasterAttributeTable::value( const int row, const int column )
return mData[ row ][ column ];
}

QVariantList QgsRasterAttributeTable::row( const double matchValue ) const
{
if ( ! isValid() )
{
return QVariantList();
}

const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };

if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
{
const int colIdx { fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::MinMax ) };
for ( int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
{
bool ok;
if ( matchValue == value( rowIdx, colIdx ).toDouble( &ok ) && ok )
{
return mData.at( rowIdx );
}
}
}
else
{
const int minColIdx { fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Min ) };
const int maxColIdx { fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Max ) };
for ( int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
{
bool ok;
if ( matchValue >= value( rowIdx, minColIdx ).toDouble( &ok ) && ok )
{
if ( matchValue < value( rowIdx, maxColIdx ).toDouble( &ok ) && ok )
{
return mData.at( rowIdx );
}
}
}
}
return QVariantList();
}

Qgis::RasterAttributeTableFieldUsage QgsRasterAttributeTable::guessFieldUsage( const QString &name, const QVariant::Type type )
{
static const QStringList minValueNames { {
Expand Down
7 changes: 6 additions & 1 deletion src/core/raster/qgsrasterattributetable.h
Expand Up @@ -275,7 +275,12 @@ class CORE_EXPORT QgsRasterAttributeTable
/**
* Returns the \a value for \a row and \a column.
*/
QVariant value( const int row, const int column );
QVariant value( const int row, const int column ) const;

/**
* Returns a row of data for the given \a matchValue or and empty row if there is not match.
*/
QVariantList row( const double matchValue ) const;

/**
* Try to determine the field usage from its \a name and \a type.
Expand Down
9 changes: 4 additions & 5 deletions src/core/raster/qgsrasterdataprovider.cpp
Expand Up @@ -315,19 +315,18 @@ QgsRasterIdentifyResult QgsRasterDataProvider::identify( const QgsPointXY &point
const double yMin = yMax - yres;
const QgsRectangle pixelExtent( xMin, yMin, xMax, yMax );

for ( int i = 1; i <= bandCount(); i++ )
for ( int bandNumber = 1; bandNumber <= bandCount(); bandNumber++ )
{
std::unique_ptr< QgsRasterBlock > bandBlock( block( i, pixelExtent, 1, 1 ) );
std::unique_ptr< QgsRasterBlock > bandBlock( block( bandNumber, pixelExtent, 1, 1 ) );

if ( bandBlock )
{
const double value = bandBlock->value( 0 );

results.insert( i, value );
results.insert( bandNumber, value );
}
else
{
results.insert( i, QVariant() );
results.insert( bandNumber, QVariant() );
}
}
return QgsRasterIdentifyResult( QgsRaster::IdentifyFormatValue, results );
Expand Down
33 changes: 33 additions & 0 deletions src/gui/qgsmaptoolidentify.cpp
Expand Up @@ -1079,6 +1079,39 @@ bool QgsMapToolIdentify::identifyRasterLayer( QList<IdentifyResult> *results, Qg
valueString = QgsRasterBlock::printValue( value.toDouble() );
}
}
if ( const QgsRasterAttributeTable *rat = layer->attributeTable( it.key() ) )
{
bool ok;
const double doubleValue { it.value().toDouble( &ok ) };
if ( ok )
{
const QVariantList row = rat->row( doubleValue );
if ( ! row.isEmpty() )
{
for ( int colIdx = 0; colIdx < std::min( rat->fields().count( ), row.count() ); ++colIdx )
{
const QgsRasterAttributeTable::Field ratField { rat->fields().at( colIdx ) };
QString ratValue;
switch ( ratField.type )
{
case QVariant::Type::Char:
case QVariant::Type::Int:
case QVariant::Type::UInt:
case QVariant::Type::LongLong:
case QVariant::Type::ULongLong:
ratValue = QLocale().toString( row.at( colIdx ).toLongLong() );
break;
case QVariant::Type::Double:
ratValue = QLocale().toString( row.at( colIdx ).toDouble( ) );
break;
default:
ratValue = row.at( colIdx ).toString();
}
derivedAttributes.insert( ratField.name, ratValue );
}
}
}
}
attributes.insert( dprovider->generateBandName( it.key() ), valueString );
}
QString label = layer->name();
Expand Down
5 changes: 5 additions & 0 deletions tests/src/python/test_qgsrasterattributetable.py
Expand Up @@ -515,6 +515,11 @@ def testClassification(self):
classes = rat.minMaxClasses(4)
self.assertEqual(len(classes), 0)

# Test row function
self.assertEqual(rat.row(0), [0, 'zero', 'zero', 'even', 0, 0, 0])
self.assertEqual(rat.row(2.0), [2, 'two', 'not0', 'even', 2, 2, 2])
self.assertEqual(rat.row(100.0), [])

def testPalettedRenderer(self):

# Create RAT for 16 bit raster
Expand Down

0 comments on commit be9b864

Please sign in to comment.