29using namespace Qt::StringLiterals;
34QStringList QgsRasterBooleanLogicAlgorithmBase::tags()
const
36 return QObject::tr(
"logical,boolean" ).split(
',' );
39QString QgsRasterBooleanLogicAlgorithmBase::group()
const
41 return QObject::tr(
"Raster analysis" );
44QString QgsRasterBooleanLogicAlgorithmBase::groupId()
const
46 return u
"rasteranalysis"_s;
49void QgsRasterBooleanLogicAlgorithmBase::initAlgorithm(
const QVariantMap & )
58 addParameter( noDataValueParam.release() );
60 std::unique_ptr<QgsProcessingParameterDefinition> typeChoice = QgsRasterAnalysisUtils::createRasterTypeParameter( u
"DATA_TYPE"_s, QObject::tr(
"Output data type" ),
Qgis::DataType::Float32 );
62 addParameter( typeChoice.release() );
66 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATE_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
67 createOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
69 addParameter( createOptsParam.release() );
71 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATION_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
72 creationOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
74 addParameter( creationOptsParam.release() );
90 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, u
"REF_LAYER"_s, context );
91 if ( !referenceLayer )
93 mCrs = referenceLayer->
crs();
96 mLayerWidth = referenceLayer->
width();
97 mLayerHeight = referenceLayer->
height();
98 mExtent = referenceLayer->
extent();
99 mNoDataValue = parameterAsDouble( parameters, u
"NO_DATA"_s, context );
100 mDataType = QgsRasterAnalysisUtils::rasterTypeChoiceToDataType( parameterAsEnum( parameters, u
"DATA_TYPE"_s, context ) );
101 if ( mDataType ==
Qgis::DataType::Int8 && atoi( GDALVersionInfo(
"VERSION_NUM" ) ) < GDAL_COMPUTE_VERSION( 3, 7, 0 ) )
104 mTreatNodataAsFalse = parameterAsBoolean( parameters, u
"NODATA_AS_FALSE"_s, context );
106 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"INPUT"_s, context );
107 QList<QgsRasterLayer *> rasterLayers;
108 rasterLayers.reserve( layers.count() );
114 QgsRasterAnalysisUtils::RasterLogicInput input;
118 input.interface = input.sourceDataProvider.get();
120 if ( layer->
crs() != mCrs )
122 input.projector = std::make_unique<QgsRasterProjector>();
123 input.projector->setInput( input.sourceDataProvider.get() );
125 input.interface = input.projector.get();
127 mInputs.emplace_back( std::move( input ) );
136 QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
138 const QString optionsString = parameterAsString( parameters, u
"CREATE_OPTIONS"_s, context );
139 if ( !optionsString.isEmpty() )
140 creationOptions = optionsString;
142 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
143 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
145 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
146 writer->setOutputProviderKey( u
"gdal"_s );
147 if ( !creationOptions.isEmpty() )
149 writer->setCreationOptions( creationOptions.split(
'|' ) );
151 writer->setOutputFormat( outputFormat );
152 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
155 if ( !provider->isValid() )
158 provider->setNoDataValue( 1, mNoDataValue );
162 const qgssize layerSize =
static_cast<qgssize>( mLayerWidth ) *
static_cast<qgssize>( mLayerHeight );
164 QgsRasterAnalysisUtils::applyRasterLogicOperator( mInputs, std::move( provider ), mNoDataValue, mTreatNodataAsFalse, mLayerWidth, mLayerHeight, mExtent, feedback, mExtractValFunc, noDataCount, trueCount, falseCount );
167 outputs.insert( u
"EXTENT"_s, mExtent.toString() );
168 outputs.insert( u
"CRS_AUTHID"_s, mCrs.authid() );
169 outputs.insert( u
"WIDTH_IN_PIXELS"_s, mLayerWidth );
170 outputs.insert( u
"HEIGHT_IN_PIXELS"_s, mLayerHeight );
171 outputs.insert( u
"TOTAL_PIXEL_COUNT"_s, layerSize );
172 outputs.insert( u
"NODATA_PIXEL_COUNT"_s, noDataCount );
173 outputs.insert( u
"TRUE_PIXEL_COUNT"_s, trueCount );
174 outputs.insert( u
"FALSE_PIXEL_COUNT"_s, falseCount );
175 outputs.insert( u
"OUTPUT"_s, outputFile );
185QgsRasterLogicalOrAlgorithm::QgsRasterLogicalOrAlgorithm()
187 mExtractValFunc = [](
const std::vector<std::unique_ptr<QgsRasterBlock>> &inputs,
bool &res,
bool &resIsNoData,
int row,
int column,
bool treatNoDataAsFalse ) {
190 bool isNoData =
false;
191 for (
auto &block : inputs )
194 if ( !block || !block->isValid() )
196 if ( treatNoDataAsFalse )
206 value = block->valueAndNoData( row, column, isNoData );
207 if ( isNoData && !treatNoDataAsFalse )
215 if ( treatNoDataAsFalse )
223QString QgsRasterLogicalOrAlgorithm::name()
const
225 return u
"rasterlogicalor"_s;
228QString QgsRasterLogicalOrAlgorithm::displayName()
const
230 return QObject::tr(
"Raster boolean OR" );
234QString QgsRasterLogicalOrAlgorithm::shortDescription()
const
236 return QObject::tr(
"Calculates the boolean OR for a set of input raster layers." );
239QString QgsRasterLogicalOrAlgorithm::shortHelpString()
const
241 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, "
242 "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"
243 "The reference layer parameter specifies an existing raster layer to use as a reference when creating the output raster. The output raster "
244 "will have the same extent, CRS, and pixel dimensions as this layer.\n\n"
245 "By default, a NoData pixel in ANY of the input layers will result in a NoData pixel in the output raster. If the "
246 "'Treat NoData values as false' option is checked, then NoData inputs will be treated the same as a 0 input value." );
249QgsRasterLogicalOrAlgorithm *QgsRasterLogicalOrAlgorithm::createInstance()
const
251 return new QgsRasterLogicalOrAlgorithm();
258QgsRasterLogicalAndAlgorithm::QgsRasterLogicalAndAlgorithm()
260 mExtractValFunc = [](
const std::vector<std::unique_ptr<QgsRasterBlock>> &inputs,
bool &res,
bool &resIsNoData,
int row,
int column,
bool treatNoDataAsFalse ) {
263 bool isNoData =
false;
264 for (
auto &block : inputs )
267 if ( !block || !block->isValid() )
269 if ( treatNoDataAsFalse )
282 value = block->valueAndNoData( row, column, isNoData );
283 if ( isNoData && !treatNoDataAsFalse )
291 if ( treatNoDataAsFalse )
299QString QgsRasterLogicalAndAlgorithm::name()
const
301 return u
"rasterbooleanand"_s;
304QString QgsRasterLogicalAndAlgorithm::displayName()
const
306 return QObject::tr(
"Raster boolean AND" );
310QString QgsRasterLogicalAndAlgorithm::shortDescription()
const
312 return QObject::tr(
"Calculates the boolean AND for a set of input raster layers." );
315QString QgsRasterLogicalAndAlgorithm::shortHelpString()
const
317 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, "
318 "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"
319 "The reference layer parameter specifies an existing raster layer to use as a reference when creating the output raster. The output raster "
320 "will have the same extent, CRS, and pixel dimensions as this layer.\n\n"
321 "By default, a NoData pixel in ANY of the input layers will result in a NoData pixel in the output raster. If the "
322 "'Treat NoData values as false' option is checked, then NoData inputs will be treated the same as a 0 input value." );
325QgsRasterLogicalAndAlgorithm *QgsRasterLogicalAndAlgorithm::createInstance()
const
327 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.
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).