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