mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
Throw exception when invalid band number specified
This commit is contained in:
parent
50c28e416d
commit
d7a37fa3eb
@ -33,7 +33,6 @@ from qgis.core import (QgsFeatureRequest,
|
||||
QgsFeatureSink,
|
||||
QgsGeometry,
|
||||
QgsWkbTypes,
|
||||
QgsProcessingParameterBand,
|
||||
QgsPoint,
|
||||
QgsProcessing,
|
||||
QgsProcessingException,
|
||||
|
@ -82,6 +82,11 @@ bool QgsRasterLayerUniqueValuesReportAlgorithm::prepareAlgorithm( const QVariant
|
||||
if ( !layer )
|
||||
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
|
||||
|
||||
mBand = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
|
||||
if ( mBand < 1 || mBand > layer->bandCount() )
|
||||
throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
|
||||
.arg( layer->bandCount() ) );
|
||||
|
||||
mInterface.reset( layer->dataProvider()->clone() );
|
||||
mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
|
||||
mLayerWidth = layer->width();
|
||||
@ -97,7 +102,6 @@ bool QgsRasterLayerUniqueValuesReportAlgorithm::prepareAlgorithm( const QVariant
|
||||
|
||||
QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
|
||||
{
|
||||
int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
|
||||
QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context );
|
||||
|
||||
QString areaUnit = QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::distanceToAreaUnit( mCrs.mapUnits() ) );
|
||||
@ -128,14 +132,14 @@ QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const Q
|
||||
QgsRasterIterator iter( mInterface.get() );
|
||||
iter.setMaximumTileWidth( maxWidth );
|
||||
iter.setMaximumTileHeight( maxHeight );
|
||||
iter.startRasterRead( band, mLayerWidth, mLayerHeight, mExtent );
|
||||
iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
|
||||
|
||||
int iterLeft = 0;
|
||||
int iterTop = 0;
|
||||
int iterCols = 0;
|
||||
int iterRows = 0;
|
||||
std::unique_ptr< QgsRasterBlock > rasterBlock;
|
||||
while ( iter.readNextRasterPart( band, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
|
||||
while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
|
||||
{
|
||||
feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
|
||||
for ( int row = 0; row < iterRows; row++ )
|
||||
@ -182,7 +186,7 @@ QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const Q
|
||||
|
||||
QTextStream out( &file );
|
||||
out << QStringLiteral( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
|
||||
out << QStringLiteral( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Analyzed file" ), mSource, QObject::tr( "band" ) ).arg( band );
|
||||
out << QStringLiteral( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Analyzed file" ), mSource, QObject::tr( "band" ) ).arg( mBand );
|
||||
out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Extent" ), mExtent.toString() );
|
||||
out << QObject::tr( "<p>%1: %2 (%3)</p>\n" ).arg( QObject::tr( "Projection" ), mCrs.description(), mCrs.authid() );
|
||||
out << QObject::tr( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Width in pixels" ) ).arg( mLayerWidth ).arg( QObject::tr( "units per pixel" ) ).arg( mRasterUnitsPerPixelX );
|
||||
|
@ -53,6 +53,7 @@ class QgsRasterLayerUniqueValuesReportAlgorithm : public QgsProcessingAlgorithm
|
||||
|
||||
std::unique_ptr< QgsRasterInterface > mInterface;
|
||||
bool mHasNoDataValue = false;
|
||||
int mBand = 1;
|
||||
int mLayerWidth;
|
||||
int mLayerHeight;
|
||||
QgsRectangle mExtent;
|
||||
|
@ -71,11 +71,15 @@ void QgsReclassifyAlgorithmBase::initAlgorithm( const QVariantMap & )
|
||||
bool QgsReclassifyAlgorithmBase::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
|
||||
{
|
||||
QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_RASTER" ), context );
|
||||
mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context );
|
||||
|
||||
if ( !layer )
|
||||
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) );
|
||||
|
||||
mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context );
|
||||
if ( mBand < 1 || mBand > layer->bandCount() )
|
||||
throw QgsProcessingException( QObject::tr( "Invalid band number for RASTER_BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
|
||||
.arg( layer->bandCount() ) );
|
||||
|
||||
mInterface.reset( layer->dataProvider()->clone() );
|
||||
mExtent = layer->extent();
|
||||
mCrs = layer->crs();
|
||||
|
Loading…
x
Reference in New Issue
Block a user