Skip to content

Commit 3deef8b

Browse files
committedJul 1, 2012
Fix picking of transparent pixels
1 parent fa292eb commit 3deef8b

File tree

7 files changed

+109
-24
lines changed

7 files changed

+109
-24
lines changed
 

‎src/app/qgsrasterlayerproperties.cpp

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,44 +1315,42 @@ void QgsRasterLayerProperties::on_pbnRemoveSelectedRow_clicked()
13151315

13161316
void QgsRasterLayerProperties::pixelSelected( const QgsPoint& canvasPoint )
13171317
{
1318-
#if 0 //needs to be fixed
1319-
//PixelSelectorTool has registered a mouse click on the canvas, so bring the dialog back to the front
1318+
QgsRasterRenderer* renderer = mRendererWidget->renderer();
1319+
if ( !renderer )
1320+
{
1321+
return;
1322+
}
1323+
13201324
raise();
13211325
setModal( true );
13221326
activateWindow();
13231327

13241328
//Get the pixel values and add a new entry to the transparency table
13251329
if ( mMapCanvas && mPixelSelectorTool )
13261330
{
1327-
QMap< QString, QString > myPixelMap;
1331+
QMap< int, QString > myPixelMap;
13281332
mMapCanvas->unsetMapTool( mPixelSelectorTool );
13291333
mRasterLayer->identify( mMapCanvas->mapRenderer()->mapToLayerCoordinates( mRasterLayer, canvasPoint ), myPixelMap );
1330-
if ( tableTransparency->columnCount() == 2 )
1331-
{
1332-
QString myValue = myPixelMap[ mRasterLayer->grayBandName()];
1333-
if ( myValue != tr( "out of extent" ) )
1334-
{
1335-
tableTransparency->insertRow( tableTransparency->rowCount() );
1336-
tableTransparency->setItem( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, new QTableWidgetItem( "100.0" ) );
1337-
tableTransparency->setItem( tableTransparency->rowCount() - 1, 0, new QTableWidgetItem( myValue ) );
1338-
}
1339-
}
1340-
else
1334+
1335+
QList<int> bands = renderer->usesBands();
1336+
tableTransparency->insertRow( tableTransparency->rowCount() );
1337+
tableTransparency->setItem( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, new QTableWidgetItem( "100.0" ) );
1338+
1339+
for ( int i = 0; i < bands.size(); ++i )
13411340
{
1342-
QString myValue = myPixelMap[ mRasterLayer->redBandName()];
1343-
if ( myValue != tr( "out of extent" ) )
1341+
QMap< int, QString >::const_iterator pixelResult = myPixelMap.find( bands.at( i ) );
1342+
if ( pixelResult != myPixelMap.constEnd() )
13441343
{
1345-
tableTransparency->insertRow( tableTransparency->rowCount() );
1346-
tableTransparency->setItem( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, new QTableWidgetItem( "100.0" ) );
1347-
tableTransparency->setItem( tableTransparency->rowCount() - 1, 0, new QTableWidgetItem( myValue ) );
1348-
tableTransparency->setItem( tableTransparency->rowCount() - 1, 1, new QTableWidgetItem( myPixelMap[ mRasterLayer->greenBandName()] ) );
1349-
tableTransparency->setItem( tableTransparency->rowCount() - 1, 2, new QTableWidgetItem( myPixelMap[ mRasterLayer->blueBandName()] ) );
1344+
QString value = pixelResult.value();
1345+
if ( value != tr( "out of extent" ) )
1346+
{
1347+
tableTransparency->setItem( tableTransparency->rowCount() - 1, i, new QTableWidgetItem( value ) );
1348+
}
13501349
}
13511350
}
13521351
}
1353-
#else
1354-
Q_UNUSED( canvasPoint );
1355-
#endif //0
1352+
1353+
delete renderer;
13561354
}
13571355

13581356
void QgsRasterLayerProperties::sliderTransparency_valueChanged( int theValue )

‎src/core/qgsrasterdataprovider.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ bool QgsRasterDataProvider::identify( const QgsPoint& thePoint, QMap<QString, QS
184184
return false;
185185
}
186186

187+
bool QgsRasterDataProvider::identify( const QgsPoint & point, QMap<int, QString>& results )
188+
{
189+
Q_UNUSED( point );
190+
results.clear();
191+
return false;
192+
}
193+
187194
QString QgsRasterDataProvider::lastErrorFormat()
188195
{
189196
return "text/plain";

‎src/core/qgsrasterdataprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider
390390
/** \brief Identify raster value(s) found on the point position */
391391
virtual bool identify( const QgsPoint & point, QMap<QString, QString>& results );
392392

393+
virtual bool identify( const QgsPoint & point, QMap<int, QString>& results );
394+
393395
/**
394396
* \brief Identify details from a server (e.g. WMS) from the last screen update
395397
*

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,18 @@ bool QgsRasterLayer::identify( const QgsPoint& thePoint, QMap<QString, QString>&
914914
return ( mDataProvider->identify( thePoint, theResults ) );
915915
}
916916

917+
bool QgsRasterLayer::identify( const QgsPoint & point, QMap<int, QString>& results )
918+
{
919+
if ( !mDataProvider )
920+
{
921+
return false;
922+
}
923+
924+
results.clear();
925+
return mDataProvider->identify( point, results );
926+
}
927+
928+
917929
/**
918930
* @note The arbitraryness of the returned document is enforced by WMS standards up to at least v1.3.0
919931
*

‎src/core/raster/qgsrasterlayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
457457
/** \brief Identify raster value(s) found on the point position */
458458
bool identify( const QgsPoint & point, QMap<QString, QString>& results );
459459

460+
/** \brief Identify raster value(s) found on the point position */
461+
bool identify( const QgsPoint & point, QMap<int, QString>& results );
462+
460463
/** \brief Identify arbitrary details from the WMS server found on the point position */
461464
QString identifyAsText( const QgsPoint & point );
462465

‎src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,67 @@ int QgsGdalProvider::yBlockSize() const
11171117
int QgsGdalProvider::xSize() const { return mWidth; }
11181118
int QgsGdalProvider::ySize() const { return mHeight; }
11191119

1120+
bool QgsGdalProvider::identify( const QgsPoint & point, QMap<int, QString>& results )
1121+
{
1122+
// QgsDebugMsg( "Entered" );
1123+
if ( !mExtent.contains( point ) )
1124+
{
1125+
// Outside the raster
1126+
for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
1127+
{
1128+
results[ i ] = tr( "out of extent" );
1129+
}
1130+
}
1131+
else
1132+
{
1133+
double x = point.x();
1134+
double y = point.y();
1135+
1136+
// Calculate the row / column where the point falls
1137+
double xres = ( mExtent.xMaximum() - mExtent.xMinimum() ) / mWidth;
1138+
double yres = ( mExtent.yMaximum() - mExtent.yMinimum() ) / mHeight;
1139+
1140+
// Offset, not the cell index -> flor
1141+
int col = ( int ) floor(( x - mExtent.xMinimum() ) / xres );
1142+
int row = ( int ) floor(( mExtent.yMaximum() - y ) / yres );
1143+
1144+
// QgsDebugMsg( "row = " + QString::number( row ) + " col = " + QString::number( col ) );
1145+
1146+
for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
1147+
{
1148+
GDALRasterBandH gdalBand = GDALGetRasterBand( mGdalDataset, i );
1149+
double value;
1150+
1151+
CPLErr err = GDALRasterIO( gdalBand, GF_Read, col, row, 1, 1,
1152+
&value, 1, 1, GDT_Float64, 0, 0 );
1153+
1154+
if ( err != CPLE_None )
1155+
{
1156+
QgsLogger::warning( "RasterIO error: " + QString::fromUtf8( CPLGetLastErrorMsg() ) );
1157+
}
1158+
1159+
//double value = readValue( data, type, 0 );
1160+
// QgsDebugMsg( QString( "value=%1" ).arg( value ) );
1161+
QString v;
1162+
1163+
if ( mValidNoDataValue && ( fabs( value - mNoDataValue[i-1] ) <= TINY_VALUE || value != value ) )
1164+
{
1165+
v = tr( "null (no data)" );
1166+
}
1167+
else
1168+
{
1169+
v.setNum( value );
1170+
}
1171+
1172+
results[ i ] = v;
1173+
1174+
//CPLFree( data );
1175+
}
1176+
}
1177+
1178+
return true;
1179+
}
1180+
11201181
bool QgsGdalProvider::identify( const QgsPoint& thePoint, QMap<QString, QString>& theResults )
11211182
{
11221183
// QgsDebugMsg( "Entered" );

‎src/providers/gdal/qgsgdalprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class QgsGdalProvider : public QgsRasterDataProvider
135135
/** \brief Identify raster value(s) found on the point position */
136136
bool identify( const QgsPoint & point, QMap<QString, QString>& results );
137137

138+
bool identify( const QgsPoint & point, QMap<int, QString>& results );
139+
138140
/**
139141
* \brief Identify details from a GDAL layer from the last screen update
140142
*

0 commit comments

Comments
 (0)
Please sign in to comment.