QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmruggedness.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmruggedness.cpp
3 ---------------------
4 begin : November 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy 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#include "qgsrasterfilewriter.h"
20#include "qgsruggednessfilter.h"
21
23
24QString QgsRuggednessAlgorithm::name() const
25{
26 return QStringLiteral( "ruggednessindex" );
27}
28
29QString QgsRuggednessAlgorithm::displayName() const
30{
31 return QObject::tr( "Ruggedness index" );
32}
33
34QStringList QgsRuggednessAlgorithm::tags() const
35{
36 return QObject::tr( "dem,ruggedness,index,terrain" ).split( ',' );
37}
38
39QString QgsRuggednessAlgorithm::group() const
40{
41 return QObject::tr( "Raster terrain analysis" );
42}
43
44QString QgsRuggednessAlgorithm::groupId() const
45{
46 return QStringLiteral( "rasterterrainanalysis" );
47}
48
49QString QgsRuggednessAlgorithm::shortHelpString() const
50{
51 return QObject::tr( "This algorithm calculates the quantitative measurement of terrain "
52 "heterogeneity described by Riley et al. (1999)." )
53 + QStringLiteral( "\n\n" )
54 + QObject::tr( "It is calculated for every location, by summarizing the change "
55 "in elevation within the 3x3 pixel grid. Each pixel contains the "
56 "difference in elevation from a center cell and the 8 cells surrounding it." );
57}
58
59QgsRuggednessAlgorithm *QgsRuggednessAlgorithm::createInstance() const
60{
61 return new QgsRuggednessAlgorithm();
62}
63
64void QgsRuggednessAlgorithm::initAlgorithm( const QVariantMap & )
65{
66 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Elevation layer" ) ) );
67 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "Z_FACTOR" ), QObject::tr( "Z factor" ), Qgis::ProcessingNumberParameterType::Double, 1, false, 0 ) );
68
69 addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Ruggedness" ) ) );
70}
71
72QVariantMap QgsRuggednessAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
73{
74 QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
75
76 if ( !inputLayer )
77 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
78
79 const double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
80
81 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
82 const QFileInfo fi( outputFile );
83 const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
84
85 QgsRuggednessFilter ruggedness( inputLayer->source(), outputFile, outputFormat );
86 ruggedness.setZFactor( zFactor );
87 ruggedness.processRaster( feedback );
88
89 QVariantMap outputs;
90 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
91 return outputs;
92}
93
QString source() const
Returns the source for the layer.
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 parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Represents a raster layer.
Calculates the ruggedness index based on a 3x3 moving window.