Skip to content

Commit

Permalink
Show a highlight on identified raster pixels when using identify tool
Browse files Browse the repository at this point in the history
Matches the vector/vector tile/point cloud behaviour of showing the
geometry of the identified result in the canvas

Very handy when a identifying a raster which has sections of similar
colored pixels and it's difficult to visually determine the exact
extent of a pixel.
  • Loading branch information
nyalldawson committed Sep 12, 2023
1 parent f64b53c commit 2162543
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/gui/qgsmaptoolidentify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ bool QgsMapToolIdentify::identifyRasterLayer( QList<IdentifyResult> *results, Qg

const double xres = layer->rasterUnitsPerPixelX();
const double yres = layer->rasterUnitsPerPixelY();
QgsRectangle pixelRect;
// Don't derive clicked column/row for providers that serve dynamically rendered map images
if ( ( dprovider->capabilities() & QgsRasterDataProvider::Size ) && !qgsDoubleNear( xres, 0 ) && !qgsDoubleNear( yres, 0 ) )
{
Expand All @@ -1075,12 +1076,16 @@ bool QgsMapToolIdentify::identifyRasterLayer( QList<IdentifyResult> *results, Qg

derivedAttributes.insert( tr( "Column (0-based)" ), QLocale().toString( rasterCol ) );
derivedAttributes.insert( tr( "Row (0-based)" ), QLocale().toString( rasterRow ) );

pixelRect = QgsRectangle( rasterCol * xres + extent.xMinimum(),
extent.yMaximum() - ( rasterRow + 1 ) * yres,
( rasterCol + 1 ) * xres + extent.xMinimum(),
extent.yMaximum() - ( rasterRow * yres ) );
}

if ( identifyResult.isValid() )
{
QMap<int, QVariant> values = identifyResult.results();
QgsGeometry geometry;
if ( format == Qgis::RasterIdentifyFormat::Value )
{
for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
Expand Down Expand Up @@ -1152,7 +1157,15 @@ bool QgsMapToolIdentify::identifyRasterLayer( QList<IdentifyResult> *results, Qg
}

QString label = layer->name();
results->append( IdentifyResult( qobject_cast<QgsMapLayer *>( layer ), label, attributes, derivedAttributes ) );
QgsFeature feature;
if ( !pixelRect.isNull() )
{
feature.setGeometry( QgsGeometry::fromRect( pixelRect ) );
}

IdentifyResult result( qobject_cast<QgsMapLayer *>( layer ), label, QgsFields(), feature, derivedAttributes );
result.mAttributes = attributes;
results->append( result );
}
else if ( format == Qgis::RasterIdentifyFormat::Feature )
{
Expand Down

0 comments on commit 2162543

Please sign in to comment.