QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmfillsinkswangliu.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmfillsinkswangliu.cpp
3 ---------------------
4 begin : April 2025
5 copyright : (C) 2025 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 "qgsrasterfilewriter.h"
21
22#include <QString>
23#include <queue>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsFillSinksWangLiuAlgorithm::name() const
30{
31 return u"fillsinkswangliu"_s;
32}
33
34QString QgsFillSinksWangLiuAlgorithm::displayName() const
35{
36 return QObject::tr( "Fill sinks (Wang & Liu)" );
37}
38
39QStringList QgsFillSinksWangLiuAlgorithm::tags() const
40{
41 return QObject::tr( "fill,filter,slope,dsm,dtm,terrain,water,shed,basin,direction,flow" ).split( ',' );
42}
43
44QString QgsFillSinksWangLiuAlgorithm::group() const
45{
46 return QObject::tr( "Raster terrain analysis" );
47}
48
49QString QgsFillSinksWangLiuAlgorithm::groupId() const
50{
51 return u"rasterterrainanalysis"_s;
52}
53
54QString QgsFillSinksWangLiuAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm uses a method proposed by Wang & Liu to identify and fill surface depressions in digital elevation models.\n\n"
57
58 "The method was enhanced to allow the creation of hydrologically sound elevation models, i.e. not only to fill the depression(s) "
59 "but also to preserve a downward slope along the flow path. If desired, this is accomplished by preserving a minimum slope "
60 "gradient (and thus elevation difference) between cells.\n\n"
61
62 "References: Wang, L. & H. Liu (2006): An efficient method for identifying and filling surface depressions in digital elevation models for hydrologic analysis and modelling. International Journal of Geographical Information Science, Vol. 20, No. 2: 193-213.\n\n"
63
64 "This algorithm is a port of the SAGA 'Fill Sinks (Wang & Liu)' tool." );
65}
66
67QString QgsFillSinksWangLiuAlgorithm::shortDescription() const
68{
69 return QObject::tr( "Identifies and fills surface depressions in digital elevation models using a method proposed by Wang & Liu." );
70}
71
72void QgsFillSinksWangLiuAlgorithm::initAlgorithm( const QVariantMap & )
73{
74 addParameter( new QgsProcessingParameterRasterLayer( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
75
76 addParameter( new QgsProcessingParameterBand( u"BAND"_s, QObject::tr( "Band number" ), 1, u"INPUT"_s ) );
77
78 auto minSlopeParam = std::make_unique<QgsProcessingParameterNumber>( u"MIN_SLOPE"_s, QObject::tr( "Minimum slope (degrees)" ), Qgis::ProcessingNumberParameterType::Double, 0.1, false, 0 );
79 minSlopeParam->setHelp( QObject::tr( "Minimum slope gradient to preserve from cell to cell. With a value of zero sinks are filled up to the spill elevation (which results in flat areas). Units are degrees." ) );
80 addParameter( minSlopeParam.release() );
81
82 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATION_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
83 createOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
84 createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
85 addParameter( createOptsParam.release() );
86
87 auto outputFilledDem = std::make_unique<QgsProcessingParameterRasterDestination>( u"OUTPUT_FILLED_DEM"_s, QObject::tr( "Output layer (filled DEM)" ), QVariant(), true, true );
88 outputFilledDem->setHelp( QObject::tr( "Depression-free digital elevation model." ) );
89 addParameter( outputFilledDem.release() );
90
91 auto outputFlowDirections = std::make_unique<QgsProcessingParameterRasterDestination>( u"OUTPUT_FLOW_DIRECTIONS"_s, QObject::tr( "Output layer (flow directions)" ), QVariant(), true, false );
92 outputFlowDirections->setHelp( QObject::tr( "Computed flow directions, 0=N, 1=NE, 2=E, ... 7=NW." ) );
93 addParameter( outputFlowDirections.release() );
94
95 auto outputWatershedBasins = std::make_unique<QgsProcessingParameterRasterDestination>( u"OUTPUT_WATERSHED_BASINS"_s, QObject::tr( "Output layer (watershed basins)" ), QVariant(), true, false );
96 outputWatershedBasins->setHelp( QObject::tr( "Delineated watershed basin." ) );
97 addParameter( outputWatershedBasins.release() );
98}
99
100QgsFillSinksWangLiuAlgorithm *QgsFillSinksWangLiuAlgorithm::createInstance() const
101{
102 return new QgsFillSinksWangLiuAlgorithm();
103}
104
105bool QgsFillSinksWangLiuAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
106{
107 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u"INPUT"_s, context );
108 if ( !layer )
109 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT"_s ) );
110
111 const int band = parameterAsInt( parameters, u"BAND"_s, context );
112
113 mBand = parameterAsInt( parameters, u"BAND"_s, context );
114 if ( mBand < 1 || mBand > layer->bandCount() )
115 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->bandCount() ) );
116
117 mInterface.reset( layer->dataProvider()->clone() );
118 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
119 mLayerWidth = layer->width();
120 mLayerHeight = layer->height();
121 mExtent = layer->extent();
122 mCrs = layer->crs();
123 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
124 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
125 mRasterDiagonal = std::sqrt( mRasterUnitsPerPixelX * mRasterUnitsPerPixelX + mRasterUnitsPerPixelY * mRasterUnitsPerPixelY );
126 mDataType = layer->dataProvider()->dataType( mBand );
127 mNoData = layer->dataProvider()->sourceNoDataValue( mBand );
128 mDirectionalLengths = { mRasterUnitsPerPixelY, mRasterDiagonal, mRasterUnitsPerPixelX, mRasterDiagonal, mRasterUnitsPerPixelY, mRasterDiagonal, mRasterUnitsPerPixelX, mRasterDiagonal };
129 return true;
130}
131
132static constexpr std::array< int, 8 > COL_DIRECTION_OFFSETS { 0, 1, 1, 1, 0, -1, -1, -1 };
133static constexpr std::array< int, 8 > ROW_DIRECTION_OFFSETS { -1, -1, 0, 1, 1, 1, 0, -1 };
134
135bool QgsFillSinksWangLiuAlgorithm::isInGrid( int row, int col ) const
136{
137 return col >= 0 && col < mLayerWidth && row >= 0 && row < mLayerHeight;
138}
139
140QgsFillSinksWangLiuAlgorithm::Direction QgsFillSinksWangLiuAlgorithm::getDir( int row, int col, double z, const QgsRasterBlock *filled ) const
141{
142 Direction steepestDirection = Invalid;
143 double maxGradient = 0;
144 bool isNoData = false;
145
146 for ( Direction direction : { North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest } )
147 {
148 const int neighborCol = col + COL_DIRECTION_OFFSETS[direction];
149 const int neighborRow = row + ROW_DIRECTION_OFFSETS[direction];
150
151 if ( isInGrid( neighborRow, neighborCol ) )
152 {
153 const double neighborZ = filled->valueAndNoData( neighborRow, neighborCol, isNoData );
154 if ( !isNoData && neighborZ < z )
155 {
156 const double gradient = ( z - neighborZ ) / mDirectionalLengths[direction];
157 if ( gradient >= maxGradient )
158 {
159 maxGradient = gradient;
160 steepestDirection = direction;
161 }
162 }
163 }
164 }
165
166 return steepestDirection;
167}
168
169struct CFillSinks_WL_Node
170{
171 int row = 0;
172 int col = 0;
173 double spill = 0;
174};
175
176class CompareGreater
177{
178 public:
179 bool operator()( CFillSinks_WL_Node n1, CFillSinks_WL_Node n2 ) const
180 {
181 return n1.spill > n2.spill;
182 }
183};
184
185typedef std::vector< CFillSinks_WL_Node > nodeVector;
186typedef std::priority_queue< CFillSinks_WL_Node, nodeVector, CompareGreater > PriorityQ;
187
188QVariantMap QgsFillSinksWangLiuAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
189{
190 const QString createOptions = parameterAsString( parameters, u"CREATION_OPTIONS"_s, context ).trimmed();
191
192 const QString filledDemOutputFile = parameterAsOutputLayer( parameters, u"OUTPUT_FILLED_DEM"_s, context );
193 const QString flowDirectionsOutputFile = parameterAsOutputLayer( parameters, u"OUTPUT_FLOW_DIRECTIONS"_s, context );
194 const QString watershedBasinsOutputFile = parameterAsOutputLayer( parameters, u"OUTPUT_WATERSHED_BASINS"_s, context );
195
196 std::unique_ptr<QgsRasterFileWriter> filledDemWriter;
197 std::unique_ptr<QgsRasterDataProvider> filledDemDestProvider;
198
199 if ( !filledDemOutputFile.isEmpty() )
200 {
201 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT_FILLED_DEM"_s, context );
202
203 filledDemWriter = std::make_unique<QgsRasterFileWriter>( filledDemOutputFile );
204 filledDemWriter->setOutputProviderKey( u"gdal"_s );
205 if ( !createOptions.isEmpty() )
206 {
207 filledDemWriter->setCreationOptions( createOptions.split( '|' ) );
208 }
209 filledDemWriter->setOutputFormat( outputFormat );
210
211 filledDemDestProvider.reset( filledDemWriter->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
212
213 if ( !filledDemDestProvider )
214 throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( filledDemOutputFile ) );
215 if ( !filledDemDestProvider->isValid() )
216 throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( filledDemOutputFile, filledDemDestProvider->error().message( QgsErrorMessage::Text ) ) );
217
218 filledDemDestProvider->setNoDataValue( 1, mNoData );
219 filledDemDestProvider->setEditable( true );
220 }
221
222 std::unique_ptr<QgsRasterFileWriter> flowDirectionsWriter;
223 std::unique_ptr<QgsRasterDataProvider> flowDirectionsDestProvider;
224
225 if ( !flowDirectionsOutputFile.isEmpty() )
226 {
227 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT_FLOW_DIRECTIONS"_s, context );
228
229 flowDirectionsWriter = std::make_unique<QgsRasterFileWriter>( flowDirectionsOutputFile );
230 flowDirectionsWriter->setOutputProviderKey( u"gdal"_s );
231 flowDirectionsWriter->setOutputFormat( outputFormat );
232
233 flowDirectionsDestProvider.reset( flowDirectionsWriter->createOneBandRaster( Qgis::DataType::Byte, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
234
235 if ( !flowDirectionsDestProvider )
236 throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( flowDirectionsOutputFile ) );
237 if ( !flowDirectionsDestProvider->isValid() )
238 throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( flowDirectionsOutputFile, flowDirectionsDestProvider->error().message( QgsErrorMessage::Text ) ) );
239
240 flowDirectionsDestProvider->setNoDataValue( 1, 255 );
241 flowDirectionsDestProvider->setEditable( true );
242 }
243
244 std::unique_ptr<QgsRasterFileWriter> watershedBasinsWriter;
245 std::unique_ptr<QgsRasterDataProvider> watershedBasinsDestProvider;
246
247 if ( !watershedBasinsOutputFile.isEmpty() )
248 {
249 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT_WATERSHED_BASINS"_s, context );
250
251 watershedBasinsWriter = std::make_unique<QgsRasterFileWriter>( watershedBasinsOutputFile );
252 watershedBasinsWriter->setOutputProviderKey( u"gdal"_s );
253 watershedBasinsWriter->setOutputFormat( outputFormat );
254
255 watershedBasinsDestProvider.reset( watershedBasinsWriter->createOneBandRaster( Qgis::DataType::Int32, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
256
257 if ( !watershedBasinsDestProvider )
258 throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( watershedBasinsOutputFile ) );
259 if ( !watershedBasinsDestProvider->isValid() )
260 throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( watershedBasinsOutputFile, watershedBasinsDestProvider->error().message( QgsErrorMessage::Text ) ) );
261
262 watershedBasinsDestProvider->setNoDataValue( 1, -1 );
263 watershedBasinsDestProvider->setEditable( true );
264 }
265
266 std::unique_ptr< QgsRasterBlock > sourceDemData( mInterface->block( mBand, mExtent, mLayerWidth, mLayerHeight ) );
267 if ( !sourceDemData )
268 {
269 throw QgsProcessingException( QObject::tr( "Could not read DEM raster" ) );
270 }
271
272 auto filledDemData = std::make_unique<QgsRasterBlock>( mDataType, mLayerWidth, mLayerHeight );
273 filledDemData->setNoDataValue( mNoData );
274 filledDemData->setIsNoData();
275
276 auto watershedData = std::make_unique<QgsRasterBlock>( Qgis::DataType::Int32, mLayerWidth, mLayerHeight );
277 watershedData->setNoDataValue( -1 );
278 watershedData->setIsNoData();
279
280 auto outputFlowData = std::make_unique<QgsRasterBlock>( Qgis::DataType::Byte, mLayerWidth, mLayerHeight );
281 outputFlowData->setNoDataValue( 255 );
282 outputFlowData->setIsNoData();
283
284 auto seedData = std::make_unique<QgsRasterBlock>( Qgis::DataType::Byte, mLayerWidth, mLayerHeight );
285 seedData->fill( 0 );
286
287 double minSlope = parameterAsDouble( parameters, u"MIN_SLOPE"_s, context );
288 double mindiff[8];
289 bool preserve = false;
290 if ( minSlope > 0.0 )
291 {
292 minSlope = tan( minSlope * M_PI / 180.0 );
293 for ( int i = 0; i < 8; i++ )
294 mindiff[i] = minSlope * mDirectionalLengths[i];
295 preserve = true;
296 }
297
298 // fill priority queue with boundary, i.e. seed cells
299 CFillSinks_WL_Node tempNode;
300 PriorityQ theQueue;
301
302 long long id = 0;
303 double value = 0;
304 bool isNoData = false;
305 feedback->setProgressText( QObject::tr( "Seed boundary cells" ) );
306
307 std::size_t processed = 0;
308 const std::size_t totalCells = static_cast< std::size_t >( mLayerWidth ) * mLayerHeight;
309
310 for ( int row = 0; row < mLayerHeight; row++ )
311 {
312 if ( feedback->isCanceled() )
313 break;
314
315 for ( int col = 0; col < mLayerWidth; col++ )
316 {
317 value = sourceDemData->valueAndNoData( row, col, isNoData );
318 if ( !isNoData )
319 {
320 for ( Direction direction : { North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest } )
321 {
322 int iCol = col + COL_DIRECTION_OFFSETS[direction];
323 int iRow = row + ROW_DIRECTION_OFFSETS[direction];
324 ;
325 if ( !isInGrid( iRow, iCol ) || sourceDemData->isNoData( iRow, iCol ) )
326 {
327 const double z = value;
328 filledDemData->setValue( row, col, z );
329 seedData->setValue( row, col, 1.0 );
330 watershedData->setValue( row, col, static_cast< double >( id ) );
331 id += 1;
332
333 tempNode.row = row;
334 tempNode.col = col;
335 tempNode.spill = z;
336 theQueue.push( tempNode );
337 processed += 1;
338 break;
339 }
340 }
341 }
342 feedback->setProgress( static_cast< double >( processed ) / static_cast< double >( totalCells ) * 100 );
343 }
344 }
345
346 if ( feedback->isCanceled() )
347 return {};
348
349 // work through least cost path
350 feedback->setProgressText( QObject::tr( "Filling using least cost paths" ) );
351
352 while ( !theQueue.empty() )
353 {
354 PriorityQ::value_type tempNode = theQueue.top();
355
356 const int row = tempNode.row;
357 const int col = tempNode.col;
358 const double z = tempNode.spill;
359 theQueue.pop();
360
361 const long long id = static_cast< long long >( watershedData->value( row, col ) );
362
363 for ( Direction direction : { North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest } )
364 {
365 const int iCol = col + COL_DIRECTION_OFFSETS[direction];
366 const int iRow = row + ROW_DIRECTION_OFFSETS[direction];
367 isNoData = false;
368 const bool iInGrid = isInGrid( iRow, iCol );
369 double iz = iInGrid ? sourceDemData->valueAndNoData( iRow, iCol, isNoData ) : 0;
370 if ( iInGrid && !isNoData )
371 {
372 if ( filledDemData->isNoData( iRow, iCol ) )
373 {
374 if ( preserve )
375 {
376 iz = std::max( iz, z + mindiff[static_cast< int >( direction )] );
377 }
378 else if ( iz <= z )
379 {
380 iz = z;
381 outputFlowData->setValue( iRow, iCol, INVERSE_DIRECTION[static_cast< int >( direction )] );
382 }
383
384 tempNode.row = iRow;
385 tempNode.col = iCol;
386 tempNode.spill = iz;
387 theQueue.push( tempNode );
388
389 filledDemData->setValue( iRow, iCol, iz );
390 watershedData->setValue( iRow, iCol, id );
391 processed += 1;
392 }
393 else if ( seedData->value( iRow, iCol ) == 1 )
394 {
395 watershedData->setValue( iRow, iCol, id );
396 }
397 }
398 }
399
400 if ( outputFlowData->isNoData( row, col ) )
401 outputFlowData->setValue( row, col, getDir( row, col, z, filledDemData.get() ) );
402
403 feedback->setProgress( static_cast< double >( processed ) / static_cast< double >( totalCells ) * 100 );
404 if ( feedback->isCanceled() )
405 break;
406 }
407
408 if ( feedback->isCanceled() )
409 return {};
410
411 QVariantMap outputs;
412
413 if ( filledDemDestProvider )
414 {
415 if ( !filledDemDestProvider->writeBlock( filledDemData.get(), 1, 0, 0 ) )
416 {
417 throw QgsProcessingException( QObject::tr( "Could not write raster block: %1" ).arg( filledDemDestProvider->error().summary() ) );
418 }
419 filledDemDestProvider->setEditable( false );
420 outputs.insert( u"OUTPUT_FILLED_DEM"_s, filledDemOutputFile );
421 }
422 if ( flowDirectionsDestProvider )
423 {
424 if ( !flowDirectionsDestProvider->writeBlock( outputFlowData.get(), 1, 0, 0 ) )
425 {
426 throw QgsProcessingException( QObject::tr( "Could not write raster block: %1" ).arg( flowDirectionsDestProvider->error().summary() ) );
427 }
428 flowDirectionsDestProvider->setEditable( false );
429 outputs.insert( u"OUTPUT_FLOW_DIRECTIONS"_s, flowDirectionsOutputFile );
430 }
431 if ( watershedBasinsDestProvider )
432 {
433 if ( !watershedBasinsDestProvider->writeBlock( watershedData.get(), 1, 0, 0 ) )
434 {
435 throw QgsProcessingException( QObject::tr( "Could not write raster block: %1" ).arg( watershedBasinsDestProvider->error().summary() ) );
436 }
437 watershedBasinsDestProvider->setEditable( false );
438 outputs.insert( u"OUTPUT_WATERSHED_BASINS"_s, watershedBasinsOutputFile );
439 }
440
441 return outputs;
442}
443
444
@ Byte
Eight bit unsigned integer (quint8).
Definition qgis.h:381
@ Int32
Thirty two bit signed integer (qint32).
Definition qgis.h:386
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3834
@ Double
Double/float values.
Definition qgis.h:3875
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
virtual QgsRectangle extent() const
Returns the extent of 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.
virtual void setProgressText(const QString &text)
Sets a progress report text string.
A raster band parameter for Processing algorithms.
A raster layer parameter for processing algorithms.
Raster data container.
double valueAndNoData(int row, int column, bool &isNoData) const
Reads a single value from the pixel at row and column, if type of block is numeric.
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.
virtual double sourceNoDataValue(int bandNo) const
Value representing no data value.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
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.