26QString QgsRasterSurfaceVolumeAlgorithm::name()
 const 
   28  return QStringLiteral( 
"rastersurfacevolume" );
 
   31QString QgsRasterSurfaceVolumeAlgorithm::displayName()
 const 
   33  return QObject::tr( 
"Raster surface volume" );
 
   36QStringList QgsRasterSurfaceVolumeAlgorithm::tags()
 const 
   38  return QObject::tr( 
"sum,volume,area,height,terrain,dem,elevation" ).split( 
',' );
 
   41QString QgsRasterSurfaceVolumeAlgorithm::group()
 const 
   43  return QObject::tr( 
"Raster analysis" );
 
   46QString QgsRasterSurfaceVolumeAlgorithm::groupId()
 const 
   48  return QStringLiteral( 
"rasteranalysis" );
 
   51void QgsRasterSurfaceVolumeAlgorithm::initAlgorithm( 
const QVariantMap & )
 
   54  addParameter( 
new QgsProcessingParameterBand( QStringLiteral( 
"BAND" ), QObject::tr( 
"Band number" ), 1, QStringLiteral( 
"INPUT" ) ) );
 
   56  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  addParameter( 
new QgsProcessingParameterFileDestination( QStringLiteral( 
"OUTPUT_HTML_FILE" ), QObject::tr( 
"Surface volume report" ), QObject::tr( 
"HTML files (*.html)" ), QVariant(), 
true ) );
 
   66QString QgsRasterSurfaceVolumeAlgorithm::shortHelpString()
 const 
   68  return QObject::tr( 
"This algorithm calculates the volume under a raster grid's surface.\n\n" 
   69                      "Several methods of volume calculation are available, which control whether " 
   70                      "only values above or below the specified base level are considered, or " 
   71                      "whether volumes below the base level should be added or subtracted from the total volume.\n\n" 
   72                      "The algorithm outputs the calculated volume, the total area, and the total number of pixels analysed. " 
   73                      "If the 'Count Only Above Base Level' or 'Count Only Below Base Level' methods are used, " 
   74                      "then the calculated area and pixel count only includes pixels which are above or below the " 
   75                      "specified base level respectively.\n\n" 
   76                      "Units of the calculated volume are dependent on the coordinate reference system of " 
   77                      "the input raster file. For a CRS in meters, with a DEM height in meters, the calculated " 
   78                      "value will be in meters³. If instead the input raster is in a geographic coordinate system " 
   79                      "(e.g. latitude/longitude values), then the result will be in degrees² × meters, and an " 
   80                      "appropriate scaling factor will need to be applied in order to convert to meters³." );
 
   83QString QgsRasterSurfaceVolumeAlgorithm::shortDescription()
 const 
   85  return QObject::tr( 
"Calculates the volume under a raster grid's surface." );
 
   88QgsRasterSurfaceVolumeAlgorithm *QgsRasterSurfaceVolumeAlgorithm::createInstance()
 const 
   90  return new QgsRasterSurfaceVolumeAlgorithm();
 
   95  QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( 
"INPUT" ), context );
 
   96  const int band = parameterAsInt( parameters, QStringLiteral( 
"BAND" ), context );
 
  101  mBand = parameterAsInt( parameters, QStringLiteral( 
"BAND" ), context );
 
  102  if ( mBand < 1 || mBand > layer->
bandCount() )
 
  103    throw QgsProcessingException( QObject::tr( 
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->
bandCount() ) );
 
  107  mLayerWidth = layer->
width();
 
  108  mLayerHeight = layer->
height();
 
  109  mExtent = layer->
extent();
 
  113  mSource = layer->
source();
 
  115  mLevel = parameterAsDouble( parameters, QStringLiteral( 
"LEVEL" ), context );
 
  116  mMethod = 
static_cast<Method
>( parameterAsEnum( parameters, QStringLiteral( 
"METHOD" ), context ) );
 
  122  const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( 
"OUTPUT_HTML_FILE" ), context );
 
  126  std::unique_ptr<QgsFeatureSink> sink;
 
  127  if ( parameters.contains( QStringLiteral( 
"OUTPUT_TABLE" ) ) && parameters.value( QStringLiteral( 
"OUTPUT_TABLE" ) ).isValid() )
 
  130    outFields.
append( 
QgsField( QStringLiteral( 
"volume" ), QMetaType::Type::Double, QString(), 20, 8 ) );
 
  131    outFields.
append( 
QgsField( areaUnit.replace( QStringLiteral( 
"²" ), QStringLiteral( 
"2" ) ), QMetaType::Type::Double, QString(), 20, 8 ) );
 
  132    outFields.
append( 
QgsField( QStringLiteral( 
"pixel_count" ), QMetaType::Type::LongLong ) );
 
  143  const int nbBlocksWidth = 
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
 
  144  const int nbBlocksHeight = 
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
 
  145  const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
 
  148  iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
 
  154  std::unique_ptr<QgsRasterBlock> rasterBlock;
 
  155  while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
 
  157    feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
 
  158    for ( 
int row = 0; row < iterRows; row++ )
 
  162      for ( 
int column = 0; column < iterCols; column++ )
 
  164        if ( mHasNoDataValue && rasterBlock->isNoData( row, column ) )
 
  169        const double z = rasterBlock->value( row, column ) - mLevel;
 
  173          case CountOnlyAboveBaseLevel:
 
  181          case CountOnlyBelowBaseLevel:
 
  189          case SubtractVolumesBelowBaseLevel:
 
  194          case AddVolumesBelowBaseLevel:
 
  195            volume += std::fabs( z );
 
  204  const double pixelArea = mRasterUnitsPerPixelX * mRasterUnitsPerPixelY;
 
  205  const double area = count * pixelArea;
 
  207  if ( !outputFile.isEmpty() )
 
  209    QFile file( outputFile );
 
  210    if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
 
  214      QTextStream out( &file );
 
  215#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) 
  216      out.setCodec( 
"UTF-8" );
 
  218      out << QStringLiteral( 
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
 
  219      out << QStringLiteral( 
"<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr( 
"Analyzed file" ), mSource, QObject::tr( 
"band" ) ).arg( mBand );
 
  220      out << QObject::tr( 
"<p>%1: %2</p>\n" ).arg( QObject::tr( 
"Volume" ), QString::number( volume, 
'g', 16 ) );
 
  221      out << QObject::tr( 
"<p>%1: %2</p>\n" ).arg( QObject::tr( 
"Pixel count" ) ).arg( count );
 
  222      out << QObject::tr( 
"<p>%1: %2 %3</p>\n" ).arg( QObject::tr( 
"Area" ), QString::number( area, 
'g', 16 ), encodedAreaUnit );
 
  223      out << QStringLiteral( 
"</body></html>" );
 
  224      outputs.insert( QStringLiteral( 
"OUTPUT_HTML_FILE" ), outputFile );
 
  233      throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( 
"OUTPUT_TABLE" ) ) );
 
  235    outputs.insert( QStringLiteral( 
"OUTPUT_TABLE" ), tableDest );
 
  237  outputs.insert( QStringLiteral( 
"VOLUME" ), volume );
 
  238  outputs.insert( QStringLiteral( 
"AREA" ), area );
 
  239  outputs.insert( QStringLiteral( 
"PIXEL_COUNT" ), count );
 
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
 
@ Double
Double/float values.
 
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...
 
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
 
bool isCanceled() const
Tells whether the operation has been canceled already.
 
void setProgress(double progress)
Sets the current progress for the feedback object.
 
Encapsulate a field in an attribute table or data source.
 
Container of fields for a vector layer.
 
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
 
virtual QgsRectangle extent() const
Returns the extent of the layer.
 
QString source() const
Returns the source for the layer.
 
QgsCoordinateReferenceSystem crs
 
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.