26QString QgsFillSinksWangLiuAlgorithm::name()
const
28 return QStringLiteral(
"fillsinkswangliu" );
31QString QgsFillSinksWangLiuAlgorithm::displayName()
const
33 return QObject::tr(
"Fill sinks (Wang & Liu)" );
36QStringList QgsFillSinksWangLiuAlgorithm::tags()
const
38 return QObject::tr(
"fill,filter,slope,dsm,dtm,terrain,water,shed,basin,direction,flow" ).split(
',' );
41QString QgsFillSinksWangLiuAlgorithm::group()
const
43 return QObject::tr(
"Raster terrain analysis" );
46QString QgsFillSinksWangLiuAlgorithm::groupId()
const
48 return QStringLiteral(
"rasterterrainanalysis" );
51QString QgsFillSinksWangLiuAlgorithm::shortHelpString()
const
53 return QObject::tr(
"This algorithm uses a method proposed by Wang & Liu to identify and fill surface depressions in digital elevation models.\n\n"
55 "The method was enhanced to allow the creation of hydrologically sound elevation models, i.e. not only to fill the depression(s) "
56 "but also to preserve a downward slope along the flow path. If desired, this is accomplished by preserving a minimum slope "
57 "gradient (and thus elevation difference) between cells.\n\n"
59 "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"
61 "This algorithm is a port of the SAGA 'Fill Sinks (Wang & Liu)' tool." );
64QString QgsFillSinksWangLiuAlgorithm::shortDescription()
const
66 return QObject::tr(
"Identifies and fills surface depressions in digital elevation models using a method proposed by Wang & Liu." );
69void QgsFillSinksWangLiuAlgorithm::initAlgorithm(
const QVariantMap & )
73 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"BAND" ), QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT" ) ) );
76 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." ) );
77 addParameter( minSlopeParam.release() );
79 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
80 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
82 addParameter( createOptsParam.release() );
84 auto outputFilledDem = std::make_unique<QgsProcessingParameterRasterDestination>( QStringLiteral(
"OUTPUT_FILLED_DEM" ), QObject::tr(
"Output layer (filled DEM)" ), QVariant(),
true,
true );
85 outputFilledDem->setHelp( QObject::tr(
"Depression-free digital elevation model." ) );
86 addParameter( outputFilledDem.release() );
88 auto outputFlowDirections = std::make_unique<QgsProcessingParameterRasterDestination>( QStringLiteral(
"OUTPUT_FLOW_DIRECTIONS" ), QObject::tr(
"Output layer (flow directions)" ), QVariant(),
true,
false );
89 outputFlowDirections->setHelp( QObject::tr(
"Computed flow directions, 0=N, 1=NE, 2=E, ... 7=NW." ) );
90 addParameter( outputFlowDirections.release() );
92 auto outputWatershedBasins = std::make_unique<QgsProcessingParameterRasterDestination>( QStringLiteral(
"OUTPUT_WATERSHED_BASINS" ), QObject::tr(
"Output layer (watershed basins)" ), QVariant(),
true,
false );
93 outputWatershedBasins->setHelp( QObject::tr(
"Delineated watershed basin." ) );
94 addParameter( outputWatershedBasins.release() );
97QgsFillSinksWangLiuAlgorithm *QgsFillSinksWangLiuAlgorithm::createInstance()
const
99 return new QgsFillSinksWangLiuAlgorithm();
104 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
108 const int band = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
110 mBand = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
111 if ( mBand < 1 || mBand > layer->
bandCount() )
112 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 mLayerWidth = layer->
width();
117 mLayerHeight = layer->
height();
118 mExtent = layer->
extent();
122 mRasterDiagonal = std::sqrt( mRasterUnitsPerPixelX * mRasterUnitsPerPixelX + mRasterUnitsPerPixelY * mRasterUnitsPerPixelY );
125 mDirectionalLengths = { mRasterUnitsPerPixelY, mRasterDiagonal, mRasterUnitsPerPixelX, mRasterDiagonal, mRasterUnitsPerPixelY, mRasterDiagonal, mRasterUnitsPerPixelX, mRasterDiagonal };
129static constexpr std::array< int, 8 > COL_DIRECTION_OFFSETS { 0, 1, 1, 1, 0, -1, -1, -1 };
130static constexpr std::array< int, 8 > ROW_DIRECTION_OFFSETS { -1, -1, 0, 1, 1, 1, 0, -1 };
132bool QgsFillSinksWangLiuAlgorithm::isInGrid(
int row,
int col )
const
134 return col >= 0 && col < mLayerWidth && row >= 0 && row < mLayerHeight;
137QgsFillSinksWangLiuAlgorithm::Direction QgsFillSinksWangLiuAlgorithm::getDir(
int row,
int col,
double z,
const QgsRasterBlock *filled )
const
140 double maxGradient = 0;
141 bool isNoData =
false;
143 for (
Direction direction : { North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest } )
145 const int neighborCol = col + COL_DIRECTION_OFFSETS[direction];
146 const int neighborRow = row + ROW_DIRECTION_OFFSETS[direction];
148 if ( isInGrid( neighborRow, neighborCol ) )
150 const double neighborZ = filled->
valueAndNoData( neighborRow, neighborCol, isNoData );
151 if ( !isNoData && neighborZ < z )
153 const double gradient = ( z - neighborZ ) / mDirectionalLengths[direction];
154 if ( gradient >= maxGradient )
156 maxGradient = gradient;
157 steepestDirection = direction;
163 return steepestDirection;
166struct CFillSinks_WL_Node
176 bool operator()( CFillSinks_WL_Node n1, CFillSinks_WL_Node n2 )
const
178 return n1.spill > n2.spill;
182typedef std::vector< CFillSinks_WL_Node > nodeVector;
183typedef std::priority_queue< CFillSinks_WL_Node, nodeVector, CompareGreater > PriorityQ;
187 const QString createOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
189 const QString filledDemOutputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT_FILLED_DEM" ), context );
190 const QString flowDirectionsOutputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT_FLOW_DIRECTIONS" ), context );
191 const QString watershedBasinsOutputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT_WATERSHED_BASINS" ), context );
193 std::unique_ptr<QgsRasterFileWriter> filledDemWriter;
194 std::unique_ptr<QgsRasterDataProvider> filledDemDestProvider;
196 if ( !filledDemOutputFile.isEmpty() )
198 const QFileInfo fi( filledDemOutputFile );
201 filledDemWriter = std::make_unique<QgsRasterFileWriter>( filledDemOutputFile );
202 filledDemWriter->setOutputProviderKey( QStringLiteral(
"gdal" ) );
203 if ( !createOptions.isEmpty() )
205 filledDemWriter->setCreationOptions( createOptions.split(
'|' ) );
207 filledDemWriter->setOutputFormat( outputFormat );
209 filledDemDestProvider.reset( filledDemWriter->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
211 if ( !filledDemDestProvider )
212 throw QgsProcessingException( QObject::tr(
"Could not create raster output: %1" ).arg( filledDemOutputFile ) );
213 if ( !filledDemDestProvider->isValid() )
216 filledDemDestProvider->setNoDataValue( 1, mNoData );
217 filledDemDestProvider->setEditable(
true );
220 std::unique_ptr<QgsRasterFileWriter> flowDirectionsWriter;
221 std::unique_ptr<QgsRasterDataProvider> flowDirectionsDestProvider;
223 if ( !flowDirectionsOutputFile.isEmpty() )
225 const QFileInfo fi( flowDirectionsOutputFile );
228 flowDirectionsWriter = std::make_unique<QgsRasterFileWriter>( flowDirectionsOutputFile );
229 flowDirectionsWriter->setOutputProviderKey( QStringLiteral(
"gdal" ) );
230 flowDirectionsWriter->setOutputFormat( outputFormat );
232 flowDirectionsDestProvider.reset( flowDirectionsWriter->createOneBandRaster(
Qgis::DataType::Byte, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
234 if ( !flowDirectionsDestProvider )
235 throw QgsProcessingException( QObject::tr(
"Could not create raster output: %1" ).arg( flowDirectionsOutputFile ) );
236 if ( !flowDirectionsDestProvider->isValid() )
239 flowDirectionsDestProvider->setNoDataValue( 1, 255 );
240 flowDirectionsDestProvider->setEditable(
true );
243 std::unique_ptr<QgsRasterFileWriter> watershedBasinsWriter;
244 std::unique_ptr<QgsRasterDataProvider> watershedBasinsDestProvider;
246 if ( !watershedBasinsOutputFile.isEmpty() )
248 const QFileInfo fi( watershedBasinsOutputFile );
251 watershedBasinsWriter = std::make_unique<QgsRasterFileWriter>( watershedBasinsOutputFile );
252 watershedBasinsWriter->setOutputProviderKey( QStringLiteral(
"gdal" ) );
253 watershedBasinsWriter->setOutputFormat( outputFormat );
255 watershedBasinsDestProvider.reset( watershedBasinsWriter->createOneBandRaster(
Qgis::DataType::Int32, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
257 if ( !watershedBasinsDestProvider )
258 throw QgsProcessingException( QObject::tr(
"Could not create raster output: %1" ).arg( watershedBasinsOutputFile ) );
259 if ( !watershedBasinsDestProvider->isValid() )
262 watershedBasinsDestProvider->setNoDataValue( 1, -1 );
263 watershedBasinsDestProvider->setEditable(
true );
266 std::unique_ptr< QgsRasterBlock > sourceDemData( mInterface->block( mBand, mExtent, mLayerWidth, mLayerHeight ) );
267 if ( !sourceDemData )
272 auto filledDemData = std::make_unique<QgsRasterBlock>( mDataType, mLayerWidth, mLayerHeight );
273 filledDemData->setNoDataValue( mNoData );
274 filledDemData->setIsNoData();
276 auto watershedData = std::make_unique<QgsRasterBlock>(
Qgis::DataType::Int32, mLayerWidth, mLayerHeight );
277 watershedData->setNoDataValue( -1 );
278 watershedData->setIsNoData();
280 auto outputFlowData = std::make_unique<QgsRasterBlock>(
Qgis::DataType::Byte, mLayerWidth, mLayerHeight );
281 outputFlowData->setNoDataValue( 255 );
282 outputFlowData->setIsNoData();
284 auto seedData = std::make_unique<QgsRasterBlock>(
Qgis::DataType::Byte, mLayerWidth, mLayerHeight );
287 double minSlope = parameterAsDouble( parameters, QStringLiteral(
"MIN_SLOPE" ), context );
289 bool preserve =
false;
290 if ( minSlope > 0.0 )
292 minSlope = tan( minSlope * M_PI / 180.0 );
293 for (
int i = 0; i < 8; i++ )
294 mindiff[i] = minSlope * mDirectionalLengths[i];
299 CFillSinks_WL_Node tempNode;
304 bool isNoData =
false;
307 std::size_t processed = 0;
308 const std::size_t totalCells =
static_cast< std::size_t
>( mLayerWidth ) * mLayerHeight;
310 for (
int row = 0; row < mLayerHeight; row++ )
315 for (
int col = 0; col < mLayerWidth; col++ )
317 value = sourceDemData->valueAndNoData( row, col, isNoData );
320 for (
Direction direction : { North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest } )
322 int iCol = col + COL_DIRECTION_OFFSETS[direction];
323 int iRow = row + ROW_DIRECTION_OFFSETS[direction];
325 if ( !isInGrid( iRow, iCol ) || sourceDemData->isNoData( iRow, iCol ) )
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 ) );
336 theQueue.push( tempNode );
342 feedback->
setProgress(
static_cast< double >( processed ) /
static_cast< double >( totalCells ) * 100 );
350 feedback->
setProgressText( QObject::tr(
"Filling using least cost paths" ) );
352 while ( !theQueue.empty() )
354 PriorityQ::value_type tempNode = theQueue.top();
356 const int row = tempNode.row;
357 const int col = tempNode.col;
358 const double z = tempNode.spill;
361 const long long id =
static_cast< long long >( watershedData->value( row, col ) );
363 for (
Direction direction : { North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest } )
365 const int iCol = col + COL_DIRECTION_OFFSETS[direction];
366 const int iRow = row + ROW_DIRECTION_OFFSETS[direction];
368 const bool iInGrid = isInGrid( iRow, iCol );
369 double iz = iInGrid ? sourceDemData->valueAndNoData( iRow, iCol, isNoData ) : 0;
370 if ( iInGrid && !isNoData )
372 if ( filledDemData->isNoData( iRow, iCol ) )
376 iz = std::max( iz, z + mindiff[
static_cast< int >( direction )] );
381 outputFlowData->setValue( iRow, iCol, INVERSE_DIRECTION[
static_cast< int >( direction )] );
387 theQueue.push( tempNode );
389 filledDemData->setValue( iRow, iCol, iz );
390 watershedData->setValue( iRow, iCol,
id );
393 else if ( seedData->value( iRow, iCol ) == 1 )
395 watershedData->setValue( iRow, iCol,
id );
400 if ( outputFlowData->isNoData( row, col ) )
401 outputFlowData->setValue( row, col, getDir( row, col, z, filledDemData.get() ) );
403 feedback->
setProgress(
static_cast< double >( processed ) /
static_cast< double >( totalCells ) * 100 );
413 if ( filledDemDestProvider )
415 if ( !filledDemDestProvider->writeBlock( filledDemData.get(), 1, 0, 0 ) )
417 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( filledDemDestProvider->error().summary() ) );
419 filledDemDestProvider->setEditable(
false );
420 outputs.insert( QStringLiteral(
"OUTPUT_FILLED_DEM" ), filledDemOutputFile );
422 if ( flowDirectionsDestProvider )
424 if ( !flowDirectionsDestProvider->writeBlock( outputFlowData.get(), 1, 0, 0 ) )
426 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( flowDirectionsDestProvider->error().summary() ) );
428 flowDirectionsDestProvider->setEditable(
false );
429 outputs.insert( QStringLiteral(
"OUTPUT_FLOW_DIRECTIONS" ), flowDirectionsOutputFile );
431 if ( watershedBasinsDestProvider )
433 if ( !watershedBasinsDestProvider->writeBlock( watershedData.get(), 1, 0, 0 ) )
435 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( watershedBasinsDestProvider->error().summary() ) );
437 watershedBasinsDestProvider->setEditable(
false );
438 outputs.insert( QStringLiteral(
"OUTPUT_WATERSHED_BASINS" ), watershedBasinsOutputFile );
@ Byte
Eight bit unsigned integer (quint8).
@ Int32
Thirty two bit signed integer (qint32).
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
virtual QgsRectangle extent() const
Returns the extent of 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.
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.
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.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
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.