29QString QgsRasterFrequencyByComparisonOperatorBase::group()
const
31 return QObject::tr(
"Raster analysis" );
34QString QgsRasterFrequencyByComparisonOperatorBase::groupId()
const
36 return QStringLiteral(
"rasteranalysis" );
39void QgsRasterFrequencyByComparisonOperatorBase::initAlgorithm(
const QVariantMap & )
42 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"INPUT_VALUE_RASTER_BAND" ), QObject::tr(
"Value raster band" ), 1, QStringLiteral(
"INPUT_VALUE_RASTER" ) ) );
46 auto output_nodata_parameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"OUTPUT_NODATA_VALUE" ), QObject::tr(
"Output NoData value" ),
Qgis::ProcessingNumberParameterType::Double, -9999,
true );
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() );
56 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"OCCURRENCE_COUNT" ), QObject::tr(
"Count of value occurrences" ) ) );
57 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"FOUND_LOCATIONS_COUNT" ), QObject::tr(
"Count of cells with equal value occurrences" ) ) );
58 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"MEAN_FREQUENCY_PER_LOCATION" ), QObject::tr(
"Mean frequency at valid cell locations" ) ) );
68 QgsRasterLayer *inputValueRaster = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_VALUE_RASTER" ), context );
69 if ( !inputValueRaster )
72 mInputValueRasterBand = parameterAsInt( parameters, QStringLiteral(
"INPUT_VALUE_RASTER_BAND" ), context );
73 mIgnoreNoData = parameterAsBool( parameters, QStringLiteral(
"IGNORE_NODATA" ), context );
76 mNoDataValue = parameterAsDouble( parameters, QStringLiteral(
"OUTPUT_NODATA_VALUE" ), context );
77 mCrs = inputValueRaster->
crs();
80 mLayerWidth = inputValueRaster->
width();
81 mLayerHeight = inputValueRaster->
height();
82 mExtent = inputValueRaster->
extent();
84 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"INPUT_RASTERS" ), context );
85 QList<QgsRasterLayer *> rasterLayers;
86 rasterLayers.reserve( layers.count() );
95 QgsRasterAnalysisUtils::RasterLogicInput input;
99 input.interface = input.sourceDataProvider.get();
101 if ( layer->
crs() != mCrs )
103 input.projector = std::make_unique<QgsRasterProjector>();
104 input.projector->setInput( input.sourceDataProvider.get() );
106 input.interface = input.projector.get();
108 mInputs.emplace_back( std::move( input ) );
117 const QString createOptions = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context ).trimmed();
118 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
119 const QFileInfo fi( outputFile );
122 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
123 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
124 if ( !createOptions.isEmpty() )
126 writer->setCreateOptions( createOptions.split(
'|' ) );
128 writer->setOutputFormat( outputFormat );
129 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster(
Qgis::DataType::Int32, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
132 if ( !provider->isValid() )
135 provider->setNoDataValue( 1, mNoDataValue );
136 const qgssize layerSize =
static_cast<qgssize>( mLayerWidth ) *
static_cast<qgssize>( mLayerHeight );
140 const int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
141 const int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
142 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
143 provider->setEditable(
true );
146 iter.startRasterRead( mInputValueRasterBand, mLayerWidth, mLayerHeight, mExtent );
153 unsigned long long occurrenceCount = 0;
154 unsigned long long noDataLocationsCount = 0;
155 std::unique_ptr<QgsRasterBlock> inputBlock;
156 while ( iter.readNextRasterPart( 1, iterCols, iterRows, inputBlock, iterLeft, iterTop, &blockExtent ) )
158 std::vector<std::unique_ptr<QgsRasterBlock>> inputBlocks;
159 for (
const QgsRasterAnalysisUtils::RasterLogicInput &i : mInputs )
163 for (
const int band : i.bands )
167 std::unique_ptr<QgsRasterBlock> b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
168 inputBlocks.emplace_back( std::move( b ) );
173 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
174 for (
int row = 0; row < iterRows; row++ )
179 for (
int col = 0; col < iterCols; col++ )
181 bool valueRasterCellIsNoData =
false;
182 const double value = inputBlock->valueAndNoData( row, col, valueRasterCellIsNoData );
184 if ( valueRasterCellIsNoData && !mIgnoreNoData )
188 outputBlock->setValue( row, col, mNoDataValue );
189 noDataLocationsCount++;
193 bool noDataInStack =
false;
194 const std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
196 if ( noDataInStack && !mIgnoreNoData )
198 outputBlock->setValue( row, col, mNoDataValue );
199 noDataLocationsCount++;
203 const int frequency = applyComparisonOperator( value, cellValues );
204 outputBlock->setValue( row, col, frequency );
205 occurrenceCount += frequency;
210 if ( !provider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop ) )
212 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
215 provider->setEditable(
false );
217 const unsigned long long foundLocationsCount = layerSize - noDataLocationsCount;
218 const double meanEqualCountPerValidLocation =
static_cast<double>( occurrenceCount ) /
static_cast<double>( foundLocationsCount * mInputs.size() );
221 outputs.insert( QStringLiteral(
"OCCURRENCE_COUNT" ), occurrenceCount );
222 outputs.insert( QStringLiteral(
"FOUND_LOCATIONS_COUNT" ), foundLocationsCount );
223 outputs.insert( QStringLiteral(
"MEAN_FREQUENCY_PER_LOCATION" ), meanEqualCountPerValidLocation );
224 outputs.insert( QStringLiteral(
"EXTENT" ), mExtent.toString() );
225 outputs.insert( QStringLiteral(
"CRS_AUTHID" ), mCrs.authid() );
226 outputs.insert( QStringLiteral(
"WIDTH_IN_PIXELS" ), mLayerWidth );
227 outputs.insert( QStringLiteral(
"HEIGHT_IN_PIXELS" ), mLayerHeight );
228 outputs.insert( QStringLiteral(
"TOTAL_PIXEL_COUNT" ), layerSize );
229 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
238QString QgsRasterFrequencyByEqualOperatorAlgorithm::displayName()
const
240 return QObject::tr(
"Equal to frequency" );
243QString QgsRasterFrequencyByEqualOperatorAlgorithm::name()
const
245 return QStringLiteral(
"equaltofrequency" );
248QStringList QgsRasterFrequencyByEqualOperatorAlgorithm::tags()
const
250 return QObject::tr(
"cell,equal,frequency,pixel,stack" ).split(
',' );
253QString QgsRasterFrequencyByEqualOperatorAlgorithm::shortHelpString()
const
255 return QObject::tr(
"The Equal to frequency algorithm evaluates on a cell-by-cell basis the frequency "
256 "(number of times) the values of an input stack of rasters are equal "
257 "to the value of a value raster. \n "
258 "If multiband rasters are used in the data raster stack, the algorithm will always "
259 "perform the analysis on the first band of the rasters - use GDAL to use other bands in the analysis. "
260 "The input value layer serves as reference layer for the sample layers. "
261 "Any NoData cells in the value raster or the data layer stack will result in a NoData cell "
262 "in the output raster if the ignore NoData parameter is not checked. "
263 "The output NoData value can be set manually. The output rasters extent and resolution "
264 "is defined by the input raster layer and is always of int32 type." );
267QgsRasterFrequencyByEqualOperatorAlgorithm *QgsRasterFrequencyByEqualOperatorAlgorithm::createInstance()
const
269 return new QgsRasterFrequencyByEqualOperatorAlgorithm();
272int QgsRasterFrequencyByEqualOperatorAlgorithm::applyComparisonOperator(
double searchValue, std::vector<double> cellValueStack )
274 return static_cast<int>( std::count( cellValueStack.begin(), cellValueStack.end(), searchValue ) );
281QString QgsRasterFrequencyByGreaterThanOperatorAlgorithm::displayName()
const
283 return QObject::tr(
"Greater than frequency" );
286QString QgsRasterFrequencyByGreaterThanOperatorAlgorithm::name()
const
288 return QStringLiteral(
"greaterthanfrequency" );
291QStringList QgsRasterFrequencyByGreaterThanOperatorAlgorithm::tags()
const
293 return QObject::tr(
"cell,greater,frequency,pixel,stack" ).split(
',' );
296QString QgsRasterFrequencyByGreaterThanOperatorAlgorithm::shortHelpString()
const
298 return QObject::tr(
"The Greater than frequency algorithm evaluates on a cell-by-cell basis the frequency "
299 "(number of times) the values of an input stack of rasters are greater than "
300 "the value of a value raster. \n "
301 "If multiband rasters are used in the data raster stack, the algorithm will always "
302 "perform the analysis on the first band of the rasters - use GDAL to use other bands in the analysis. "
303 "The input value layer serves as reference layer for the sample layers. "
304 "Any NoData cells in the value raster or the data layer stack will result in a NoData cell "
305 "in the output raster if the ignore NoData parameter is not checked. "
306 "The output NoData value can be set manually. The output rasters extent and resolution "
307 "is defined by the input raster layer and is always of int32 type." );
310QgsRasterFrequencyByGreaterThanOperatorAlgorithm *QgsRasterFrequencyByGreaterThanOperatorAlgorithm::createInstance()
const
312 return new QgsRasterFrequencyByGreaterThanOperatorAlgorithm();
315int QgsRasterFrequencyByGreaterThanOperatorAlgorithm::applyComparisonOperator(
double searchValue, std::vector<double> cellValueStack )
317 return static_cast<int>( std::count_if( cellValueStack.begin(), cellValueStack.end(), [&](
double const &stackValue ) { return stackValue > searchValue; } ) );
324QString QgsRasterFrequencyByLessThanOperatorAlgorithm::displayName()
const
326 return QObject::tr(
"Less than frequency" );
329QString QgsRasterFrequencyByLessThanOperatorAlgorithm::name()
const
331 return QStringLiteral(
"lessthanfrequency" );
334QStringList QgsRasterFrequencyByLessThanOperatorAlgorithm::tags()
const
336 return QObject::tr(
"cell,less,lower,frequency,pixel,stack" ).split(
',' );
339QString QgsRasterFrequencyByLessThanOperatorAlgorithm::shortHelpString()
const
341 return QObject::tr(
"The Less than frequency algorithm evaluates on a cell-by-cell basis the frequency "
342 "(number of times) the values of an input stack of rasters are less than "
343 "the value of a value raster. \n "
344 "If multiband rasters are used in the data raster stack, the algorithm will always "
345 "perform the analysis on the first band of the rasters - use GDAL to use other bands in the analysis. "
346 "The input value layer serves as reference layer for the sample layers. "
347 "Any NoData cells in the value raster or the data layer stack will result in a NoData cell "
348 "in the output raster if the ignore NoData parameter is not checked. "
349 "The output NoData value can be set manually. The output rasters extent and resolution "
350 "is defined by the input raster layer and is always of int32 type." );
353QgsRasterFrequencyByLessThanOperatorAlgorithm *QgsRasterFrequencyByLessThanOperatorAlgorithm::createInstance()
const
355 return new QgsRasterFrequencyByLessThanOperatorAlgorithm();
358int QgsRasterFrequencyByLessThanOperatorAlgorithm::applyComparisonOperator(
double searchValue, std::vector<double> cellValueStack )
360 return static_cast<int>( std::count_if( cellValueStack.begin(), cellValueStack.end(), [&](
double const &stackValue ) { return stackValue < searchValue; } ) );
@ 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.
A parameter for processing algorithms which accepts multiple map layers.
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...