26QString QgsCellStatisticsAlgorithmBase::group()
const
28 return QObject::tr(
"Raster analysis" );
31QString QgsCellStatisticsAlgorithmBase::groupId()
const
33 return QStringLiteral(
"rasteranalysis" );
36void QgsCellStatisticsAlgorithmBase::initAlgorithm(
const QVariantMap & )
40 addSpecificAlgorithmParams();
46 auto output_nodata_parameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"OUTPUT_NODATA_VALUE" ), QObject::tr(
"Output NoData value" ),
Qgis::ProcessingNumberParameterType::Double, -9999,
false );
48 addParameter( output_nodata_parameter.release() );
50 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
51 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
53 addParameter( createOptsParam.release() );
66 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"REFERENCE_LAYER" ), context );
67 if ( !referenceLayer )
70 mIgnoreNoData = parameterAsBool( parameters, QStringLiteral(
"IGNORE_NODATA" ), context );
71 mNoDataValue = parameterAsDouble( parameters, QStringLiteral(
"OUTPUT_NODATA_VALUE" ), context );
72 mCrs = referenceLayer->
crs();
75 mLayerWidth = referenceLayer->
width();
76 mLayerHeight = referenceLayer->
height();
77 mExtent = referenceLayer->
extent();
79 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"INPUT" ), context );
80 QList<QgsRasterLayer *> rasterLayers;
81 rasterLayers.reserve( layers.count() );
90 QgsRasterAnalysisUtils::RasterLogicInput input;
94 input.interface = input.sourceDataProvider.get();
96 if ( layer->
crs() != mCrs )
98 input.projector = std::make_unique<QgsRasterProjector>();
99 input.projector->setInput( input.sourceDataProvider.get() );
101 input.interface = input.projector.get();
103 mInputs.emplace_back( std::move( input ) );
110 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : std::as_const( mInputs ) )
112 for (
int band : i.bands )
115 if (
static_cast<int>( mDataType ) <
static_cast<int>( inputDataType ) )
116 mDataType = inputDataType;
120 prepareSpecificAlgorithmParameters( parameters, context, feedback );
128 const QString createOptions = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context ).trimmed();
129 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
130 QFileInfo fi( outputFile );
133 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
134 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
135 if ( !createOptions.isEmpty() )
137 writer->setCreateOptions( createOptions.split(
'|' ) );
139 writer->setOutputFormat( outputFormat );
140 mOutputRasterDataProvider.reset( writer->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
141 if ( !mOutputRasterDataProvider )
143 if ( !mOutputRasterDataProvider->isValid() )
146 mOutputRasterDataProvider->setNoDataValue( 1, mNoDataValue );
150 processRasterStack( feedback );
152 mOutputRasterDataProvider.reset();
155 outputs.insert( QStringLiteral(
"EXTENT" ), mExtent.toString() );
156 outputs.insert( QStringLiteral(
"CRS_AUTHID" ), mCrs.authid() );
157 outputs.insert( QStringLiteral(
"WIDTH_IN_PIXELS" ), mLayerWidth );
158 outputs.insert( QStringLiteral(
"HEIGHT_IN_PIXELS" ), mLayerHeight );
159 outputs.insert( QStringLiteral(
"TOTAL_PIXEL_COUNT" ), layerSize );
160 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
169QString QgsCellStatisticsAlgorithm::displayName()
const
171 return QObject::tr(
"Cell statistics" );
174QString QgsCellStatisticsAlgorithm::name()
const
176 return QStringLiteral(
"cellstatistics" );
179QStringList QgsCellStatisticsAlgorithm::tags()
const
181 return QObject::tr(
"cell,pixel,statistic,count,mean,sum,majority,minority,variance,variety,range,median,minimum,maximum" ).split(
',' );
184QString QgsCellStatisticsAlgorithm::shortHelpString()
const
186 return QObject::tr(
"The Cell statistics algorithm computes a value for each cell of the "
187 "output raster. At each cell location, "
188 "the output value is defined as a function of all overlaid cell values of the "
190 "The output raster's extent and resolution is defined by a reference "
191 "raster. The following functions can be applied on the input "
192 "raster cells per output raster cell location:\n"
198 " <li>Standard deviation</li>"
202 " <li>Minority (least frequent value)</li>"
203 " <li>Majority (most frequent value)</li>"
204 " <li>Range (max-min)</li>"
205 " <li>Variety (count of unique values)</li>"
207 "Input raster layers that do not match the cell size of the reference raster layer will be "
208 "resampled using nearest neighbor resampling. The output raster data type will be set to "
209 "the most complex data type present in the input datasets except when using the functions "
210 "Mean, Standard deviation and Variance (data type is always Float32/Float64 depending on input float type) or Count and Variety (data type is always Int32).\n"
211 "<i>Calculation details - general:</i> NoData values in any of the input layers will result in a NoData cell output if the Ignore NoData parameter is not set.\n"
212 "<i>Calculation details - Count:</i> Count will always result in the number of cells without NoData values at the current cell location.\n"
213 "<i>Calculation details - Median:</i> If the number of input layers is even, the median will be calculated as the "
214 "arithmetic mean of the two middle values of the ordered cell input values. In this case the output data type is Float32.\n"
215 "<i>Calculation details - Minority/Majority:</i> If no unique minority or majority could be found, the result is NoData, except all "
216 "input cell values are equal." );
219QgsCellStatisticsAlgorithm *QgsCellStatisticsAlgorithm::createInstance()
const
221 return new QgsCellStatisticsAlgorithm();
224void QgsCellStatisticsAlgorithm::addSpecificAlgorithmParams()
226 QStringList statistics = QStringList();
227 statistics << QObject::tr(
"Sum" )
228 << QObject::tr(
"Count" )
229 << QObject::tr(
"Mean" )
230 << QObject::tr(
"Median" )
231 << QObject::tr(
"Standard deviation" )
232 << QObject::tr(
"Variance" )
233 << QObject::tr(
"Minimum" )
234 << QObject::tr(
"Maximum" )
235 << QObject::tr(
"Minority" )
236 << QObject::tr(
"Majority" )
237 << QObject::tr(
"Range" )
238 << QObject::tr(
"Variety" );
240 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"STATISTIC" ), QObject::tr(
"Statistic" ), statistics,
false, 0,
false ) );
247 mMethod =
static_cast<QgsRasterAnalysisUtils::CellValueStatisticMethods
>( parameterAsEnum( parameters, QStringLiteral(
"STATISTIC" ), context ) );
251 mMethod == QgsRasterAnalysisUtils::Mean || mMethod == QgsRasterAnalysisUtils::StandardDeviation || mMethod == QgsRasterAnalysisUtils::Variance || ( mMethod == QgsRasterAnalysisUtils::Median && ( mInputs.size() % 2 == 0 ) )
254 if (
static_cast<int>( mDataType ) < 6 )
257 else if ( mMethod == QgsRasterAnalysisUtils::Count || mMethod == QgsRasterAnalysisUtils::Variety )
259 if (
static_cast<int>( mDataType ) > 5 )
269 int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
270 int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
271 int nbBlocks = nbBlocksWidth * nbBlocksHeight;
272 mOutputRasterDataProvider->setEditable(
true );
274 outputIter.startRasterRead( 1, mLayerWidth, mLayerHeight, mExtent );
281 std::unique_ptr<QgsRasterBlock> outputBlock;
282 while ( outputIter.readNextRasterPart( 1, iterCols, iterRows, outputBlock, iterLeft, iterTop, &blockExtent ) )
284 std::vector<std::unique_ptr<QgsRasterBlock>> inputBlocks;
285 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : std::as_const( mInputs ) )
289 for (
int band : i.bands )
293 std::unique_ptr<QgsRasterBlock> b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
294 inputBlocks.emplace_back( std::move( b ) );
298 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
299 for (
int row = 0; row < iterRows; row++ )
304 for (
int col = 0; col < iterCols; col++ )
307 bool noDataInStack =
false;
308 std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
309 int cellValueStackSize = cellValues.size();
311 if ( noDataInStack && !mIgnoreNoData )
315 if ( mMethod == QgsRasterAnalysisUtils::Count )
316 outputBlock->setValue( row, col, cellValueStackSize );
319 outputBlock->setValue( row, col, mNoDataValue );
322 else if ( !noDataInStack || ( mIgnoreNoData && cellValueStackSize > 0 ) )
326 case QgsRasterAnalysisUtils::Sum:
327 result = std::accumulate( cellValues.begin(), cellValues.end(), 0.0 );
329 case QgsRasterAnalysisUtils::Count:
330 result = cellValueStackSize;
332 case QgsRasterAnalysisUtils::Mean:
333 result = QgsRasterAnalysisUtils::meanFromCellValues( cellValues, cellValueStackSize );
335 case QgsRasterAnalysisUtils::Median:
336 result = QgsRasterAnalysisUtils::medianFromCellValues( cellValues, cellValueStackSize );
338 case QgsRasterAnalysisUtils::StandardDeviation:
339 result = QgsRasterAnalysisUtils::stddevFromCellValues( cellValues, cellValueStackSize );
341 case QgsRasterAnalysisUtils::Variance:
342 result = QgsRasterAnalysisUtils::varianceFromCellValues( cellValues, cellValueStackSize );
344 case QgsRasterAnalysisUtils::Minimum:
345 result = QgsRasterAnalysisUtils::minimumFromCellValues( cellValues );
347 case QgsRasterAnalysisUtils::Maximum:
348 result = QgsRasterAnalysisUtils::maximumFromCellValues( cellValues );
350 case QgsRasterAnalysisUtils::Minority:
351 result = QgsRasterAnalysisUtils::minorityFromCellValues( cellValues, mNoDataValue, cellValueStackSize );
353 case QgsRasterAnalysisUtils::Majority:
354 result = QgsRasterAnalysisUtils::majorityFromCellValues( cellValues, mNoDataValue, cellValueStackSize );
356 case QgsRasterAnalysisUtils::Range:
357 result = QgsRasterAnalysisUtils::rangeFromCellValues( cellValues );
359 case QgsRasterAnalysisUtils::Variety:
360 result = QgsRasterAnalysisUtils::varietyFromCellValues( cellValues );
363 outputBlock->setValue( row, col, result );
368 outputBlock->setValue( row, col, mNoDataValue );
372 if ( !mOutputRasterDataProvider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop ) )
374 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( mOutputRasterDataProvider->error().summary() ) );
377 mOutputRasterDataProvider->setEditable(
false );
383QString QgsCellStatisticsPercentileAlgorithm::displayName()
const
385 return QObject::tr(
"Cell stack percentile" );
388QString QgsCellStatisticsPercentileAlgorithm::name()
const
390 return QStringLiteral(
"cellstackpercentile" );
393QStringList QgsCellStatisticsPercentileAlgorithm::tags()
const
395 return QObject::tr(
"cell,pixel,statistic,percentile,quantile,quartile" ).split(
',' );
398QString QgsCellStatisticsPercentileAlgorithm::shortHelpString()
const
400 return QObject::tr(
"The Cell stack percentile algorithm returns the cell-wise percentile value of a stack of rasters "
401 "and writes the results to an output raster. The percentile to return is determined by the percentile input value (ranges between 0 and 1). "
402 "At each cell location, the specified percentile is obtained using the respective value from "
403 "the stack of all overlaid and sorted cell values of the input rasters.\n\n"
404 "There are three methods for percentile calculation:"
406 " <li>Nearest rank</li>"
407 " <li>Inclusive linear interpolation (PERCENTILE.INC)</li>"
408 " <li>Exclusive linear interpolation (PERCENTILE.EXC)</li>"
410 "While the output value can stay the same for the nearest rank method (obtains the value that is nearest to the "
411 "specified percentile), the linear interpolation method return unique values for different percentiles. Both interpolation "
412 "methods follow their counterpart methods implemented by LibreOffice or Microsoft Excel. \n\n"
413 "The output raster's extent and resolution is defined by a reference "
414 "raster. If the input raster layers that do not match the cell size of the reference raster layer will be "
415 "resampled using nearest neighbor resampling. NoData values in any of the input layers will result in a NoData cell output if the Ignore NoData parameter is not set. "
416 "The output raster data type will be set to the most complex data type present in the input datasets. " );
419QgsCellStatisticsPercentileAlgorithm *QgsCellStatisticsPercentileAlgorithm::createInstance()
const
421 return new QgsCellStatisticsPercentileAlgorithm();
424void QgsCellStatisticsPercentileAlgorithm::addSpecificAlgorithmParams()
426 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Nearest rank" ) << QObject::tr(
"Inclusive linear interpolation (PERCENTILE.INC)" ) << QObject::tr(
"Exclusive linear interpolation (PERCENTILE.EXC)" ),
false, 0,
false ) );
433 mMethod =
static_cast<QgsRasterAnalysisUtils::CellValuePercentileMethods
>( parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context ) );
434 mPercentile = parameterAsDouble( parameters, QStringLiteral(
"PERCENTILE" ), context );
438 if ( mMethod != QgsRasterAnalysisUtils::CellValuePercentileMethods::NearestRankPercentile &&
static_cast<int>( mDataType ) < 6 )
448 int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
449 int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
450 int nbBlocks = nbBlocksWidth * nbBlocksHeight;
451 mOutputRasterDataProvider->setEditable(
true );
453 outputIter.startRasterRead( 1, mLayerWidth, mLayerHeight, mExtent );
460 std::unique_ptr<QgsRasterBlock> outputBlock;
461 while ( outputIter.readNextRasterPart( 1, iterCols, iterRows, outputBlock, iterLeft, iterTop, &blockExtent ) )
463 std::vector<std::unique_ptr<QgsRasterBlock>> inputBlocks;
464 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : std::as_const( mInputs ) )
468 for (
int band : i.bands )
472 std::unique_ptr<QgsRasterBlock> b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
473 inputBlocks.emplace_back( std::move( b ) );
477 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
478 for (
int row = 0; row < iterRows; row++ )
483 for (
int col = 0; col < iterCols; col++ )
486 bool noDataInStack =
false;
487 std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
488 int cellValueStackSize = cellValues.size();
490 if ( noDataInStack && !mIgnoreNoData )
492 outputBlock->setValue( row, col, mNoDataValue );
494 else if ( !noDataInStack || ( mIgnoreNoData && cellValueStackSize > 0 ) )
498 case QgsRasterAnalysisUtils::NearestRankPercentile:
499 result = QgsRasterAnalysisUtils::nearestRankPercentile( cellValues, cellValueStackSize, mPercentile );
501 case QgsRasterAnalysisUtils::InterpolatedPercentileInc:
502 result = QgsRasterAnalysisUtils::interpolatedPercentileInc( cellValues, cellValueStackSize, mPercentile );
504 case QgsRasterAnalysisUtils::InterpolatedPercentileExc:
505 result = QgsRasterAnalysisUtils::interpolatedPercentileExc( cellValues, cellValueStackSize, mPercentile, mNoDataValue );
508 outputBlock->setValue( row, col, result );
513 outputBlock->setValue( row, col, mNoDataValue );
517 if ( !mOutputRasterDataProvider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop ) )
519 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( mOutputRasterDataProvider->error().summary() ) );
522 mOutputRasterDataProvider->setEditable(
false );
528QString QgsCellStatisticsPercentRankFromValueAlgorithm::displayName()
const
530 return QObject::tr(
"Cell stack percent rank from value" );
533QString QgsCellStatisticsPercentRankFromValueAlgorithm::name()
const
535 return QStringLiteral(
"cellstackpercentrankfromvalue" );
538QStringList QgsCellStatisticsPercentRankFromValueAlgorithm::tags()
const
540 return QObject::tr(
"cell,pixel,statistic,percentrank,rank,percent,value" ).split(
',' );
543QString QgsCellStatisticsPercentRankFromValueAlgorithm::shortHelpString()
const
545 return QObject::tr(
"The Cell stack percentrank from value algorithm calculates the cell-wise percentrank value of a stack of rasters based on a single input value "
546 "and writes them to an output raster.\n\n"
547 "At each cell location, the specified value is ranked among the respective values in the stack of all overlaid and sorted cell values from the input rasters. "
548 "For values outside of the stack value distribution, the algorithm returns NoData because the value cannot be ranked among the cell values.\n\n"
549 "There are two methods for percentile calculation:"
551 " <li>Inclusive linearly interpolated percent rank (PERCENTRANK.INC)</li>"
552 " <li>Exclusive linearly interpolated percent rank (PERCENTRANK.EXC)</li>"
554 "The linear interpolation method return the unique percent rank for different values. Both interpolation "
555 "methods follow their counterpart methods implemented by LibreOffice or Microsoft Excel. \n\n"
556 "The output raster's extent and resolution is defined by a reference "
557 "raster. If the input raster layers that do not match the cell size of the reference raster layer will be "
558 "resampled using nearest neighbor resampling. NoData values in any of the input layers will result in a NoData cell output if the Ignore NoData parameter is not set. "
559 "The output raster data type will always be Float32." );
562QgsCellStatisticsPercentRankFromValueAlgorithm *QgsCellStatisticsPercentRankFromValueAlgorithm::createInstance()
const
564 return new QgsCellStatisticsPercentRankFromValueAlgorithm();
567void QgsCellStatisticsPercentRankFromValueAlgorithm::addSpecificAlgorithmParams()
569 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Inclusive linear interpolation (PERCENTRANK.INC)" ) << QObject::tr(
"Exclusive linear interpolation (PERCENTRANK.EXC)" ),
false, 0,
false ) );
576 mMethod =
static_cast<QgsRasterAnalysisUtils::CellValuePercentRankMethods
>( parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context ) );
577 mValue = parameterAsDouble( parameters, QStringLiteral(
"VALUE" ), context );
584void QgsCellStatisticsPercentRankFromValueAlgorithm::processRasterStack(
QgsProcessingFeedback *feedback )
588 int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
589 int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
590 int nbBlocks = nbBlocksWidth * nbBlocksHeight;
591 mOutputRasterDataProvider->setEditable(
true );
593 outputIter.startRasterRead( 1, mLayerWidth, mLayerHeight, mExtent );
600 std::unique_ptr<QgsRasterBlock> outputBlock;
601 while ( outputIter.readNextRasterPart( 1, iterCols, iterRows, outputBlock, iterLeft, iterTop, &blockExtent ) )
603 std::vector<std::unique_ptr<QgsRasterBlock>> inputBlocks;
604 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : std::as_const( mInputs ) )
608 for (
int band : i.bands )
612 std::unique_ptr<QgsRasterBlock> b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
613 inputBlocks.emplace_back( std::move( b ) );
617 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
618 for (
int row = 0; row < iterRows; row++ )
623 for (
int col = 0; col < iterCols; col++ )
626 bool noDataInStack =
false;
627 std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
628 int cellValueStackSize = cellValues.size();
630 if ( noDataInStack && !mIgnoreNoData )
632 outputBlock->setValue( row, col, mNoDataValue );
634 else if ( !noDataInStack || ( mIgnoreNoData && cellValueStackSize > 0 ) )
638 case QgsRasterAnalysisUtils::InterpolatedPercentRankInc:
639 result = QgsRasterAnalysisUtils::interpolatedPercentRankInc( cellValues, cellValueStackSize, mValue, mNoDataValue );
641 case QgsRasterAnalysisUtils::InterpolatedPercentRankExc:
642 result = QgsRasterAnalysisUtils::interpolatedPercentRankExc( cellValues, cellValueStackSize, mValue, mNoDataValue );
645 outputBlock->setValue( row, col, result );
650 outputBlock->setValue( row, col, mNoDataValue );
654 if ( !mOutputRasterDataProvider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop ) )
656 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( mOutputRasterDataProvider->error().summary() ) );
659 mOutputRasterDataProvider->setEditable(
false );
666QString QgsCellStatisticsPercentRankFromRasterAlgorithm::displayName()
const
668 return QObject::tr(
"Cell stack percentrank from raster layer" );
671QString QgsCellStatisticsPercentRankFromRasterAlgorithm::name()
const
673 return QStringLiteral(
"cellstackpercentrankfromrasterlayer" );
676QStringList QgsCellStatisticsPercentRankFromRasterAlgorithm::tags()
const
678 return QObject::tr(
"cell,pixel,statistic,percentrank,rank,percent,value,raster" ).split(
',' );
681QString QgsCellStatisticsPercentRankFromRasterAlgorithm::shortHelpString()
const
683 return QObject::tr(
"The Cell stack percentrank from raster layer algorithm calculates the cell-wise percentrank value of a stack of rasters based on an input value raster "
684 "and writes them to an output raster.\n\n"
685 "At each cell location, the current value of the value raster is used ranked among the respective values in the stack of all overlaid and sorted cell values of the input rasters. "
686 "For values outside of the the stack value distribution, the algorithm returns NoData because the value cannot be ranked among the cell values.\n\n"
687 "There are two methods for percentile calculation:"
689 " <li>Inclusive linearly interpolated percent rank (PERCENTRANK.INC)</li>"
690 " <li>Exclusive linearly interpolated percent rank (PERCENTRANK.EXC)</li>"
692 "The linear interpolation method return the unique percent rank for different values. Both interpolation "
693 "methods follow their counterpart methods implemented by LibreOffice or Microsoft Excel. \n\n"
694 "The output raster's extent and resolution is defined by a reference "
695 "raster. If the input raster layers that do not match the cell size of the reference raster layer will be "
696 "resampled using nearest neighbor resampling. NoData values in any of the input layers will result in a NoData cell output if the Ignore NoData parameter is not set. "
697 "The output raster data type will always be Float32." );
700QgsCellStatisticsPercentRankFromRasterAlgorithm *QgsCellStatisticsPercentRankFromRasterAlgorithm::createInstance()
const
702 return new QgsCellStatisticsPercentRankFromRasterAlgorithm();
705void QgsCellStatisticsPercentRankFromRasterAlgorithm::addSpecificAlgorithmParams()
708 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"VALUE_RASTER_BAND" ), QObject::tr(
"Value raster band" ), 1, QStringLiteral(
"VALUE_LAYER" ) ) );
709 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Inclusive linear interpolation (PERCENTRANK.INC)" ) << QObject::tr(
"Exclusive linear interpolation (PERCENTRANK.EXC)" ),
false, 0,
false ) );
715 mMethod =
static_cast<QgsRasterAnalysisUtils::CellValuePercentRankMethods
>( parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context ) );
717 QgsRasterLayer *inputValueRaster = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_VALUE_RASTER" ), context );
718 if ( !inputValueRaster )
723 mValueRasterBand = parameterAsInt( parameters, QStringLiteral(
"VALUE_RASTER_BAND" ), context );
730void QgsCellStatisticsPercentRankFromRasterAlgorithm::processRasterStack(
QgsProcessingFeedback *feedback )
734 int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
735 int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
736 int nbBlocks = nbBlocksWidth * nbBlocksHeight;
737 mOutputRasterDataProvider->setEditable(
true );
739 outputIter.startRasterRead( 1, mLayerWidth, mLayerHeight, mExtent );
746 std::unique_ptr<QgsRasterBlock> outputBlock;
747 while ( outputIter.readNextRasterPart( 1, iterCols, iterRows, outputBlock, iterLeft, iterTop, &blockExtent ) )
749 std::unique_ptr<QgsRasterBlock> valueBlock( mValueRasterInterface->block( mValueRasterBand, blockExtent, iterCols, iterRows ) );
751 std::vector<std::unique_ptr<QgsRasterBlock>> inputBlocks;
752 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : std::as_const( mInputs ) )
756 for (
int band : i.bands )
760 std::unique_ptr<QgsRasterBlock> b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
761 inputBlocks.emplace_back( std::move( b ) );
765 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
766 for (
int row = 0; row < iterRows; row++ )
771 for (
int col = 0; col < iterCols; col++ )
773 bool percentRankValueIsNoData =
false;
774 double percentRankValue = valueBlock->valueAndNoData( row, col, percentRankValueIsNoData );
777 bool noDataInStack =
false;
778 std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
779 int cellValueStackSize = cellValues.size();
781 if ( noDataInStack && !mIgnoreNoData && !percentRankValueIsNoData )
783 outputBlock->setValue( row, col, mNoDataValue );
785 else if ( !noDataInStack || ( !percentRankValueIsNoData && mIgnoreNoData && cellValueStackSize > 0 ) )
789 case QgsRasterAnalysisUtils::InterpolatedPercentRankInc:
790 result = QgsRasterAnalysisUtils::interpolatedPercentRankInc( cellValues, cellValueStackSize, percentRankValue, mNoDataValue );
792 case QgsRasterAnalysisUtils::InterpolatedPercentRankExc:
793 result = QgsRasterAnalysisUtils::interpolatedPercentRankExc( cellValues, cellValueStackSize, percentRankValue, mNoDataValue );
796 outputBlock->setValue( row, col, result );
801 outputBlock->setValue( row, col, mNoDataValue );
805 if ( !mOutputRasterDataProvider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop ) )
807 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( mOutputRasterDataProvider->error().summary() ) );
810 mOutputRasterDataProvider->setEditable(
false );
DataType
Raster data types.
@ Float32
Thirty two bit floating point (float)
@ Byte
Eight bit unsigned integer (quint8)
@ Int32
Thirty two bit signed integer (qint32)
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Base class for all map layer types.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A numeric output for processing algorithms.
A string output for processing algorithms.
A raster band parameter for Processing algorithms.
A boolean parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
virtual bool sourceHasNoDataValue(int bandNo) const
Returns true if source band has no data value.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Iterator for sequentially processing raster cells.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
Default maximum tile width.
static const int DEFAULT_MAXIMUM_TILE_HEIGHT
Default maximum tile height.
Represents a raster layer.
int height() const
Returns the height of the (unclipped) raster.
double rasterUnitsPerPixelX() const
Returns the number of raster units per each raster pixel in X axis.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
double rasterUnitsPerPixelY() const
Returns the number of raster units per each raster pixel in Y axis.
int width() const
Returns the width of the (unclipped) raster.
A rectangle specified with double values.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...