QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmrasterlayeruniquevalues.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrasterlayeruniquevalues.cpp
3 ---------------------
4 begin : April 2017
5 copyright : (C) 2017 by Mathieu Pellerin
6 email : nirvn dot asia at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsstringutils.h"
21#include "qgsunittypes.h"
22
23#include <QTextStream>
24
26
27QString QgsRasterLayerUniqueValuesReportAlgorithm::name() const
28{
29 return QStringLiteral( "rasterlayeruniquevaluesreport" );
30}
31
32QString QgsRasterLayerUniqueValuesReportAlgorithm::displayName() const
33{
34 return QObject::tr( "Raster layer unique values report" );
35}
36
37QStringList QgsRasterLayerUniqueValuesReportAlgorithm::tags() const
38{
39 return QObject::tr( "count,area,statistics" ).split( ',' );
40}
41
42QString QgsRasterLayerUniqueValuesReportAlgorithm::group() const
43{
44 return QObject::tr( "Raster analysis" );
45}
46
47QString QgsRasterLayerUniqueValuesReportAlgorithm::groupId() const
48{
49 return QStringLiteral( "rasteranalysis" );
50}
51
52void QgsRasterLayerUniqueValuesReportAlgorithm::initAlgorithm( const QVariantMap & )
53{
54 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
55 addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ), QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT" ) ) );
56 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT_HTML_FILE" ), QObject::tr( "Unique values report" ), QObject::tr( "HTML files (*.html)" ), QVariant(), true ) );
57 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT_TABLE" ), QObject::tr( "Unique values table" ), Qgis::ProcessingSourceType::Vector, QVariant(), true, false ) );
58
59 addOutput( new QgsProcessingOutputString( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ) ) );
60 addOutput( new QgsProcessingOutputString( QStringLiteral( "CRS_AUTHID" ), QObject::tr( "CRS authority identifier" ) ) );
61 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "WIDTH_IN_PIXELS" ), QObject::tr( "Width in pixels" ) ) );
62 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "HEIGHT_IN_PIXELS" ), QObject::tr( "Height in pixels" ) ) );
63 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "TOTAL_PIXEL_COUNT" ), QObject::tr( "Total pixel count" ) ) );
64 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NODATA_PIXEL_COUNT" ), QObject::tr( "NoData pixel count" ) ) );
65}
66
67QString QgsRasterLayerUniqueValuesReportAlgorithm::shortHelpString() const
68{
69 return QObject::tr( "This algorithm returns the count and area of each unique value in a given raster layer. "
70 "The area calculation is done in the area unit of the layer's CRS." );
71}
72
73QString QgsRasterLayerUniqueValuesReportAlgorithm::shortDescription() const
74{
75 return QObject::tr( "Returns the count and area of each unique value in a given raster layer." );
76}
77
78QgsRasterLayerUniqueValuesReportAlgorithm *QgsRasterLayerUniqueValuesReportAlgorithm::createInstance() const
79{
80 return new QgsRasterLayerUniqueValuesReportAlgorithm();
81}
82
83bool QgsRasterLayerUniqueValuesReportAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
84{
85 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
86 const int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
87
88 if ( !layer )
89 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
90
91 mBand = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
92 if ( mBand < 1 || mBand > layer->bandCount() )
93 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->bandCount() ) );
94
95 mInterface.reset( layer->dataProvider()->clone() );
96 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
97 mLayerWidth = layer->width();
98 mLayerHeight = layer->height();
99 mExtent = layer->extent();
100 mCrs = layer->crs();
101 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
102 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
103 mSource = layer->source();
104
105 return true;
106}
107
108QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
109{
110 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context );
111
112 QString areaUnit = QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::distanceToAreaUnit( mCrs.mapUnits() ) );
113
114 QString tableDest;
115 std::unique_ptr<QgsFeatureSink> sink;
116 if ( parameters.contains( QStringLiteral( "OUTPUT_TABLE" ) ) && parameters.value( QStringLiteral( "OUTPUT_TABLE" ) ).isValid() )
117 {
118 QgsFields outFields;
119 outFields.append( QgsField( QStringLiteral( "value" ), QMetaType::Type::Double, QString(), 20, 8 ) );
120 outFields.append( QgsField( QStringLiteral( "count" ), QMetaType::Type::LongLong, QString(), 20 ) );
121 outFields.append( QgsField( areaUnit.replace( QStringLiteral( "²" ), QStringLiteral( "2" ) ), QMetaType::Type::Double, QString(), 20, 8 ) );
122 sink.reset( parameterAsSink( parameters, QStringLiteral( "OUTPUT_TABLE" ), context, tableDest, outFields, Qgis::WkbType::NoGeometry, QgsCoordinateReferenceSystem() ) );
123 if ( !sink )
124 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT_TABLE" ) ) );
125 }
126
127 QHash<double, qgssize> uniqueValues;
128 qgssize noDataCount = 0;
129
130 const qgssize layerSize = static_cast<qgssize>( mLayerWidth ) * static_cast<qgssize>( mLayerHeight );
133 const int nbBlocksWidth = std::ceil( 1.0 * mLayerWidth / maxWidth );
134 const int nbBlocksHeight = std::ceil( 1.0 * mLayerHeight / maxHeight );
135 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
136
137 QgsRasterIterator iter( mInterface.get() );
138 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
139
140 int iterLeft = 0;
141 int iterTop = 0;
142 int iterCols = 0;
143 int iterRows = 0;
144 bool isNoData = false;
145 std::unique_ptr<QgsRasterBlock> rasterBlock;
146 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
147 {
148 feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
149 for ( int row = 0; row < iterRows; row++ )
150 {
151 if ( feedback->isCanceled() )
152 break;
153 for ( int column = 0; column < iterCols; column++ )
154 {
155 const double value = rasterBlock->valueAndNoData( row, column, isNoData );
156 if ( mHasNoDataValue && isNoData )
157 {
158 noDataCount++;
159 }
160 else
161 {
162 uniqueValues[value]++;
163 }
164 }
165 }
166 if ( feedback->isCanceled() )
167 break;
168 }
169
170 QMap<double, qgssize> sortedUniqueValues;
171 for ( auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it )
172 {
173 sortedUniqueValues.insert( it.key(), it.value() );
174 }
175
176 QVariantMap outputs;
177 outputs.insert( QStringLiteral( "EXTENT" ), mExtent.toString() );
178 outputs.insert( QStringLiteral( "CRS_AUTHID" ), mCrs.authid() );
179 outputs.insert( QStringLiteral( "WIDTH_IN_PIXELS" ), mLayerWidth );
180 outputs.insert( QStringLiteral( "HEIGHT_IN_PIXELS" ), mLayerHeight );
181 outputs.insert( QStringLiteral( "TOTAL_PIXEL_COUNT" ), layerSize );
182 outputs.insert( QStringLiteral( "NODATA_PIXEL_COUNT" ), noDataCount );
183
184 const double pixelArea = mRasterUnitsPerPixelX * mRasterUnitsPerPixelY;
185
186 if ( !outputFile.isEmpty() )
187 {
188 QFile file( outputFile );
189 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
190 {
191 const QString encodedAreaUnit = QgsStringUtils::ampersandEncode( areaUnit );
192
193 QTextStream out( &file );
194#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
195 out.setCodec( "UTF-8" );
196#endif
197 out << QStringLiteral( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
198 out << QStringLiteral( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Analyzed file" ), mSource, QObject::tr( "band" ) ).arg( mBand );
199 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Extent" ), mExtent.toString() );
200 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Projection" ), mCrs.userFriendlyIdentifier() );
201 out << QObject::tr( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Width in pixels" ) ).arg( mLayerWidth ).arg( QObject::tr( "units per pixel" ) ).arg( mRasterUnitsPerPixelX );
202 out << QObject::tr( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Height in pixels" ) ).arg( mLayerHeight ).arg( QObject::tr( "units per pixel" ) ).arg( mRasterUnitsPerPixelY );
203 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Total pixel count" ) ).arg( layerSize );
204 if ( mHasNoDataValue )
205 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "NoData pixel count" ) ).arg( noDataCount );
206 out << QStringLiteral( "<table><tr><td>%1</td><td>%2</td><td>%3 (%4)</td></tr>\n" ).arg( QObject::tr( "Value" ), QObject::tr( "Pixel count" ), QObject::tr( "Area" ), encodedAreaUnit );
207
208 for ( auto it = sortedUniqueValues.constBegin(); it != sortedUniqueValues.constEnd(); ++it )
209 {
210 const double area = it.value() * pixelArea;
211 out << QStringLiteral( "<tr><td>%1</td><td>%2</td><td>%3</td></tr>\n" ).arg( it.key() ).arg( it.value() ).arg( QString::number( area, 'g', 16 ) );
212 }
213 out << QStringLiteral( "</table>\n</body></html>" );
214 outputs.insert( QStringLiteral( "OUTPUT_HTML_FILE" ), outputFile );
215 }
216 }
217
218 if ( sink )
219 {
220 for ( auto it = sortedUniqueValues.constBegin(); it != sortedUniqueValues.constEnd(); ++it )
221 {
222 QgsFeature f;
223 const double area = it.value() * pixelArea;
224 f.setAttributes( QgsAttributes() << it.key() << it.value() << area );
225 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
226 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT_TABLE" ) ) );
227 }
228 sink->finalize();
229 outputs.insert( QStringLiteral( "OUTPUT_TABLE" ), tableDest );
230 }
231
232 return outputs;
233}
234
235
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ NoGeometry
No geometry.
Definition qgis.h:294
A vector of attributes.
Represents a coordinate reference system (CRS).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:73
virtual QgsRectangle extent() const
Returns the extent of the layer.
QString source() const
Returns the source for the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:87
Contains information about the context in which a processing algorithm is executed.
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 generic file based destination parameter, for specifying the destination path for a file (non-map l...
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.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
Default maximum tile width.
static const int DEFAULT_MAXIMUM_TILE_HEIGHT
Default maximum tile height.
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.
static QString ampersandEncode(const QString &string)
Makes a raw string safe for inclusion as a HTML/XML string literal.
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...
Definition qgis.h:7142