Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
-Added a new option to the legend menu that will stretch the current …
…layer using the min and max pixel values of the current extent

-Added a convenience function QgsRasterLayer::setMinimumMaximumUsingLastExtent that will set the min max of the current band or bands being displayed using the pixel values from the last extent

git-svn-id: http://svn.osgeo.org/qgis/trunk@14031 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
ersts committed Aug 9, 2010
1 parent 68e21e7 commit d5e0f6b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions python/core/qgsrasterlayer.sip
Expand Up @@ -417,6 +417,9 @@ public:
/** \brief Mutator for setting the maximum value for contrast enhancement */
void setMaximumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );

/** \brief Sets the minimum and maximum values for the band(s) currently being displayed using the only pixel values from the last/current extent */
void setMinimumMaximumUsingLastExtent();

/** \brief Mutator for setting the minimum value for contrast enhancement */
void setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );

Expand Down
29 changes: 29 additions & 0 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -1624,6 +1624,35 @@ void QgsLegend::legendLayerZoomNative()
}
}

void QgsLegend::legendLayerStretchUsingCurrentExtent()
{
//find current Layer
QgsLegendLayer* currentLayer = dynamic_cast<QgsLegendLayer *>( currentItem() );
if ( !currentLayer )
return;

QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( currentLayer->layer() );
if ( layer )
{
if ( layer->drawingStyle() == QgsRasterLayer::SingleBandPseudoColor )
{
layer->setDrawingStyle( QgsRasterLayer::SingleBandGray );
}
else if ( layer->drawingStyle() == QgsRasterLayer::MultiBandSingleBandPseudoColor )
{
layer->setDrawingStyle( QgsRasterLayer::MultiBandSingleGandGray );
}

if ( layer->contrastEnhancementAlgorithmAsString() == "NoEnhancement" )
{
layer->setContrastEnhancementAlgorithm( "StretchToMinimumMaximum" );
}

layer->setMinimumMaximumUsingLastExtent();
mMapCanvas->refresh();
}
}

void QgsLegend::readProject( const QDomDocument & doc )
{
QDomNodeList nodes = doc.elementsByTagName( "legend" );
Expand Down
6 changes: 5 additions & 1 deletion src/app/legend/qgslegend.h
Expand Up @@ -253,10 +253,14 @@ class QgsLegend : public QTreeWidget
legend layer files*/
void legendLayerZoom();

/***Zooms so that the pixels of the raster layer occupies exactly one screen pixel.
/**Zooms so that the pixels of the raster layer occupies exactly one screen pixel.
Only works on raster layers*/
void legendLayerZoomNative();

/**Stretches the raster layer, if stretching is active, based on the min and max of the current extent.
Only workds on raster layers*/
void legendLayerStretchUsingCurrentExtent();

/**Updates check states when the map canvas layer set is changed */
void refreshCheckStates();
protected:
Expand Down
6 changes: 6 additions & 0 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -395,6 +395,12 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
if ( lyr->type() == QgsMapLayer::RasterLayer )
{
theMenu.addAction( tr( "&Zoom to best scale (100%)" ), legend(), SLOT( legendLayerZoomNative() ) );

QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( lyr );
if( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette )
{
theMenu.addAction( tr( "&Stretch using current extent" ), legend(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
}
}

// show in overview
Expand Down
29 changes: 29 additions & 0 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -3490,6 +3490,35 @@ void QgsRasterLayer::setMaximumValue( QString theBand, double theValue, bool the
}
}

void QgsRasterLayer::setMinimumMaximumUsingLastExtent()
{
double myMinMax[2];
if ( rasterType() == QgsRasterLayer::GrayOrUndefined || drawingStyle() == QgsRasterLayer::SingleBandGray || drawingStyle() == QgsRasterLayer::MultiBandSingleGandGray )
{
computeMinimumMaximumFromLastExtent( grayBandName(), myMinMax );
setMinimumValue( grayBandName(), myMinMax[0] );
setMaximumValue( grayBandName(), myMinMax[1] );

setUserDefinedGrayMinimumMaximum( true );
}
else if ( rasterType() == QgsRasterLayer::Multiband )
{
computeMinimumMaximumFromLastExtent( redBandName(), myMinMax );
setMinimumValue( redBandName(), myMinMax[0], false );
setMaximumValue( redBandName(), myMinMax[1], false );

computeMinimumMaximumFromLastExtent( greenBandName(), myMinMax );
setMinimumValue( greenBandName(), myMinMax[0], false );
setMaximumValue( greenBandName(), myMinMax[1], false );

computeMinimumMaximumFromLastExtent( blueBandName(), myMinMax );
setMinimumValue( blueBandName(), myMinMax[0], false );
setMaximumValue( blueBandName(), myMinMax[1], false );

setUserDefinedRGBMinimumMaximum( true );
}
}

void QgsRasterLayer::setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag )
{
if ( 0 < theBand && theBand <= bandCount() )
Expand Down
3 changes: 3 additions & 0 deletions src/core/raster/qgsrasterlayer.h
Expand Up @@ -583,6 +583,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
/** \brief Mutator for setting the maximum value for contrast enhancement */
void setMaximumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );

/** \brief Sets the minimum and maximum values for the band(s) currently being displayed using the only pixel values from the last/current extent */
void setMinimumMaximumUsingLastExtent();

/** \brief Mutator for setting the minimum value for contrast enhancement */
void setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );

Expand Down

0 comments on commit d5e0f6b

Please sign in to comment.