30QStringList QgsRasterBooleanLogicAlgorithmBase::tags()
const
32 return QObject::tr(
"logical,boolean" ).split(
',' );
35QString QgsRasterBooleanLogicAlgorithmBase::group()
const
37 return QObject::tr(
"Raster analysis" );
40QString QgsRasterBooleanLogicAlgorithmBase::groupId()
const
42 return QStringLiteral(
"rasteranalysis" );
45void QgsRasterBooleanLogicAlgorithmBase::initAlgorithm(
const QVariantMap & )
50 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"NODATA_AS_FALSE" ), QObject::tr(
"Treat NoData values as false" ),
false ) );
54 addParameter( noDataValueParam.release() );
56 std::unique_ptr<QgsProcessingParameterDefinition> typeChoice = QgsRasterAnalysisUtils::createRasterTypeParameter( QStringLiteral(
"DATA_TYPE" ), QObject::tr(
"Output data type" ),
Qgis::DataType::Float32 );
58 addParameter( typeChoice.release() );
62 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
63 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
65 addParameter( createOptsParam.release() );
67 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
68 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
70 addParameter( creationOptsParam.release() );
79 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"NODATA_PIXEL_COUNT" ), QObject::tr(
"NoData pixel count" ) ) );
86 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"REF_LAYER" ), context );
87 if ( !referenceLayer )
89 mCrs = referenceLayer->
crs();
92 mLayerWidth = referenceLayer->
width();
93 mLayerHeight = referenceLayer->
height();
94 mExtent = referenceLayer->
extent();
95 mNoDataValue = parameterAsDouble( parameters, QStringLiteral(
"NO_DATA" ), context );
96 mDataType = QgsRasterAnalysisUtils::rasterTypeChoiceToDataType( parameterAsEnum( parameters, QStringLiteral(
"DATA_TYPE" ), context ) );
97 if ( mDataType ==
Qgis::DataType::Int8 && atoi( GDALVersionInfo(
"VERSION_NUM" ) ) < GDAL_COMPUTE_VERSION( 3, 7, 0 ) )
100 mTreatNodataAsFalse = parameterAsBoolean( parameters, QStringLiteral(
"NODATA_AS_FALSE" ), context );
102 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral(
"INPUT" ), context );
103 QList<QgsRasterLayer *> rasterLayers;
104 rasterLayers.reserve( layers.count() );
110 QgsRasterAnalysisUtils::RasterLogicInput input;
114 input.interface = input.sourceDataProvider.get();
116 if ( layer->
crs() != mCrs )
118 input.projector = std::make_unique<QgsRasterProjector>();
119 input.projector->setInput( input.sourceDataProvider.get() );
121 input.interface = input.projector.get();
123 mInputs.emplace_back( std::move( input ) );
132 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
134 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
135 if ( !optionsString.isEmpty() )
136 creationOptions = optionsString;
138 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
139 const QFileInfo fi( outputFile );
142 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
143 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
144 if ( !creationOptions.isEmpty() )
146 writer->setCreationOptions( creationOptions.split(
'|' ) );
148 writer->setOutputFormat( outputFormat );
149 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
152 if ( !provider->isValid() )
155 provider->setNoDataValue( 1, mNoDataValue );
159 const qgssize layerSize =
static_cast<qgssize>( mLayerWidth ) *
static_cast<qgssize>( mLayerHeight );
161 QgsRasterAnalysisUtils::applyRasterLogicOperator( mInputs, provider.get(), mNoDataValue, mTreatNodataAsFalse, mLayerWidth, mLayerHeight, mExtent, feedback, mExtractValFunc, noDataCount, trueCount, falseCount );
164 outputs.insert( QStringLiteral(
"EXTENT" ), mExtent.toString() );
165 outputs.insert( QStringLiteral(
"CRS_AUTHID" ), mCrs.authid() );
166 outputs.insert( QStringLiteral(
"WIDTH_IN_PIXELS" ), mLayerWidth );
167 outputs.insert( QStringLiteral(
"HEIGHT_IN_PIXELS" ), mLayerHeight );
168 outputs.insert( QStringLiteral(
"TOTAL_PIXEL_COUNT" ), layerSize );
169 outputs.insert( QStringLiteral(
"NODATA_PIXEL_COUNT" ), noDataCount );
170 outputs.insert( QStringLiteral(
"TRUE_PIXEL_COUNT" ), trueCount );
171 outputs.insert( QStringLiteral(
"FALSE_PIXEL_COUNT" ), falseCount );
172 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
182QgsRasterLogicalOrAlgorithm::QgsRasterLogicalOrAlgorithm()
184 mExtractValFunc = [](
const std::vector<std::unique_ptr<QgsRasterBlock>> &inputs,
bool &res,
bool &resIsNoData,
int row,
int column,
bool treatNoDataAsFalse ) {
187 bool isNoData =
false;
188 for (
auto &block : inputs )
191 if ( !block || !block->isValid() )
193 if ( treatNoDataAsFalse )
203 value = block->valueAndNoData( row, column, isNoData );
204 if ( isNoData && !treatNoDataAsFalse )
212 if ( treatNoDataAsFalse )
220QString QgsRasterLogicalOrAlgorithm::name()
const
222 return QStringLiteral(
"rasterlogicalor" );
225QString QgsRasterLogicalOrAlgorithm::displayName()
const
227 return QObject::tr(
"Raster boolean OR" );
231QString QgsRasterLogicalOrAlgorithm::shortDescription()
const
233 return QObject::tr(
"Calculates the boolean OR for a set of input raster layers." );
236QString QgsRasterLogicalOrAlgorithm::shortHelpString()
const
238 return QObject::tr(
"This algorithm calculates the boolean OR for a set of input rasters. If any of the input rasters have a non-zero value for a pixel, "
239 "that pixel will be set to 1 in the output raster. If all the input rasters have 0 values for the pixel it will be set to 0 in the output raster.\n\n"
240 "The reference layer parameter specifies an existing raster layer to use as a reference when creating the output raster. The output raster "
241 "will have the same extent, CRS, and pixel dimensions as this layer.\n\n"
242 "By default, a NoData pixel in ANY of the input layers will result in a NoData pixel in the output raster. If the "
243 "'Treat NoData values as false' option is checked, then NoData inputs will be treated the same as a 0 input value." );
246QgsRasterLogicalOrAlgorithm *QgsRasterLogicalOrAlgorithm::createInstance()
const
248 return new QgsRasterLogicalOrAlgorithm();
255QgsRasterLogicalAndAlgorithm::QgsRasterLogicalAndAlgorithm()
257 mExtractValFunc = [](
const std::vector<std::unique_ptr<QgsRasterBlock>> &inputs,
bool &res,
bool &resIsNoData,
int row,
int column,
bool treatNoDataAsFalse ) {
260 bool isNoData =
false;
261 for (
auto &block : inputs )
264 if ( !block || !block->isValid() )
266 if ( treatNoDataAsFalse )
279 value = block->valueAndNoData( row, column, isNoData );
280 if ( isNoData && !treatNoDataAsFalse )
288 if ( treatNoDataAsFalse )
296QString QgsRasterLogicalAndAlgorithm::name()
const
298 return QStringLiteral(
"rasterbooleanand" );
301QString QgsRasterLogicalAndAlgorithm::displayName()
const
303 return QObject::tr(
"Raster boolean AND" );
307QString QgsRasterLogicalAndAlgorithm::shortDescription()
const
309 return QObject::tr(
"Calculates the boolean AND for a set of input raster layers." );
312QString QgsRasterLogicalAndAlgorithm::shortHelpString()
const
314 return QObject::tr(
"This algorithm calculates the boolean AND for a set of input rasters. If all of the input rasters have a non-zero value for a pixel, "
315 "that pixel will be set to 1 in the output raster. If any of the input rasters have 0 values for the pixel it will be set to 0 in the output raster.\n\n"
316 "The reference layer parameter specifies an existing raster layer to use as a reference when creating the output raster. The output raster "
317 "will have the same extent, CRS, and pixel dimensions as this layer.\n\n"
318 "By default, a NoData pixel in ANY of the input layers will result in a NoData pixel in the output raster. If the "
319 "'Treat NoData values as false' option is checked, then NoData inputs will be treated the same as a 0 input value." );
322QgsRasterLogicalAndAlgorithm *QgsRasterLogicalAndAlgorithm::createInstance()
const
324 return new QgsRasterLogicalAndAlgorithm();
@ Float32
Thirty two bit floating point (float).
@ Int8
Eight bit signed integer (qint8) (added in QGIS 3.30).
@ 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.
Base class for all map layer types.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A numeric output for processing algorithms.
A string output for processing algorithms.
A boolean parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
virtual bool sourceHasNoDataValue(int bandNo) const
Returns true if source band has no data value.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Represents a raster layer.
int height() const
Returns the height of the (unclipped) raster.
double rasterUnitsPerPixelX() const
Returns the number of raster units per each raster pixel in X axis.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
double rasterUnitsPerPixelY() const
Returns the number of raster units per each raster pixel in Y axis.
int width() const
Returns the width of the (unclipped) raster.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).