QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
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
21#include "qgsrasterfilewriter.h"
22#include "qgsruggednessfilter.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30QString QgsRuggednessAlgorithm::name() const
31{
32 return u"ruggednessindex"_s;
33}
34
35QString QgsRuggednessAlgorithm::displayName() const
36{
37 return QObject::tr( "Ruggedness index" );
38}
39
40QStringList QgsRuggednessAlgorithm::tags() const
41{
42 return QObject::tr( "dem,ruggedness,index,terrain" ).split( ',' );
43}
44
45QString QgsRuggednessAlgorithm::group() const
46{
47 return QObject::tr( "Raster terrain analysis" );
48}
49
50QString QgsRuggednessAlgorithm::groupId() const
51{
52 return u"rasterterrainanalysis"_s;
53}
54
55QString QgsRuggednessAlgorithm::shortHelpString() const
56{
57 return QObject::tr(
58 "This algorithm calculates the quantitative measurement of terrain "
59 "heterogeneity described by Riley et al. (1999)."
60 )
61 + u"\n\n"_s
62 + QObject::tr(
63 "It is calculated for every location, by summarizing the change "
64 "in elevation within the 3x3 pixel grid. Each pixel contains the "
65 "difference in elevation from a center cell and the 8 cells surrounding it."
66 );
67}
68
69QString QgsRuggednessAlgorithm::shortDescription() const
70{
71 return QObject::tr( "Calculates the quantitative measurement of terrain heterogeneity described by Riley et al. (1999)." );
72}
73
74QList<QgsAcademicReference> QgsRuggednessAlgorithm::academicReferences() const
75{
76 const QgsAcademicReference rileyReference = QgsAcademicReference::
77 createJournalArticle( { u"Riley, S."_s, u"Degloria, S."_s, u"Elliot, S. D."_s }, 1999, u"A Terrain Ruggedness Index that Quantifies Topographic Heterogeneity"_s, u"International Journal of Science"_s, u"5"_s, QString(), u"23-27"_s );
78 return { rileyReference };
79}
80
81QgsRuggednessAlgorithm *QgsRuggednessAlgorithm::createInstance() const
82{
83 return new QgsRuggednessAlgorithm();
84}
85
86void QgsRuggednessAlgorithm::initAlgorithm( const QVariantMap & )
87{
88 addParameter( new QgsProcessingParameterRasterLayer( u"INPUT"_s, QObject::tr( "Elevation layer" ) ) );
89
90 auto zFactorParam = std::make_unique<QgsProcessingParameterNumber>( u"Z_FACTOR"_s, QObject::tr( "Z factor" ), Qgis::ProcessingNumberParameterType::Double, 1.0, false, 0.0 );
91 zFactorParam->setHelp( QObject::tr( "Multiplication factor to convert vertical Z units to horizontal XY units." ) );
92 zFactorParam->setMetadata( { QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"decimals"_s, 12 } } ) } } ) } );
93 addParameter( zFactorParam.release() );
94
95 auto outputNodataParam = std::make_unique<QgsProcessingParameterNumber>( u"NODATA"_s, QObject::tr( "Output NoData value" ), Qgis::ProcessingNumberParameterType::Double, -9999.0 );
96 outputNodataParam->setFlags( outputNodataParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
97 addParameter( outputNodataParam.release() );
98
99 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATION_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
100 creationOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
101 creationOptsParam->setFlags( creationOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
102 addParameter( creationOptsParam.release() );
103
104 addParameter( new QgsProcessingParameterRasterDestination( u"OUTPUT"_s, QObject::tr( "Ruggedness" ) ) );
105}
106
107bool QgsRuggednessAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
108{
109 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u"INPUT"_s, context );
110 if ( !layer )
111 {
112 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT"_s ) );
113 }
114
115 mLayerSource = layer->source();
116 return true;
117}
118
119QVariantMap QgsRuggednessAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
120{
121 const double zFactor = parameterAsDouble( parameters, u"Z_FACTOR"_s, context );
122 const QString creationOptions = parameterAsString( parameters, u"CREATION_OPTIONS"_s, context ).trimmed();
123 const double outputNodata = parameterAsDouble( parameters, u"NODATA"_s, context );
124
125 const QString outputFile = parameterAsOutputLayer( parameters, u"OUTPUT"_s, context );
126 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT"_s, context );
127
128 QgsRuggednessFilter ruggedness( mLayerSource, outputFile, outputFormat );
129 ruggedness.setZFactor( zFactor );
130 if ( !creationOptions.isEmpty() )
131 {
132 ruggedness.setCreationOptions( creationOptions.split( '|' ) );
133 }
134 ruggedness.setOutputNodataValue( outputNodata );
135 ruggedness.processRaster( feedback );
136
137 QVariantMap outputs;
138 outputs.insert( u"OUTPUT"_s, outputFile );
139 return outputs;
140}
141
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3982
@ Double
Double/float values.
Definition qgis.h:4023
Encapsulates an academic reference and formats it according to style guidelines.
static QgsAcademicReference createJournalArticle(const QStringList &authors, int year, const QString &title, const QString &journal, const QString &volume=QString(), const QString &issue=QString(), const QString &pages=QString())
Creates a journal article reference.
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 raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
Represents a raster layer.
Calculates the ruggedness index based on a 3x3 moving window.