28using namespace Qt::StringLiterals;
35QString QgsRandomRasterAlgorithmBase::group()
const
37 return QObject::tr(
"Raster creation" );
40QString QgsRandomRasterAlgorithmBase::groupId()
const
42 return u
"rastercreation"_s;
45void QgsRandomRasterAlgorithmBase::initAlgorithm(
const QVariantMap & )
56 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATE_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
57 createOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
59 addParameter( createOptsParam.release() );
61 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATION_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
62 creationOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
64 addParameter( creationOptsParam.release() );
72 mCrs = parameterAsCrs( parameters, u
"TARGET_CRS"_s, context );
73 mExtent = parameterAsExtent( parameters, u
"EXTENT"_s, context, mCrs );
74 mPixelSize = parameterAsDouble( parameters, u
"PIXEL_SIZE"_s, context );
76 if ( mPixelSize <= 0 )
86 const int typeId = parameterAsInt( parameters, u
"OUTPUT_TYPE"_s, context );
88 mRasterDataType = getRasterDataType( typeId );
89 prepareRandomParameters( parameters, context );
91 std::random_device rd {};
92 std::mt19937 mersenneTwister { rd() };
94 QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
96 const QString optionsString = parameterAsString( parameters, u
"CREATE_OPTIONS"_s, context );
97 if ( !optionsString.isEmpty() )
98 creationOptions = optionsString;
100 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
101 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
105 const int rows =
static_cast<int>( 0.5 + mExtent.height() / mPixelSize );
106 const int cols =
static_cast<int>( 0.5 + mExtent.width() / mPixelSize );
110 const QgsRectangle rasterExtent =
QgsRectangle( mExtent.xMinimum(), mExtent.yMaximum() - ( rows * mPixelSize ), mExtent.xMinimum() + ( cols * mPixelSize ), mExtent.yMaximum() );
112 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
113 writer->setOutputProviderKey( u
"gdal"_s );
114 if ( !creationOptions.isEmpty() )
116 writer->setCreationOptions( creationOptions.split(
'|' ) );
119 writer->setOutputFormat( outputFormat );
120 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mRasterDataType, cols, rows, rasterExtent, mCrs ) );
123 if ( !provider->isValid() )
126 const bool hasReportsDuringClose = provider->hasReportsDuringClose();
127 const double maxProgressDuringBlockWriting = hasReportsDuringClose ? 50.0 : 100.0;
129 const double step = rows > 0 ? maxProgressDuringBlockWriting / rows : 1;
131 for (
int row = 0; row < rows; row++ )
139 switch ( mRasterDataType )
143 std::vector<quint8> byteRow( cols );
144 for (
int col = 0; col < cols; col++ )
146 byteRow[col] =
static_cast<quint8
>( generateRandomLongValue( mersenneTwister ) );
153 std::vector<qint8> int8Row( cols );
154 for (
int col = 0; col < cols; col++ )
156 int8Row[col] =
static_cast<qint8
>( generateRandomLongValue( mersenneTwister ) );
163 std::vector<qint16> int16Row( cols );
164 for (
int col = 0; col < cols; col++ )
166 int16Row[col] =
static_cast<qint16
>( generateRandomLongValue( mersenneTwister ) );
173 std::vector<quint16> uInt16Row( cols );
174 for (
int col = 0; col < cols; col++ )
176 uInt16Row[col] =
static_cast<quint16
>( generateRandomLongValue( mersenneTwister ) );
183 std::vector<qint32> int32Row( cols );
184 for (
int col = 0; col < cols; col++ )
186 int32Row[col] = generateRandomLongValue( mersenneTwister );
193 std::vector<quint32> uInt32Row( cols );
194 for (
int col = 0; col < cols; col++ )
196 uInt32Row[col] =
static_cast<quint32
>( generateRandomLongValue( mersenneTwister ) );
203 std::vector<float> float32Row( cols );
204 for (
int col = 0; col < cols; col++ )
206 float32Row[col] =
static_cast<float>( generateRandomDoubleValue( mersenneTwister ) );
213 std::vector<double> float64Row( cols );
214 for (
int col = 0; col < cols; col++ )
216 float64Row[col] = generateRandomDoubleValue( mersenneTwister );
224 if ( !provider->writeBlock( &block, 1, 0, row ) )
226 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
231 if ( feedback && hasReportsDuringClose )
234 if ( !provider->closeWithProgress( scaledFeedback.get() ) )
243 outputs.insert( u
"OUTPUT"_s, outputFile );
250QString QgsRandomUniformRasterAlgorithm::name()
const
252 return u
"createrandomuniformrasterlayer"_s;
255QString QgsRandomUniformRasterAlgorithm::displayName()
const
257 return QObject::tr(
"Create random raster layer (uniform distribution)" );
260QStringList QgsRandomUniformRasterAlgorithm::tags()
const
262 return QObject::tr(
"raster,create,random" ).split(
',' );
265QString QgsRandomUniformRasterAlgorithm::shortHelpString()
const
267 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
268 "filled with random values.\n"
269 "By default, the values will range between the minimum and "
270 "maximum value of the specified output raster type. This can "
271 "be overridden by using the advanced parameters for lower and "
272 "upper bound value. If the bounds have the same value or both "
273 "are zero (default) the algorithm will create random values in "
274 "the full value range of the chosen raster data type. "
275 "Choosing bounds outside the acceptable range of the output "
276 "raster type will abort the algorithm." );
279QString QgsRandomUniformRasterAlgorithm::shortDescription()
const
281 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
282 "filled with random values." );
285QgsRandomUniformRasterAlgorithm *QgsRandomUniformRasterAlgorithm::createInstance()
const
287 return new QgsRandomUniformRasterAlgorithm();
290void QgsRandomUniformRasterAlgorithm::addAlgorithmParams()
292 QStringList rasterDataTypes = QStringList();
293 rasterDataTypes << u
"Byte"_s
295 << u
"Unsigned Integer16"_s
297 << u
"Unsigned Integer32"_s
301 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
303 addParameter( rasterTypeParameter.release() );
307 addParameter( lowerBoundParameter.release() );
311 addParameter( upperBoundParameter.release() );
314Qgis::DataType QgsRandomUniformRasterAlgorithm::getRasterDataType(
int typeId )
337bool QgsRandomUniformRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
339 mRandomUpperBound = parameterAsDouble( parameters, u
"UPPER_BOUND"_s, context );
340 mRandomLowerBound = parameterAsDouble( parameters, u
"LOWER_BOUND"_s, context );
342 if ( mRandomLowerBound > mRandomUpperBound )
343 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." ) );
345 const int typeId = parameterAsInt( parameters, u
"OUTPUT_TYPE"_s, context );
346 const Qgis::DataType rasterDataType = getRasterDataType( typeId );
348 switch ( rasterDataType )
351 if ( mRandomLowerBound < std::numeric_limits<quint8>::min() || mRandomUpperBound > std::numeric_limits<quint8>::max() )
352 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(
"Byte"_L1 ) );
356 mRandomUpperBound = std::numeric_limits<quint8>::max();
357 mRandomLowerBound = std::numeric_limits<quint8>::min();
361 if ( mRandomLowerBound < std::numeric_limits<qint8>::min() || mRandomUpperBound > std::numeric_limits<qint8>::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<qint8>::min() ).arg( std::numeric_limits<qint8>::max() ).arg(
"Int8"_L1 ) );
366 mRandomUpperBound = std::numeric_limits<qint8>::max();
367 mRandomLowerBound = std::numeric_limits<qint8>::min();
371 if ( mRandomLowerBound < std::numeric_limits<qint16>::min() || mRandomUpperBound > std::numeric_limits<qint16>::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<qint16>::min() ).arg( std::numeric_limits<qint16>::max() ).arg(
"Integer16"_L1 ) );
375 mRandomUpperBound = std::numeric_limits<qint16>::max();
376 mRandomLowerBound = std::numeric_limits<qint16>::min();
380 if ( mRandomLowerBound < std::numeric_limits<quint16>::min() || mRandomUpperBound > std::numeric_limits<quint16>::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<quint16>::min() ).arg( std::numeric_limits<quint16>::max() ).arg(
"Unsigned Integer16"_L1 ) );
384 mRandomUpperBound = std::numeric_limits<quint16>::max();
385 mRandomLowerBound = std::numeric_limits<quint16>::min();
389 if ( mRandomLowerBound < std::numeric_limits<qint32>::min() || mRandomUpperBound > std::numeric_limits<qint32>::max() )
390 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(
"Integer32"_L1 ) );
393 mRandomUpperBound = std::numeric_limits<qint32>::max();
394 mRandomLowerBound = std::numeric_limits<qint32>::min();
398 if ( mRandomLowerBound < std::numeric_limits<quint32>::min() || mRandomUpperBound > std::numeric_limits<quint32>::max() )
399 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(
"Unsigned Integer32"_L1 ) );
402 mRandomUpperBound = std::numeric_limits<quint32>::max();
403 mRandomLowerBound = std::numeric_limits<quint32>::min();
409 mRandomUpperBound = std::numeric_limits<float>::max();
410 mRandomLowerBound = std::numeric_limits<float>::min();
416 mRandomUpperBound = std::numeric_limits<double>::max();
417 mRandomLowerBound = std::numeric_limits<double>::min();
430 mRandomUniformIntDistribution = std::uniform_int_distribution<long>( mRandomLowerBound, mRandomUpperBound );
431 mRandomUniformDoubleDistribution = std::uniform_real_distribution<double>( mRandomLowerBound, mRandomUpperBound );
436long QgsRandomUniformRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
438 return mRandomUniformIntDistribution( mersenneTwister );
441double QgsRandomUniformRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
443 return mRandomUniformDoubleDistribution( mersenneTwister );
449QString QgsRandomBinomialRasterAlgorithm::name()
const
451 return u
"createrandombinomialrasterlayer"_s;
454QString QgsRandomBinomialRasterAlgorithm::displayName()
const
456 return QObject::tr(
"Create random raster layer (binomial distribution)" );
459QStringList QgsRandomBinomialRasterAlgorithm::tags()
const
461 return QObject::tr(
"raster,create,binomial,random" ).split(
',' );
464QString QgsRandomBinomialRasterAlgorithm::shortHelpString()
const
466 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
467 "filled with binomially distributed random values.\n"
468 "By default, the values will be chosen given an N of 10 and a probability of 0.5. "
469 "This can be overridden by using the advanced parameter for N and probability. "
470 "The raster data type is set to Integer types (Integer16 by default). "
471 "The binomial distribution random values are defined as positive integer numbers. "
472 "A floating point raster will represent a cast of integer values "
473 "to floating point." );
476QString QgsRandomBinomialRasterAlgorithm::shortDescription()
const
478 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
479 "filled with binomially distributed random values." );
482QgsRandomBinomialRasterAlgorithm *QgsRandomBinomialRasterAlgorithm::createInstance()
const
484 return new QgsRandomBinomialRasterAlgorithm();
488void QgsRandomBinomialRasterAlgorithm::addAlgorithmParams()
490 QStringList rasterDataTypes = QStringList();
491 rasterDataTypes << u
"Integer16"_s
492 << u
"Unsigned Integer16"_s
494 << u
"Unsigned Integer32"_s
498 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
500 addParameter( rasterTypeParameter.release() );
504 addParameter( nParameter.release() );
508 addParameter( probabilityParameter.release() );
511Qgis::DataType QgsRandomBinomialRasterAlgorithm::getRasterDataType(
int typeId )
532bool QgsRandomBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
534 const int n = parameterAsInt( parameters, u
"N"_s, context );
535 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
536 mRandombinomialDistribution = std::binomial_distribution<long>( n, probability );
540long QgsRandomBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
542 return mRandombinomialDistribution( mersenneTwister );
545double QgsRandomBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
547 return static_cast<double>( mRandombinomialDistribution( mersenneTwister ) );
553QString QgsRandomExponentialRasterAlgorithm::name()
const
555 return u
"createrandomexponentialrasterlayer"_s;
558QString QgsRandomExponentialRasterAlgorithm::displayName()
const
560 return QObject::tr(
"Create random raster layer (exponential distribution)" );
563QStringList QgsRandomExponentialRasterAlgorithm::tags()
const
565 return QObject::tr(
"raster,create,random,exponential" ).split(
',' );
568QString QgsRandomExponentialRasterAlgorithm::shortHelpString()
const
570 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
571 "filled with exponentially distributed random values.\n"
572 "By default, the values will be chosen given a lambda of 1.0. "
573 "This can be overridden by using the advanced parameter for lambda. "
574 "The raster data type is set to Float32 by default as "
575 "the exponential distribution random values are floating point numbers." );
578QString QgsRandomExponentialRasterAlgorithm::shortDescription()
const
580 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
581 "filled with exponentially distributed random values." );
584QgsRandomExponentialRasterAlgorithm *QgsRandomExponentialRasterAlgorithm::createInstance()
const
586 return new QgsRandomExponentialRasterAlgorithm();
590void QgsRandomExponentialRasterAlgorithm::addAlgorithmParams()
592 QStringList rasterDataTypes = QStringList();
593 rasterDataTypes << u
"Float32"_s
596 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
598 addParameter( rasterTypeParameter.release() );
602 addParameter( lambdaParameter.release() );
605Qgis::DataType QgsRandomExponentialRasterAlgorithm::getRasterDataType(
int typeId )
618bool QgsRandomExponentialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
620 const double lambda = parameterAsDouble( parameters, u
"LAMBDA"_s, context );
621 mRandomExponentialDistribution = std::exponential_distribution<double>( lambda );
625long QgsRandomExponentialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
627 return static_cast<long>( mRandomExponentialDistribution( mersenneTwister ) );
630double QgsRandomExponentialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
632 return mRandomExponentialDistribution( mersenneTwister );
638QString QgsRandomGammaRasterAlgorithm::name()
const
640 return u
"createrandomgammarasterlayer"_s;
643QString QgsRandomGammaRasterAlgorithm::displayName()
const
645 return QObject::tr(
"Create random raster layer (gamma distribution)" );
648QStringList QgsRandomGammaRasterAlgorithm::tags()
const
650 return QObject::tr(
"raster,create,random,gamma" ).split(
',' );
653QString QgsRandomGammaRasterAlgorithm::shortHelpString()
const
655 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
656 "filled with gamma distributed random values.\n"
657 "By default, the values will be chosen given an alpha and beta value of 1.0. "
658 "This can be overridden by using the advanced parameter for alpha and beta. "
659 "The raster data type is set to Float32 by default as "
660 "the gamma distribution random values are floating point numbers." );
663QString QgsRandomGammaRasterAlgorithm::shortDescription()
const
665 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
666 "filled with gamma distributed random values." );
669QgsRandomGammaRasterAlgorithm *QgsRandomGammaRasterAlgorithm::createInstance()
const
671 return new QgsRandomGammaRasterAlgorithm();
675void QgsRandomGammaRasterAlgorithm::addAlgorithmParams()
677 QStringList rasterDataTypes = QStringList();
678 rasterDataTypes << u
"Float32"_s
681 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
683 addParameter( rasterTypeParameter.release() );
687 addParameter( alphaParameter.release() );
691 addParameter( betaParameter.release() );
694Qgis::DataType QgsRandomGammaRasterAlgorithm::getRasterDataType(
int typeId )
707bool QgsRandomGammaRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
709 const double alpha = parameterAsDouble( parameters, u
"ALPHA"_s, context );
710 const double beta = parameterAsDouble( parameters, u
"BETA"_s, context );
711 mRandomGammaDistribution = std::gamma_distribution<double>( alpha, beta );
715long QgsRandomGammaRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
717 return static_cast<long>( mRandomGammaDistribution( mersenneTwister ) );
720double QgsRandomGammaRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
722 return mRandomGammaDistribution( mersenneTwister );
728QString QgsRandomGeometricRasterAlgorithm::name()
const
730 return u
"createrandomgeometricrasterlayer"_s;
733QString QgsRandomGeometricRasterAlgorithm::displayName()
const
735 return QObject::tr(
"Create random raster layer (geometric distribution)" );
738QStringList QgsRandomGeometricRasterAlgorithm::tags()
const
740 return QObject::tr(
"raster,create,random,geometric" ).split(
',' );
743QString QgsRandomGeometricRasterAlgorithm::shortHelpString()
const
745 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
746 "filled with geometrically distributed random values.\n"
747 "By default, the values will be chosen given a probability of 0.5. "
748 "This can be overridden by using the advanced parameter for mean "
749 "value. The raster data type is set to Integer types (Integer16 by default). "
750 "The geometric distribution random values are defined as positive integer numbers. "
751 "A floating point raster will represent a cast of "
752 "integer values to floating point." );
755QString QgsRandomGeometricRasterAlgorithm::shortDescription()
const
757 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
758 "filled with geometrically distributed random values." );
761QgsRandomGeometricRasterAlgorithm *QgsRandomGeometricRasterAlgorithm::createInstance()
const
763 return new QgsRandomGeometricRasterAlgorithm();
767void QgsRandomGeometricRasterAlgorithm::addAlgorithmParams()
769 QStringList rasterDataTypes = QStringList();
770 rasterDataTypes << u
"Integer16"_s
771 << u
"Unsigned Integer16"_s
773 << u
"Unsigned Integer32"_s
777 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
779 addParameter( rasterTypeParameter.release() );
783 addParameter( probabilityParameter.release() );
786Qgis::DataType QgsRandomGeometricRasterAlgorithm::getRasterDataType(
int typeId )
807bool QgsRandomGeometricRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
809 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
810 mRandomGeometricDistribution = std::geometric_distribution<long>( probability );
814long QgsRandomGeometricRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
816 return mRandomGeometricDistribution( mersenneTwister );
819double QgsRandomGeometricRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
821 return static_cast<double>( mRandomGeometricDistribution( mersenneTwister ) );
827QString QgsRandomNegativeBinomialRasterAlgorithm::name()
const
829 return u
"createrandomnegativebinomialrasterlayer"_s;
832QString QgsRandomNegativeBinomialRasterAlgorithm::displayName()
const
834 return QObject::tr(
"Create random raster layer (negative binomial distribution)" );
837QStringList QgsRandomNegativeBinomialRasterAlgorithm::tags()
const
839 return QObject::tr(
"raster,create,random,negative,binomial,negative-binomial" ).split(
',' );
842QString QgsRandomNegativeBinomialRasterAlgorithm::shortHelpString()
const
844 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
845 "filled with negative binomially distributed random values.\n"
846 "By default, the values will be chosen given a distribution parameter k of 10.0 "
847 "and a probability of 0.5. "
848 "This can be overridden by using the advanced parameters for k and probability. "
849 "The raster data type is set to Integer types (Integer 16 by default). "
850 "The negative binomial distribution random values are defined as positive integer numbers. "
851 "A floating point raster will represent a cast of "
852 "integer values to floating point." );
855QString QgsRandomNegativeBinomialRasterAlgorithm::shortDescription()
const
857 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
858 "filled with negative binomially distributed random values." );
861QgsRandomNegativeBinomialRasterAlgorithm *QgsRandomNegativeBinomialRasterAlgorithm::createInstance()
const
863 return new QgsRandomNegativeBinomialRasterAlgorithm();
867void QgsRandomNegativeBinomialRasterAlgorithm::addAlgorithmParams()
869 QStringList rasterDataTypes = QStringList();
870 rasterDataTypes << u
"Integer16"_s
871 << u
"Unsigned Integer16"_s
873 << u
"Unsigned Integer32"_s
877 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
879 addParameter( rasterTypeParameter.release() );
883 addParameter( kParameter.release() );
887 addParameter( probabilityParameter.release() );
890Qgis::DataType QgsRandomNegativeBinomialRasterAlgorithm::getRasterDataType(
int typeId )
911bool QgsRandomNegativeBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
913 const int k = parameterAsInt( parameters, u
"K_PARAMETER"_s, context );
914 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
915 mRandomNegativeBinomialDistribution = std::negative_binomial_distribution<long>( k, probability );
919long QgsRandomNegativeBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
921 return mRandomNegativeBinomialDistribution( mersenneTwister );
924double QgsRandomNegativeBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
926 return static_cast<double>( mRandomNegativeBinomialDistribution( mersenneTwister ) );
932QString QgsRandomNormalRasterAlgorithm::name()
const
934 return u
"createrandomnormalrasterlayer"_s;
937QString QgsRandomNormalRasterAlgorithm::displayName()
const
939 return QObject::tr(
"Create random raster layer (normal distribution)" );
942QStringList QgsRandomNormalRasterAlgorithm::tags()
const
944 return QObject::tr(
"raster,create,normal,distribution,random" ).split(
',' );
947QString QgsRandomNormalRasterAlgorithm::shortHelpString()
const
949 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
950 "filled with normally distributed random values.\n"
951 "By default, the values will be chosen given a mean of 0.0 and "
952 "a standard deviation of 1.0. This can be overridden by "
953 "using the advanced parameters for mean and standard deviation "
954 "value. The raster data type is set to Float32 by default "
955 "as the normal distribution random values are floating point numbers." );
958QString QgsRandomNormalRasterAlgorithm::shortDescription()
const
960 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
961 "filled with normally distributed random values." );
964QgsRandomNormalRasterAlgorithm *QgsRandomNormalRasterAlgorithm::createInstance()
const
966 return new QgsRandomNormalRasterAlgorithm();
969void QgsRandomNormalRasterAlgorithm::addAlgorithmParams()
971 QStringList rasterDataTypes = QStringList();
972 rasterDataTypes << u
"Float32"_s
975 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
977 addParameter( rasterTypeParameter.release() );
981 addParameter( meanParameter.release() );
985 addParameter( stdevParameter.release() );
988Qgis::DataType QgsRandomNormalRasterAlgorithm::getRasterDataType(
int typeId )
1001bool QgsRandomNormalRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
1003 const double mean = parameterAsDouble( parameters, u
"MEAN"_s, context );
1004 const double stddev = parameterAsDouble( parameters, u
"STDDEV"_s, context );
1005 mRandomNormalDistribution = std::normal_distribution<double>( mean, stddev );
1009long QgsRandomNormalRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1011 return static_cast<long>( mRandomNormalDistribution( mersenneTwister ) );
1014double QgsRandomNormalRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1016 return mRandomNormalDistribution( mersenneTwister );
1022QString QgsRandomPoissonRasterAlgorithm::name()
const
1024 return u
"createrandompoissonrasterlayer"_s;
1027QString QgsRandomPoissonRasterAlgorithm::displayName()
const
1029 return QObject::tr(
"Create random raster layer (poisson distribution)" );
1032QStringList QgsRandomPoissonRasterAlgorithm::tags()
const
1034 return QObject::tr(
"raster,create,random,poisson" ).split(
',' );
1037QString QgsRandomPoissonRasterAlgorithm::shortHelpString()
const
1039 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell size "
1040 "filled with poisson distributed random values.\n"
1041 "By default, the values will be chosen given a mean of 1.0. "
1042 "This can be overridden by using the advanced parameter for mean "
1043 "value. The raster data type is set to Integer types (Integer16 by default). "
1044 "The poisson distribution random values are positive integer numbers. "
1045 "A floating point raster will represent a cast of integer values to floating point." );
1048QString QgsRandomPoissonRasterAlgorithm::shortDescription()
const
1050 return QObject::tr(
"Generates a raster layer for a given extent and cell size "
1051 "filled with poisson distributed random values." );
1054QgsRandomPoissonRasterAlgorithm *QgsRandomPoissonRasterAlgorithm::createInstance()
const
1056 return new QgsRandomPoissonRasterAlgorithm();
1060void QgsRandomPoissonRasterAlgorithm::addAlgorithmParams()
1062 QStringList rasterDataTypes = QStringList();
1063 rasterDataTypes << u
"Integer16"_s
1064 << u
"Unsigned Integer16"_s
1066 << u
"Unsigned Integer32"_s
1070 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
1072 addParameter( rasterTypeParameter.release() );
1076 addParameter( upperBoundParameter.release() );
1079Qgis::DataType QgsRandomPoissonRasterAlgorithm::getRasterDataType(
int typeId )
1100bool QgsRandomPoissonRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
1102 const double mean = parameterAsDouble( parameters, u
"MEAN"_s, context );
1103 mRandomPoissonDistribution = std::poisson_distribution<long>( mean );
1107long QgsRandomPoissonRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1109 return mRandomPoissonDistribution( mersenneTwister );
1112double QgsRandomPoissonRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1114 return static_cast<double>( mRandomPoissonDistribution( mersenneTwister ) );
DataType
Raster data types.
@ 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.
@ 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.
@ Double
Double/float values.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
static std::unique_ptr< QgsFeedback > createScaledFeedback(QgsFeedback *parentFeedback, double startPercentage, double endPercentage)
Returns a feedback object whose [0, 100] progression range will be mapped to parentFeedback [startPer...
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 ...
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).