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 QGS_MARK_ALGORITHM_SOURCE
340 mRandomUpperBound = parameterAsDouble( parameters, u
"UPPER_BOUND"_s, context );
341 mRandomLowerBound = parameterAsDouble( parameters, u
"LOWER_BOUND"_s, context );
343 if ( mRandomLowerBound > mRandomUpperBound )
344 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." ) );
346 const int typeId = parameterAsInt( parameters, u
"OUTPUT_TYPE"_s, context );
347 const Qgis::DataType rasterDataType = getRasterDataType( typeId );
349 switch ( rasterDataType )
352 if ( mRandomLowerBound < std::numeric_limits<quint8>::min() || mRandomUpperBound > std::numeric_limits<quint8>::max() )
354 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
355 .arg( std::numeric_limits<quint8>::min() )
356 .arg( std::numeric_limits<quint8>::max() )
362 mRandomUpperBound = std::numeric_limits<quint8>::max();
363 mRandomLowerBound = std::numeric_limits<quint8>::min();
367 if ( mRandomLowerBound < std::numeric_limits<qint8>::min() || mRandomUpperBound > std::numeric_limits<qint8>::max() )
369 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
370 .arg( std::numeric_limits<qint8>::min() )
371 .arg( std::numeric_limits<qint8>::max() )
377 mRandomUpperBound = std::numeric_limits<qint8>::max();
378 mRandomLowerBound = std::numeric_limits<qint8>::min();
382 if ( mRandomLowerBound < std::numeric_limits<qint16>::min() || mRandomUpperBound > std::numeric_limits<qint16>::max() )
384 QObject::tr(
"Raster datasets of type %3 only accept values between %1 and %2. Please choose other bounds for random values." )
385 .arg( std::numeric_limits<qint16>::min() )
386 .arg( std::numeric_limits<qint16>::max() )
387 .arg(
"Integer16"_L1 )
391 mRandomUpperBound = std::numeric_limits<qint16>::max();
392 mRandomLowerBound = std::numeric_limits<qint16>::min();
396 if ( mRandomLowerBound < std::numeric_limits<quint16>::min() || mRandomUpperBound > std::numeric_limits<quint16>::max() )
398 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
399 .arg( std::numeric_limits<quint16>::min() )
400 .arg( std::numeric_limits<quint16>::max() )
401 .arg(
"Unsigned Integer16"_L1 )
405 mRandomUpperBound = std::numeric_limits<quint16>::max();
406 mRandomLowerBound = std::numeric_limits<quint16>::min();
410 if ( mRandomLowerBound < std::numeric_limits<qint32>::min() || mRandomUpperBound > std::numeric_limits<qint32>::max() )
412 QObject::tr(
"Raster datasets of type %3 only accept values between %1 and %2. Please choose other bounds for random values." )
413 .arg( std::numeric_limits<qint32>::min() )
414 .arg( std::numeric_limits<qint32>::max() )
415 .arg(
"Integer32"_L1 )
419 mRandomUpperBound = std::numeric_limits<qint32>::max();
420 mRandomLowerBound = std::numeric_limits<qint32>::min();
424 if ( mRandomLowerBound < std::numeric_limits<quint32>::min() || mRandomUpperBound > std::numeric_limits<quint32>::max() )
426 QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2. Please choose other bounds for random values." )
427 .arg( std::numeric_limits<quint32>::min() )
428 .arg( std::numeric_limits<quint32>::max() )
429 .arg(
"Unsigned Integer32"_L1 )
433 mRandomUpperBound = std::numeric_limits<quint32>::max();
434 mRandomLowerBound = std::numeric_limits<quint32>::min();
440 mRandomUpperBound = std::numeric_limits<float>::max();
441 mRandomLowerBound = std::numeric_limits<float>::min();
447 mRandomUpperBound = std::numeric_limits<double>::max();
448 mRandomLowerBound = std::numeric_limits<double>::min();
461 mRandomUniformIntDistribution = std::uniform_int_distribution<long>( mRandomLowerBound, mRandomUpperBound );
462 mRandomUniformDoubleDistribution = std::uniform_real_distribution<double>( mRandomLowerBound, mRandomUpperBound );
467long QgsRandomUniformRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
469 return mRandomUniformIntDistribution( mersenneTwister );
472double QgsRandomUniformRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
474 return mRandomUniformDoubleDistribution( mersenneTwister );
480QString QgsRandomBinomialRasterAlgorithm::name()
const
482 return u
"createrandombinomialrasterlayer"_s;
485QString QgsRandomBinomialRasterAlgorithm::displayName()
const
487 return QObject::tr(
"Create random raster layer (binomial distribution)" );
490QStringList QgsRandomBinomialRasterAlgorithm::tags()
const
492 return QObject::tr(
"raster,create,binomial,random" ).split(
',' );
495QString QgsRandomBinomialRasterAlgorithm::shortHelpString()
const
498 "This algorithm generates a raster layer for a given extent and cell size "
499 "filled with binomially distributed random values.\n"
500 "By default, the values will be chosen given an N of 10 and a probability of 0.5. "
501 "This can be overridden by using the advanced parameter for N and probability. "
502 "The raster data type is set to Integer types (Integer16 by default). "
503 "The binomial distribution random values are defined as positive integer numbers. "
504 "A floating point raster will represent a cast of integer values "
509QString QgsRandomBinomialRasterAlgorithm::shortDescription()
const
512 "Generates a raster layer for a given extent and cell size "
513 "filled with binomially distributed random values."
517QgsRandomBinomialRasterAlgorithm *QgsRandomBinomialRasterAlgorithm::createInstance()
const
519 return new QgsRandomBinomialRasterAlgorithm();
523void QgsRandomBinomialRasterAlgorithm::addAlgorithmParams()
525 QStringList rasterDataTypes = QStringList();
526 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
528 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
529 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
531 addParameter( rasterTypeParameter.release() );
535 addParameter( nParameter.release() );
539 addParameter( probabilityParameter.release() );
542Qgis::DataType QgsRandomBinomialRasterAlgorithm::getRasterDataType(
int typeId )
563bool QgsRandomBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
565 QGS_MARK_ALGORITHM_SOURCE
567 const int n = parameterAsInt( parameters, u
"N"_s, context );
568 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
569 mRandombinomialDistribution = std::binomial_distribution<long>( n, probability );
573long QgsRandomBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
575 return mRandombinomialDistribution( mersenneTwister );
578double QgsRandomBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
580 return static_cast<double>( mRandombinomialDistribution( mersenneTwister ) );
586QString QgsRandomExponentialRasterAlgorithm::name()
const
588 return u
"createrandomexponentialrasterlayer"_s;
591QString QgsRandomExponentialRasterAlgorithm::displayName()
const
593 return QObject::tr(
"Create random raster layer (exponential distribution)" );
596QStringList QgsRandomExponentialRasterAlgorithm::tags()
const
598 return QObject::tr(
"raster,create,random,exponential" ).split(
',' );
601QString QgsRandomExponentialRasterAlgorithm::shortHelpString()
const
604 "This algorithm generates a raster layer for a given extent and cell size "
605 "filled with exponentially distributed random values.\n"
606 "By default, the values will be chosen given a lambda of 1.0. "
607 "This can be overridden by using the advanced parameter for lambda. "
608 "The raster data type is set to Float32 by default as "
609 "the exponential distribution random values are floating point numbers."
613QString QgsRandomExponentialRasterAlgorithm::shortDescription()
const
616 "Generates a raster layer for a given extent and cell size "
617 "filled with exponentially distributed random values."
621QgsRandomExponentialRasterAlgorithm *QgsRandomExponentialRasterAlgorithm::createInstance()
const
623 return new QgsRandomExponentialRasterAlgorithm();
627void QgsRandomExponentialRasterAlgorithm::addAlgorithmParams()
629 QStringList rasterDataTypes = QStringList();
630 rasterDataTypes << u
"Float32"_s << u
"Float64"_s;
632 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
633 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
635 addParameter( rasterTypeParameter.release() );
639 addParameter( lambdaParameter.release() );
642Qgis::DataType QgsRandomExponentialRasterAlgorithm::getRasterDataType(
int typeId )
655bool QgsRandomExponentialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
657 QGS_MARK_ALGORITHM_SOURCE
659 const double lambda = parameterAsDouble( parameters, u
"LAMBDA"_s, context );
660 mRandomExponentialDistribution = std::exponential_distribution<double>( lambda );
664long QgsRandomExponentialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
666 return static_cast<long>( mRandomExponentialDistribution( mersenneTwister ) );
669double QgsRandomExponentialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
671 return mRandomExponentialDistribution( mersenneTwister );
677QString QgsRandomGammaRasterAlgorithm::name()
const
679 return u
"createrandomgammarasterlayer"_s;
682QString QgsRandomGammaRasterAlgorithm::displayName()
const
684 return QObject::tr(
"Create random raster layer (gamma distribution)" );
687QStringList QgsRandomGammaRasterAlgorithm::tags()
const
689 return QObject::tr(
"raster,create,random,gamma" ).split(
',' );
692QString QgsRandomGammaRasterAlgorithm::shortHelpString()
const
695 "This algorithm generates a raster layer for a given extent and cell size "
696 "filled with gamma distributed random values.\n"
697 "By default, the values will be chosen given an alpha and beta value of 1.0. "
698 "This can be overridden by using the advanced parameter for alpha and beta. "
699 "The raster data type is set to Float32 by default as "
700 "the gamma distribution random values are floating point numbers."
704QString QgsRandomGammaRasterAlgorithm::shortDescription()
const
707 "Generates a raster layer for a given extent and cell size "
708 "filled with gamma distributed random values."
712QgsRandomGammaRasterAlgorithm *QgsRandomGammaRasterAlgorithm::createInstance()
const
714 return new QgsRandomGammaRasterAlgorithm();
718void QgsRandomGammaRasterAlgorithm::addAlgorithmParams()
720 QStringList rasterDataTypes = QStringList();
721 rasterDataTypes << u
"Float32"_s << u
"Float64"_s;
723 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
724 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
726 addParameter( rasterTypeParameter.release() );
730 addParameter( alphaParameter.release() );
734 addParameter( betaParameter.release() );
737Qgis::DataType QgsRandomGammaRasterAlgorithm::getRasterDataType(
int typeId )
750bool QgsRandomGammaRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
752 QGS_MARK_ALGORITHM_SOURCE
754 const double alpha = parameterAsDouble( parameters, u
"ALPHA"_s, context );
755 const double beta = parameterAsDouble( parameters, u
"BETA"_s, context );
756 mRandomGammaDistribution = std::gamma_distribution<double>( alpha, beta );
760long QgsRandomGammaRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
762 return static_cast<long>( mRandomGammaDistribution( mersenneTwister ) );
765double QgsRandomGammaRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
767 return mRandomGammaDistribution( mersenneTwister );
773QString QgsRandomGeometricRasterAlgorithm::name()
const
775 return u
"createrandomgeometricrasterlayer"_s;
778QString QgsRandomGeometricRasterAlgorithm::displayName()
const
780 return QObject::tr(
"Create random raster layer (geometric distribution)" );
783QStringList QgsRandomGeometricRasterAlgorithm::tags()
const
785 return QObject::tr(
"raster,create,random,geometric" ).split(
',' );
788QString QgsRandomGeometricRasterAlgorithm::shortHelpString()
const
791 "This algorithm generates a raster layer for a given extent and cell size "
792 "filled with geometrically distributed random values.\n"
793 "By default, the values will be chosen given a probability of 0.5. "
794 "This can be overridden by using the advanced parameter for mean "
795 "value. The raster data type is set to Integer types (Integer16 by default). "
796 "The geometric distribution random values are defined as positive integer numbers. "
797 "A floating point raster will represent a cast of "
798 "integer values to floating point."
802QString QgsRandomGeometricRasterAlgorithm::shortDescription()
const
805 "Generates a raster layer for a given extent and cell size "
806 "filled with geometrically distributed random values."
810QgsRandomGeometricRasterAlgorithm *QgsRandomGeometricRasterAlgorithm::createInstance()
const
812 return new QgsRandomGeometricRasterAlgorithm();
816void QgsRandomGeometricRasterAlgorithm::addAlgorithmParams()
818 QStringList rasterDataTypes = QStringList();
819 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
821 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
822 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
824 addParameter( rasterTypeParameter.release() );
828 addParameter( probabilityParameter.release() );
831Qgis::DataType QgsRandomGeometricRasterAlgorithm::getRasterDataType(
int typeId )
852bool QgsRandomGeometricRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
854 QGS_MARK_ALGORITHM_SOURCE
856 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
857 mRandomGeometricDistribution = std::geometric_distribution<long>( probability );
861long QgsRandomGeometricRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
863 return mRandomGeometricDistribution( mersenneTwister );
866double QgsRandomGeometricRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
868 return static_cast<double>( mRandomGeometricDistribution( mersenneTwister ) );
874QString QgsRandomNegativeBinomialRasterAlgorithm::name()
const
876 return u
"createrandomnegativebinomialrasterlayer"_s;
879QString QgsRandomNegativeBinomialRasterAlgorithm::displayName()
const
881 return QObject::tr(
"Create random raster layer (negative binomial distribution)" );
884QStringList QgsRandomNegativeBinomialRasterAlgorithm::tags()
const
886 return QObject::tr(
"raster,create,random,negative,binomial,negative-binomial" ).split(
',' );
889QString QgsRandomNegativeBinomialRasterAlgorithm::shortHelpString()
const
892 "This algorithm generates a raster layer for a given extent and cell size "
893 "filled with negative binomially distributed random values.\n"
894 "By default, the values will be chosen given a distribution parameter k of 10.0 "
895 "and a probability of 0.5. "
896 "This can be overridden by using the advanced parameters for k and probability. "
897 "The raster data type is set to Integer types (Integer 16 by default). "
898 "The negative binomial distribution random values are defined as positive integer numbers. "
899 "A floating point raster will represent a cast of "
900 "integer values to floating point."
904QString QgsRandomNegativeBinomialRasterAlgorithm::shortDescription()
const
907 "Generates a raster layer for a given extent and cell size "
908 "filled with negative binomially distributed random values."
912QgsRandomNegativeBinomialRasterAlgorithm *QgsRandomNegativeBinomialRasterAlgorithm::createInstance()
const
914 return new QgsRandomNegativeBinomialRasterAlgorithm();
918void QgsRandomNegativeBinomialRasterAlgorithm::addAlgorithmParams()
920 QStringList rasterDataTypes = QStringList();
921 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
923 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
924 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
926 addParameter( rasterTypeParameter.release() );
930 addParameter( kParameter.release() );
934 addParameter( probabilityParameter.release() );
937Qgis::DataType QgsRandomNegativeBinomialRasterAlgorithm::getRasterDataType(
int typeId )
958bool QgsRandomNegativeBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
960 QGS_MARK_ALGORITHM_SOURCE
962 const int k = parameterAsInt( parameters, u
"K_PARAMETER"_s, context );
963 const double probability = parameterAsDouble( parameters, u
"PROBABILITY"_s, context );
964 mRandomNegativeBinomialDistribution = std::negative_binomial_distribution<long>( k, probability );
968long QgsRandomNegativeBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
970 return mRandomNegativeBinomialDistribution( mersenneTwister );
973double QgsRandomNegativeBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
975 return static_cast<double>( mRandomNegativeBinomialDistribution( mersenneTwister ) );
981QString QgsRandomNormalRasterAlgorithm::name()
const
983 return u
"createrandomnormalrasterlayer"_s;
986QString QgsRandomNormalRasterAlgorithm::displayName()
const
988 return QObject::tr(
"Create random raster layer (normal distribution)" );
991QStringList QgsRandomNormalRasterAlgorithm::tags()
const
993 return QObject::tr(
"raster,create,normal,distribution,random" ).split(
',' );
996QString QgsRandomNormalRasterAlgorithm::shortHelpString()
const
999 "This algorithm generates a raster layer for a given extent and cell size "
1000 "filled with normally distributed random values.\n"
1001 "By default, the values will be chosen given a mean of 0.0 and "
1002 "a standard deviation of 1.0. This can be overridden by "
1003 "using the advanced parameters for mean and standard deviation "
1004 "value. The raster data type is set to Float32 by default "
1005 "as the normal distribution random values are floating point numbers."
1009QString QgsRandomNormalRasterAlgorithm::shortDescription()
const
1012 "Generates a raster layer for a given extent and cell size "
1013 "filled with normally distributed random values."
1017QgsRandomNormalRasterAlgorithm *QgsRandomNormalRasterAlgorithm::createInstance()
const
1019 return new QgsRandomNormalRasterAlgorithm();
1022void QgsRandomNormalRasterAlgorithm::addAlgorithmParams()
1024 QStringList rasterDataTypes = QStringList();
1025 rasterDataTypes << u
"Float32"_s << u
"Float64"_s;
1027 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
1028 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
1030 addParameter( rasterTypeParameter.release() );
1034 addParameter( meanParameter.release() );
1038 addParameter( stdevParameter.release() );
1041Qgis::DataType QgsRandomNormalRasterAlgorithm::getRasterDataType(
int typeId )
1054bool QgsRandomNormalRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
1056 QGS_MARK_ALGORITHM_SOURCE
1058 const double mean = parameterAsDouble( parameters, u
"MEAN"_s, context );
1059 const double stddev = parameterAsDouble( parameters, u
"STDDEV"_s, context );
1060 mRandomNormalDistribution = std::normal_distribution<double>( mean, stddev );
1064long QgsRandomNormalRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1066 return static_cast<long>( mRandomNormalDistribution( mersenneTwister ) );
1069double QgsRandomNormalRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1071 return mRandomNormalDistribution( mersenneTwister );
1077QString QgsRandomPoissonRasterAlgorithm::name()
const
1079 return u
"createrandompoissonrasterlayer"_s;
1082QString QgsRandomPoissonRasterAlgorithm::displayName()
const
1084 return QObject::tr(
"Create random raster layer (poisson distribution)" );
1087QStringList QgsRandomPoissonRasterAlgorithm::tags()
const
1089 return QObject::tr(
"raster,create,random,poisson" ).split(
',' );
1092QString QgsRandomPoissonRasterAlgorithm::shortHelpString()
const
1095 "This algorithm generates a raster layer for a given extent and cell size "
1096 "filled with poisson distributed random values.\n"
1097 "By default, the values will be chosen given a mean of 1.0. "
1098 "This can be overridden by using the advanced parameter for mean "
1099 "value. The raster data type is set to Integer types (Integer16 by default). "
1100 "The poisson distribution random values are positive integer numbers. "
1101 "A floating point raster will represent a cast of integer values to floating point."
1105QString QgsRandomPoissonRasterAlgorithm::shortDescription()
const
1108 "Generates a raster layer for a given extent and cell size "
1109 "filled with poisson distributed random values."
1113QgsRandomPoissonRasterAlgorithm *QgsRandomPoissonRasterAlgorithm::createInstance()
const
1115 return new QgsRandomPoissonRasterAlgorithm();
1119void QgsRandomPoissonRasterAlgorithm::addAlgorithmParams()
1121 QStringList rasterDataTypes = QStringList();
1122 rasterDataTypes << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
1124 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
1125 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
1127 addParameter( rasterTypeParameter.release() );
1131 addParameter( upperBoundParameter.release() );
1134Qgis::DataType QgsRandomPoissonRasterAlgorithm::getRasterDataType(
int typeId )
1155bool QgsRandomPoissonRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
1157 QGS_MARK_ALGORITHM_SOURCE
1159 const double mean = parameterAsDouble( parameters, u
"MEAN"_s, context );
1160 mRandomPoissonDistribution = std::poisson_distribution<long>( mean );
1164long QgsRandomPoissonRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
1166 return mRandomPoissonDistribution( mersenneTwister );
1169double QgsRandomPoissonRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
1171 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).