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