QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmaspect.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmaspect.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 "qgsalgorithmaspect.h"
19 #include "qgsrasterfilewriter.h"
20 #include "qgsaspectfilter.h"
21 
23 
24 QString QgsAspectAlgorithm::name() const
25 {
26  return QStringLiteral( "aspect" );
27 }
28 
29 QString QgsAspectAlgorithm::displayName() const
30 {
31  return QObject::tr( "Aspect" );
32 }
33 
34 QStringList QgsAspectAlgorithm::tags() const
35 {
36  return QObject::tr( "dem,aspect,terrain" ).split( ',' );
37 }
38 
39 QString QgsAspectAlgorithm::group() const
40 {
41  return QObject::tr( "Raster terrain analysis" );
42 }
43 
44 QString QgsAspectAlgorithm::groupId() const
45 {
46  return QStringLiteral( "rasterterrainanalysis" );
47 }
48 
49 QString QgsAspectAlgorithm::shortHelpString() const
50 {
51  return QObject::tr( "This algorithm calculates the aspect of the Digital Terrain Model in input." )
52  + QStringLiteral( "\n\n" )
53  + QObject::tr( "The final aspect raster layer contains values from 0 to 360 that express "
54  "the slope direction: starting from North (0°) and continuing clockwise." );
55 }
56 
57 QgsAspectAlgorithm *QgsAspectAlgorithm::createInstance() const
58 {
59  return new QgsAspectAlgorithm();
60 }
61 
62 void QgsAspectAlgorithm::initAlgorithm( const QVariantMap & )
63 {
64  addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Elevation layer" ) ) );
65  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "Z_FACTOR" ), QObject::tr( "Z factor" ),
66  QgsProcessingParameterNumber::Double, 1, false, 0 ) );
67 
68  addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Aspect" ) ) );
69 }
70 
71 QVariantMap QgsAspectAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
72 {
73  QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
74 
75  if ( !inputLayer )
76  throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
77 
78  const double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
79 
80  const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
81  const QFileInfo fi( outputFile );
82  const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
83 
84  QgsAspectFilter aspect( inputLayer->source(), outputFile, outputFormat );
85  aspect.setZFactor( zFactor );
86  aspect.processRaster( feedback );
87 
88  QVariantMap outputs;
89  outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
90  return outputs;
91 }
92 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:2187
QgsProcessingParameterNumber
A numeric parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2179
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
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:1066
QgsProcessingParameterRasterDestination
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
Definition: qgsprocessingparameters.h:3390
qgsaspectfilter.h
QgsAspectFilter
Calculates aspect values in a window of 3x3 cells based on first order derivatives in x- and y- direc...
Definition: qgsaspectfilter.h:30
qgsrasterfilewriter.h
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsalgorithmaspect.h
QgsProcessingParameterRasterLayer
A raster layer parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2495
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsMapLayer::source
QString source() const
Returns the source for the layer.
Definition: qgsmaplayer.cpp:300
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82