QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmrastersurfacevolume.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrasterlayeruniquevalues.cpp
3 ---------------------
4 begin : January 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson 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 QgsRasterSurfaceVolumeAlgorithm::name() const
28{
29 return QStringLiteral( "rastersurfacevolume" );
30}
31
32QString QgsRasterSurfaceVolumeAlgorithm::displayName() const
33{
34 return QObject::tr( "Raster surface volume" );
35}
36
37QStringList QgsRasterSurfaceVolumeAlgorithm::tags() const
38{
39 return QObject::tr( "sum,volume,area,height,terrain,dem,elevation" ).split( ',' );
40}
41
42QString QgsRasterSurfaceVolumeAlgorithm::group() const
43{
44 return QObject::tr( "Raster analysis" );
45}
46
47QString QgsRasterSurfaceVolumeAlgorithm::groupId() const
48{
49 return QStringLiteral( "rasteranalysis" );
50}
51
52void QgsRasterSurfaceVolumeAlgorithm::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 QgsProcessingParameterNumber( QStringLiteral( "LEVEL" ), QObject::tr( "Base level" ), Qgis::ProcessingNumberParameterType::Double, 0 ) );
57 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "METHOD" ), QObject::tr( "Method" ), QStringList() << QObject::tr( "Count Only Above Base Level" ) << QObject::tr( "Count Only Below Base Level" ) << QObject::tr( "Subtract Volumes Below Base Level" ) << QObject::tr( "Add Volumes Below Base Level" ) ) );
58
59 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT_HTML_FILE" ), QObject::tr( "Surface volume report" ), QObject::tr( "HTML files (*.html)" ), QVariant(), true ) );
60 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT_TABLE" ), QObject::tr( "Surface volume table" ), Qgis::ProcessingSourceType::Vector, QVariant(), true, false ) );
61
62 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "VOLUME" ), QObject::tr( "Volume" ) ) );
63 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "PIXEL_COUNT" ), QObject::tr( "Pixel count" ) ) );
64 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "AREA" ), QObject::tr( "Area" ) ) );
65}
66
67QString QgsRasterSurfaceVolumeAlgorithm::shortHelpString() const
68{
69 return QObject::tr( "This algorithm calculates the volume under a raster grid's surface.\n\n"
70 "Several methods of volume calculation are available, which control whether "
71 "only values above or below the specified base level are considered, or "
72 "whether volumes below the base level should be added or subtracted from the total volume.\n\n"
73 "The algorithm outputs the calculated volume, the total area, and the total number of pixels analysed. "
74 "If the 'Count Only Above Base Level' or 'Count Only Below Base Level' methods are used, "
75 "then the calculated area and pixel count only includes pixels which are above or below the "
76 "specified base level respectively.\n\n"
77 "Units of the calculated volume are dependent on the coordinate reference system of "
78 "the input raster file. For a CRS in meters, with a DEM height in meters, the calculated "
79 "value will be in meters³. If instead the input raster is in a geographic coordinate system "
80 "(e.g. latitude/longitude values), then the result will be in degrees² × meters, and an "
81 "appropriate scaling factor will need to be applied in order to convert to meters³." );
82}
83
84QString QgsRasterSurfaceVolumeAlgorithm::shortDescription() const
85{
86 return QObject::tr( "Calculates the volume under a raster grid's surface." );
87}
88
89QgsRasterSurfaceVolumeAlgorithm *QgsRasterSurfaceVolumeAlgorithm::createInstance() const
90{
91 return new QgsRasterSurfaceVolumeAlgorithm();
92}
93
94bool QgsRasterSurfaceVolumeAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
95{
96 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
97 const int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
98
99 if ( !layer )
100 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
101
102 mBand = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
103 if ( mBand < 1 || mBand > layer->bandCount() )
104 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->bandCount() ) );
105
106 mInterface.reset( layer->dataProvider()->clone() );
107 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
108 mLayerWidth = layer->width();
109 mLayerHeight = layer->height();
110 mExtent = layer->extent();
111 mCrs = layer->crs();
112 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
113 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
114 mSource = layer->source();
115
116 mLevel = parameterAsDouble( parameters, QStringLiteral( "LEVEL" ), context );
117 mMethod = static_cast<Method>( parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context ) );
118 return true;
119}
120
121QVariantMap QgsRasterSurfaceVolumeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
122{
123 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context );
124 QString areaUnit = QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::distanceToAreaUnit( mCrs.mapUnits() ) );
125
126 QString tableDest;
127 std::unique_ptr<QgsFeatureSink> sink;
128 if ( parameters.contains( QStringLiteral( "OUTPUT_TABLE" ) ) && parameters.value( QStringLiteral( "OUTPUT_TABLE" ) ).isValid() )
129 {
130 QgsFields outFields;
131 outFields.append( QgsField( QStringLiteral( "volume" ), QMetaType::Type::Double, QString(), 20, 8 ) );
132 outFields.append( QgsField( areaUnit.replace( QStringLiteral( "²" ), QStringLiteral( "2" ) ), QMetaType::Type::Double, QString(), 20, 8 ) );
133 outFields.append( QgsField( QStringLiteral( "pixel_count" ), QMetaType::Type::LongLong ) );
134 sink.reset( parameterAsSink( parameters, QStringLiteral( "OUTPUT_TABLE" ), context, tableDest, outFields, Qgis::WkbType::NoGeometry, QgsCoordinateReferenceSystem() ) );
135 if ( !sink )
136 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT_TABLE" ) ) );
137 }
138
139 double volume = 0;
140 long long count = 0;
141
144 const int nbBlocksWidth = static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
145 const int nbBlocksHeight = static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
146 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
147
148 QgsRasterIterator iter( mInterface.get() );
149 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
150
151 int iterLeft = 0;
152 int iterTop = 0;
153 int iterCols = 0;
154 int iterRows = 0;
155 std::unique_ptr<QgsRasterBlock> rasterBlock;
156 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
157 {
158 feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
159 for ( int row = 0; row < iterRows; row++ )
160 {
161 if ( feedback->isCanceled() )
162 break;
163 for ( int column = 0; column < iterCols; column++ )
164 {
165 if ( mHasNoDataValue && rasterBlock->isNoData( row, column ) )
166 {
167 continue;
168 }
169
170 const double z = rasterBlock->value( row, column ) - mLevel;
171
172 switch ( mMethod )
173 {
174 case CountOnlyAboveBaseLevel:
175 if ( z > 0.0 )
176 {
177 volume += z;
178 count++;
179 }
180 continue;
181
182 case CountOnlyBelowBaseLevel:
183 if ( z < 0.0 )
184 {
185 volume += z;
186 count++;
187 }
188 continue;
189
190 case SubtractVolumesBelowBaseLevel:
191 volume += z;
192 count++;
193 continue;
194
195 case AddVolumesBelowBaseLevel:
196 volume += std::fabs( z );
197 count++;
198 continue;
199 }
200 }
201 }
202 }
203
204 QVariantMap outputs;
205 const double pixelArea = mRasterUnitsPerPixelX * mRasterUnitsPerPixelY;
206 const double area = count * pixelArea;
207 volume *= pixelArea;
208 if ( !outputFile.isEmpty() )
209 {
210 QFile file( outputFile );
211 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
212 {
213 const QString encodedAreaUnit = QgsStringUtils::ampersandEncode( areaUnit );
214
215 QTextStream out( &file );
216#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
217 out.setCodec( "UTF-8" );
218#endif
219 out << QStringLiteral( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
220 out << QStringLiteral( "<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( "Analyzed file" ), mSource, QObject::tr( "band" ) ).arg( mBand );
221 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Volume" ), QString::number( volume, 'g', 16 ) );
222 out << QObject::tr( "<p>%1: %2</p>\n" ).arg( QObject::tr( "Pixel count" ) ).arg( count );
223 out << QObject::tr( "<p>%1: %2 %3</p>\n" ).arg( QObject::tr( "Area" ), QString::number( area, 'g', 16 ), encodedAreaUnit );
224 out << QStringLiteral( "</body></html>" );
225 outputs.insert( QStringLiteral( "OUTPUT_HTML_FILE" ), outputFile );
226 }
227 }
228
229 if ( sink )
230 {
231 QgsFeature f;
232 f.setAttributes( QgsAttributes() << volume << area << count );
233 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
234 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT_TABLE" ) ) );
235 sink->finalize();
236 outputs.insert( QStringLiteral( "OUTPUT_TABLE" ), tableDest );
237 }
238 outputs.insert( QStringLiteral( "VOLUME" ), volume );
239 outputs.insert( QStringLiteral( "AREA" ), area );
240 outputs.insert( QStringLiteral( "PIXEL_COUNT" ), count );
241 return outputs;
242}
243
244
@ 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
@ Double
Double/float values.
Definition qgis.h:3804
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 raster band parameter for Processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
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 numeric parameter 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.
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.