27using namespace Qt::StringLiterals;
31QString QgsRasterLayerZonalStatsAlgorithm::name()
const
33 return u
"rasterlayerzonalstats"_s;
36QString QgsRasterLayerZonalStatsAlgorithm::displayName()
const
38 return QObject::tr(
"Raster layer zonal statistics" );
41QStringList QgsRasterLayerZonalStatsAlgorithm::tags()
const
43 return QObject::tr(
"count,area,statistics,stats,zones,categories,minimum,maximum,mean,sum,total" ).split(
',' );
46QString QgsRasterLayerZonalStatsAlgorithm::group()
const
48 return QObject::tr(
"Raster analysis" );
51QString QgsRasterLayerZonalStatsAlgorithm::groupId()
const
53 return u
"rasteranalysis"_s;
56void QgsRasterLayerZonalStatsAlgorithm::initAlgorithm(
const QVariantMap & )
63 auto refParam = std::make_unique<QgsProcessingParameterEnum>( u
"REF_LAYER"_s, QObject::tr(
"Reference layer" ), QStringList() << QObject::tr(
"Input layer" ) << QObject::tr(
"Zones layer" ),
false, 0 );
65 addParameter( refParam.release() );
77QString QgsRasterLayerZonalStatsAlgorithm::shortDescription()
const
79 return QObject::tr(
"Calculates statistics for a raster layer's values, categorized by zones defined in another raster layer." );
82QString QgsRasterLayerZonalStatsAlgorithm::shortHelpString()
const
84 return QObject::tr(
"This algorithm calculates statistics for a raster layer's values, categorized by zones defined in another raster layer.\n\n"
85 "If the reference layer parameter is set to \"Input layer\", then zones are determined by sampling the zone raster layer value at the centroid of each pixel from the source raster layer.\n\n"
86 "If the reference layer parameter is set to \"Zones layer\", then the input raster layer will be sampled at the centroid of each pixel from the zones raster layer.\n\n"
87 "If either the source raster layer or the zone raster layer value is NoData for a pixel, that pixel's value will be skipped and not included in the calculated statistics." );
90QgsRasterLayerZonalStatsAlgorithm *QgsRasterLayerZonalStatsAlgorithm::createInstance()
const
92 return new QgsRasterLayerZonalStatsAlgorithm();
97 mRefLayer =
static_cast<RefLayer
>( parameterAsEnum( parameters, u
"REF_LAYER"_s, context ) );
99 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u
"INPUT"_s, context );
100 const int band = parameterAsInt( parameters, u
"BAND"_s, context );
105 mBand = parameterAsInt( parameters, u
"BAND"_s, context );
106 if ( mBand < 1 || mBand > layer->
bandCount() )
107 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->
bandCount() ) );
111 QgsRasterLayer *zonesLayer = parameterAsRasterLayer( parameters, u
"ZONES"_s, context );
116 mZonesBand = parameterAsInt( parameters, u
"ZONES_BAND"_s, context );
117 if ( mZonesBand < 1 || mZonesBand > zonesLayer->
bandCount() )
118 throw QgsProcessingException( QObject::tr(
"Invalid band number for ZONES_BAND (%1): Valid values for input raster are 1 to %2" ).arg( mZonesBand ).arg( zonesLayer->
bandCount() ) );
122 mSourceInterface = mSourceDataProvider.get();
124 mZonesInterface = mZonesDataProvider.get();
132 mLayerWidth = layer->
width();
133 mLayerHeight = layer->
height();
134 mExtent = layer->
extent();
137 if ( layer->
crs() != zonesLayer->
crs() )
139 mProjector = std::make_unique<QgsRasterProjector>();
140 mProjector->setInput( mZonesDataProvider.get() );
142 mZonesInterface = mProjector.get();
147 mCrs = zonesLayer->
crs();
150 mLayerWidth = zonesLayer->
width();
151 mLayerHeight = zonesLayer->
height();
152 mExtent = zonesLayer->
extent();
155 if ( layer->
crs() != zonesLayer->
crs() )
157 mProjector = std::make_unique<QgsRasterProjector>();
158 mProjector->setInput( mSourceDataProvider.get() );
160 mSourceInterface = mProjector.get();
173 std::unique_ptr<QgsFeatureSink> sink;
174 if ( parameters.contains( u
"OUTPUT_TABLE"_s ) && parameters.value( u
"OUTPUT_TABLE"_s ).isValid() )
177 outFields.
append(
QgsField( u
"zone"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
178 outFields.
append(
QgsField( areaUnit.isEmpty() ?
"area" : areaUnit.replace( u
"²"_s,
"2"_L1 ), QMetaType::Type::Double, QString(), 20, 8 ) );
179 outFields.
append(
QgsField( u
"sum"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
180 outFields.
append(
QgsField( u
"count"_s, QMetaType::Type::LongLong, QString(), 20 ) );
181 outFields.
append(
QgsField( u
"min"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
182 outFields.
append(
QgsField( u
"max"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
183 outFields.
append(
QgsField( u
"mean"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
190 struct StatCalculator
196 QHash<double, StatCalculator> zoneStats;
199 const qgssize layerSize =
static_cast<qgssize>( mLayerWidth ) *
static_cast<qgssize>( mLayerHeight );
203 iter.
startRasterRead( mRefLayer == Source ? mBand : mZonesBand, mLayerWidth, mLayerHeight, mExtent );
210 std::unique_ptr<QgsRasterBlock> rasterBlock;
211 std::unique_ptr<QgsRasterBlock> zonesRasterBlock;
212 bool isNoData =
false;
216 if ( mRefLayer == Source )
219 if ( !iter.
readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop, &blockExtent ) )
222 zonesRasterBlock.reset( mZonesInterface->block( mZonesBand, blockExtent, iterCols, iterRows ) );
227 if ( !iter.
readNextRasterPart( mZonesBand, iterCols, iterRows, zonesRasterBlock, iterLeft, iterTop, &blockExtent ) )
230 rasterBlock.reset( mSourceInterface->block( mBand, blockExtent, iterCols, iterRows ) );
232 if ( !zonesRasterBlock || !rasterBlock )
236 if ( !rasterBlock->isValid() || rasterBlock->isEmpty() || !zonesRasterBlock->isValid() || zonesRasterBlock->isEmpty() )
239 for (
int row = 0; row < iterRows; row++ )
244 for (
int column = 0; column < iterCols; column++ )
246 const double value = rasterBlock->valueAndNoData( row, column, isNoData );
247 if ( mHasNoDataValue && isNoData )
252 const double zone = zonesRasterBlock->valueAndNoData( row, column, isNoData );
253 if ( mZonesHasNoDataValue && isNoData )
258 zoneStats[zone].s.addValue( value );
264 outputs.insert( u
"EXTENT"_s, mExtent.toString() );
265 outputs.insert( u
"CRS_AUTHID"_s, mCrs.authid() );
266 outputs.insert( u
"WIDTH_IN_PIXELS"_s, mLayerWidth );
267 outputs.insert( u
"HEIGHT_IN_PIXELS"_s, mLayerHeight );
268 outputs.insert( u
"TOTAL_PIXEL_COUNT"_s, layerSize );
269 outputs.insert( u
"NODATA_PIXEL_COUNT"_s, noDataCount );
271 const double pixelArea = mRasterUnitsPerPixelX * mRasterUnitsPerPixelY;
273 for (
auto it = zoneStats.begin(); it != zoneStats.end(); ++it )
277 f.setAttributes( QgsAttributes() << it.key() << it->s.count() * pixelArea << it->s.sum() << it->s.count() << it->s.min() << it->s.max() << it->s.mean() );
278 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
279 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u
"OUTPUT_TABLE"_s ) );
282 outputs.insert( u
"OUTPUT_TABLE"_s, tableDest );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Represents a coordinate reference system (CRS).
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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 raster band parameter for Processing algorithms.
A feature sink output for processing algorithms.
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.
Iterator for sequentially processing raster cells.
bool readNextRasterPart(int bandNumber, int &nCols, int &nRows, QgsRasterBlock **block, int &topLeftCol, int &topLeftRow)
Fetches next part of raster data, caller takes ownership of the block and caller should delete the bl...
double progress(int bandNumber, double currentBlockProgress=-1) const
Returns the raster iteration progress as a fraction from 0 to 1.0, for the specified bandNumber.
void startRasterRead(int bandNumber, qgssize nCols, qgssize nRows, const QgsRectangle &extent, QgsRasterBlockFeedback *feedback=nullptr)
Start reading of raster band.
Represents a raster layer.
int height() const
Returns the height of the (unclipped) raster.
int bandCount() const
Returns the number of bands in this layer.
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.
A rectangle specified with double values.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
static Q_INVOKABLE Qgis::AreaUnit distanceToAreaUnit(Qgis::DistanceUnit distanceUnit)
Converts a distance unit to its corresponding area unit, e.g., meters to square meters.
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...