24 QString QgsZonalHistogramAlgorithm::name()
const 26 return QStringLiteral(
"zonalhistogram" );
29 QString QgsZonalHistogramAlgorithm::displayName()
const 31 return QObject::tr(
"Zonal histogram" );
34 QStringList QgsZonalHistogramAlgorithm::tags()
const 36 return QObject::tr(
"raster,unique,values,count,area,statistics" ).split(
',' );
39 QString QgsZonalHistogramAlgorithm::group()
const 41 return QObject::tr(
"Raster analysis" );
44 QString QgsZonalHistogramAlgorithm::groupId()
const 46 return QStringLiteral(
"rasteranalysis" );
49 void QgsZonalHistogramAlgorithm::initAlgorithm(
const QVariantMap & )
52 QObject::tr(
"Raster layer" ) ) );
54 QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT_RASTER" ) ) );
59 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COLUMN_PREFIX" ), QObject::tr(
"Output column prefix" ), QStringLiteral(
"HISTO_" ),
false,
true ) );
64 QString QgsZonalHistogramAlgorithm::shortHelpString()
const 66 return QObject::tr(
"This algorithm appends fields representing counts of each unique value from a raster layer contained within zones defined as polygons." );
69 QgsZonalHistogramAlgorithm *QgsZonalHistogramAlgorithm::createInstance()
const 71 return new QgsZonalHistogramAlgorithm();
76 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT_RASTER" ), context );
80 mRasterBand = parameterAsInt( parameters, QStringLiteral(
"RASTER_BAND" ), context );
84 mRasterExtent = layer->
extent();
88 mNbCellsXProvider = mRasterInterface->xSize();
89 mNbCellsYProvider = mRasterInterface->ySize();
97 std::unique_ptr< QgsFeatureSource > zones( parameterAsSource( parameters, QStringLiteral(
"INPUT_VECTOR" ), context ) );
101 long count = zones->featureCount();
102 double step = count > 0 ? 100.0 / count : 1;
105 QList< double > uniqueValues;
106 QMap< QgsFeatureId, QHash< double, qgssize > > featuresUniqueValues;
111 if ( zones->sourceCrs() != mCrs )
139 int nCellsX, nCellsY;
141 QgsRasterAnalysisUtils::cellInfoForBBox( mRasterExtent, featureRect, mCellSizeX, mCellSizeY, nCellsX, nCellsY, mNbCellsXProvider, mNbCellsYProvider, rasterBlockExtent );
143 QHash< double, qgssize > fUniqueValues;
144 QgsRasterAnalysisUtils::statisticsFromMiddlePointTest( mRasterInterface.get(), mRasterBand, featureGeometry, nCellsX, nCellsY, mCellSizeX, mCellSizeY,
145 rasterBlockExtent, [ &fUniqueValues](
double value ) { fUniqueValues[value]++; }, false );
147 if ( fUniqueValues.count() < 1 )
151 QgsRasterAnalysisUtils::statisticsFromPreciseIntersection( mRasterInterface.get(), mRasterBand, featureGeometry, nCellsX, nCellsY, mCellSizeX, mCellSizeY,
152 rasterBlockExtent, [ &fUniqueValues](
double value, double ) { fUniqueValues[value]++; }, false );
155 for (
auto it = fUniqueValues.constBegin(); it != fUniqueValues.constEnd(); ++it )
157 if ( uniqueValues.indexOf( it.key() ) == -1 )
159 uniqueValues << it.key();
161 featuresUniqueValues[f.
id()][it.key()] += it.value();
167 std::sort( uniqueValues.begin(), uniqueValues.end() );
169 QString fieldPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
171 for (
auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it )
173 newFields.
append(
QgsField( QStringLiteral(
"%1%2" ).arg( fieldPrefix ).arg( mHasNoDataValue && *it == mNodataValue ? QStringLiteral(
"NODATA" ) : QString::number( *it ) ), QVariant::LongLong, QString(), -1, 0 ) );
178 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
179 zones->wkbType(), zones->sourceCrs() ) );
187 QHash< double, qgssize > fUniqueValues = featuresUniqueValues.value( f.
id() );
188 for (
auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it )
190 attributes += fUniqueValues.value( *it, 0 );
201 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
A rectangle specified with double values.
Base class for providing feedback from a processing algorithm.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
double rasterUnitsPerPixelX() const
Returns the number of raster units per each raster pixel in X axis.
QgsRasterInterface * clone() const override=0
Clone itself, create deep copy.
void setProgress(double progress)
Sets the current progress for the feedback object.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
A raster band parameter for Processing algorithms.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
virtual double sourceNoDataValue(int bandNo) const
Value representing no data value.
A feature sink output for processing algorithms.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsRasterDataProvider * dataProvider() override
Returns the layer's data provider.
A raster layer parameter for processing algorithms.
bool isEmpty() const
Returns true if the rectangle is empty.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB)
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsCoordinateReferenceSystem crs() const
Returns the layer's spatial reference system.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Encapsulate a field in an attribute table or data source.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
virtual bool sourceHasNoDataValue(int bandNo) const
Returns true if source band has no data value.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Contains information about the context in which a processing algorithm is executed.
A string parameter for processing algorithms.