QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsalgorithmslope.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmslope.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 
18 #include "qgsalgorithmslope.h"
19 #include "qgsrasterfilewriter.h"
20 #include "qgsslopefilter.h"
21 
23 
24 QString QgsSlopeAlgorithm::name() const
25 {
26  return QStringLiteral( "slope" );
27 }
28 
29 QString QgsSlopeAlgorithm::displayName() const
30 {
31  return QObject::tr( "Slope" );
32 }
33 
34 QStringList QgsSlopeAlgorithm::tags() const
35 {
36  return QObject::tr( "dem,slope,terrain" ).split( ',' );
37 }
38 
39 QString QgsSlopeAlgorithm::group() const
40 {
41  return QObject::tr( "Raster terrain analysis" );
42 }
43 
44 QString QgsSlopeAlgorithm::groupId() const
45 {
46  return QStringLiteral( "rasterterrainanalysis" );
47 }
48 
49 QString QgsSlopeAlgorithm::shortHelpString() const
50 {
51  return QObject::tr( "This algorithm calculates the angle of inclination "
52  "of the terrain from an input raster layer. The slope "
53  "is expressed in degrees." );
54 }
55 
56 QgsSlopeAlgorithm *QgsSlopeAlgorithm::createInstance() const
57 {
58  return new QgsSlopeAlgorithm();
59 }
60 
61 void QgsSlopeAlgorithm::initAlgorithm( const QVariantMap & )
62 {
63  addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Elevation layer" ) ) );
64  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "Z_FACTOR" ), QObject::tr( "Z factor" ),
65  QgsProcessingParameterNumber::Double, 1, false, 0 ) );
66 
67  addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Slope" ) ) );
68 }
69 
70 QVariantMap QgsSlopeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
71 {
72  QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
73 
74  if ( !inputLayer )
75  throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
76 
77  double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
78 
79  const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
80  QFileInfo fi( outputFile );
81  const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
82 
83  QgsSlopeFilter slope( inputLayer->source(), outputFile, outputFormat );
84  slope.setZFactor( zFactor );
85  slope.processRaster( feedback );
86 
87  QVariantMap outputs;
88  outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
89  return outputs;
90 }
91 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:1846
QgsProcessingParameterNumber
Definition: qgsprocessingparameters.h:1838
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsRasterFileWriter::driverForExtension
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Definition: qgsrasterfilewriter.cpp:1061
QgsProcessingParameterRasterDestination
Definition: qgsprocessingparameters.h:2944
QgsSlopeFilter
Definition: qgsslopefilter.h:27
qgsrasterfilewriter.h
qgsslopefilter.h
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
qgsalgorithmslope.h
QgsProcessingParameterRasterLayer
Definition: qgsprocessingparameters.h:2101
QgsRasterLayer
Definition: qgsrasterlayer.h:72
QgsMapLayer::source
QString source() const
Returns the source for the layer.
Definition: qgsmaplayer.cpp:192
QgsProcessingException
Definition: qgsexception.h:82