QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmrandomraster.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrandomraster.cpp
3 ---------------------
4 begin : May 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 "qgsrasterfilewriter.h"
20#include "qgsstringutils.h"
21#include "random"
22#include "limits"
23
25
26//
27// QgsRandomRasterAlgorithmBase
28//
29QString QgsRandomRasterAlgorithmBase::group() const
30{
31 return QObject::tr( "Raster creation" );
32}
33
34QString QgsRandomRasterAlgorithmBase::groupId() const
35{
36 return QStringLiteral( "rastercreation" );
37}
38
39void QgsRandomRasterAlgorithmBase::initAlgorithm( const QVariantMap & )
40{
41 addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Desired extent" ) ) );
42 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "ProjectCrs" ) ) );
43 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "PIXEL_SIZE" ), QObject::tr( "Pixel size" ), Qgis::ProcessingNumberParameterType::Double, 1, false, 0 ) );
44
45 //add specific parameters
46 addAlgorithmParams();
47
48 // backwards compatibility parameter
49 // TODO QGIS 4: remove parameter and related logic
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" ) } } ) } } ) );
52 createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Hidden );
53 addParameter( createOptsParam.release() );
54
55 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral( "CREATION_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
56 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral( "widget_wrapper" ), QVariantMap( { { QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) } } ) } } ) );
57 creationOptsParam->setFlags( creationOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
58 addParameter( creationOptsParam.release() );
59
60 addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output raster" ) ) );
61}
62
63bool QgsRandomRasterAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
64{
65 Q_UNUSED( feedback );
66 mCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
67 mExtent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, mCrs );
68 mPixelSize = parameterAsDouble( parameters, QStringLiteral( "PIXEL_SIZE" ), context );
69
70 if ( mPixelSize <= 0 )
71 {
72 throw QgsProcessingException( QObject::tr( "Pixel size must be greater than 0." ) );
73 }
74
75 return true;
76}
77
78QVariantMap QgsRandomRasterAlgorithmBase::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
79{
80 const int typeId = parameterAsInt( parameters, QStringLiteral( "OUTPUT_TYPE" ), context );
81 //prepare specific parameters
82 mRasterDataType = getRasterDataType( typeId );
83 prepareRandomParameters( parameters, context );
84
85 std::random_device rd {};
86 std::mt19937 mersenneTwister { rd() };
87
88 QString creationOptions = parameterAsString( parameters, QStringLiteral( "CREATION_OPTIONS" ), context ).trimmed();
89 // handle backwards compatibility parameter CREATE_OPTIONS
90 const QString optionsString = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context );
91 if ( !optionsString.isEmpty() )
92 creationOptions = optionsString;
93
94 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
95 const QFileInfo fi( outputFile );
96 const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
97
98 // round up width and height to the nearest integer as GDAL does (e.g. in gdal_rasterize)
99 // see https://github.com/qgis/QGIS/issues/43547
100 const int rows = static_cast<int>( 0.5 + mExtent.height() / mPixelSize );
101 const int cols = static_cast<int>( 0.5 + mExtent.width() / mPixelSize );
102
103 //build new raster extent based on number of columns and cellsize
104 //this prevents output cellsize being calculated too small
105 const QgsRectangle rasterExtent = QgsRectangle( mExtent.xMinimum(), mExtent.yMaximum() - ( rows * mPixelSize ), mExtent.xMinimum() + ( cols * mPixelSize ), mExtent.yMaximum() );
106
107 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
108 writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
109 if ( !creationOptions.isEmpty() )
110 {
111 writer->setCreationOptions( creationOptions.split( '|' ) );
112 }
113
114 writer->setOutputFormat( outputFormat );
115 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mRasterDataType, cols, rows, rasterExtent, mCrs ) );
116 if ( !provider )
117 throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( outputFile ) );
118 if ( !provider->isValid() )
119 throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( outputFile, provider->error().message( QgsErrorMessage::Text ) ) );
120
121 const double step = rows > 0 ? 100.0 / rows : 1;
122
123 for ( int row = 0; row < rows; row++ )
124 {
125 if ( feedback->isCanceled() )
126 {
127 break;
128 }
129 //prepare raw data depending on raster data type
130 QgsRasterBlock block( mRasterDataType, cols, 1 );
131 switch ( mRasterDataType )
132 {
134 {
135 std::vector<quint8> byteRow( cols );
136 for ( int col = 0; col < cols; col++ )
137 {
138 byteRow[col] = static_cast<quint8>( generateRandomLongValue( mersenneTwister ) );
139 }
140 block.setData( QByteArray( reinterpret_cast<const char *>( byteRow.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Byte ) * cols ) );
141 break;
142 }
144 {
145 std::vector<qint8> int8Row( cols );
146 for ( int col = 0; col < cols; col++ )
147 {
148 int8Row[col] = static_cast<qint8>( generateRandomLongValue( mersenneTwister ) );
149 }
150 block.setData( QByteArray( reinterpret_cast<const char *>( int8Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Int8 ) * cols ) );
151 break;
152 }
154 {
155 std::vector<qint16> int16Row( cols );
156 for ( int col = 0; col < cols; col++ )
157 {
158 int16Row[col] = static_cast<qint16>( generateRandomLongValue( mersenneTwister ) );
159 }
160 block.setData( QByteArray( reinterpret_cast<const char *>( int16Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Int16 ) * cols ) );
161 break;
162 }
164 {
165 std::vector<quint16> uInt16Row( cols );
166 for ( int col = 0; col < cols; col++ )
167 {
168 uInt16Row[col] = static_cast<quint16>( generateRandomLongValue( mersenneTwister ) );
169 }
170 block.setData( QByteArray( reinterpret_cast<const char *>( uInt16Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::UInt16 ) * cols ) );
171 break;
172 }
174 {
175 std::vector<qint32> int32Row( cols );
176 for ( int col = 0; col < cols; col++ )
177 {
178 int32Row[col] = generateRandomLongValue( mersenneTwister );
179 }
180 block.setData( QByteArray( reinterpret_cast<const char *>( int32Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Int32 ) * cols ) );
181 break;
182 }
184 {
185 std::vector<quint32> uInt32Row( cols );
186 for ( int col = 0; col < cols; col++ )
187 {
188 uInt32Row[col] = static_cast<quint32>( generateRandomLongValue( mersenneTwister ) );
189 }
190 block.setData( QByteArray( reinterpret_cast<const char *>( uInt32Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::UInt32 ) * cols ) );
191 break;
192 }
194 {
195 std::vector<float> float32Row( cols );
196 for ( int col = 0; col < cols; col++ )
197 {
198 float32Row[col] = static_cast<float>( generateRandomDoubleValue( mersenneTwister ) );
199 }
200 block.setData( QByteArray( reinterpret_cast<const char *>( float32Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Float32 ) * cols ) );
201 break;
202 }
204 {
205 std::vector<double> float64Row( cols );
206 for ( int col = 0; col < cols; col++ )
207 {
208 float64Row[col] = generateRandomDoubleValue( mersenneTwister );
209 }
210 block.setData( QByteArray( reinterpret_cast<const char *>( float64Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Float64 ) * cols ) );
211 break;
212 }
213 default:
214 break;
215 }
216 if ( !provider->writeBlock( &block, 1, 0, row ) )
217 {
218 throw QgsProcessingException( QObject::tr( "Could not write raster block: %1" ).arg( provider->error().summary() ) );
219 }
220 feedback->setProgress( row * step );
221 }
222
223 QVariantMap outputs;
224 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
225 return outputs;
226}
227
228//
229//QgsRandomUniformRasterAlgorithm
230//
231QString QgsRandomUniformRasterAlgorithm::name() const
232{
233 return QStringLiteral( "createrandomuniformrasterlayer" );
234}
235
236QString QgsRandomUniformRasterAlgorithm::displayName() const
237{
238 return QObject::tr( "Create random raster layer (uniform distribution)" );
239}
240
241QStringList QgsRandomUniformRasterAlgorithm::tags() const
242{
243 return QObject::tr( "raster,create,random" ).split( ',' );
244}
245
246QString QgsRandomUniformRasterAlgorithm::shortHelpString() const
247{
248 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
249 "filled with random values.\n"
250 "By default, the values will range between the minimum and "
251 "maximum value of the specified output raster type. This can "
252 "be overridden by using the advanced parameters for lower and "
253 "upper bound value. If the bounds have the same value or both "
254 "are zero (default) the algorithm will create random values in "
255 "the full value range of the chosen raster data type. "
256 "Choosing bounds outside the acceptable range of the output "
257 "raster type will abort the algorithm." );
258}
259
260QString QgsRandomUniformRasterAlgorithm::shortDescription() const
261{
262 return QObject::tr( "Generates a raster layer for a given extent and cell size "
263 "filled with random values." );
264}
265
266QgsRandomUniformRasterAlgorithm *QgsRandomUniformRasterAlgorithm::createInstance() const
267{
268 return new QgsRandomUniformRasterAlgorithm();
269}
270
271void QgsRandomUniformRasterAlgorithm::addAlgorithmParams()
272{
273 QStringList rasterDataTypes = QStringList();
274 rasterDataTypes << QStringLiteral( "Byte" )
275 << QStringLiteral( "Integer16" )
276 << QStringLiteral( "Unsigned Integer16" )
277 << QStringLiteral( "Integer32" )
278 << QStringLiteral( "Unsigned Integer32" )
279 << QStringLiteral( "Float32" )
280 << QStringLiteral( "Float64" );
281
282 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 5, false );
283 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
284 addParameter( rasterTypeParameter.release() );
285
286 auto lowerBoundParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "LOWER_BOUND" ), QStringLiteral( "Lower bound for random number range" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
287 lowerBoundParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
288 addParameter( lowerBoundParameter.release() );
289
290 auto upperBoundParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "UPPER_BOUND" ), QStringLiteral( "Upper bound for random number range" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
291 upperBoundParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
292 addParameter( upperBoundParameter.release() );
293}
294
295Qgis::DataType QgsRandomUniformRasterAlgorithm::getRasterDataType( int typeId )
296{
297 switch ( typeId )
298 {
299 case 0:
301 case 1:
303 case 2:
305 case 3:
307 case 4:
309 case 5:
311 case 6:
313 default:
315 }
316}
317
318bool QgsRandomUniformRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
319{
320 mRandomUpperBound = parameterAsDouble( parameters, QStringLiteral( "UPPER_BOUND" ), context );
321 mRandomLowerBound = parameterAsDouble( parameters, QStringLiteral( "LOWER_BOUND" ), context );
322
323 if ( mRandomLowerBound > mRandomUpperBound )
324 throw QgsProcessingException( QObject::tr( "The chosen lower bound for random number range is greater than the upper bound. The lower bound value must be smaller than the upper bound value." ) );
325
326 const int typeId = parameterAsInt( parameters, QStringLiteral( "OUTPUT_TYPE" ), context );
327 const Qgis::DataType rasterDataType = getRasterDataType( typeId );
328
329 switch ( rasterDataType )
330 {
332 if ( mRandomLowerBound < std::numeric_limits<quint8>::min() || mRandomUpperBound > std::numeric_limits<quint8>::max() )
333 throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." ).arg( std::numeric_limits<quint8>::min() ).arg( std::numeric_limits<quint8>::max() ).arg( QLatin1String( "Byte" ) ) );
334 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
335 {
336 //if parameters unset (=both are 0 or equal) --> use the whole value range
337 mRandomUpperBound = std::numeric_limits<quint8>::max();
338 mRandomLowerBound = std::numeric_limits<quint8>::min();
339 }
340 break;
342 if ( mRandomLowerBound < std::numeric_limits<qint8>::min() || mRandomUpperBound > std::numeric_limits<qint8>::max() )
343 throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." ).arg( std::numeric_limits<qint8>::min() ).arg( std::numeric_limits<qint8>::max() ).arg( QLatin1String( "Int8" ) ) );
344 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
345 {
346 //if parameters unset (=both are 0 or equal) --> use the whole value range
347 mRandomUpperBound = std::numeric_limits<qint8>::max();
348 mRandomLowerBound = std::numeric_limits<qint8>::min();
349 }
350 break;
352 if ( mRandomLowerBound < std::numeric_limits<qint16>::min() || mRandomUpperBound > std::numeric_limits<qint16>::max() )
353 throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept values between %1 and %2. Please choose other bounds for random values." ).arg( std::numeric_limits<qint16>::min() ).arg( std::numeric_limits<qint16>::max() ).arg( QLatin1String( "Integer16" ) ) );
354 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
355 {
356 mRandomUpperBound = std::numeric_limits<qint16>::max();
357 mRandomLowerBound = std::numeric_limits<qint16>::min();
358 }
359 break;
361 if ( mRandomLowerBound < std::numeric_limits<quint16>::min() || mRandomUpperBound > std::numeric_limits<quint16>::max() )
362 throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." ).arg( std::numeric_limits<quint16>::min() ).arg( std::numeric_limits<quint16>::max() ).arg( QLatin1String( "Unsigned Integer16" ) ) );
363 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
364 {
365 mRandomUpperBound = std::numeric_limits<quint16>::max();
366 mRandomLowerBound = std::numeric_limits<quint16>::min();
367 }
368 break;
370 if ( mRandomLowerBound < std::numeric_limits<qint32>::min() || mRandomUpperBound > std::numeric_limits<qint32>::max() )
371 throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept values between %1 and %2. Please choose other bounds for random values." ).arg( std::numeric_limits<qint32>::min() ).arg( std::numeric_limits<qint32>::max() ).arg( QLatin1String( "Integer32" ) ) );
372 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
373 {
374 mRandomUpperBound = std::numeric_limits<qint32>::max();
375 mRandomLowerBound = std::numeric_limits<qint32>::min();
376 }
377 break;
379 if ( mRandomLowerBound < std::numeric_limits<quint32>::min() || mRandomUpperBound > std::numeric_limits<quint32>::max() )
380 throw QgsProcessingException( QObject::tr( "Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." ).arg( std::numeric_limits<quint32>::min() ).arg( std::numeric_limits<quint32>::max() ).arg( QLatin1String( "Unsigned Integer32" ) ) );
381 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
382 {
383 mRandomUpperBound = std::numeric_limits<quint32>::max();
384 mRandomLowerBound = std::numeric_limits<quint32>::min();
385 }
386 break;
388 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
389 {
390 mRandomUpperBound = std::numeric_limits<float>::max();
391 mRandomLowerBound = std::numeric_limits<float>::min();
392 }
393 break;
395 if ( ( qgsDoubleNear( mRandomLowerBound, 0.0 ) && qgsDoubleNear( mRandomUpperBound, 0.0 ) ) || qgsDoubleNear( mRandomUpperBound, mRandomLowerBound ) )
396 {
397 mRandomUpperBound = std::numeric_limits<double>::max();
398 mRandomLowerBound = std::numeric_limits<double>::min();
399 }
400 break;
408 break;
409 }
410
411 mRandomUniformIntDistribution = std::uniform_int_distribution<long>( mRandomLowerBound, mRandomUpperBound );
412 mRandomUniformDoubleDistribution = std::uniform_real_distribution<double>( mRandomLowerBound, mRandomUpperBound );
413
414 return true;
415}
416
417long QgsRandomUniformRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
418{
419 return mRandomUniformIntDistribution( mersenneTwister );
420}
421
422double QgsRandomUniformRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
423{
424 return mRandomUniformDoubleDistribution( mersenneTwister );
425}
426
427//
428// QgsRandomBinomialRasterAlgorithm
429//
430QString QgsRandomBinomialRasterAlgorithm::name() const
431{
432 return QStringLiteral( "createrandombinomialrasterlayer" );
433}
434
435QString QgsRandomBinomialRasterAlgorithm::displayName() const
436{
437 return QObject::tr( "Create random raster layer (binomial distribution)" );
438}
439
440QStringList QgsRandomBinomialRasterAlgorithm::tags() const
441{
442 return QObject::tr( "raster,create,binomial,random" ).split( ',' );
443}
444
445QString QgsRandomBinomialRasterAlgorithm::shortHelpString() const
446{
447 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
448 "filled with binomially distributed random values.\n"
449 "By default, the values will be chosen given an N of 10 and a probability of 0.5. "
450 "This can be overridden by using the advanced parameter for N and probability. "
451 "The raster data type is set to Integer types (Integer16 by default). "
452 "The binomial distribution random values are defined as positive integer numbers. "
453 "A floating point raster will represent a cast of integer values "
454 "to floating point." );
455}
456
457QString QgsRandomBinomialRasterAlgorithm::shortDescription() const
458{
459 return QObject::tr( "Generates a raster layer for a given extent and cell size "
460 "filled with binomially distributed random values." );
461}
462
463QgsRandomBinomialRasterAlgorithm *QgsRandomBinomialRasterAlgorithm::createInstance() const
464{
465 return new QgsRandomBinomialRasterAlgorithm();
466}
467
468
469void QgsRandomBinomialRasterAlgorithm::addAlgorithmParams()
470{
471 QStringList rasterDataTypes = QStringList();
472 rasterDataTypes << QStringLiteral( "Integer16" )
473 << QStringLiteral( "Unsigned Integer16" )
474 << QStringLiteral( "Integer32" )
475 << QStringLiteral( "Unsigned Integer32" )
476 << QStringLiteral( "Float32" )
477 << QStringLiteral( "Float64" );
478
479 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 0, false );
480 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
481 addParameter( rasterTypeParameter.release() );
482
483 auto nParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "N" ), QStringLiteral( "N" ), Qgis::ProcessingNumberParameterType::Integer, 10, true, 0 );
484 nParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
485 addParameter( nParameter.release() );
486
487 auto probabilityParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "PROBABILITY" ), QStringLiteral( "Probability" ), Qgis::ProcessingNumberParameterType::Double, 0.5, true, 0 );
488 probabilityParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
489 addParameter( probabilityParameter.release() );
490}
491
492Qgis::DataType QgsRandomBinomialRasterAlgorithm::getRasterDataType( int typeId )
493{
494 switch ( typeId )
495 {
496 case 0:
498 case 1:
500 case 2:
502 case 3:
504 case 4:
506 case 5:
508 default:
510 }
511}
512
513bool QgsRandomBinomialRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
514{
515 const int n = parameterAsInt( parameters, QStringLiteral( "N" ), context );
516 const double probability = parameterAsDouble( parameters, QStringLiteral( "PROBABILITY" ), context );
517 mRandombinomialDistribution = std::binomial_distribution<long>( n, probability );
518 return true;
519}
520
521long QgsRandomBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
522{
523 return mRandombinomialDistribution( mersenneTwister );
524}
525
526double QgsRandomBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
527{
528 return static_cast<double>( mRandombinomialDistribution( mersenneTwister ) );
529}
530
531//
532// QgsRandomExponentialRasterAlgorithm
533//
534QString QgsRandomExponentialRasterAlgorithm::name() const
535{
536 return QStringLiteral( "createrandomexponentialrasterlayer" );
537}
538
539QString QgsRandomExponentialRasterAlgorithm::displayName() const
540{
541 return QObject::tr( "Create random raster layer (exponential distribution)" );
542}
543
544QStringList QgsRandomExponentialRasterAlgorithm::tags() const
545{
546 return QObject::tr( "raster,create,random,exponential" ).split( ',' );
547}
548
549QString QgsRandomExponentialRasterAlgorithm::shortHelpString() const
550{
551 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
552 "filled with exponentially distributed random values.\n"
553 "By default, the values will be chosen given a lambda of 1.0. "
554 "This can be overridden by using the advanced parameter for lambda. "
555 "The raster data type is set to Float32 by default as "
556 "the exponential distribution random values are floating point numbers." );
557}
558
559QString QgsRandomExponentialRasterAlgorithm::shortDescription() const
560{
561 return QObject::tr( "Generates a raster layer for a given extent and cell size "
562 "filled with exponentially distributed random values." );
563}
564
565QgsRandomExponentialRasterAlgorithm *QgsRandomExponentialRasterAlgorithm::createInstance() const
566{
567 return new QgsRandomExponentialRasterAlgorithm();
568}
569
570
571void QgsRandomExponentialRasterAlgorithm::addAlgorithmParams()
572{
573 QStringList rasterDataTypes = QStringList();
574 rasterDataTypes << QStringLiteral( "Float32" )
575 << QStringLiteral( "Float64" );
576
577 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 0, false );
578 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
579 addParameter( rasterTypeParameter.release() );
580
581 auto lambdaParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "LAMBDA" ), QStringLiteral( "Lambda" ), Qgis::ProcessingNumberParameterType::Double, 1.0, true, 0.000001 );
582 lambdaParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
583 addParameter( lambdaParameter.release() );
584}
585
586Qgis::DataType QgsRandomExponentialRasterAlgorithm::getRasterDataType( int typeId )
587{
588 switch ( typeId )
589 {
590 case 0:
592 case 1:
594 default:
596 }
597}
598
599bool QgsRandomExponentialRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
600{
601 const double lambda = parameterAsDouble( parameters, QStringLiteral( "LAMBDA" ), context );
602 mRandomExponentialDistribution = std::exponential_distribution<double>( lambda );
603 return true;
604}
605
606long QgsRandomExponentialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
607{
608 return static_cast<long>( mRandomExponentialDistribution( mersenneTwister ) );
609}
610
611double QgsRandomExponentialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
612{
613 return mRandomExponentialDistribution( mersenneTwister );
614}
615
616//
617// QgsRandomGammaRasterAlgorithm
618//
619QString QgsRandomGammaRasterAlgorithm::name() const
620{
621 return QStringLiteral( "createrandomgammarasterlayer" );
622}
623
624QString QgsRandomGammaRasterAlgorithm::displayName() const
625{
626 return QObject::tr( "Create random raster layer (gamma distribution)" );
627}
628
629QStringList QgsRandomGammaRasterAlgorithm::tags() const
630{
631 return QObject::tr( "raster,create,random,gamma" ).split( ',' );
632}
633
634QString QgsRandomGammaRasterAlgorithm::shortHelpString() const
635{
636 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
637 "filled with gamma distributed random values.\n"
638 "By default, the values will be chosen given an alpha and beta value of 1.0. "
639 "This can be overridden by using the advanced parameter for alpha and beta. "
640 "The raster data type is set to Float32 by default as "
641 "the gamma distribution random values are floating point numbers." );
642}
643
644QString QgsRandomGammaRasterAlgorithm::shortDescription() const
645{
646 return QObject::tr( "Generates a raster layer for a given extent and cell size "
647 "filled with gamma distributed random values." );
648}
649
650QgsRandomGammaRasterAlgorithm *QgsRandomGammaRasterAlgorithm::createInstance() const
651{
652 return new QgsRandomGammaRasterAlgorithm();
653}
654
655
656void QgsRandomGammaRasterAlgorithm::addAlgorithmParams()
657{
658 QStringList rasterDataTypes = QStringList();
659 rasterDataTypes << QStringLiteral( "Float32" )
660 << QStringLiteral( "Float64" );
661
662 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 0, false );
663 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
664 addParameter( rasterTypeParameter.release() );
665
666 auto alphaParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "ALPHA" ), QStringLiteral( "Alpha" ), Qgis::ProcessingNumberParameterType::Double, 1.0, true, 0.000001 );
667 alphaParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
668 addParameter( alphaParameter.release() );
669
670 auto betaParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "BETA" ), QStringLiteral( "Beta" ), Qgis::ProcessingNumberParameterType::Double, 1.0, true, 0.000001 );
671 betaParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
672 addParameter( betaParameter.release() );
673}
674
675Qgis::DataType QgsRandomGammaRasterAlgorithm::getRasterDataType( int typeId )
676{
677 switch ( typeId )
678 {
679 case 0:
681 case 1:
683 default:
685 }
686}
687
688bool QgsRandomGammaRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
689{
690 const double alpha = parameterAsDouble( parameters, QStringLiteral( "ALPHA" ), context );
691 const double beta = parameterAsDouble( parameters, QStringLiteral( "BETA" ), context );
692 mRandomGammaDistribution = std::gamma_distribution<double>( alpha, beta );
693 return true;
694}
695
696long QgsRandomGammaRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
697{
698 return static_cast<long>( mRandomGammaDistribution( mersenneTwister ) );
699}
700
701double QgsRandomGammaRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
702{
703 return mRandomGammaDistribution( mersenneTwister );
704}
705
706//
707// QgsRandomGeometricRasterAlgorithm
708//
709QString QgsRandomGeometricRasterAlgorithm::name() const
710{
711 return QStringLiteral( "createrandomgeometricrasterlayer" );
712}
713
714QString QgsRandomGeometricRasterAlgorithm::displayName() const
715{
716 return QObject::tr( "Create random raster layer (geometric distribution)" );
717}
718
719QStringList QgsRandomGeometricRasterAlgorithm::tags() const
720{
721 return QObject::tr( "raster,create,random,geometric" ).split( ',' );
722}
723
724QString QgsRandomGeometricRasterAlgorithm::shortHelpString() const
725{
726 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
727 "filled with geometrically distributed random values.\n"
728 "By default, the values will be chosen given a probability of 0.5. "
729 "This can be overridden by using the advanced parameter for mean "
730 "value. The raster data type is set to Integer types (Integer16 by default). "
731 "The geometric distribution random values are defined as positive integer numbers. "
732 "A floating point raster will represent a cast of "
733 "integer values to floating point." );
734}
735
736QString QgsRandomGeometricRasterAlgorithm::shortDescription() const
737{
738 return QObject::tr( "Generates a raster layer for a given extent and cell size "
739 "filled with geometrically distributed random values." );
740}
741
742QgsRandomGeometricRasterAlgorithm *QgsRandomGeometricRasterAlgorithm::createInstance() const
743{
744 return new QgsRandomGeometricRasterAlgorithm();
745}
746
747
748void QgsRandomGeometricRasterAlgorithm::addAlgorithmParams()
749{
750 QStringList rasterDataTypes = QStringList();
751 rasterDataTypes << QStringLiteral( "Integer16" )
752 << QStringLiteral( "Unsigned Integer16" )
753 << QStringLiteral( "Integer32" )
754 << QStringLiteral( "Unsigned Integer32" )
755 << QStringLiteral( "Float32" )
756 << QStringLiteral( "Float64" );
757
758 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 0, false );
759 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
760 addParameter( rasterTypeParameter.release() );
761
762 auto probabilityParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "PROBABILITY" ), QStringLiteral( "Probability" ), Qgis::ProcessingNumberParameterType::Double, 0.5, true, 0.00001 );
763 probabilityParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
764 addParameter( probabilityParameter.release() );
765}
766
767Qgis::DataType QgsRandomGeometricRasterAlgorithm::getRasterDataType( int typeId )
768{
769 switch ( typeId )
770 {
771 case 0:
773 case 1:
775 case 2:
777 case 3:
779 case 4:
781 case 5:
783 default:
785 }
786}
787
788bool QgsRandomGeometricRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
789{
790 const double probability = parameterAsDouble( parameters, QStringLiteral( "PROBABILITY" ), context );
791 mRandomGeometricDistribution = std::geometric_distribution<long>( probability );
792 return true;
793}
794
795long QgsRandomGeometricRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
796{
797 return mRandomGeometricDistribution( mersenneTwister );
798}
799
800double QgsRandomGeometricRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
801{
802 return static_cast<double>( mRandomGeometricDistribution( mersenneTwister ) );
803}
804
805//
806// QgsRandomNegativeBinomialRasterAlgorithm
807//
808QString QgsRandomNegativeBinomialRasterAlgorithm::name() const
809{
810 return QStringLiteral( "createrandomnegativebinomialrasterlayer" );
811}
812
813QString QgsRandomNegativeBinomialRasterAlgorithm::displayName() const
814{
815 return QObject::tr( "Create random raster layer (negative binomial distribution)" );
816}
817
818QStringList QgsRandomNegativeBinomialRasterAlgorithm::tags() const
819{
820 return QObject::tr( "raster,create,random,negative,binomial,negative-binomial" ).split( ',' );
821}
822
823QString QgsRandomNegativeBinomialRasterAlgorithm::shortHelpString() const
824{
825 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
826 "filled with negative binomially distributed random values.\n"
827 "By default, the values will be chosen given a distribution parameter k of 10.0 "
828 "and a probability of 0.5. "
829 "This can be overridden by using the advanced parameters for k and probability. "
830 "The raster data type is set to Integer types (Integer 16 by default). "
831 "The negative binomial distribution random values are defined as positive integer numbers. "
832 "A floating point raster will represent a cast of "
833 "integer values to floating point." );
834}
835
836QString QgsRandomNegativeBinomialRasterAlgorithm::shortDescription() const
837{
838 return QObject::tr( "Generates a raster layer for a given extent and cell size "
839 "filled with negative binomially distributed random values." );
840}
841
842QgsRandomNegativeBinomialRasterAlgorithm *QgsRandomNegativeBinomialRasterAlgorithm::createInstance() const
843{
844 return new QgsRandomNegativeBinomialRasterAlgorithm();
845}
846
847
848void QgsRandomNegativeBinomialRasterAlgorithm::addAlgorithmParams()
849{
850 QStringList rasterDataTypes = QStringList();
851 rasterDataTypes << QStringLiteral( "Integer16" )
852 << QStringLiteral( "Unsigned Integer16" )
853 << QStringLiteral( "Integer32" )
854 << QStringLiteral( "Unsigned Integer32" )
855 << QStringLiteral( "Float32" )
856 << QStringLiteral( "Float64" );
857
858 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 0, false );
859 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
860 addParameter( rasterTypeParameter.release() );
861
862 auto kParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "K_PARAMETER" ), QStringLiteral( "Distribution parameter k" ), Qgis::ProcessingNumberParameterType::Integer, 10, true, 0.00001 );
863 kParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
864 addParameter( kParameter.release() );
865
866 auto probabilityParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "PROBABILITY" ), QStringLiteral( "Probability" ), Qgis::ProcessingNumberParameterType::Double, 0.5, true, 0.00001 );
867 probabilityParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
868 addParameter( probabilityParameter.release() );
869}
870
871Qgis::DataType QgsRandomNegativeBinomialRasterAlgorithm::getRasterDataType( int typeId )
872{
873 switch ( typeId )
874 {
875 case 0:
877 case 1:
879 case 2:
881 case 3:
883 case 4:
885 case 5:
887 default:
889 }
890}
891
892bool QgsRandomNegativeBinomialRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
893{
894 const int k = parameterAsInt( parameters, QStringLiteral( "K_PARAMETER" ), context );
895 const double probability = parameterAsDouble( parameters, QStringLiteral( "PROBABILITY" ), context );
896 mRandomNegativeBinomialDistribution = std::negative_binomial_distribution<long>( k, probability );
897 return true;
898}
899
900long QgsRandomNegativeBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
901{
902 return mRandomNegativeBinomialDistribution( mersenneTwister );
903}
904
905double QgsRandomNegativeBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
906{
907 return static_cast<double>( mRandomNegativeBinomialDistribution( mersenneTwister ) );
908}
909
910//
911// QgsRandomNormalRasterAlgorithm
912//
913QString QgsRandomNormalRasterAlgorithm::name() const
914{
915 return QStringLiteral( "createrandomnormalrasterlayer" );
916}
917
918QString QgsRandomNormalRasterAlgorithm::displayName() const
919{
920 return QObject::tr( "Create random raster layer (normal distribution)" );
921}
922
923QStringList QgsRandomNormalRasterAlgorithm::tags() const
924{
925 return QObject::tr( "raster,create,normal,distribution,random" ).split( ',' );
926}
927
928QString QgsRandomNormalRasterAlgorithm::shortHelpString() const
929{
930 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
931 "filled with normally distributed random values.\n"
932 "By default, the values will be chosen given a mean of 0.0 and "
933 "a standard deviation of 1.0. This can be overridden by "
934 "using the advanced parameters for mean and standard deviation "
935 "value. The raster data type is set to Float32 by default "
936 "as the normal distribution random values are floating point numbers." );
937}
938
939QString QgsRandomNormalRasterAlgorithm::shortDescription() const
940{
941 return QObject::tr( "Generates a raster layer for a given extent and cell size "
942 "filled with normally distributed random values." );
943}
944
945QgsRandomNormalRasterAlgorithm *QgsRandomNormalRasterAlgorithm::createInstance() const
946{
947 return new QgsRandomNormalRasterAlgorithm();
948}
949
950void QgsRandomNormalRasterAlgorithm::addAlgorithmParams()
951{
952 QStringList rasterDataTypes = QStringList();
953 rasterDataTypes << QStringLiteral( "Float32" )
954 << QStringLiteral( "Float64" );
955
956 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 0, false );
957 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
958 addParameter( rasterTypeParameter.release() );
959
960 auto meanParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MEAN" ), QStringLiteral( "Mean of normal distribution" ), Qgis::ProcessingNumberParameterType::Double, 0, true );
961 meanParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
962 addParameter( meanParameter.release() );
963
964 auto stdevParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "STDDEV" ), QStringLiteral( "Standard deviation of normal distribution" ), Qgis::ProcessingNumberParameterType::Double, 1, true, 0 );
965 stdevParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
966 addParameter( stdevParameter.release() );
967}
968
969Qgis::DataType QgsRandomNormalRasterAlgorithm::getRasterDataType( int typeId )
970{
971 switch ( typeId )
972 {
973 case 0:
975 case 1:
977 default:
979 }
980}
981
982bool QgsRandomNormalRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
983{
984 const double mean = parameterAsDouble( parameters, QStringLiteral( "MEAN" ), context );
985 const double stddev = parameterAsDouble( parameters, QStringLiteral( "STDDEV" ), context );
986 mRandomNormalDistribution = std::normal_distribution<double>( mean, stddev );
987 return true;
988}
989
990long QgsRandomNormalRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
991{
992 return static_cast<long>( mRandomNormalDistribution( mersenneTwister ) );
993}
994
995double QgsRandomNormalRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
996{
997 return mRandomNormalDistribution( mersenneTwister );
998}
999
1000//
1001// QgsRandomPoissonRasterAlgorithm
1002//
1003QString QgsRandomPoissonRasterAlgorithm::name() const
1004{
1005 return QStringLiteral( "createrandompoissonrasterlayer" );
1006}
1007
1008QString QgsRandomPoissonRasterAlgorithm::displayName() const
1009{
1010 return QObject::tr( "Create random raster layer (poisson distribution)" );
1011}
1012
1013QStringList QgsRandomPoissonRasterAlgorithm::tags() const
1014{
1015 return QObject::tr( "raster,create,random,poisson" ).split( ',' );
1016}
1017
1018QString QgsRandomPoissonRasterAlgorithm::shortHelpString() const
1019{
1020 return QObject::tr( "This algorithm generates a raster layer for a given extent and cell size "
1021 "filled with poisson distributed random values.\n"
1022 "By default, the values will be chosen given a mean of 1.0. "
1023 "This can be overridden by using the advanced parameter for mean "
1024 "value. The raster data type is set to Integer types (Integer16 by default). "
1025 "The poisson distribution random values are positive integer numbers. "
1026 "A floating point raster will represent a cast of integer values to floating point." );
1027}
1028
1029QString QgsRandomPoissonRasterAlgorithm::shortDescription() const
1030{
1031 return QObject::tr( "Generates a raster layer for a given extent and cell size "
1032 "filled with poisson distributed random values." );
1033}
1034
1035QgsRandomPoissonRasterAlgorithm *QgsRandomPoissonRasterAlgorithm::createInstance() const
1036{
1037 return new QgsRandomPoissonRasterAlgorithm();
1038}
1039
1040
1041void QgsRandomPoissonRasterAlgorithm::addAlgorithmParams()
1042{
1043 QStringList rasterDataTypes = QStringList();
1044 rasterDataTypes << QStringLiteral( "Integer16" )
1045 << QStringLiteral( "Unsigned Integer16" )
1046 << QStringLiteral( "Integer32" )
1047 << QStringLiteral( "Unsigned Integer32" )
1048 << QStringLiteral( "Float32" )
1049 << QStringLiteral( "Float64" );
1050
1051 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral( "OUTPUT_TYPE" ), QObject::tr( "Output raster data type" ), rasterDataTypes, false, 0, false );
1052 rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
1053 addParameter( rasterTypeParameter.release() );
1054
1055 auto upperBoundParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MEAN" ), QStringLiteral( "Mean" ), Qgis::ProcessingNumberParameterType::Double, 1.0, true, 0 );
1056 upperBoundParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
1057 addParameter( upperBoundParameter.release() );
1058}
1059
1060Qgis::DataType QgsRandomPoissonRasterAlgorithm::getRasterDataType( int typeId )
1061{
1062 switch ( typeId )
1063 {
1064 case 0:
1065 return Qgis::DataType::Int16;
1066 case 1:
1068 case 2:
1069 return Qgis::DataType::Int32;
1070 case 3:
1072 case 4:
1074 case 5:
1076 default:
1078 }
1079}
1080
1081bool QgsRandomPoissonRasterAlgorithm::prepareRandomParameters( const QVariantMap &parameters, QgsProcessingContext &context )
1082{
1083 const double mean = parameterAsDouble( parameters, QStringLiteral( "MEAN" ), context );
1084 mRandomPoissonDistribution = std::poisson_distribution<long>( mean );
1085 return true;
1086}
1087
1088long QgsRandomPoissonRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1089{
1090 return mRandomPoissonDistribution( mersenneTwister );
1091}
1092
1093double QgsRandomPoissonRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1094{
1095 return static_cast<double>( mRandomPoissonDistribution( mersenneTwister ) );
1096}
1097
DataType
Raster data types.
Definition qgis.h:351
@ CInt32
Complex Int32.
@ Float32
Thirty two bit floating point (float)
@ CFloat64
Complex Float64.
@ Int16
Sixteen bit signed integer (qint16)
@ ARGB32_Premultiplied
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32_Premultiplied.
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ UnknownDataType
Unknown or unspecified type.
@ ARGB32
Color, alpha, red, green, blue, 4 bytes the same as QImage::Format_ARGB32.
@ Int32
Thirty two bit signed integer (qint32)
@ Float64
Sixty four bit floating point (double)
@ CFloat32
Complex Float32.
@ CInt16
Complex Int16.
@ UInt32
Thirty two bit unsigned integer (quint32)
@ Hidden
Parameter is hidden and should not be shown to users.
@ 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
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A coordinate reference system parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A numeric parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
Raster data container.
static int typeSize(Qgis::DataType dataType)
Returns the size in bytes for the specified dataType.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
A rectangle specified with double values.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6302