29QString QgsRandomRasterAlgorithmBase::group()
const
31 return QObject::tr(
"Raster creation" );
34QString QgsRandomRasterAlgorithmBase::groupId()
const
36 return QStringLiteral(
"rastercreation" );
39void QgsRandomRasterAlgorithmBase::initAlgorithm(
const QVariantMap & )
42 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"TARGET_CRS" ), QObject::tr(
"Target CRS" ), QStringLiteral(
"ProjectCrs" ) ) );
50 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
51 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
53 addParameter( createOptsParam.release() );
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" ) } } ) } } ) );
58 addParameter( creationOptsParam.release() );
66 mCrs = parameterAsCrs( parameters, QStringLiteral(
"TARGET_CRS" ), context );
67 mExtent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, mCrs );
68 mPixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
70 if ( mPixelSize <= 0 )
80 const int typeId = parameterAsInt( parameters, QStringLiteral(
"OUTPUT_TYPE" ), context );
82 mRasterDataType = getRasterDataType( typeId );
83 prepareRandomParameters( parameters, context );
85 std::random_device rd {};
86 std::mt19937 mersenneTwister { rd() };
88 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
90 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
91 if ( !optionsString.isEmpty() )
92 creationOptions = optionsString;
94 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
95 const QFileInfo fi( outputFile );
100 const int rows =
static_cast<int>( 0.5 + mExtent.height() / mPixelSize );
101 const int cols =
static_cast<int>( 0.5 + mExtent.width() / mPixelSize );
105 const QgsRectangle rasterExtent =
QgsRectangle( mExtent.xMinimum(), mExtent.yMaximum() - ( rows * mPixelSize ), mExtent.xMinimum() + ( cols * mPixelSize ), mExtent.yMaximum() );
107 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
108 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
109 if ( !creationOptions.isEmpty() )
111 writer->setCreationOptions( creationOptions.split(
'|' ) );
114 writer->setOutputFormat( outputFormat );
115 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mRasterDataType, cols, rows, rasterExtent, mCrs ) );
118 if ( !provider->isValid() )
121 const double step = rows > 0 ? 100.0 / rows : 1;
123 for (
int row = 0; row < rows; row++ )
131 switch ( mRasterDataType )
135 std::vector<quint8> byteRow( cols );
136 for (
int col = 0; col < cols; col++ )
138 byteRow[col] =
static_cast<quint8
>( generateRandomLongValue( mersenneTwister ) );
145 std::vector<qint8> int8Row( cols );
146 for (
int col = 0; col < cols; col++ )
148 int8Row[col] =
static_cast<qint8
>( generateRandomLongValue( mersenneTwister ) );
155 std::vector<qint16> int16Row( cols );
156 for (
int col = 0; col < cols; col++ )
158 int16Row[col] =
static_cast<qint16
>( generateRandomLongValue( mersenneTwister ) );
165 std::vector<quint16> uInt16Row( cols );
166 for (
int col = 0; col < cols; col++ )
168 uInt16Row[col] =
static_cast<quint16
>( generateRandomLongValue( mersenneTwister ) );
175 std::vector<qint32> int32Row( cols );
176 for (
int col = 0; col < cols; col++ )
178 int32Row[col] = generateRandomLongValue( mersenneTwister );
185 std::vector<quint32> uInt32Row( cols );
186 for (
int col = 0; col < cols; col++ )
188 uInt32Row[col] =
static_cast<quint32
>( generateRandomLongValue( mersenneTwister ) );
195 std::vector<float> float32Row( cols );
196 for (
int col = 0; col < cols; col++ )
198 float32Row[col] =
static_cast<float>( generateRandomDoubleValue( mersenneTwister ) );
205 std::vector<double> float64Row( cols );
206 for (
int col = 0; col < cols; col++ )
208 float64Row[col] = generateRandomDoubleValue( mersenneTwister );
216 if ( !provider->writeBlock( &block, 1, 0, row ) )
218 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
224 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
231QString QgsRandomUniformRasterAlgorithm::name()
const
233 return QStringLiteral(
"createrandomuniformrasterlayer" );
236QString QgsRandomUniformRasterAlgorithm::displayName()
const
238 return QObject::tr(
"Create random raster layer (uniform distribution)" );
241QStringList QgsRandomUniformRasterAlgorithm::tags()
const
243 return QObject::tr(
"raster,create,random" ).split(
',' );
246QString QgsRandomUniformRasterAlgorithm::shortHelpString()
const
248 return QObject::tr(
"Generates a raster layer for 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." );
260QgsRandomUniformRasterAlgorithm *QgsRandomUniformRasterAlgorithm::createInstance()
const
262 return new QgsRandomUniformRasterAlgorithm();
265void QgsRandomUniformRasterAlgorithm::addAlgorithmParams()
267 QStringList rasterDataTypes = QStringList();
268 rasterDataTypes << QStringLiteral(
"Byte" )
269 << QStringLiteral(
"Integer16" )
270 << QStringLiteral(
"Unsigned Integer16" )
271 << QStringLiteral(
"Integer32" )
272 << QStringLiteral(
"Unsigned Integer32" )
273 << QStringLiteral(
"Float32" )
274 << QStringLiteral(
"Float64" );
276 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
278 addParameter( rasterTypeParameter.release() );
280 auto lowerBoundParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"LOWER_BOUND" ), QStringLiteral(
"Lower bound for random number range" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true );
282 addParameter( lowerBoundParameter.release() );
284 auto upperBoundParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"UPPER_BOUND" ), QStringLiteral(
"Upper bound for random number range" ),
Qgis::ProcessingNumberParameterType::Double, QVariant(),
true );
286 addParameter( upperBoundParameter.release() );
289Qgis::DataType QgsRandomUniformRasterAlgorithm::getRasterDataType(
int typeId )
312bool QgsRandomUniformRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
314 mRandomUpperBound = parameterAsDouble( parameters, QStringLiteral(
"UPPER_BOUND" ), context );
315 mRandomLowerBound = parameterAsDouble( parameters, QStringLiteral(
"LOWER_BOUND" ), context );
317 if ( mRandomLowerBound > mRandomUpperBound )
318 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." ) );
320 const int typeId = parameterAsInt( parameters, QStringLiteral(
"OUTPUT_TYPE" ), context );
321 const Qgis::DataType rasterDataType = getRasterDataType( typeId );
323 switch ( rasterDataType )
326 if ( mRandomLowerBound < std::numeric_limits<quint8>::min() || mRandomUpperBound > std::numeric_limits<quint8>::max() )
327 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" ) ) );
331 mRandomUpperBound = std::numeric_limits<quint8>::max();
332 mRandomLowerBound = std::numeric_limits<quint8>::min();
336 if ( mRandomLowerBound < std::numeric_limits<qint8>::min() || mRandomUpperBound > std::numeric_limits<qint8>::max() )
337 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" ) ) );
341 mRandomUpperBound = std::numeric_limits<qint8>::max();
342 mRandomLowerBound = std::numeric_limits<qint8>::min();
346 if ( mRandomLowerBound < std::numeric_limits<qint16>::min() || mRandomUpperBound > std::numeric_limits<qint16>::max() )
347 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" ) ) );
350 mRandomUpperBound = std::numeric_limits<qint16>::max();
351 mRandomLowerBound = std::numeric_limits<qint16>::min();
355 if ( mRandomLowerBound < std::numeric_limits<quint16>::min() || mRandomUpperBound > std::numeric_limits<quint16>::max() )
356 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" ) ) );
359 mRandomUpperBound = std::numeric_limits<quint16>::max();
360 mRandomLowerBound = std::numeric_limits<quint16>::min();
364 if ( mRandomLowerBound < std::numeric_limits<qint32>::min() || mRandomUpperBound > std::numeric_limits<qint32>::max() )
365 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" ) ) );
368 mRandomUpperBound = std::numeric_limits<qint32>::max();
369 mRandomLowerBound = std::numeric_limits<qint32>::min();
373 if ( mRandomLowerBound < std::numeric_limits<quint32>::min() || mRandomUpperBound > std::numeric_limits<quint32>::max() )
374 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" ) ) );
377 mRandomUpperBound = std::numeric_limits<quint32>::max();
378 mRandomLowerBound = std::numeric_limits<quint32>::min();
384 mRandomUpperBound = std::numeric_limits<float>::max();
385 mRandomLowerBound = std::numeric_limits<float>::min();
391 mRandomUpperBound = std::numeric_limits<double>::max();
392 mRandomLowerBound = std::numeric_limits<double>::min();
405 mRandomUniformIntDistribution = std::uniform_int_distribution<long>( mRandomLowerBound, mRandomUpperBound );
406 mRandomUniformDoubleDistribution = std::uniform_real_distribution<double>( mRandomLowerBound, mRandomUpperBound );
411long QgsRandomUniformRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
413 return mRandomUniformIntDistribution( mersenneTwister );
416double QgsRandomUniformRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
418 return mRandomUniformDoubleDistribution( mersenneTwister );
424QString QgsRandomBinomialRasterAlgorithm::name()
const
426 return QStringLiteral(
"createrandombinomialrasterlayer" );
429QString QgsRandomBinomialRasterAlgorithm::displayName()
const
431 return QObject::tr(
"Create random raster layer (binomial distribution)" );
434QStringList QgsRandomBinomialRasterAlgorithm::tags()
const
436 return QObject::tr(
"raster,create,binomial,random" ).split(
',' );
439QString QgsRandomBinomialRasterAlgorithm::shortHelpString()
const
441 return QObject::tr(
"Generates a raster layer for given extent and cell size "
442 "filled with binomially distributed random values.\n"
443 "By default, the values will be chosen given an N of 10 and a probability of 0.5. "
444 "This can be overridden by using the advanced parameter for N and probability. "
445 "The raster data type is set to Integer types (Integer16 by default). "
446 "The binomial distribution random values are defined as positive integer numbers. "
447 "A floating point raster will represent a cast of integer values "
448 "to floating point." );
451QgsRandomBinomialRasterAlgorithm *QgsRandomBinomialRasterAlgorithm::createInstance()
const
453 return new QgsRandomBinomialRasterAlgorithm();
457void QgsRandomBinomialRasterAlgorithm::addAlgorithmParams()
459 QStringList rasterDataTypes = QStringList();
460 rasterDataTypes << QStringLiteral(
"Integer16" )
461 << QStringLiteral(
"Unsigned Integer16" )
462 << QStringLiteral(
"Integer32" )
463 << QStringLiteral(
"Unsigned Integer32" )
464 << QStringLiteral(
"Float32" )
465 << QStringLiteral(
"Float64" );
467 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
469 addParameter( rasterTypeParameter.release() );
473 addParameter( nParameter.release() );
477 addParameter( probabilityParameter.release() );
480Qgis::DataType QgsRandomBinomialRasterAlgorithm::getRasterDataType(
int typeId )
501bool QgsRandomBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
503 const int n = parameterAsInt( parameters, QStringLiteral(
"N" ), context );
504 const double probability = parameterAsDouble( parameters, QStringLiteral(
"PROBABILITY" ), context );
505 mRandombinomialDistribution = std::binomial_distribution<long>( n, probability );
509long QgsRandomBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
511 return mRandombinomialDistribution( mersenneTwister );
514double QgsRandomBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
516 return static_cast<double>( mRandombinomialDistribution( mersenneTwister ) );
522QString QgsRandomExponentialRasterAlgorithm::name()
const
524 return QStringLiteral(
"createrandomexponentialrasterlayer" );
527QString QgsRandomExponentialRasterAlgorithm::displayName()
const
529 return QObject::tr(
"Create random raster layer (exponential distribution)" );
532QStringList QgsRandomExponentialRasterAlgorithm::tags()
const
534 return QObject::tr(
"raster,create,random,exponential" ).split(
',' );
537QString QgsRandomExponentialRasterAlgorithm::shortHelpString()
const
539 return QObject::tr(
"Generates a raster layer for given extent and cell size "
540 "filled with exponentially distributed random values.\n"
541 "By default, the values will be chosen given a lambda of 1.0. "
542 "This can be overridden by using the advanced parameter for lambda. "
543 "The raster data type is set to Float32 by default as "
544 "the exponential distribution random values are floating point numbers." );
547QgsRandomExponentialRasterAlgorithm *QgsRandomExponentialRasterAlgorithm::createInstance()
const
549 return new QgsRandomExponentialRasterAlgorithm();
553void QgsRandomExponentialRasterAlgorithm::addAlgorithmParams()
555 QStringList rasterDataTypes = QStringList();
556 rasterDataTypes << QStringLiteral(
"Float32" )
557 << QStringLiteral(
"Float64" );
559 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
561 addParameter( rasterTypeParameter.release() );
565 addParameter( lambdaParameter.release() );
568Qgis::DataType QgsRandomExponentialRasterAlgorithm::getRasterDataType(
int typeId )
581bool QgsRandomExponentialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
583 const double lambda = parameterAsDouble( parameters, QStringLiteral(
"LAMBDA" ), context );
584 mRandomExponentialDistribution = std::exponential_distribution<double>( lambda );
588long QgsRandomExponentialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
590 return static_cast<long>( mRandomExponentialDistribution( mersenneTwister ) );
593double QgsRandomExponentialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
595 return mRandomExponentialDistribution( mersenneTwister );
601QString QgsRandomGammaRasterAlgorithm::name()
const
603 return QStringLiteral(
"createrandomgammarasterlayer" );
606QString QgsRandomGammaRasterAlgorithm::displayName()
const
608 return QObject::tr(
"Create random raster layer (gamma distribution)" );
611QStringList QgsRandomGammaRasterAlgorithm::tags()
const
613 return QObject::tr(
"raster,create,random,gamma" ).split(
',' );
616QString QgsRandomGammaRasterAlgorithm::shortHelpString()
const
618 return QObject::tr(
"Generates a raster layer for given extent and cell size "
619 "filled with gamma distributed random values.\n"
620 "By default, the values will be chosen given an alpha and beta value of 1.0. "
621 "This can be overridden by using the advanced parameter for alpha and beta. "
622 "The raster data type is set to Float32 by default as "
623 "the gamma distribution random values are floating point numbers." );
626QgsRandomGammaRasterAlgorithm *QgsRandomGammaRasterAlgorithm::createInstance()
const
628 return new QgsRandomGammaRasterAlgorithm();
632void QgsRandomGammaRasterAlgorithm::addAlgorithmParams()
634 QStringList rasterDataTypes = QStringList();
635 rasterDataTypes << QStringLiteral(
"Float32" )
636 << QStringLiteral(
"Float64" );
638 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
640 addParameter( rasterTypeParameter.release() );
644 addParameter( alphaParameter.release() );
648 addParameter( betaParameter.release() );
651Qgis::DataType QgsRandomGammaRasterAlgorithm::getRasterDataType(
int typeId )
664bool QgsRandomGammaRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
666 const double alpha = parameterAsDouble( parameters, QStringLiteral(
"ALPHA" ), context );
667 const double beta = parameterAsDouble( parameters, QStringLiteral(
"BETA" ), context );
668 mRandomGammaDistribution = std::gamma_distribution<double>( alpha, beta );
672long QgsRandomGammaRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
674 return static_cast<long>( mRandomGammaDistribution( mersenneTwister ) );
677double QgsRandomGammaRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
679 return mRandomGammaDistribution( mersenneTwister );
685QString QgsRandomGeometricRasterAlgorithm::name()
const
687 return QStringLiteral(
"createrandomgeometricrasterlayer" );
690QString QgsRandomGeometricRasterAlgorithm::displayName()
const
692 return QObject::tr(
"Create random raster layer (geometric distribution)" );
695QStringList QgsRandomGeometricRasterAlgorithm::tags()
const
697 return QObject::tr(
"raster,create,random,geometric" ).split(
',' );
700QString QgsRandomGeometricRasterAlgorithm::shortHelpString()
const
702 return QObject::tr(
"Generates a raster layer for given extent and cell size "
703 "filled with geometrically distributed random values.\n"
704 "By default, the values will be chosen given a probability of 0.5. "
705 "This can be overridden by using the advanced parameter for mean "
706 "value. The raster data type is set to Integer types (Integer16 by default). "
707 "The geometric distribution random values are defined as positive integer numbers. "
708 "A floating point raster will represent a cast of "
709 "integer values to floating point." );
712QgsRandomGeometricRasterAlgorithm *QgsRandomGeometricRasterAlgorithm::createInstance()
const
714 return new QgsRandomGeometricRasterAlgorithm();
718void QgsRandomGeometricRasterAlgorithm::addAlgorithmParams()
720 QStringList rasterDataTypes = QStringList();
721 rasterDataTypes << QStringLiteral(
"Integer16" )
722 << QStringLiteral(
"Unsigned Integer16" )
723 << QStringLiteral(
"Integer32" )
724 << QStringLiteral(
"Unsigned Integer32" )
725 << QStringLiteral(
"Float32" )
726 << QStringLiteral(
"Float64" );
728 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
730 addParameter( rasterTypeParameter.release() );
734 addParameter( probabilityParameter.release() );
737Qgis::DataType QgsRandomGeometricRasterAlgorithm::getRasterDataType(
int typeId )
758bool QgsRandomGeometricRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
760 const double probability = parameterAsDouble( parameters, QStringLiteral(
"PROBABILITY" ), context );
761 mRandomGeometricDistribution = std::geometric_distribution<long>( probability );
765long QgsRandomGeometricRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
767 return mRandomGeometricDistribution( mersenneTwister );
770double QgsRandomGeometricRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
772 return static_cast<double>( mRandomGeometricDistribution( mersenneTwister ) );
778QString QgsRandomNegativeBinomialRasterAlgorithm::name()
const
780 return QStringLiteral(
"createrandomnegativebinomialrasterlayer" );
783QString QgsRandomNegativeBinomialRasterAlgorithm::displayName()
const
785 return QObject::tr(
"Create random raster layer (negative binomial distribution)" );
788QStringList QgsRandomNegativeBinomialRasterAlgorithm::tags()
const
790 return QObject::tr(
"raster,create,random,negative,binomial,negative-binomial" ).split(
',' );
793QString QgsRandomNegativeBinomialRasterAlgorithm::shortHelpString()
const
795 return QObject::tr(
"Generates a raster layer for given extent and cell size "
796 "filled with negative binomially distributed random values.\n"
797 "By default, the values will be chosen given a distribution parameter k of 10.0 "
798 "and a probability of 0.5. "
799 "This can be overridden by using the advanced parameters for k and probability. "
800 "The raster data type is set to Integer types (Integer 16 by default). "
801 "The negative binomial distribution random values are defined as positive integer numbers. "
802 "A floating point raster will represent a cast of "
803 "integer values to floating point." );
806QgsRandomNegativeBinomialRasterAlgorithm *QgsRandomNegativeBinomialRasterAlgorithm::createInstance()
const
808 return new QgsRandomNegativeBinomialRasterAlgorithm();
812void QgsRandomNegativeBinomialRasterAlgorithm::addAlgorithmParams()
814 QStringList rasterDataTypes = QStringList();
815 rasterDataTypes << QStringLiteral(
"Integer16" )
816 << QStringLiteral(
"Unsigned Integer16" )
817 << QStringLiteral(
"Integer32" )
818 << QStringLiteral(
"Unsigned Integer32" )
819 << QStringLiteral(
"Float32" )
820 << QStringLiteral(
"Float64" );
822 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
824 addParameter( rasterTypeParameter.release() );
828 addParameter( kParameter.release() );
832 addParameter( probabilityParameter.release() );
835Qgis::DataType QgsRandomNegativeBinomialRasterAlgorithm::getRasterDataType(
int typeId )
856bool QgsRandomNegativeBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
858 const int k = parameterAsInt( parameters, QStringLiteral(
"K_PARAMETER" ), context );
859 const double probability = parameterAsDouble( parameters, QStringLiteral(
"PROBABILITY" ), context );
860 mRandomNegativeBinomialDistribution = std::negative_binomial_distribution<long>( k, probability );
864long QgsRandomNegativeBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
866 return mRandomNegativeBinomialDistribution( mersenneTwister );
869double QgsRandomNegativeBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
871 return static_cast<double>( mRandomNegativeBinomialDistribution( mersenneTwister ) );
877QString QgsRandomNormalRasterAlgorithm::name()
const
879 return QStringLiteral(
"createrandomnormalrasterlayer" );
882QString QgsRandomNormalRasterAlgorithm::displayName()
const
884 return QObject::tr(
"Create random raster layer (normal distribution)" );
887QStringList QgsRandomNormalRasterAlgorithm::tags()
const
889 return QObject::tr(
"raster,create,normal,distribution,random" ).split(
',' );
892QString QgsRandomNormalRasterAlgorithm::shortHelpString()
const
894 return QObject::tr(
"Generates a raster layer for given extent and cell size "
895 "filled with normally distributed random values.\n"
896 "By default, the values will be chosen given a mean of 0.0 and "
897 "a standard deviation of 1.0. This can be overridden by "
898 "using the advanced parameters for mean and standard deviation "
899 "value. The raster data type is set to Float32 by default "
900 "as the normal distribution random values are floating point numbers." );
903QgsRandomNormalRasterAlgorithm *QgsRandomNormalRasterAlgorithm::createInstance()
const
905 return new QgsRandomNormalRasterAlgorithm();
908void QgsRandomNormalRasterAlgorithm::addAlgorithmParams()
910 QStringList rasterDataTypes = QStringList();
911 rasterDataTypes << QStringLiteral(
"Float32" )
912 << QStringLiteral(
"Float64" );
914 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
916 addParameter( rasterTypeParameter.release() );
920 addParameter( meanParameter.release() );
922 auto stdevParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"STDDEV" ), QStringLiteral(
"Standard deviation of normal distribution" ),
Qgis::ProcessingNumberParameterType::Double, 1,
true, 0 );
924 addParameter( stdevParameter.release() );
927Qgis::DataType QgsRandomNormalRasterAlgorithm::getRasterDataType(
int typeId )
940bool QgsRandomNormalRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
942 const double mean = parameterAsDouble( parameters, QStringLiteral(
"MEAN" ), context );
943 const double stddev = parameterAsDouble( parameters, QStringLiteral(
"STDDEV" ), context );
944 mRandomNormalDistribution = std::normal_distribution<double>( mean, stddev );
948long QgsRandomNormalRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
950 return static_cast<long>( mRandomNormalDistribution( mersenneTwister ) );
953double QgsRandomNormalRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
955 return mRandomNormalDistribution( mersenneTwister );
961QString QgsRandomPoissonRasterAlgorithm::name()
const
963 return QStringLiteral(
"createrandompoissonrasterlayer" );
966QString QgsRandomPoissonRasterAlgorithm::displayName()
const
968 return QObject::tr(
"Create random raster layer (poisson distribution)" );
971QStringList QgsRandomPoissonRasterAlgorithm::tags()
const
973 return QObject::tr(
"raster,create,random,poisson" ).split(
',' );
976QString QgsRandomPoissonRasterAlgorithm::shortHelpString()
const
978 return QObject::tr(
"Generates a raster layer for given extent and cell size "
979 "filled with poisson distributed random values.\n"
980 "By default, the values will be chosen given a mean of 1.0. "
981 "This can be overridden by using the advanced parameter for mean "
982 "value. The raster data type is set to Integer types (Integer16 by default). "
983 "The poisson distribution random values are positive integer numbers. "
984 "A floating point raster will represent a cast of integer values to floating point." );
987QgsRandomPoissonRasterAlgorithm *QgsRandomPoissonRasterAlgorithm::createInstance()
const
989 return new QgsRandomPoissonRasterAlgorithm();
993void QgsRandomPoissonRasterAlgorithm::addAlgorithmParams()
995 QStringList rasterDataTypes = QStringList();
996 rasterDataTypes << QStringLiteral(
"Integer16" )
997 << QStringLiteral(
"Unsigned Integer16" )
998 << QStringLiteral(
"Integer32" )
999 << QStringLiteral(
"Unsigned Integer32" )
1000 << QStringLiteral(
"Float32" )
1001 << QStringLiteral(
"Float64" );
1003 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
1005 addParameter( rasterTypeParameter.release() );
1009 addParameter( upperBoundParameter.release() );
1012Qgis::DataType QgsRandomPoissonRasterAlgorithm::getRasterDataType(
int typeId )
1033bool QgsRandomPoissonRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
1035 const double mean = parameterAsDouble( parameters, QStringLiteral(
"MEAN" ), context );
1036 mRandomPoissonDistribution = std::poisson_distribution<long>( mean );
1040long QgsRandomPoissonRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1042 return mRandomPoissonDistribution( mersenneTwister );
1045double QgsRandomPoissonRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1047 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.
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.
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)