20 #include <QTextStream>
24 QString QgsRasterSurfaceVolumeAlgorithm::name()
const
26 return QStringLiteral(
"rastersurfacevolume" );
29 QString QgsRasterSurfaceVolumeAlgorithm::displayName()
const
31 return QObject::tr(
"Raster surface volume" );
34 QStringList QgsRasterSurfaceVolumeAlgorithm::tags()
const
36 return QObject::tr(
"sum,volume,area,height,terrain,dem,elevation" ).split(
',' );
39 QString QgsRasterSurfaceVolumeAlgorithm::group()
const
41 return QObject::tr(
"Raster analysis" );
44 QString QgsRasterSurfaceVolumeAlgorithm::groupId()
const
46 return QStringLiteral(
"rasteranalysis" );
49 void QgsRasterSurfaceVolumeAlgorithm::initAlgorithm(
const QVariantMap & )
52 QObject::tr(
"Input layer" ) ) );
54 QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT" ) ) );
58 QObject::tr(
"Method" ), QStringList()
59 << QObject::tr(
"Count Only Above Base Level" )
60 << QObject::tr(
"Count Only Below Base Level" )
61 << QObject::tr(
"Subtract Volumes Below Base Level" )
62 << QObject::tr(
"Add Volumes Below Base Level" ) ) );
65 QObject::tr(
"Surface volume report" ), QObject::tr(
"HTML files (*.html)" ), QVariant(),
true ) );
74 QString QgsRasterSurfaceVolumeAlgorithm::shortHelpString()
const
76 return QObject::tr(
"This algorithm calculates the volume under a raster grid's surface.\n\n"
77 "Several methods of volume calculation are available, which control whether "
78 "only values above or below the specified base level are considered, or "
79 "whether volumes below the base level should be added or subtracted from the total volume.\n\n"
80 "The algorithm outputs the calculated volume, the total area, and the total number of pixels analysed. "
81 "If the 'Count Only Above Base Level' or 'Count Only Below Base Level' methods are used, "
82 "then the calculated area and pixel count only includes pixels which are above or below the "
83 "specified base level respectively.\n\n"
84 "Units of the calculated volume are dependent on the coordinate reference system of "
85 "the input raster file. For a CRS in meters, with a DEM height in meters, the calculated "
86 "value will be in meters³. If instead the input raster is in a geographic coordinate system "
87 "(e.g. latitude/longitude values), then the result will be in degrees² × meters, and an "
88 "appropriate scaling factor will need to be applied in order to convert to meters³." );
91 QString QgsRasterSurfaceVolumeAlgorithm::shortDescription()
const
93 return QObject::tr(
"Calculates the volume under a raster grid's surface." );
96 QgsRasterSurfaceVolumeAlgorithm *QgsRasterSurfaceVolumeAlgorithm::createInstance()
const
98 return new QgsRasterSurfaceVolumeAlgorithm();
103 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
104 const int band = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
109 mBand = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
110 if ( mBand < 1 || mBand > layer->
bandCount() )
111 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand )
116 mLayerWidth = layer->
width();
117 mLayerHeight = layer->
height();
118 mExtent = layer->
extent();
122 mSource = layer->
source();
124 mLevel = parameterAsDouble( parameters, QStringLiteral(
"LEVEL" ), context );
125 mMethod =
static_cast< Method
>( parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context ) );
131 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT_HTML_FILE" ), context );
135 std::unique_ptr< QgsFeatureSink > sink;
136 if ( parameters.contains( QStringLiteral(
"OUTPUT_TABLE" ) ) && parameters.value( QStringLiteral(
"OUTPUT_TABLE" ) ).isValid() )
139 outFields.
append(
QgsField( QStringLiteral(
"volume" ), QVariant::Double, QString(), 20, 8 ) );
140 outFields.
append(
QgsField( areaUnit.replace( QStringLiteral(
"²" ), QStringLiteral(
"2" ) ), QVariant::Double, QString(), 20, 8 ) );
141 outFields.
append(
QgsField( QStringLiteral(
"pixel_count" ), QVariant::LongLong ) );
152 const int nbBlocksWidth =
static_cast< int >( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
153 const int nbBlocksHeight =
static_cast< int >( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
154 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
157 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
163 std::unique_ptr< QgsRasterBlock > rasterBlock;
164 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
166 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
167 for (
int row = 0; row < iterRows; row++ )
171 for (
int column = 0; column < iterCols; column++ )
173 if ( mHasNoDataValue && rasterBlock->isNoData( row, column ) )
178 const double z = rasterBlock->value( row, column ) - mLevel;
182 case CountOnlyAboveBaseLevel:
190 case CountOnlyBelowBaseLevel:
198 case SubtractVolumesBelowBaseLevel:
203 case AddVolumesBelowBaseLevel:
204 volume += std::fabs( z );
213 const double pixelArea = mRasterUnitsPerPixelX * mRasterUnitsPerPixelY;
214 const double area = count * pixelArea;
216 if ( !outputFile.isEmpty() )
218 QFile file( outputFile );
219 if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
223 QTextStream out( &file );
224 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
225 out.setCodec(
"UTF-8" );
227 out << QStringLiteral(
"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" );
228 out << QStringLiteral(
"<p>%1: %2 (%3 %4)</p>\n" ).arg( QObject::tr(
"Analyzed file" ), mSource, QObject::tr(
"band" ) ).arg( mBand );
229 out << QObject::tr(
"<p>%1: %2</p>\n" ).arg( QObject::tr(
"Volume" ), QString::number( volume,
'g', 16 ) );
230 out << QObject::tr(
"<p>%1: %2</p>\n" ).arg( QObject::tr(
"Pixel count" ) ).arg( count );
231 out << QObject::tr(
"<p>%1: %2 %3</p>\n" ).arg( QObject::tr(
"Area" ), QString::number( area,
'g', 16 ), encodedAreaUnit );
232 out << QStringLiteral(
"</body></html>" );
233 outputs.insert( QStringLiteral(
"OUTPUT_HTML_FILE" ), outputFile );
242 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral(
"OUTPUT_TABLE" ) ) );
243 outputs.insert( QStringLiteral(
"OUTPUT_TABLE" ), tableDest );
245 outputs.insert( QStringLiteral(
"VOLUME" ), volume );
246 outputs.insert( QStringLiteral(
"AREA" ), area );
247 outputs.insert( QStringLiteral(
"PIXEL_COUNT" ), count );