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
268 "This algorithm generates a raster layer for a given extent and cell size "
269 "filled with random values.\n"
270 "By default, the values will range between the minimum and "
271 "maximum value of the specified output raster type. This can "
272 "be overridden by using the advanced parameters for lower and "
273 "upper bound value. If the bounds have the same value or both "
274 "are zero (default) the algorithm will create random values in "
275 "the full value range of the chosen raster data type. "
276 "Choosing bounds outside the acceptable range of the output "
277 "raster type will abort the algorithm."
281QString QgsRandomUniformRasterAlgorithm::shortDescription()
const
284 "Generates a raster layer for a given extent and cell size "
285 "filled with random values."
289QgsRandomUniformRasterAlgorithm *QgsRandomUniformRasterAlgorithm::createInstance()
const
291 return new QgsRandomUniformRasterAlgorithm();
294void QgsRandomUniformRasterAlgorithm::addAlgorithmParams()
296 QStringList rasterDataTypes = QStringList();
297 rasterDataTypes << u
"Byte"_s << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
299 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
300 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
302 addParameter( rasterTypeParameter.release() );
306 addParameter( lowerBoundParameter.release() );
310 addParameter( upperBoundParameter.release() );
313Qgis::DataType QgsRandomUniformRasterAlgorithm::getRasterDataType(
int typeId )
336bool QgsRandomUniformRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
338 mRandomUpperBound = parameterAsDouble( parameters, u
"UPPER_BOUND"_s, context );
339 mRandomLowerBound = parameterAsDouble( parameters, u
"LOWER_BOUND"_s, context );
341 if ( mRandomLowerBound > mRandomUpperBound )
342 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." ) );
344 const int typeId = parameterAsInt( parameters, u
"OUTPUT_TYPE"_s, context );
345 const Qgis::DataType rasterDataType = getRasterDataType( typeId );
347 switch ( rasterDataType )
350 if ( mRandomLowerBound < std::numeric_limits<quint8>::min() || mRandomUpperBound > std::numeric_limits<quint8>::max() )
352 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
353 .arg( std::numeric_limits<quint8>::min() )
354 .arg( std::numeric_limits<quint8>::max() )
360 mRandomUpperBound = std::numeric_limits<quint8>::max();
361 mRandomLowerBound = std::numeric_limits<quint8>::min();
365 if ( mRandomLowerBound < std::numeric_limits<qint8>::min() || mRandomUpperBound > std::numeric_limits<qint8>::max() )
367 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
368 .arg( std::numeric_limits<qint8>::min() )
369 .arg( std::numeric_limits<qint8>::max() )
375 mRandomUpperBound = std::numeric_limits<qint8>::max();
376 mRandomLowerBound = std::numeric_limits<qint8>::min();
380 if ( mRandomLowerBound < std::numeric_limits<qint16>::min() || mRandomUpperBound > std::numeric_limits<qint16>::max() )
382 QObject::tr(
"Raster datasets of type %3 only accept values between %1 and %2. Please choose other bounds for random values." )
383 .arg( std::numeric_limits<qint16>::min() )
384 .arg( std::numeric_limits<qint16>::max() )
385 .arg(
"Integer16"_L1 )
389 mRandomUpperBound = std::numeric_limits<qint16>::max();
390 mRandomLowerBound = std::numeric_limits<qint16>::min();
394 if ( mRandomLowerBound < std::numeric_limits<quint16>::min() || mRandomUpperBound > std::numeric_limits<quint16>::max() )
396 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
397 .arg( std::numeric_limits<quint16>::min() )
398 .arg( std::numeric_limits<quint16>::max() )
399 .arg(
"Unsigned Integer16"_L1 )
403 mRandomUpperBound = std::numeric_limits<quint16>::max();
404 mRandomLowerBound = std::numeric_limits<quint16>::min();
408 if ( mRandomLowerBound < std::numeric_limits<qint32>::min() || mRandomUpperBound > std::numeric_limits<qint32>::max() )
410 QObject::tr(
"Raster datasets of type %3 only accept values between %1 and %2. Please choose other bounds for random values." )
411 .arg( std::numeric_limits<qint32>::min() )
412 .arg( std::numeric_limits<qint32>::max() )
413 .arg(
"Integer32"_L1 )
417 mRandomUpperBound = std::numeric_limits<qint32>::max();
418 mRandomLowerBound = std::numeric_limits<qint32>::min();
422 if ( mRandomLowerBound < std::numeric_limits<quint32>::min() || mRandomUpperBound > std::numeric_limits<quint32>::max() )
424 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
425 .arg( std::numeric_limits<quint32>::min() )
426 .arg( std::numeric_limits<quint32>::max() )
427 .arg(
"Unsigned Integer32"_L1 )
431 mRandomUpperBound = std::numeric_limits<quint32>::max();
432 mRandomLowerBound = std::numeric_limits<quint32>::min();
438 mRandomUpperBound = std::numeric_limits<float>::max();
439 mRandomLowerBound = std::numeric_limits<float>::min();
445 mRandomUpperBound = std::numeric_limits<double>::max();
446 mRandomLowerBound = std::numeric_limits<double>::min();
459 mRandomUniformIntDistribution = std::uniform_int_distribution<long>( mRandomLowerBound, mRandomUpperBound );
460 mRandomUniformDoubleDistribution = std::uniform_real_distribution<double>( mRandomLowerBound, mRandomUpperBound );
465long QgsRandomUniformRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
467 return mRandomUniformIntDistribution( mersenneTwister );
470double QgsRandomUniformRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
472 return mRandomUniformDoubleDistribution( mersenneTwister );
478QString QgsRandomBinomialRasterAlgorithm::name()
const
480 return u
"createrandombinomialrasterlayer"_s;
483QString QgsRandomBinomialRasterAlgorithm::displayName()
const
485 return QObject::tr(
"Create random raster layer (binomial distribution)" );
488QStringList QgsRandomBinomialRasterAlgorithm::tags()
const
490 return QObject::tr(
"raster,create,binomial,random" ).split(
',' );
493QString QgsRandomBinomialRasterAlgorithm::shortHelpString()
const
496 "This algorithm generates a raster layer for a given extent and cell size "
497 "filled with binomially distributed random values.\n"
498 "By default, the values will be chosen given an N of 10 and a probability of 0.5. "
499 "This can be overridden by using the advanced parameter for N and probability. "
500 "The raster data type is set to Integer types (Integer16 by default). "
501 "The binomial distribution random values are defined as positive integer numbers. "
502 "A floating point raster will represent a cast of integer values "
507QString QgsRandomBinomialRasterAlgorithm::shortDescription()
const
510 "Generates a raster layer for a given extent and cell size "
511 "filled with binomially distributed random values."
515QgsRandomBinomialRasterAlgorithm *QgsRandomBinomialRasterAlgorithm::createInstance()
const
517 return new QgsRandomBinomialRasterAlgorithm();
521void QgsRandomBinomialRasterAlgorithm::addAlgorithmParams()
523 QStringList rasterDataTypes = QStringList();
524 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
526 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
527 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
529 addParameter( rasterTypeParameter.release() );
533 addParameter( nParameter.release() );
537 addParameter( probabilityParameter.release() );
540Qgis::DataType QgsRandomBinomialRasterAlgorithm::getRasterDataType(
int typeId )
561bool QgsRandomBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
563 const int n = parameterAsInt( parameters, u
"N"_s, context );
564 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
565 mRandombinomialDistribution = std::binomial_distribution<long>( n, probability );
569long QgsRandomBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
571 return mRandombinomialDistribution( mersenneTwister );
574double QgsRandomBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
576 return static_cast<double>( mRandombinomialDistribution( mersenneTwister ) );
582QString QgsRandomExponentialRasterAlgorithm::name()
const
584 return u
"createrandomexponentialrasterlayer"_s;
587QString QgsRandomExponentialRasterAlgorithm::displayName()
const
589 return QObject::tr(
"Create random raster layer (exponential distribution)" );
592QStringList QgsRandomExponentialRasterAlgorithm::tags()
const
594 return QObject::tr(
"raster,create,random,exponential" ).split(
',' );
597QString QgsRandomExponentialRasterAlgorithm::shortHelpString()
const
600 "This algorithm generates a raster layer for a given extent and cell size "
601 "filled with exponentially distributed random values.\n"
602 "By default, the values will be chosen given a lambda of 1.0. "
603 "This can be overridden by using the advanced parameter for lambda. "
604 "The raster data type is set to Float32 by default as "
605 "the exponential distribution random values are floating point numbers."
609QString QgsRandomExponentialRasterAlgorithm::shortDescription()
const
612 "Generates a raster layer for a given extent and cell size "
613 "filled with exponentially distributed random values."
617QgsRandomExponentialRasterAlgorithm *QgsRandomExponentialRasterAlgorithm::createInstance()
const
619 return new QgsRandomExponentialRasterAlgorithm();
623void QgsRandomExponentialRasterAlgorithm::addAlgorithmParams()
625 QStringList rasterDataTypes = QStringList();
626 rasterDataTypes << u
"Float32"_s << u
"Float64"_s;
628 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
629 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
631 addParameter( rasterTypeParameter.release() );
635 addParameter( lambdaParameter.release() );
638Qgis::DataType QgsRandomExponentialRasterAlgorithm::getRasterDataType(
int typeId )
651bool QgsRandomExponentialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
653 const double lambda = parameterAsDouble( parameters, u
"LAMBDA"_s, context );
654 mRandomExponentialDistribution = std::exponential_distribution<double>( lambda );
658long QgsRandomExponentialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
660 return static_cast<long>( mRandomExponentialDistribution( mersenneTwister ) );
663double QgsRandomExponentialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
665 return mRandomExponentialDistribution( mersenneTwister );
671QString QgsRandomGammaRasterAlgorithm::name()
const
673 return u
"createrandomgammarasterlayer"_s;
676QString QgsRandomGammaRasterAlgorithm::displayName()
const
678 return QObject::tr(
"Create random raster layer (gamma distribution)" );
681QStringList QgsRandomGammaRasterAlgorithm::tags()
const
683 return QObject::tr(
"raster,create,random,gamma" ).split(
',' );
686QString QgsRandomGammaRasterAlgorithm::shortHelpString()
const
689 "This algorithm generates a raster layer for a given extent and cell size "
690 "filled with gamma distributed random values.\n"
691 "By default, the values will be chosen given an alpha and beta value of 1.0. "
692 "This can be overridden by using the advanced parameter for alpha and beta. "
693 "The raster data type is set to Float32 by default as "
694 "the gamma distribution random values are floating point numbers."
698QString QgsRandomGammaRasterAlgorithm::shortDescription()
const
701 "Generates a raster layer for a given extent and cell size "
702 "filled with gamma distributed random values."
706QgsRandomGammaRasterAlgorithm *QgsRandomGammaRasterAlgorithm::createInstance()
const
708 return new QgsRandomGammaRasterAlgorithm();
712void QgsRandomGammaRasterAlgorithm::addAlgorithmParams()
714 QStringList rasterDataTypes = QStringList();
715 rasterDataTypes << u
"Float32"_s << u
"Float64"_s;
717 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
718 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
720 addParameter( rasterTypeParameter.release() );
724 addParameter( alphaParameter.release() );
728 addParameter( betaParameter.release() );
731Qgis::DataType QgsRandomGammaRasterAlgorithm::getRasterDataType(
int typeId )
744bool QgsRandomGammaRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
746 const double alpha = parameterAsDouble( parameters, u
"ALPHA"_s, context );
747 const double beta = parameterAsDouble( parameters, u
"BETA"_s, context );
748 mRandomGammaDistribution = std::gamma_distribution<double>( alpha, beta );
752long QgsRandomGammaRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
754 return static_cast<long>( mRandomGammaDistribution( mersenneTwister ) );
757double QgsRandomGammaRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
759 return mRandomGammaDistribution( mersenneTwister );
765QString QgsRandomGeometricRasterAlgorithm::name()
const
767 return u
"createrandomgeometricrasterlayer"_s;
770QString QgsRandomGeometricRasterAlgorithm::displayName()
const
772 return QObject::tr(
"Create random raster layer (geometric distribution)" );
775QStringList QgsRandomGeometricRasterAlgorithm::tags()
const
777 return QObject::tr(
"raster,create,random,geometric" ).split(
',' );
780QString QgsRandomGeometricRasterAlgorithm::shortHelpString()
const
783 "This algorithm generates a raster layer for a given extent and cell size "
784 "filled with geometrically distributed random values.\n"
785 "By default, the values will be chosen given a probability of 0.5. "
786 "This can be overridden by using the advanced parameter for mean "
787 "value. The raster data type is set to Integer types (Integer16 by default). "
788 "The geometric distribution random values are defined as positive integer numbers. "
789 "A floating point raster will represent a cast of "
790 "integer values to floating point."
794QString QgsRandomGeometricRasterAlgorithm::shortDescription()
const
797 "Generates a raster layer for a given extent and cell size "
798 "filled with geometrically distributed random values."
802QgsRandomGeometricRasterAlgorithm *QgsRandomGeometricRasterAlgorithm::createInstance()
const
804 return new QgsRandomGeometricRasterAlgorithm();
808void QgsRandomGeometricRasterAlgorithm::addAlgorithmParams()
810 QStringList rasterDataTypes = QStringList();
811 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
813 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
814 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
816 addParameter( rasterTypeParameter.release() );
820 addParameter( probabilityParameter.release() );
823Qgis::DataType QgsRandomGeometricRasterAlgorithm::getRasterDataType(
int typeId )
844bool QgsRandomGeometricRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
846 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
847 mRandomGeometricDistribution = std::geometric_distribution<long>( probability );
851long QgsRandomGeometricRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
853 return mRandomGeometricDistribution( mersenneTwister );
856double QgsRandomGeometricRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
858 return static_cast<double>( mRandomGeometricDistribution( mersenneTwister ) );
864QString QgsRandomNegativeBinomialRasterAlgorithm::name()
const
866 return u
"createrandomnegativebinomialrasterlayer"_s;
869QString QgsRandomNegativeBinomialRasterAlgorithm::displayName()
const
871 return QObject::tr(
"Create random raster layer (negative binomial distribution)" );
874QStringList QgsRandomNegativeBinomialRasterAlgorithm::tags()
const
876 return QObject::tr(
"raster,create,random,negative,binomial,negative-binomial" ).split(
',' );
879QString QgsRandomNegativeBinomialRasterAlgorithm::shortHelpString()
const
882 "This algorithm generates a raster layer for a given extent and cell size "
883 "filled with negative binomially distributed random values.\n"
884 "By default, the values will be chosen given a distribution parameter k of 10.0 "
885 "and a probability of 0.5. "
886 "This can be overridden by using the advanced parameters for k and probability. "
887 "The raster data type is set to Integer types (Integer 16 by default). "
888 "The negative binomial distribution random values are defined as positive integer numbers. "
889 "A floating point raster will represent a cast of "
890 "integer values to floating point."
894QString QgsRandomNegativeBinomialRasterAlgorithm::shortDescription()
const
897 "Generates a raster layer for a given extent and cell size "
898 "filled with negative binomially distributed random values."
902QgsRandomNegativeBinomialRasterAlgorithm *QgsRandomNegativeBinomialRasterAlgorithm::createInstance()
const
904 return new QgsRandomNegativeBinomialRasterAlgorithm();
908void QgsRandomNegativeBinomialRasterAlgorithm::addAlgorithmParams()
910 QStringList rasterDataTypes = QStringList();
911 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
913 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
914 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
916 addParameter( rasterTypeParameter.release() );
920 addParameter( kParameter.release() );
924 addParameter( probabilityParameter.release() );
927Qgis::DataType QgsRandomNegativeBinomialRasterAlgorithm::getRasterDataType(
int typeId )
948bool QgsRandomNegativeBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
950 const int k = parameterAsInt( parameters, u
"K_PARAMETER"_s, context );
951 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
952 mRandomNegativeBinomialDistribution = std::negative_binomial_distribution<long>( k, probability );
956long QgsRandomNegativeBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
958 return mRandomNegativeBinomialDistribution( mersenneTwister );
961double QgsRandomNegativeBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
963 return static_cast<double>( mRandomNegativeBinomialDistribution( mersenneTwister ) );
969QString QgsRandomNormalRasterAlgorithm::name()
const
971 return u
"createrandomnormalrasterlayer"_s;
974QString QgsRandomNormalRasterAlgorithm::displayName()
const
976 return QObject::tr(
"Create random raster layer (normal distribution)" );
979QStringList QgsRandomNormalRasterAlgorithm::tags()
const
981 return QObject::tr(
"raster,create,normal,distribution,random" ).split(
',' );
984QString QgsRandomNormalRasterAlgorithm::shortHelpString()
const
987 "This algorithm generates a raster layer for a given extent and cell size "
988 "filled with normally distributed random values.\n"
989 "By default, the values will be chosen given a mean of 0.0 and "
990 "a standard deviation of 1.0. This can be overridden by "
991 "using the advanced parameters for mean and standard deviation "
992 "value. The raster data type is set to Float32 by default "
993 "as the normal distribution random values are floating point numbers."
997QString QgsRandomNormalRasterAlgorithm::shortDescription()
const
1000 "Generates a raster layer for a given extent and cell size "
1001 "filled with normally distributed random values."
1005QgsRandomNormalRasterAlgorithm *QgsRandomNormalRasterAlgorithm::createInstance()
const
1007 return new QgsRandomNormalRasterAlgorithm();
1010void QgsRandomNormalRasterAlgorithm::addAlgorithmParams()
1012 QStringList rasterDataTypes = QStringList();
1013 rasterDataTypes << u
"Float32"_s << u
"Float64"_s;
1015 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
1016 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
1018 addParameter( rasterTypeParameter.release() );
1022 addParameter( meanParameter.release() );
1026 addParameter( stdevParameter.release() );
1029Qgis::DataType QgsRandomNormalRasterAlgorithm::getRasterDataType(
int typeId )
1042bool QgsRandomNormalRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
1044 const double mean = parameterAsDouble( parameters, u
"MEAN"_s, context );
1045 const double stddev = parameterAsDouble( parameters, u
"STDDEV"_s, context );
1046 mRandomNormalDistribution = std::normal_distribution<double>( mean, stddev );
1050long QgsRandomNormalRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1052 return static_cast<long>( mRandomNormalDistribution( mersenneTwister ) );
1055double QgsRandomNormalRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1057 return mRandomNormalDistribution( mersenneTwister );
1063QString QgsRandomPoissonRasterAlgorithm::name()
const
1065 return u
"createrandompoissonrasterlayer"_s;
1068QString QgsRandomPoissonRasterAlgorithm::displayName()
const
1070 return QObject::tr(
"Create random raster layer (poisson distribution)" );
1073QStringList QgsRandomPoissonRasterAlgorithm::tags()
const
1075 return QObject::tr(
"raster,create,random,poisson" ).split(
',' );
1078QString QgsRandomPoissonRasterAlgorithm::shortHelpString()
const
1081 "This algorithm generates a raster layer for a given extent and cell size "
1082 "filled with poisson distributed random values.\n"
1083 "By default, the values will be chosen given a mean of 1.0. "
1084 "This can be overridden by using the advanced parameter for mean "
1085 "value. The raster data type is set to Integer types (Integer16 by default). "
1086 "The poisson distribution random values are positive integer numbers. "
1087 "A floating point raster will represent a cast of integer values to floating point."
1091QString QgsRandomPoissonRasterAlgorithm::shortDescription()
const
1094 "Generates a raster layer for a given extent and cell size "
1095 "filled with poisson distributed random values."
1099QgsRandomPoissonRasterAlgorithm *QgsRandomPoissonRasterAlgorithm::createInstance()
const
1101 return new QgsRandomPoissonRasterAlgorithm();
1105void QgsRandomPoissonRasterAlgorithm::addAlgorithmParams()
1107 QStringList rasterDataTypes = QStringList();
1108 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
1110 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
1111 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
1113 addParameter( rasterTypeParameter.release() );
1117 addParameter( upperBoundParameter.release() );
1120Qgis::DataType QgsRandomPoissonRasterAlgorithm::getRasterDataType(
int typeId )
1141bool QgsRandomPoissonRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
1143 const double mean = parameterAsDouble( parameters, u
"MEAN"_s, context );
1144 mRandomPoissonDistribution = std::poisson_distribution<long>( mean );
1148long QgsRandomPoissonRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1150 return mRandomPoissonDistribution( mersenneTwister );
1153double QgsRandomPoissonRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1155 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).