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" ) ) );
55 mCrs = parameterAsCrs( parameters, QStringLiteral(
"TARGET_CRS" ), context );
56 mExtent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, mCrs );
57 mPixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
64 const int typeId = parameterAsInt( parameters, QStringLiteral(
"OUTPUT_TYPE" ), context );
66 mRasterDataType = getRasterDataType( typeId );
67 prepareRandomParameters( parameters, context );
69 std::random_device rd {};
70 std::mt19937 mersenneTwister{rd()};
72 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
73 const QFileInfo fi( outputFile );
76 const int rows = std::max( std::ceil( mExtent.height() / mPixelSize ), 1.0 );
77 const int cols = std::max( std::ceil( mExtent.width() / mPixelSize ), 1.0 );
81 const QgsRectangle rasterExtent =
QgsRectangle( mExtent.xMinimum(), mExtent.yMaximum() - ( rows * mPixelSize ), mExtent.xMinimum() + ( cols * mPixelSize ), mExtent.yMaximum() );
83 std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
84 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
85 writer->setOutputFormat( outputFormat );
86 std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( mRasterDataType, cols, rows, rasterExtent, mCrs ) );
89 if ( !provider->isValid() )
92 const double step = rows > 0 ? 100.0 / rows : 1;
94 for (
int row = 0; row < rows ; row++ )
102 switch ( mRasterDataType )
106 std::vector<quint8> byteRow( cols );
107 for (
int col = 0; col < cols; col++ )
109 byteRow[col] =
static_cast<quint8
>( generateRandomLongValue( mersenneTwister ) );
116 std::vector<qint16> int16Row( cols );
117 for (
int col = 0; col < cols; col++ )
119 int16Row[col] =
static_cast<qint16
>( generateRandomLongValue( mersenneTwister ) );
126 std::vector<quint16> uInt16Row( cols );
127 for (
int col = 0; col < cols; col++ )
129 uInt16Row[col] =
static_cast<quint16
>( generateRandomLongValue( mersenneTwister ) );
136 std::vector<qint32> int32Row( cols );
137 for (
int col = 0; col < cols; col++ )
139 int32Row[col] = generateRandomLongValue( mersenneTwister );
146 std::vector<quint32> uInt32Row( cols );
147 for (
int col = 0; col < cols; col++ )
149 uInt32Row[col] =
static_cast<quint32
>( generateRandomLongValue( mersenneTwister ) );
156 std::vector<float> float32Row( cols );
157 for (
int col = 0; col < cols; col++ )
159 float32Row[col] =
static_cast<float>( generateRandomDoubleValue( mersenneTwister ) );
166 std::vector<double> float64Row( cols );
167 for (
int col = 0; col < cols; col++ )
169 float64Row[col] = generateRandomDoubleValue( mersenneTwister );
177 provider->writeBlock( &block, 1, 0, row );
182 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
189QString QgsRandomUniformRasterAlgorithm::name()
const
191 return QStringLiteral(
"createrandomuniformrasterlayer" );
194QString QgsRandomUniformRasterAlgorithm::displayName()
const
196 return QObject::tr(
"Create random raster layer (uniform distribution)" );
199QStringList QgsRandomUniformRasterAlgorithm::tags()
const
201 return QObject::tr(
"raster,create,random" ).split(
',' );
204QString QgsRandomUniformRasterAlgorithm::shortHelpString()
const
206 return QObject::tr(
"Generates a raster layer for given extent and cell size "
207 "filled with random values.\n"
208 "By default, the values will range between the minimum and "
209 "maximum value of the specified output raster type. This can "
210 "be overridden by using the advanced parameters for lower and "
211 "upper bound value. If the bounds have the same value or both "
212 "are zero (default) the algorithm will create random values in "
213 "the full value range of the chosen raster data type. "
214 "Choosing bounds outside the acceptable range of the output "
215 "raster type will abort the algorithm." );
218QgsRandomUniformRasterAlgorithm *QgsRandomUniformRasterAlgorithm::createInstance()
const
220 return new QgsRandomUniformRasterAlgorithm();
223void QgsRandomUniformRasterAlgorithm::addAlgorithmParams()
225 QStringList rasterDataTypes = QStringList();
226 rasterDataTypes << QStringLiteral(
"Byte" )
227 << QStringLiteral(
"Integer16" )
228 << QStringLiteral(
"Unsigned Integer16" )
229 << QStringLiteral(
"Integer32" )
230 << QStringLiteral(
"Unsigned Integer32" )
231 << QStringLiteral(
"Float32" )
232 << QStringLiteral(
"Float64" );
234 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
236 addParameter( rasterTypeParameter.release() );
238 std::unique_ptr< QgsProcessingParameterNumber > lowerBoundParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"LOWER_BOUND" ), QStringLiteral(
"Lower bound for random number range" ),
QgsProcessingParameterNumber::Double, QVariant(),
true );
240 addParameter( lowerBoundParameter.release() );
242 std::unique_ptr< QgsProcessingParameterNumber > upperBoundParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"UPPER_BOUND" ), QStringLiteral(
"Upper bound for random number range" ),
QgsProcessingParameterNumber::Double, QVariant(),
true );
244 addParameter( upperBoundParameter.release() );
247Qgis::DataType QgsRandomUniformRasterAlgorithm::getRasterDataType(
int typeId )
270bool QgsRandomUniformRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
272 mRandomUpperBound = parameterAsDouble( parameters, QStringLiteral(
"UPPER_BOUND" ), context );
273 mRandomLowerBound = parameterAsDouble( parameters, QStringLiteral(
"LOWER_BOUND" ), context );
275 if ( mRandomLowerBound > mRandomUpperBound )
276 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." ) );
278 const int typeId = parameterAsInt( parameters, QStringLiteral(
"OUTPUT_TYPE" ), context );
279 const Qgis::DataType rasterDataType = getRasterDataType( typeId );
281 switch ( rasterDataType )
284 if ( mRandomLowerBound < std::numeric_limits<quint8>::min() || mRandomUpperBound > std::numeric_limits<quint8>::max() )
285 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" ) ) );
289 mRandomUpperBound = std::numeric_limits<quint8>::max();
290 mRandomLowerBound = std::numeric_limits<quint8>::min();
294 if ( mRandomLowerBound < std::numeric_limits<qint16>::min() || mRandomUpperBound > std::numeric_limits<qint16>::max() )
295 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" ) ) );
298 mRandomUpperBound = std::numeric_limits<qint16>::max();
299 mRandomLowerBound = std::numeric_limits<qint16>::min();
303 if ( mRandomLowerBound < std::numeric_limits<quint16>::min() || mRandomUpperBound > std::numeric_limits<quint16>::max() )
304 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" ) ) );
307 mRandomUpperBound = std::numeric_limits<quint16>::max();
308 mRandomLowerBound = std::numeric_limits<quint16>::min();
312 if ( mRandomLowerBound < std::numeric_limits<qint32>::min() || mRandomUpperBound > std::numeric_limits<qint32>::max() )
313 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" ) ) );
316 mRandomUpperBound = std::numeric_limits<qint32>::max();
317 mRandomLowerBound = std::numeric_limits<qint32>::min();
321 if ( mRandomLowerBound < std::numeric_limits<quint32>::min() || mRandomUpperBound > std::numeric_limits<quint32>::max() )
322 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" ) ) );
325 mRandomUpperBound = std::numeric_limits<quint32>::max();
326 mRandomLowerBound = std::numeric_limits<quint32>::min();
332 mRandomUpperBound = std::numeric_limits<float>::max();
333 mRandomLowerBound = std::numeric_limits<float>::min();
339 mRandomUpperBound = std::numeric_limits<double>::max();
340 mRandomLowerBound = std::numeric_limits<double>::min();
347 mRandomUniformIntDistribution = std::uniform_int_distribution<long>( mRandomLowerBound, mRandomUpperBound );
348 mRandomUniformDoubleDistribution = std::uniform_real_distribution<double>( mRandomLowerBound, mRandomUpperBound );
353long QgsRandomUniformRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
355 return mRandomUniformIntDistribution( mersenneTwister );
358double QgsRandomUniformRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
360 return mRandomUniformDoubleDistribution( mersenneTwister );
366QString QgsRandomBinomialRasterAlgorithm::name()
const
368 return QStringLiteral(
"createrandombinomialrasterlayer" );
371QString QgsRandomBinomialRasterAlgorithm::displayName()
const
373 return QObject::tr(
"Create random raster layer (binomial distribution)" );
376QStringList QgsRandomBinomialRasterAlgorithm::tags()
const
378 return QObject::tr(
"raster,create,binomial,random" ).split(
',' );
381QString QgsRandomBinomialRasterAlgorithm::shortHelpString()
const
383 return QObject::tr(
"Generates a raster layer for given extent and cell size "
384 "filled with binomially distributed random values.\n"
385 "By default, the values will be chosen given an N of 10 and a probability of 0.5. "
386 "This can be overridden by using the advanced parameter for N and probability. "
387 "The raster data type is set to Integer types (Integer16 by default). "
388 "The binomial distribution random values are defined as positive integer numbers. "
389 "A floating point raster will represent a cast of integer values "
390 "to floating point." );
393QgsRandomBinomialRasterAlgorithm *QgsRandomBinomialRasterAlgorithm::createInstance()
const
395 return new QgsRandomBinomialRasterAlgorithm();
399void QgsRandomBinomialRasterAlgorithm::addAlgorithmParams( )
401 QStringList rasterDataTypes = QStringList();
402 rasterDataTypes << QStringLiteral(
"Integer16" )
403 << QStringLiteral(
"Unsigned Integer16" )
404 << QStringLiteral(
"Integer32" )
405 << QStringLiteral(
"Unsigned Integer32" )
406 << QStringLiteral(
"Float32" )
407 << QStringLiteral(
"Float64" );
409 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
411 addParameter( rasterTypeParameter.release() );
413 std::unique_ptr< QgsProcessingParameterNumber > nParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"N" ), QStringLiteral(
"N" ),
QgsProcessingParameterNumber::Integer, 10,
true, 0 );
415 addParameter( nParameter.release() );
417 std::unique_ptr< QgsProcessingParameterNumber > probabilityParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"PROBABILITY" ), QStringLiteral(
"Probability" ),
QgsProcessingParameterNumber::Double, 0.5,
true, 0 );
419 addParameter( probabilityParameter.release() );
422Qgis::DataType QgsRandomBinomialRasterAlgorithm::getRasterDataType(
int typeId )
443bool QgsRandomBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
445 const int n = parameterAsInt( parameters, QStringLiteral(
"N" ), context );
446 const double probability = parameterAsDouble( parameters, QStringLiteral(
"PROBABILITY" ), context );
447 mRandombinomialDistribution = std::binomial_distribution<long>( n, probability );
451long QgsRandomBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
453 return mRandombinomialDistribution( mersenneTwister );
456double QgsRandomBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
458 return static_cast<double>( mRandombinomialDistribution( mersenneTwister ) );
464QString QgsRandomExponentialRasterAlgorithm::name()
const
466 return QStringLiteral(
"createrandomexponentialrasterlayer" );
469QString QgsRandomExponentialRasterAlgorithm::displayName()
const
471 return QObject::tr(
"Create random raster layer (exponential distribution)" );
474QStringList QgsRandomExponentialRasterAlgorithm::tags()
const
476 return QObject::tr(
"raster,create,random,exponential" ).split(
',' );
479QString QgsRandomExponentialRasterAlgorithm::shortHelpString()
const
481 return QObject::tr(
"Generates a raster layer for given extent and cell size "
482 "filled with exponentially distributed random values.\n"
483 "By default, the values will be chosen given a lambda of 1.0. "
484 "This can be overridden by using the advanced parameter for lambda. "
485 "The raster data type is set to Float32 by default as "
486 "the exponential distribution random values are floating point numbers." );
489QgsRandomExponentialRasterAlgorithm *QgsRandomExponentialRasterAlgorithm::createInstance()
const
491 return new QgsRandomExponentialRasterAlgorithm();
495void QgsRandomExponentialRasterAlgorithm::addAlgorithmParams()
497 QStringList rasterDataTypes = QStringList();
498 rasterDataTypes << QStringLiteral(
"Float32" )
499 << QStringLiteral(
"Float64" );
501 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
503 addParameter( rasterTypeParameter.release() );
505 std::unique_ptr< QgsProcessingParameterNumber > lambdaParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"LAMBDA" ), QStringLiteral(
"Lambda" ),
QgsProcessingParameterNumber::Double, 1.0,
true, 0.000001 );
507 addParameter( lambdaParameter.release() );
510Qgis::DataType QgsRandomExponentialRasterAlgorithm::getRasterDataType(
int typeId )
523bool QgsRandomExponentialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
525 const double lambda = parameterAsDouble( parameters, QStringLiteral(
"LAMBDA" ), context );
526 mRandomExponentialDistribution = std::exponential_distribution<double>( lambda );
530long QgsRandomExponentialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
532 return static_cast<long>( mRandomExponentialDistribution( mersenneTwister ) );
535double QgsRandomExponentialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
537 return mRandomExponentialDistribution( mersenneTwister );
543QString QgsRandomGammaRasterAlgorithm::name()
const
545 return QStringLiteral(
"createrandomgammarasterlayer" );
548QString QgsRandomGammaRasterAlgorithm::displayName()
const
550 return QObject::tr(
"Create random raster layer (gamma distribution)" );
553QStringList QgsRandomGammaRasterAlgorithm::tags()
const
555 return QObject::tr(
"raster,create,random,gamma" ).split(
',' );
558QString QgsRandomGammaRasterAlgorithm::shortHelpString()
const
560 return QObject::tr(
"Generates a raster layer for given extent and cell size "
561 "filled with gamma distributed random values.\n"
562 "By default, the values will be chosen given an alpha and beta value of 1.0. "
563 "This can be overridden by using the advanced parameter for alpha and beta. "
564 "The raster data type is set to Float32 by default as "
565 "the gamma distribution random values are floating point numbers." );
568QgsRandomGammaRasterAlgorithm *QgsRandomGammaRasterAlgorithm::createInstance()
const
570 return new QgsRandomGammaRasterAlgorithm();
574void QgsRandomGammaRasterAlgorithm::addAlgorithmParams()
576 QStringList rasterDataTypes = QStringList();
577 rasterDataTypes << QStringLiteral(
"Float32" )
578 << QStringLiteral(
"Float64" );
580 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
582 addParameter( rasterTypeParameter.release() );
584 std::unique_ptr< QgsProcessingParameterNumber > alphaParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"ALPHA" ), QStringLiteral(
"Alpha" ),
QgsProcessingParameterNumber::Double, 1.0,
true, 0.000001 );
586 addParameter( alphaParameter.release() );
588 std::unique_ptr< QgsProcessingParameterNumber > betaParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"BETA" ), QStringLiteral(
"Beta" ),
QgsProcessingParameterNumber::Double, 1.0,
true, 0.000001 );
590 addParameter( betaParameter.release() );
593Qgis::DataType QgsRandomGammaRasterAlgorithm::getRasterDataType(
int typeId )
606bool QgsRandomGammaRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
608 const double alpha = parameterAsDouble( parameters, QStringLiteral(
"ALPHA" ), context );
609 const double beta = parameterAsDouble( parameters, QStringLiteral(
"BETA" ), context );
610 mRandomGammaDistribution = std::gamma_distribution<double>( alpha, beta );
614long QgsRandomGammaRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
616 return static_cast<long>( mRandomGammaDistribution( mersenneTwister ) );
619double QgsRandomGammaRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
621 return mRandomGammaDistribution( mersenneTwister );
627QString QgsRandomGeometricRasterAlgorithm::name()
const
629 return QStringLiteral(
"createrandomgeometricrasterlayer" );
632QString QgsRandomGeometricRasterAlgorithm::displayName()
const
634 return QObject::tr(
"Create random raster layer (geometric distribution)" );
637QStringList QgsRandomGeometricRasterAlgorithm::tags()
const
639 return QObject::tr(
"raster,create,random,geometric" ).split(
',' );
642QString QgsRandomGeometricRasterAlgorithm::shortHelpString()
const
644 return QObject::tr(
"Generates a raster layer for given extent and cell size "
645 "filled with geometrically distributed random values.\n"
646 "By default, the values will be chosen given a probability of 0.5. "
647 "This can be overridden by using the advanced parameter for mean "
648 "value. The raster data type is set to Integer types (Integer16 by default). "
649 "The geometric distribution random values are defined as positive integer numbers. "
650 "A floating point raster will represent a cast of "
651 "integer values to floating point." );
654QgsRandomGeometricRasterAlgorithm *QgsRandomGeometricRasterAlgorithm::createInstance()
const
656 return new QgsRandomGeometricRasterAlgorithm();
660void QgsRandomGeometricRasterAlgorithm::addAlgorithmParams()
662 QStringList rasterDataTypes = QStringList();
663 rasterDataTypes << QStringLiteral(
"Integer16" )
664 << QStringLiteral(
"Unsigned Integer16" )
665 << QStringLiteral(
"Integer32" )
666 << QStringLiteral(
"Unsigned Integer32" )
667 << QStringLiteral(
"Float32" )
668 << QStringLiteral(
"Float64" );
670 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
672 addParameter( rasterTypeParameter.release() );
674 std::unique_ptr< QgsProcessingParameterNumber > probabilityParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"PROBABILITY" ), QStringLiteral(
"Probability" ),
QgsProcessingParameterNumber::Double, 0.5,
true, 0.00001 );
676 addParameter( probabilityParameter.release() );
679Qgis::DataType QgsRandomGeometricRasterAlgorithm::getRasterDataType(
int typeId )
700bool QgsRandomGeometricRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
702 const double probability = parameterAsDouble( parameters, QStringLiteral(
"PROBABILITY" ), context );
703 mRandomGeometricDistribution = std::geometric_distribution<long>( probability );
707long QgsRandomGeometricRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
709 return mRandomGeometricDistribution( mersenneTwister );
712double QgsRandomGeometricRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
714 return static_cast<double>( mRandomGeometricDistribution( mersenneTwister ) );
720QString QgsRandomNegativeBinomialRasterAlgorithm::name()
const
722 return QStringLiteral(
"createrandomnegativebinomialrasterlayer" );
725QString QgsRandomNegativeBinomialRasterAlgorithm::displayName()
const
727 return QObject::tr(
"Create random raster layer (negative binomial distribution)" );
730QStringList QgsRandomNegativeBinomialRasterAlgorithm::tags()
const
732 return QObject::tr(
"raster,create,random,negative,binomial,negative-binomial" ).split(
',' );
735QString QgsRandomNegativeBinomialRasterAlgorithm::shortHelpString()
const
737 return QObject::tr(
"Generates a raster layer for given extent and cell size "
738 "filled with negative binomially distributed random values.\n"
739 "By default, the values will be chosen given a distribution parameter k of 10.0 "
740 "and a probability of 0.5. "
741 "This can be overridden by using the advanced parameters for k and probability. "
742 "The raster data type is set to Integer types (Integer 16 by default). "
743 "The negative binomial distribution random values are defined as positive integer numbers. "
744 "A floating point raster will represent a cast of "
745 "integer values to floating point." );
748QgsRandomNegativeBinomialRasterAlgorithm *QgsRandomNegativeBinomialRasterAlgorithm::createInstance()
const
750 return new QgsRandomNegativeBinomialRasterAlgorithm();
754void QgsRandomNegativeBinomialRasterAlgorithm::addAlgorithmParams( )
756 QStringList rasterDataTypes = QStringList();
757 rasterDataTypes << QStringLiteral(
"Integer16" )
758 << QStringLiteral(
"Unsigned Integer16" )
759 << QStringLiteral(
"Integer32" )
760 << QStringLiteral(
"Unsigned Integer32" )
761 << QStringLiteral(
"Float32" )
762 << QStringLiteral(
"Float64" );
764 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
766 addParameter( rasterTypeParameter.release() );
768 std::unique_ptr< QgsProcessingParameterNumber > kParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"K_PARAMETER" ), QStringLiteral(
"Distribution parameter k" ),
QgsProcessingParameterNumber::Integer, 10,
true, 0.00001 );
770 addParameter( kParameter.release() );
772 std::unique_ptr< QgsProcessingParameterNumber > probabilityParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"PROBABILITY" ), QStringLiteral(
"Probability" ),
QgsProcessingParameterNumber::Double, 0.5,
true, 0.00001 );
774 addParameter( probabilityParameter.release() );
777Qgis::DataType QgsRandomNegativeBinomialRasterAlgorithm::getRasterDataType(
int typeId )
798bool QgsRandomNegativeBinomialRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
800 const int k = parameterAsInt( parameters, QStringLiteral(
"K_PARAMETER" ), context );
801 const double probability = parameterAsDouble( parameters, QStringLiteral(
"PROBABILITY" ), context );
802 mRandomNegativeBinomialDistribution = std::negative_binomial_distribution<long>( k, probability );
806long QgsRandomNegativeBinomialRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
808 return mRandomNegativeBinomialDistribution( mersenneTwister );
811double QgsRandomNegativeBinomialRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
813 return static_cast<double>( mRandomNegativeBinomialDistribution( mersenneTwister ) );
819QString QgsRandomNormalRasterAlgorithm::name()
const
821 return QStringLiteral(
"createrandomnormalrasterlayer" );
824QString QgsRandomNormalRasterAlgorithm::displayName()
const
826 return QObject::tr(
"Create random raster layer (normal distribution)" );
829QStringList QgsRandomNormalRasterAlgorithm::tags()
const
831 return QObject::tr(
"raster,create,normal,distribution,random" ).split(
',' );
834QString QgsRandomNormalRasterAlgorithm::shortHelpString()
const
836 return QObject::tr(
"Generates a raster layer for given extent and cell size "
837 "filled with normally distributed random values.\n"
838 "By default, the values will be chosen given a mean of 0.0 and "
839 "a standard deviation of 1.0. This can be overridden by "
840 "using the advanced parameters for mean and standard deviation "
841 "value. The raster data type is set to Float32 by default "
842 "as the normal distribution random values are floating point numbers." );
845QgsRandomNormalRasterAlgorithm *QgsRandomNormalRasterAlgorithm::createInstance()
const
847 return new QgsRandomNormalRasterAlgorithm();
850void QgsRandomNormalRasterAlgorithm::addAlgorithmParams()
852 QStringList rasterDataTypes = QStringList();
853 rasterDataTypes << QStringLiteral(
"Float32" )
854 << QStringLiteral(
"Float64" );
856 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
858 addParameter( rasterTypeParameter.release() );
860 std::unique_ptr< QgsProcessingParameterNumber > meanParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"MEAN" ), QStringLiteral(
"Mean of normal distribution" ),
QgsProcessingParameterNumber::Double, 0,
true );
862 addParameter( meanParameter.release() );
864 std::unique_ptr< QgsProcessingParameterNumber > stdevParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"STDDEV" ), QStringLiteral(
"Standard deviation of normal distribution" ),
QgsProcessingParameterNumber::Double, 1,
true, 0 );
866 addParameter( stdevParameter.release() );
869Qgis::DataType QgsRandomNormalRasterAlgorithm::getRasterDataType(
int typeId )
882bool QgsRandomNormalRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
884 const double mean = parameterAsDouble( parameters, QStringLiteral(
"MEAN" ), context );
885 const double stddev = parameterAsDouble( parameters, QStringLiteral(
"STDDEV" ), context );
886 mRandomNormalDistribution = std::normal_distribution<double>( mean, stddev );
890long QgsRandomNormalRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
892 return static_cast<long>( mRandomNormalDistribution( mersenneTwister ) );
895double QgsRandomNormalRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
897 return mRandomNormalDistribution( mersenneTwister );
903QString QgsRandomPoissonRasterAlgorithm::name()
const
905 return QStringLiteral(
"createrandompoissonrasterlayer" );
908QString QgsRandomPoissonRasterAlgorithm::displayName()
const
910 return QObject::tr(
"Create random raster layer (poisson distribution)" );
913QStringList QgsRandomPoissonRasterAlgorithm::tags()
const
915 return QObject::tr(
"raster,create,random,poisson" ).split(
',' );
918QString QgsRandomPoissonRasterAlgorithm::shortHelpString()
const
920 return QObject::tr(
"Generates a raster layer for given extent and cell size "
921 "filled with poisson distributed random values.\n"
922 "By default, the values will be chosen given a mean of 1.0. "
923 "This can be overridden by using the advanced parameter for mean "
924 "value. The raster data type is set to Integer types (Integer16 by default). "
925 "The poisson distribution random values are positive integer numbers. "
926 "A floating point raster will represent a cast of integer values to floating point." );
929QgsRandomPoissonRasterAlgorithm *QgsRandomPoissonRasterAlgorithm::createInstance()
const
931 return new QgsRandomPoissonRasterAlgorithm();
935void QgsRandomPoissonRasterAlgorithm::addAlgorithmParams()
937 QStringList rasterDataTypes = QStringList();
938 rasterDataTypes << QStringLiteral(
"Integer16" )
939 << QStringLiteral(
"Unsigned Integer16" )
940 << QStringLiteral(
"Integer32" )
941 << QStringLiteral(
"Unsigned Integer32" )
942 << QStringLiteral(
"Float32" )
943 << QStringLiteral(
"Float64" );
945 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 0,
false );
947 addParameter( rasterTypeParameter.release() );
949 std::unique_ptr< QgsProcessingParameterNumber > upperBoundParameter = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"MEAN" ), QStringLiteral(
"Mean" ),
QgsProcessingParameterNumber::Double, 1.0,
true, 0 );
951 addParameter( upperBoundParameter.release() );
954Qgis::DataType QgsRandomPoissonRasterAlgorithm::getRasterDataType(
int typeId )
975bool QgsRandomPoissonRasterAlgorithm::prepareRandomParameters(
const QVariantMap ¶meters,
QgsProcessingContext &context )
977 const double mean = parameterAsDouble( parameters, QStringLiteral(
"MEAN" ), context );
978 mRandomPoissonDistribution = std::poisson_distribution<long>( mean );
982long QgsRandomPoissonRasterAlgorithm::generateRandomLongValue( std::mt19937 &mersenneTwister )
984 return mRandomPoissonDistribution( mersenneTwister );
987double QgsRandomPoissonRasterAlgorithm::generateRandomDoubleValue( std::mt19937 &mersenneTwister )
989 return static_cast<double>( mRandomPoissonDistribution( mersenneTwister ) );
DataType
Raster data types.
@ Float32
Thirty two bit floating point (float)
@ Int16
Sixteen bit signed integer (qint16)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ Int32
Thirty two bit signed integer (qint32)
@ Float64
Sixty four bit floating point (double)
@ UInt32
Thirty two bit unsigned integer (quint32)
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.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
A rectangular map extent parameter for processing algorithms.
A numeric parameter for processing algorithms.
@ Double
Double/float values.
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)