Skip to content

Commit

Permalink
Fix picking of transparent pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jul 1, 2012
1 parent fa292eb commit 3deef8b
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/app/qgsrasterlayerproperties.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1315,44 +1315,42 @@ void QgsRasterLayerProperties::on_pbnRemoveSelectedRow_clicked()


void QgsRasterLayerProperties::pixelSelected( const QgsPoint& canvasPoint ) void QgsRasterLayerProperties::pixelSelected( const QgsPoint& canvasPoint )
{ {
#if 0 //needs to be fixed QgsRasterRenderer* renderer = mRendererWidget->renderer();
//PixelSelectorTool has registered a mouse click on the canvas, so bring the dialog back to the front if ( !renderer )
{
return;
}

raise(); raise();
setModal( true ); setModal( true );
activateWindow(); activateWindow();


//Get the pixel values and add a new entry to the transparency table //Get the pixel values and add a new entry to the transparency table
if ( mMapCanvas && mPixelSelectorTool ) if ( mMapCanvas && mPixelSelectorTool )
{ {
QMap< QString, QString > myPixelMap; QMap< int, QString > myPixelMap;
mMapCanvas->unsetMapTool( mPixelSelectorTool ); mMapCanvas->unsetMapTool( mPixelSelectorTool );
mRasterLayer->identify( mMapCanvas->mapRenderer()->mapToLayerCoordinates( mRasterLayer, canvasPoint ), myPixelMap ); mRasterLayer->identify( mMapCanvas->mapRenderer()->mapToLayerCoordinates( mRasterLayer, canvasPoint ), myPixelMap );
if ( tableTransparency->columnCount() == 2 )
{ QList<int> bands = renderer->usesBands();
QString myValue = myPixelMap[ mRasterLayer->grayBandName()]; tableTransparency->insertRow( tableTransparency->rowCount() );
if ( myValue != tr( "out of extent" ) ) tableTransparency->setItem( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, new QTableWidgetItem( "100.0" ) );
{
tableTransparency->insertRow( tableTransparency->rowCount() ); for ( int i = 0; i < bands.size(); ++i )
tableTransparency->setItem( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, new QTableWidgetItem( "100.0" ) );
tableTransparency->setItem( tableTransparency->rowCount() - 1, 0, new QTableWidgetItem( myValue ) );
}
}
else
{ {
QString myValue = myPixelMap[ mRasterLayer->redBandName()]; QMap< int, QString >::const_iterator pixelResult = myPixelMap.find( bands.at( i ) );
if ( myValue != tr( "out of extent" ) ) if ( pixelResult != myPixelMap.constEnd() )
{ {
tableTransparency->insertRow( tableTransparency->rowCount() ); QString value = pixelResult.value();
tableTransparency->setItem( tableTransparency->rowCount() - 1, tableTransparency->columnCount() - 1, new QTableWidgetItem( "100.0" ) ); if ( value != tr( "out of extent" ) )
tableTransparency->setItem( tableTransparency->rowCount() - 1, 0, new QTableWidgetItem( myValue ) ); {
tableTransparency->setItem( tableTransparency->rowCount() - 1, 1, new QTableWidgetItem( myPixelMap[ mRasterLayer->greenBandName()] ) ); tableTransparency->setItem( tableTransparency->rowCount() - 1, i, new QTableWidgetItem( value ) );
tableTransparency->setItem( tableTransparency->rowCount() - 1, 2, new QTableWidgetItem( myPixelMap[ mRasterLayer->blueBandName()] ) ); }
} }
} }
} }
#else
Q_UNUSED( canvasPoint ); delete renderer;
#endif //0
} }


void QgsRasterLayerProperties::sliderTransparency_valueChanged( int theValue ) void QgsRasterLayerProperties::sliderTransparency_valueChanged( int theValue )
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsrasterdataprovider.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ bool QgsRasterDataProvider::identify( const QgsPoint& thePoint, QMap<QString, QS
return false; return false;
} }


bool QgsRasterDataProvider::identify( const QgsPoint & point, QMap<int, QString>& results )
{
Q_UNUSED( point );
results.clear();
return false;
}

QString QgsRasterDataProvider::lastErrorFormat() QString QgsRasterDataProvider::lastErrorFormat()
{ {
return "text/plain"; return "text/plain";
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsrasterdataprovider.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider
/** \brief Identify raster value(s) found on the point position */ /** \brief Identify raster value(s) found on the point position */
virtual bool identify( const QgsPoint & point, QMap<QString, QString>& results ); virtual bool identify( const QgsPoint & point, QMap<QString, QString>& results );


virtual bool identify( const QgsPoint & point, QMap<int, QString>& results );

/** /**
* \brief Identify details from a server (e.g. WMS) from the last screen update * \brief Identify details from a server (e.g. WMS) from the last screen update
* *
Expand Down
12 changes: 12 additions & 0 deletions src/core/raster/qgsrasterlayer.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -914,6 +914,18 @@ bool QgsRasterLayer::identify( const QgsPoint& thePoint, QMap<QString, QString>&
return ( mDataProvider->identify( thePoint, theResults ) ); return ( mDataProvider->identify( thePoint, theResults ) );
} }


bool QgsRasterLayer::identify( const QgsPoint & point, QMap<int, QString>& results )
{
if ( !mDataProvider )
{
return false;
}

results.clear();
return mDataProvider->identify( point, results );
}


/** /**
* @note The arbitraryness of the returned document is enforced by WMS standards up to at least v1.3.0 * @note The arbitraryness of the returned document is enforced by WMS standards up to at least v1.3.0
* *
Expand Down
3 changes: 3 additions & 0 deletions src/core/raster/qgsrasterlayer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** \brief Identify raster value(s) found on the point position */ /** \brief Identify raster value(s) found on the point position */
bool identify( const QgsPoint & point, QMap<QString, QString>& results ); bool identify( const QgsPoint & point, QMap<QString, QString>& results );


/** \brief Identify raster value(s) found on the point position */
bool identify( const QgsPoint & point, QMap<int, QString>& results );

/** \brief Identify arbitrary details from the WMS server found on the point position */ /** \brief Identify arbitrary details from the WMS server found on the point position */
QString identifyAsText( const QgsPoint & point ); QString identifyAsText( const QgsPoint & point );


Expand Down
61 changes: 61 additions & 0 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1117,6 +1117,67 @@ int QgsGdalProvider::yBlockSize() const
int QgsGdalProvider::xSize() const { return mWidth; } int QgsGdalProvider::xSize() const { return mWidth; }
int QgsGdalProvider::ySize() const { return mHeight; } int QgsGdalProvider::ySize() const { return mHeight; }


bool QgsGdalProvider::identify( const QgsPoint & point, QMap<int, QString>& results )
{
// QgsDebugMsg( "Entered" );
if ( !mExtent.contains( point ) )
{
// Outside the raster
for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
{
results[ i ] = tr( "out of extent" );
}
}
else
{
double x = point.x();
double y = point.y();

// Calculate the row / column where the point falls
double xres = ( mExtent.xMaximum() - mExtent.xMinimum() ) / mWidth;
double yres = ( mExtent.yMaximum() - mExtent.yMinimum() ) / mHeight;

// Offset, not the cell index -> flor
int col = ( int ) floor(( x - mExtent.xMinimum() ) / xres );
int row = ( int ) floor(( mExtent.yMaximum() - y ) / yres );

// QgsDebugMsg( "row = " + QString::number( row ) + " col = " + QString::number( col ) );

for ( int i = 1; i <= GDALGetRasterCount( mGdalDataset ); i++ )
{
GDALRasterBandH gdalBand = GDALGetRasterBand( mGdalDataset, i );
double value;

CPLErr err = GDALRasterIO( gdalBand, GF_Read, col, row, 1, 1,
&value, 1, 1, GDT_Float64, 0, 0 );

if ( err != CPLE_None )
{
QgsLogger::warning( "RasterIO error: " + QString::fromUtf8( CPLGetLastErrorMsg() ) );
}

//double value = readValue( data, type, 0 );
// QgsDebugMsg( QString( "value=%1" ).arg( value ) );
QString v;

if ( mValidNoDataValue && ( fabs( value - mNoDataValue[i-1] ) <= TINY_VALUE || value != value ) )
{
v = tr( "null (no data)" );
}
else
{
v.setNum( value );
}

results[ i ] = v;

//CPLFree( data );
}
}

return true;
}

bool QgsGdalProvider::identify( const QgsPoint& thePoint, QMap<QString, QString>& theResults ) bool QgsGdalProvider::identify( const QgsPoint& thePoint, QMap<QString, QString>& theResults )
{ {
// QgsDebugMsg( "Entered" ); // QgsDebugMsg( "Entered" );
Expand Down
2 changes: 2 additions & 0 deletions src/providers/gdal/qgsgdalprovider.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class QgsGdalProvider : public QgsRasterDataProvider
/** \brief Identify raster value(s) found on the point position */ /** \brief Identify raster value(s) found on the point position */
bool identify( const QgsPoint & point, QMap<QString, QString>& results ); bool identify( const QgsPoint & point, QMap<QString, QString>& results );


bool identify( const QgsPoint & point, QMap<int, QString>& results );

/** /**
* \brief Identify details from a GDAL layer from the last screen update * \brief Identify details from a GDAL layer from the last screen update
* *
Expand Down

0 comments on commit 3deef8b

Please sign in to comment.