QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmrasterfrequencybycomparisonoperator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrasterfrequencybycomparisonoperator.cpp
3 ---------------------
4 begin : June 2020
5 copyright : (C) 2020 by Clemens Raffler
6 email : clemens dot raffler at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgsrasterprojector.h"
20#include "qgsrasterfilewriter.h"
22
24
25//
26//QgsRasterFrequencyByComparisonOperatorBase
27//
28
29QString QgsRasterFrequencyByComparisonOperatorBase::group() const
30{
31 return QObject::tr( "Raster analysis" );
32}
33
34QString QgsRasterFrequencyByComparisonOperatorBase::groupId() const
35{
36 return QStringLiteral( "rasteranalysis" );
37}
38
39void QgsRasterFrequencyByComparisonOperatorBase::initAlgorithm( const QVariantMap & )
40{
41 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT_VALUE_RASTER" ), QObject::tr( "Input value raster" ) ) );
42 addParameter( new QgsProcessingParameterBand( QStringLiteral( "INPUT_VALUE_RASTER_BAND" ), QObject::tr( "Value raster band" ), 1, QStringLiteral( "INPUT_VALUE_RASTER" ) ) );
43 addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "INPUT_RASTERS" ), QObject::tr( "Input raster layers" ), Qgis::ProcessingSourceType::Raster ) );
44 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "IGNORE_NODATA" ), QObject::tr( "Ignore NoData values" ), false ) );
45
46 std::unique_ptr<QgsProcessingParameterNumber> output_nodata_parameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "OUTPUT_NODATA_VALUE" ), QObject::tr( "Output NoData value" ), Qgis::ProcessingNumberParameterType::Double, -9999, true );
47 output_nodata_parameter->setFlags( output_nodata_parameter->flags() | Qgis::ProcessingParameterFlag::Advanced );
48 addParameter( output_nodata_parameter.release() );
49
50 std::unique_ptr<QgsProcessingParameterString> 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" ) } } ) } } ) );
52 createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
53 addParameter( createOptsParam.release() );
54
55 addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ) ) );
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" ) ) );
59 addOutput( new QgsProcessingOutputString( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ) ) );
60 addOutput( new QgsProcessingOutputString( QStringLiteral( "CRS_AUTHID" ), QObject::tr( "CRS authority identifier" ) ) );
61 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "WIDTH_IN_PIXELS" ), QObject::tr( "Width in pixels" ) ) );
62 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "HEIGHT_IN_PIXELS" ), QObject::tr( "Height in pixels" ) ) );
63 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "TOTAL_PIXEL_COUNT" ), QObject::tr( "Total pixel count" ) ) );
64}
65
66bool QgsRasterFrequencyByComparisonOperatorBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
67{
68 QgsRasterLayer *inputValueRaster = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_VALUE_RASTER" ), context );
69 if ( !inputValueRaster )
70 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_VALUE_RASTER" ) ) );
71
72 mInputValueRasterBand = parameterAsInt( parameters, QStringLiteral( "INPUT_VALUE_RASTER_BAND" ), context );
73 mIgnoreNoData = parameterAsBool( parameters, QStringLiteral( "IGNORE_NODATA" ), context );
74
75 mInputValueRasterInterface.reset( inputValueRaster->dataProvider()->clone() );
76 mNoDataValue = parameterAsDouble( parameters, QStringLiteral( "OUTPUT_NODATA_VALUE" ), context );
77 mCrs = inputValueRaster->crs();
78 mRasterUnitsPerPixelX = inputValueRaster->rasterUnitsPerPixelX();
79 mRasterUnitsPerPixelY = inputValueRaster->rasterUnitsPerPixelY();
80 mLayerWidth = inputValueRaster->width();
81 mLayerHeight = inputValueRaster->height();
82 mExtent = inputValueRaster->extent();
83
84 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "INPUT_RASTERS" ), context );
85 QList<QgsRasterLayer *> rasterLayers;
86 rasterLayers.reserve( layers.count() );
87 for ( QgsMapLayer *l : layers )
88 {
89 if ( feedback->isCanceled() )
90 break; //in case some slow data sources are loaded
91
92 if ( l->type() == Qgis::LayerType::Raster )
93 {
94 QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( l );
95 QgsRasterAnalysisUtils::RasterLogicInput input;
96 const int band = 1; //could be made dynamic
97 input.hasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
98 input.sourceDataProvider.reset( layer->dataProvider()->clone() );
99 input.interface = input.sourceDataProvider.get();
100 // add projector if necessary
101 if ( layer->crs() != mCrs )
102 {
103 input.projector = std::make_unique<QgsRasterProjector>();
104 input.projector->setInput( input.sourceDataProvider.get() );
105 input.projector->setCrs( layer->crs(), mCrs, context.transformContext() );
106 input.interface = input.projector.get();
107 }
108 mInputs.emplace_back( std::move( input ) );
109 }
110 }
111
112 return true;
113}
114
115QVariantMap QgsRasterFrequencyByComparisonOperatorBase::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
116{
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 );
120 const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
121
122 std::unique_ptr<QgsRasterFileWriter> writer = std::make_unique<QgsRasterFileWriter>( outputFile );
123 writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
124 if ( !createOptions.isEmpty() )
125 {
126 writer->setCreateOptions( createOptions.split( '|' ) );
127 }
128 writer->setOutputFormat( outputFormat );
129 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( Qgis::DataType::Int32, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
130 if ( !provider )
131 throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( outputFile ) );
132 if ( !provider->isValid() )
133 throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( outputFile, provider->error().message( QgsErrorMessage::Text ) ) );
134
135 provider->setNoDataValue( 1, mNoDataValue );
136 const qgssize layerSize = static_cast<qgssize>( mLayerWidth ) * static_cast<qgssize>( mLayerHeight );
137
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 );
144
145 QgsRasterIterator iter( mInputValueRasterInterface.get() );
146 iter.startRasterRead( mInputValueRasterBand, mLayerWidth, mLayerHeight, mExtent );
147 int iterLeft = 0;
148 int iterTop = 0;
149 int iterCols = 0;
150 int iterRows = 0;
151 QgsRectangle blockExtent;
152
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 ) )
157 {
158 std::vector<std::unique_ptr<QgsRasterBlock>> inputBlocks;
159 for ( const QgsRasterAnalysisUtils::RasterLogicInput &i : mInputs )
160 {
161 if ( feedback->isCanceled() )
162 break; //in case some slow data sources are loaded
163 for ( const int band : i.bands )
164 {
165 if ( feedback->isCanceled() )
166 break; //in case some slow data sources are loaded
167 std::unique_ptr<QgsRasterBlock> b( i.interface->block( band, blockExtent, iterCols, iterRows ) );
168 inputBlocks.emplace_back( std::move( b ) );
169 }
170 }
171
172 std::unique_ptr<QgsRasterBlock> outputBlock = std::make_unique<QgsRasterBlock>( Qgis::DataType::Int32, iterCols, iterRows );
173 feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
174 for ( int row = 0; row < iterRows; row++ )
175 {
176 if ( feedback->isCanceled() )
177 break;
178
179 for ( int col = 0; col < iterCols; col++ )
180 {
181 bool valueRasterCellIsNoData = false;
182 const double value = inputBlock->valueAndNoData( row, col, valueRasterCellIsNoData );
183
184 if ( valueRasterCellIsNoData && !mIgnoreNoData )
185 {
186 //output cell will always be NoData if NoData occurs in valueRaster or cellValueStack and NoData is not ignored
187 //this saves unnecessary iterations on the cellValueStack
188 outputBlock->setValue( row, col, mNoDataValue );
189 noDataLocationsCount++;
190 }
191 else
192 {
193 bool noDataInStack = false;
194 const std::vector<double> cellValues = QgsRasterAnalysisUtils::getCellValuesFromBlockStack( inputBlocks, row, col, noDataInStack );
195
196 if ( noDataInStack && !mIgnoreNoData )
197 {
198 outputBlock->setValue( row, col, mNoDataValue );
199 noDataLocationsCount++;
200 }
201 else
202 {
203 const int frequency = applyComparisonOperator( value, cellValues );
204 outputBlock->setValue( row, col, frequency );
205 occurrenceCount += frequency;
206 }
207 }
208 }
209 }
210 provider->writeBlock( outputBlock.get(), 1, iterLeft, iterTop );
211 }
212 provider->setEditable( false );
213
214 const unsigned long long foundLocationsCount = layerSize - noDataLocationsCount;
215 const double meanEqualCountPerValidLocation = static_cast<double>( occurrenceCount ) / static_cast<double>( foundLocationsCount * mInputs.size() );
216
217 QVariantMap outputs;
218 outputs.insert( QStringLiteral( "OCCURRENCE_COUNT" ), occurrenceCount );
219 outputs.insert( QStringLiteral( "FOUND_LOCATIONS_COUNT" ), foundLocationsCount );
220 outputs.insert( QStringLiteral( "MEAN_FREQUENCY_PER_LOCATION" ), meanEqualCountPerValidLocation );
221 outputs.insert( QStringLiteral( "EXTENT" ), mExtent.toString() );
222 outputs.insert( QStringLiteral( "CRS_AUTHID" ), mCrs.authid() );
223 outputs.insert( QStringLiteral( "WIDTH_IN_PIXELS" ), mLayerWidth );
224 outputs.insert( QStringLiteral( "HEIGHT_IN_PIXELS" ), mLayerHeight );
225 outputs.insert( QStringLiteral( "TOTAL_PIXEL_COUNT" ), layerSize );
226 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
227
228 return outputs;
229}
230
231//
232// QgsRasterFrequencyByEqualOperatorAlgorithm
233//
234
235QString QgsRasterFrequencyByEqualOperatorAlgorithm::displayName() const
236{
237 return QObject::tr( "Equal to frequency" );
238}
239
240QString QgsRasterFrequencyByEqualOperatorAlgorithm::name() const
241{
242 return QStringLiteral( "equaltofrequency" );
243}
244
245QStringList QgsRasterFrequencyByEqualOperatorAlgorithm::tags() const
246{
247 return QObject::tr( "cell,equal,frequency,pixel,stack" ).split( ',' );
248}
249
250QString QgsRasterFrequencyByEqualOperatorAlgorithm::shortHelpString() const
251{
252 return QObject::tr( "The Equal to frequency algorithm evaluates on a cell-by-cell basis the frequency "
253 "(number of times) the values of an input stack of rasters are equal "
254 "to the value of a value raster. \n "
255 "If multiband rasters are used in the data raster stack, the algorithm will always "
256 "perform the analysis on the first band of the rasters - use GDAL to use other bands in the analysis. "
257 "The input value layer serves as reference layer for the sample layers. "
258 "Any NoData cells in the value raster or the data layer stack will result in a NoData cell "
259 "in the output raster if the ignore NoData parameter is not checked. "
260 "The output NoData value can be set manually. The output rasters extent and resolution "
261 "is defined by the input raster layer and is always of int32 type." );
262}
263
264QgsRasterFrequencyByEqualOperatorAlgorithm *QgsRasterFrequencyByEqualOperatorAlgorithm::createInstance() const
265{
266 return new QgsRasterFrequencyByEqualOperatorAlgorithm();
267}
268
269int QgsRasterFrequencyByEqualOperatorAlgorithm::applyComparisonOperator( double searchValue, std::vector<double> cellValueStack )
270{
271 return static_cast<int>( std::count( cellValueStack.begin(), cellValueStack.end(), searchValue ) );
272}
273
274//
275// QgsRasterFrequencyByGreaterThanOperatorAlgorithm
276//
277
278QString QgsRasterFrequencyByGreaterThanOperatorAlgorithm::displayName() const
279{
280 return QObject::tr( "Greater than frequency" );
281}
282
283QString QgsRasterFrequencyByGreaterThanOperatorAlgorithm::name() const
284{
285 return QStringLiteral( "greaterthanfrequency" );
286}
287
288QStringList QgsRasterFrequencyByGreaterThanOperatorAlgorithm::tags() const
289{
290 return QObject::tr( "cell,greater,frequency,pixel,stack" ).split( ',' );
291}
292
293QString QgsRasterFrequencyByGreaterThanOperatorAlgorithm::shortHelpString() const
294{
295 return QObject::tr( "The Greater than frequency algorithm evaluates on a cell-by-cell basis the frequency "
296 "(number of times) the values of an input stack of rasters are greater than "
297 "the value of a value raster. \n "
298 "If multiband rasters are used in the data raster stack, the algorithm will always "
299 "perform the analysis on the first band of the rasters - use GDAL to use other bands in the analysis. "
300 "The input value layer serves as reference layer for the sample layers. "
301 "Any NoData cells in the value raster or the data layer stack will result in a NoData cell "
302 "in the output raster if the ignore NoData parameter is not checked. "
303 "The output NoData value can be set manually. The output rasters extent and resolution "
304 "is defined by the input raster layer and is always of int32 type." );
305}
306
307QgsRasterFrequencyByGreaterThanOperatorAlgorithm *QgsRasterFrequencyByGreaterThanOperatorAlgorithm::createInstance() const
308{
309 return new QgsRasterFrequencyByGreaterThanOperatorAlgorithm();
310}
311
312int QgsRasterFrequencyByGreaterThanOperatorAlgorithm::applyComparisonOperator( double searchValue, std::vector<double> cellValueStack )
313{
314 return static_cast<int>( std::count_if( cellValueStack.begin(), cellValueStack.end(), [&]( double const &stackValue ) { return stackValue > searchValue; } ) );
315}
316
317//
318// QgsRasterFrequencyByLessThanOperatorAlgorithm
319//
320
321QString QgsRasterFrequencyByLessThanOperatorAlgorithm::displayName() const
322{
323 return QObject::tr( "Less than frequency" );
324}
325
326QString QgsRasterFrequencyByLessThanOperatorAlgorithm::name() const
327{
328 return QStringLiteral( "lessthanfrequency" );
329}
330
331QStringList QgsRasterFrequencyByLessThanOperatorAlgorithm::tags() const
332{
333 return QObject::tr( "cell,less,lower,frequency,pixel,stack" ).split( ',' );
334}
335
336QString QgsRasterFrequencyByLessThanOperatorAlgorithm::shortHelpString() const
337{
338 return QObject::tr( "The Less than frequency algorithm evaluates on a cell-by-cell basis the frequency "
339 "(number of times) the values of an input stack of rasters are less than "
340 "the value of a value raster. \n "
341 "If multiband rasters are used in the data raster stack, the algorithm will always "
342 "perform the analysis on the first band of the rasters - use GDAL to use other bands in the analysis. "
343 "The input value layer serves as reference layer for the sample layers. "
344 "Any NoData cells in the value raster or the data layer stack will result in a NoData cell "
345 "in the output raster if the ignore NoData parameter is not checked. "
346 "The output NoData value can be set manually. The output rasters extent and resolution "
347 "is defined by the input raster layer and is always of int32 type." );
348}
349
350QgsRasterFrequencyByLessThanOperatorAlgorithm *QgsRasterFrequencyByLessThanOperatorAlgorithm::createInstance() const
351{
352 return new QgsRasterFrequencyByLessThanOperatorAlgorithm();
353}
354
355int QgsRasterFrequencyByLessThanOperatorAlgorithm::applyComparisonOperator( double searchValue, std::vector<double> cellValueStack )
356{
357 return static_cast<int>( std::count_if( cellValueStack.begin(), cellValueStack.end(), [&]( double const &stackValue ) { return stackValue < searchValue; } ) );
358}
359
@ Int32
Thirty two bit signed integer (qint32)
@ Raster
Raster layer.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Base class for all map layer types.
Definition qgsmaplayer.h:76
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
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...
Definition qgis.h:6614