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