Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raster paletted renderer: support constant int rasters #51975

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/core/raster/qgspalettedrasterrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,25 @@ QgsPalettedRasterRenderer::ClassData QgsPalettedRasterRenderer::classDataFromRas

const double interval = ( histogram.maximum - histogram.minimum + 1 ) / histogram.binCount;
double currentValue = histogram.minimum;
for ( int idx = 0; idx < histogram.binCount; ++idx )

if ( histogram.valid )
{
const int count = histogram.histogramVector.at( idx );
if ( count > 0 )
for ( int idx = 0; idx < histogram.binCount; ++idx )
{
data << Class( currentValue, QColor(), QLocale().toString( currentValue ) );
numClasses++;
const int count = histogram.histogramVector.at( idx );
if ( count > 0 )
{
data << Class( currentValue, QColor(), QLocale().toString( currentValue ) );
numClasses++;
}
currentValue += interval;
}
currentValue += interval;
}
else if ( histogram.maximum == histogram.minimum && histogram.binCount == 1 ) // Constant raster
{
data << Class( histogram.maximum, QColor(), QLocale().toString( histogram.maximum ) );
}

}

// assign colors from ramp
Expand Down
16 changes: 16 additions & 0 deletions tests/src/core/testqgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgstest.h"
#include <QObject>
#include <QString>
Expand Down Expand Up @@ -92,6 +93,7 @@ class TestQgsRasterLayer : public QgsTest
void palettedRendererNoData();
void palettedRendererRasterAttributeTable();
void palettedRendererNoDataColor();
void palettedRendererConstantInt();
void singleBandGrayRendererNoData();
void singleBandGrayRendererNoDataColor();
void singleBandPseudoRendererNoData();
Expand Down Expand Up @@ -808,6 +810,20 @@ void TestQgsRasterLayer::palettedRendererNoDataColor()
QVERIFY( render( QStringLiteral( "raster_palettedrenderer_nodata_color" ) ) );
}

void TestQgsRasterLayer::palettedRendererConstantInt()
{
char data { 2 };
std::unique_ptr< QgsRasterLayer> rl = std::make_unique< QgsRasterLayer >( QStringLiteral( "MEM:::DATAPOINTER=0x%1,PIXELS=1,LINES=1,BANDS=1,DATATYPE=Byte" )
.arg( reinterpret_cast<quintptr>( &data ),
QT_POINTER_SIZE * 2, 16, QChar( '0' ) ).toStdString().c_str(),
QStringLiteral( "rl" ) );
Q_ASSERT( rl->isValid() );
const auto classData { QgsPalettedRasterRenderer::classDataFromRaster( rl->dataProvider(), 1 ) };
QCOMPARE( classData.size(), 1 );
QCOMPARE( classData.first().value, 2.0 );
rl.reset( );
}

void TestQgsRasterLayer::singleBandGrayRendererNoData()
{
// this test modifies the raster, so work on a copy
Expand Down