Skip to content

Commit 7a8849e

Browse files
author
ersts
committedAug 9, 2010
-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/qgis@14031 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

6 files changed

+75
-1
lines changed

6 files changed

+75
-1
lines changed
 

‎python/core/qgsrasterlayer.sip

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ public:
417417
/** \brief Mutator for setting the maximum value for contrast enhancement */
418418
void setMaximumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );
419419

420+
/** \brief Sets the minimum and maximum values for the band(s) currently being displayed using the only pixel values from the last/current extent */
421+
void setMinimumMaximumUsingLastExtent();
422+
420423
/** \brief Mutator for setting the minimum value for contrast enhancement */
421424
void setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );
422425

‎src/app/legend/qgslegend.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,35 @@ void QgsLegend::legendLayerZoomNative()
16241624
}
16251625
}
16261626

1627+
void QgsLegend::legendLayerStretchUsingCurrentExtent()
1628+
{
1629+
//find current Layer
1630+
QgsLegendLayer* currentLayer = dynamic_cast<QgsLegendLayer *>( currentItem() );
1631+
if ( !currentLayer )
1632+
return;
1633+
1634+
QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( currentLayer->layer() );
1635+
if ( layer )
1636+
{
1637+
if ( layer->drawingStyle() == QgsRasterLayer::SingleBandPseudoColor )
1638+
{
1639+
layer->setDrawingStyle( QgsRasterLayer::SingleBandGray );
1640+
}
1641+
else if ( layer->drawingStyle() == QgsRasterLayer::MultiBandSingleBandPseudoColor )
1642+
{
1643+
layer->setDrawingStyle( QgsRasterLayer::MultiBandSingleGandGray );
1644+
}
1645+
1646+
if ( layer->contrastEnhancementAlgorithmAsString() == "NoEnhancement" )
1647+
{
1648+
layer->setContrastEnhancementAlgorithm( "StretchToMinimumMaximum" );
1649+
}
1650+
1651+
layer->setMinimumMaximumUsingLastExtent();
1652+
mMapCanvas->refresh();
1653+
}
1654+
}
1655+
16271656
void QgsLegend::readProject( const QDomDocument & doc )
16281657
{
16291658
QDomNodeList nodes = doc.elementsByTagName( "legend" );

‎src/app/legend/qgslegend.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,14 @@ class QgsLegend : public QTreeWidget
253253
legend layer files*/
254254
void legendLayerZoom();
255255

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

260+
/**Stretches the raster layer, if stretching is active, based on the min and max of the current extent.
261+
Only workds on raster layers*/
262+
void legendLayerStretchUsingCurrentExtent();
263+
260264
/**Updates check states when the map canvas layer set is changed */
261265
void refreshCheckStates();
262266
protected:

‎src/app/legend/qgslegendlayer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
395395
if ( lyr->type() == QgsMapLayer::RasterLayer )
396396
{
397397
theMenu.addAction( tr( "&Zoom to best scale (100%)" ), legend(), SLOT( legendLayerZoomNative() ) );
398+
399+
QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( lyr );
400+
if( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette )
401+
{
402+
theMenu.addAction( tr( "&Stretch using current extent" ), legend(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
403+
}
398404
}
399405

400406
// show in overview

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,6 +3490,35 @@ void QgsRasterLayer::setMaximumValue( QString theBand, double theValue, bool the
34903490
}
34913491
}
34923492

3493+
void QgsRasterLayer::setMinimumMaximumUsingLastExtent()
3494+
{
3495+
double myMinMax[2];
3496+
if ( rasterType() == QgsRasterLayer::GrayOrUndefined || drawingStyle() == QgsRasterLayer::SingleBandGray || drawingStyle() == QgsRasterLayer::MultiBandSingleGandGray )
3497+
{
3498+
computeMinimumMaximumFromLastExtent( grayBandName(), myMinMax );
3499+
setMinimumValue( grayBandName(), myMinMax[0] );
3500+
setMaximumValue( grayBandName(), myMinMax[1] );
3501+
3502+
setUserDefinedGrayMinimumMaximum( true );
3503+
}
3504+
else if ( rasterType() == QgsRasterLayer::Multiband )
3505+
{
3506+
computeMinimumMaximumFromLastExtent( redBandName(), myMinMax );
3507+
setMinimumValue( redBandName(), myMinMax[0], false );
3508+
setMaximumValue( redBandName(), myMinMax[1], false );
3509+
3510+
computeMinimumMaximumFromLastExtent( greenBandName(), myMinMax );
3511+
setMinimumValue( greenBandName(), myMinMax[0], false );
3512+
setMaximumValue( greenBandName(), myMinMax[1], false );
3513+
3514+
computeMinimumMaximumFromLastExtent( blueBandName(), myMinMax );
3515+
setMinimumValue( blueBandName(), myMinMax[0], false );
3516+
setMaximumValue( blueBandName(), myMinMax[1], false );
3517+
3518+
setUserDefinedRGBMinimumMaximum( true );
3519+
}
3520+
}
3521+
34933522
void QgsRasterLayer::setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag )
34943523
{
34953524
if ( 0 < theBand && theBand <= bandCount() )

‎src/core/raster/qgsrasterlayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
583583
/** \brief Mutator for setting the maximum value for contrast enhancement */
584584
void setMaximumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );
585585

586+
/** \brief Sets the minimum and maximum values for the band(s) currently being displayed using the only pixel values from the last/current extent */
587+
void setMinimumMaximumUsingLastExtent();
588+
586589
/** \brief Mutator for setting the minimum value for contrast enhancement */
587590
void setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );
588591

0 commit comments

Comments
 (0)
Please sign in to comment.