27 QStringList QgsRasterBooleanLogicAlgorithmBase::tags()
const
29 return QObject::tr(
"logical,boolean" ).split(
',' );
32 QString QgsRasterBooleanLogicAlgorithmBase::group()
const
34 return QObject::tr(
"Raster analysis" );
37 QString QgsRasterBooleanLogicAlgorithmBase::groupId()
const
39 return QStringLiteral(
"rasteranalysis" );
42 void QgsRasterBooleanLogicAlgorithmBase::initAlgorithm(
const QVariantMap & )
48 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"NODATA_AS_FALSE" ), QObject::tr(
"Treat nodata values as false" ),
false ) );
50 std::unique_ptr< QgsProcessingParameterNumber > noDataValueParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"NO_DATA" ),
53 addParameter( noDataValueParam.release() );
55 std::unique_ptr< QgsProcessingParameterDefinition > typeChoice = QgsRasterAnalysisUtils::createRasterTypeParameter( QStringLiteral(
"DATA_TYPE" ), QObject::tr(
"Output data type" ),
Qgis::DataType::Float32 );
57 addParameter( typeChoice.release() );
60 QObject::tr(
"Output layer" ) ) );
67 addOutput(
new QgsProcessingOutputNumber( QStringLiteral(
"NODATA_PIXEL_COUNT" ), QObject::tr(
"NODATA pixel count" ) ) );
74 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, QStringLiteral(
"REF_LAYER" ), context );
75 if ( !referenceLayer )
77 mCrs = referenceLayer->
crs();
80 mLayerWidth = referenceLayer->
width();
81 mLayerHeight = referenceLayer->
height();
82 mExtent = referenceLayer->
extent();
83 mNoDataValue = parameterAsDouble( parameters, QStringLiteral(
"NO_DATA" ), context );
84 mDataType = QgsRasterAnalysisUtils::rasterTypeChoiceToDataType( parameterAsEnum( parameters, QStringLiteral(
"DATA_TYPE" ), context ) );
86 mTreatNodataAsFalse = parameterAsBoolean( parameters, QStringLiteral(
"NODATA_AS_FALSE" ), context );
88 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"INPUT" ), context );
89 QList< QgsRasterLayer * > rasterLayers;
90 rasterLayers.reserve( layers.count() );
96 QgsRasterAnalysisUtils::RasterLogicInput input;
100 input.interface = input.sourceDataProvider.get();
102 if ( layer->
crs() != mCrs )
104 input.projector = std::make_unique< QgsRasterProjector >();
105 input.projector->setInput( input.sourceDataProvider.get() );
107 input.interface = input.projector.get();
109 mInputs.emplace_back( std::move( input ) );
118 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
119 const QFileInfo fi( outputFile );
122 std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
123 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
124 writer->setOutputFormat( outputFormat );
125 std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
128 if ( !provider->isValid() )
131 provider->setNoDataValue( 1, mNoDataValue );
135 const qgssize layerSize =
static_cast< qgssize >( mLayerWidth ) *
static_cast< qgssize >( mLayerHeight );
137 QgsRasterAnalysisUtils::applyRasterLogicOperator( mInputs, provider.get(), mNoDataValue, mTreatNodataAsFalse, mLayerWidth, mLayerHeight,
138 mExtent, feedback, mExtractValFunc, noDataCount, trueCount, falseCount );
141 outputs.insert( QStringLiteral(
"EXTENT" ), mExtent.toString() );
142 outputs.insert( QStringLiteral(
"CRS_AUTHID" ), mCrs.authid() );
143 outputs.insert( QStringLiteral(
"WIDTH_IN_PIXELS" ), mLayerWidth );
144 outputs.insert( QStringLiteral(
"HEIGHT_IN_PIXELS" ), mLayerHeight );
145 outputs.insert( QStringLiteral(
"TOTAL_PIXEL_COUNT" ), layerSize );
146 outputs.insert( QStringLiteral(
"NODATA_PIXEL_COUNT" ), noDataCount );
147 outputs.insert( QStringLiteral(
"TRUE_PIXEL_COUNT" ), trueCount );
148 outputs.insert( QStringLiteral(
"FALSE_PIXEL_COUNT" ), falseCount );
149 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
159 QgsRasterLogicalOrAlgorithm::QgsRasterLogicalOrAlgorithm()
161 mExtractValFunc = [ = ](
const std::vector< std::unique_ptr< QgsRasterBlock > > &inputs,
bool & res,
bool & resIsNoData,
int row,
int column,
bool treatNoDataAsFalse )
165 bool isNoData =
false;
166 for (
auto &block : inputs )
169 if ( !block || !block->isValid() )
171 if ( treatNoDataAsFalse )
181 value = block->valueAndNoData( row, column, isNoData );
182 if ( isNoData && !treatNoDataAsFalse )
190 if ( treatNoDataAsFalse )
198 QString QgsRasterLogicalOrAlgorithm::name()
const
200 return QStringLiteral(
"rasterlogicalor" );
203 QString QgsRasterLogicalOrAlgorithm::displayName()
const
205 return QObject::tr(
"Raster boolean OR" );
209 QString QgsRasterLogicalOrAlgorithm::shortDescription()
const
211 return QObject::tr(
"Calculates the boolean OR for a set of input raster layers" );
214 QString QgsRasterLogicalOrAlgorithm::shortHelpString()
const
216 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, "
217 "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"
218 "The reference layer parameter specifies an existing raster layer to use as a reference when creating the output raster. The output raster "
219 "will have the same extent, CRS, and pixel dimensions as this layer.\n\n"
220 "By default, a nodata pixel in ANY of the input layers will result in a nodata pixel in the output raster. If the "
221 "'Treat nodata values as false' option is checked, then nodata inputs will be treated the same as a 0 input value." );
224 QgsRasterLogicalOrAlgorithm *QgsRasterLogicalOrAlgorithm::createInstance()
const
226 return new QgsRasterLogicalOrAlgorithm();
233 QgsRasterLogicalAndAlgorithm::QgsRasterLogicalAndAlgorithm()
235 mExtractValFunc = [ = ](
const std::vector< std::unique_ptr< QgsRasterBlock > > &inputs,
bool & res,
bool & resIsNoData,
int row,
int column,
bool treatNoDataAsFalse )
239 bool isNoData =
false;
240 for (
auto &block : inputs )
243 if ( !block || !block->isValid() )
245 if ( treatNoDataAsFalse )
258 value = block->valueAndNoData( row, column, isNoData );
259 if ( isNoData && !treatNoDataAsFalse )
267 if ( treatNoDataAsFalse )
275 QString QgsRasterLogicalAndAlgorithm::name()
const
277 return QStringLiteral(
"rasterbooleanand" );
280 QString QgsRasterLogicalAndAlgorithm::displayName()
const
282 return QObject::tr(
"Raster boolean AND" );
286 QString QgsRasterLogicalAndAlgorithm::shortDescription()
const
288 return QObject::tr(
"Calculates the boolean AND for a set of input raster layers" );
291 QString QgsRasterLogicalAndAlgorithm::shortHelpString()
const
293 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, "
294 "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"
295 "The reference layer parameter specifies an existing raster layer to use as a reference when creating the output raster. The output raster "
296 "will have the same extent, CRS, and pixel dimensions as this layer.\n\n"
297 "By default, a nodata pixel in ANY of the input layers will result in a nodata pixel in the output raster. If the "
298 "'Treat nodata values as false' option is checked, then nodata inputs will be treated the same as a 0 input value." );
301 QgsRasterLogicalAndAlgorithm *QgsRasterLogicalAndAlgorithm::createInstance()
const
303 return new QgsRasterLogicalAndAlgorithm();
@ Float32
Thirty two bit floating point (float)
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.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
A parameter for processing algorithms which accepts multiple map layers.
@ Double
Double/float values.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
@ TypeRaster
Raster layers.
virtual bool sourceHasNoDataValue(int bandNo) const
Returns true if source band has no data value.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
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)