QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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#include "qgsstringutils.h"
20#include "qgsunittypes.h"
21
22#include <QTextStream>
23
25
26QString QgsRasterLayerUniqueValuesReportAlgorithm::name() const
27{
28 return QStringLiteral( "rasterlayeruniquevaluesreport" );
29}
30
31QString QgsRasterLayerUniqueValuesReportAlgorithm::displayName() const
32{
33 return QObject::tr( "Raster layer unique values report" );
34}
35
36QStringList QgsRasterLayerUniqueValuesReportAlgorithm::tags() const
37{
38 return QObject::tr( "count,area,statistics" ).split( ',' );
39}
40
41QString QgsRasterLayerUniqueValuesReportAlgorithm::group() const
42{
43 return QObject::tr( "Raster analysis" );
44}
45
46QString QgsRasterLayerUniqueValuesReportAlgorithm::groupId() const
47{
48 return QStringLiteral( "rasteranalysis" );
49}
50
51void QgsRasterLayerUniqueValuesReportAlgorithm::initAlgorithm( const QVariantMap & )
52{
53 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ),
54 QObject::tr( "Input layer" ) ) );
55 addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ),
56 QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT" ) ) );
57 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT_HTML_FILE" ),
58 QObject::tr( "Unique values report" ), QObject::tr( "HTML files (*.html)" ), QVariant(), true ) );
59 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT_TABLE" ),
60 QObject::tr( "Unique values table" ), QgsProcessing::TypeVector, QVariant(), true, false ) );
61
62 addOutput( new QgsProcessingOutputString( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ) ) );
63 addOutput( new QgsProcessingOutputString( QStringLiteral( "CRS_AUTHID" ), QObject::tr( "CRS authority identifier" ) ) );
64 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "WIDTH_IN_PIXELS" ), QObject::tr( "Width in pixels" ) ) );
65 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "HEIGHT_IN_PIXELS" ), QObject::tr( "Height in pixels" ) ) );
66 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "TOTAL_PIXEL_COUNT" ), QObject::tr( "Total pixel count" ) ) );
67 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NODATA_PIXEL_COUNT" ), QObject::tr( "NODATA pixel count" ) ) );
68}
69
70QString QgsRasterLayerUniqueValuesReportAlgorithm::shortHelpString() const
71{
72 return QObject::tr( "This algorithm returns the count and area of each unique value in a given raster layer." );
73}
74
75QgsRasterLayerUniqueValuesReportAlgorithm *QgsRasterLayerUniqueValuesReportAlgorithm::createInstance() const
76{
77 return new QgsRasterLayerUniqueValuesReportAlgorithm();
78}
79
80bool QgsRasterLayerUniqueValuesReportAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
81{
82 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
83 const int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
84
85 if ( !layer )
86 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
87
88 mBand = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
89 if ( mBand < 1 || mBand > layer->bandCount() )
90 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
91 .arg( layer->bandCount() ) );
92
93 mInterface.reset( layer->dataProvider()->clone() );
94 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
95 mLayerWidth = layer->width();
96 mLayerHeight = layer->height();
97 mExtent = layer->extent();
98 mCrs = layer->crs();
99 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
100 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
101 mSource = layer->source();
102
103 return true;
104}
105
106QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
107{
108 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context );
109
110 QString areaUnit = QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::distanceToAreaUnit( mCrs.mapUnits() ) );
111
112 QString tableDest;
113 std::unique_ptr< QgsFeatureSink > sink;
114 if ( parameters.contains( QStringLiteral( "OUTPUT_TABLE" ) ) && parameters.value( QStringLiteral( "OUTPUT_TABLE" ) ).isValid() )
115 {
116 QgsFields outFields;
117 outFields.append( QgsField( QStringLiteral( "value" ), QVariant::Double, QString(), 20, 8 ) );
118 outFields.append( QgsField( QStringLiteral( "count" ), QVariant::LongLong, QString(), 20 ) );
119 outFields.append( QgsField( areaUnit.replace( QStringLiteral( "²" ), QStringLiteral( "2" ) ), QVariant::Double, QString(), 20, 8 ) );
120 sink.reset( parameterAsSink( parameters, QStringLiteral( "OUTPUT_TABLE" ), context, tableDest, outFields, Qgis::WkbType::NoGeometry, QgsCoordinateReferenceSystem() ) );
121 if ( !sink )
122 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT_TABLE" ) ) );
123 }
124
125 QHash< double, qgssize > uniqueValues;
126 qgssize noDataCount = 0;
127
128 const qgssize layerSize = static_cast< qgssize >( mLayerWidth ) * static_cast< qgssize >( mLayerHeight );
131 const int nbBlocksWidth = std::ceil( 1.0 * mLayerWidth / maxWidth );
132 const int nbBlocksHeight = std::ceil( 1.0 * mLayerHeight / maxHeight );
133 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
134
135 QgsRasterIterator iter( mInterface.get() );
136 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
137
138 int iterLeft = 0;
139 int iterTop = 0;
140 int iterCols = 0;
141 int iterRows = 0;
142 bool isNoData = false;
143 std::unique_ptr< QgsRasterBlock > rasterBlock;
144 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
145 {
146 feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
147 for ( int row = 0; row < iterRows; row++ )
148 {
149 if ( feedback->isCanceled() )
150 break;
151 for ( int column = 0; column < iterCols; column++ )
152 {
153 const double value = rasterBlock->valueAndNoData( row, column, isNoData );
154 if ( mHasNoDataValue && isNoData )
155 {
156 noDataCount++;
157 }
158 else
159 {
160 uniqueValues[ value ]++;
161 }
162 }
163 }
164 if ( feedback->isCanceled() )
165 break;
166 }
167
168 QMap< double, qgssize > sortedUniqueValues;
169 for ( auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it )
170 {
171 sortedUniqueValues.insert( it.key(), it.value() );
172 }
173
174 QVariantMap outputs;
175 outputs.insert( QStringLiteral( "EXTENT" ), mExtent.toString() );
176 outputs.insert( QStringLiteral( "CRS_AUTHID" ), mCrs.authid() );
177 outputs.insert( QStringLiteral( "WIDTH_IN_PIXELS" ), mLayerWidth );
178 outputs.insert( QStringLiteral( "HEIGHT_IN_PIXELS" ), mLayerHeight );
179 outputs.insert( QStringLiteral( "TOTAL_PIXEL_COUNT" ), layerSize );
180 outputs.insert( QStringLiteral( "NODATA_PIXEL_COUNT" ), noDataCount );
181
182 const double pixelArea = mRasterUnitsPerPixelX * mRasterUnitsPerPixelY;
183
184 if ( !outputFile.isEmpty() )
185 {
186 QFile file( outputFile );
187 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
188 {
189 const QString encodedAreaUnit = QgsStringUtils::ampersandEncode( areaUnit );
190
191 QTextStream out( &file );
192#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
193 out.setCodec( "UTF-8" );
194#endif
195 out << QStringLiteral( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
196 out << QStringLiteral( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Analyzed file" ), mSource, QObject::tr( "band" ) ).arg( mBand );
197 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Extent" ), mExtent.toString() );
198 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Projection" ), mCrs.userFriendlyIdentifier() );
199 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 );
200 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 );
201 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Total pixel count" ) ).arg( layerSize );
202 if ( mHasNoDataValue )
203 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "NODATA pixel count" ) ).arg( noDataCount );
204 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 );
205
206 for ( auto it = sortedUniqueValues.constBegin(); it != sortedUniqueValues.constEnd(); ++it )
207 {
208 const double area = it.value() * pixelArea;
209 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 ) );
210 }
211 out << QStringLiteral( "</table>\n</body></html>" );
212 outputs.insert( QStringLiteral( "OUTPUT_HTML_FILE" ), outputFile );
213 }
214 }
215
216 if ( sink )
217 {
218 for ( auto it = sortedUniqueValues.constBegin(); it != sortedUniqueValues.constEnd(); ++it )
219 {
220 QgsFeature f;
221 const double area = it.value() * pixelArea;
222 f.setAttributes( QgsAttributes() << it.key() << it.value() << area );
223 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
224 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT_TABLE" ) ) );
225 }
226 outputs.insert( QStringLiteral( "OUTPUT_TABLE" ), tableDest );
227 }
228
229 return outputs;
230}
231
232
234
235
236
@ NoGeometry
No geometry.
A vector of attributes.
This class 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:56
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:54
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:45
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition qgsfields.cpp:59
virtual QgsRectangle extent() const
Returns the extent of the layer.
QString source() const
Returns the source for the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:80
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.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
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:4887