QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
20#include "qgsaspectfilter.h"
21#include "qgsrasterfilewriter.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsAspectAlgorithm::name() const
30{
31 return u"aspect"_s;
32}
33
34QString QgsAspectAlgorithm::displayName() const
35{
36 return QObject::tr( "Aspect" );
37}
38
39QStringList QgsAspectAlgorithm::tags() const
40{
41 return QObject::tr( "dem,aspect,terrain,slope" ).split( ',' );
42}
43
44QString QgsAspectAlgorithm::group() const
45{
46 return QObject::tr( "Raster terrain analysis" );
47}
48
49QString QgsAspectAlgorithm::groupId() const
50{
51 return u"rasterterrainanalysis"_s;
52}
53
54QString QgsAspectAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm calculates the aspect of the Digital Terrain Model in input." )
57 + u"\n\n"_s
58 + QObject::tr( "The final aspect raster layer contains values from 0 to 360 that express "
59 "the slope direction: starting from North (0°) and continuing clockwise." );
60}
61
62QString QgsAspectAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Generates a raster layer representing the slope direction from a Digital Terrain Model." );
65}
66
67QgsAspectAlgorithm *QgsAspectAlgorithm::createInstance() const
68{
69 return new QgsAspectAlgorithm();
70}
71
72void QgsAspectAlgorithm::initAlgorithm( const QVariantMap & )
73{
74 addParameter( new QgsProcessingParameterRasterLayer( u"INPUT"_s, QObject::tr( "Elevation layer" ) ) );
75
76 auto zFactorParam = std::make_unique<QgsProcessingParameterNumber>( u"Z_FACTOR"_s, QObject::tr( "Z factor" ), Qgis::ProcessingNumberParameterType::Double, 1.0, false, 0.0 );
77 zFactorParam->setHelp( QObject::tr( "Multiplication factor to convert vertical Z units to horizontal XY units." ) );
78 zFactorParam->setMetadata(
79 { QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"decimals"_s, 12 } } ) } } )
80 }
81 );
82 addParameter( zFactorParam.release() );
83
84 auto outputNodataParam = std::make_unique<QgsProcessingParameterNumber>( u"NODATA"_s, QObject::tr( "Output NoData value" ), Qgis::ProcessingNumberParameterType::Double, -9999.0 );
85 outputNodataParam->setFlags( outputNodataParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
86 addParameter( outputNodataParam.release() );
87
88 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATION_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
89 creationOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
90 creationOptsParam->setFlags( creationOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
91 addParameter( creationOptsParam.release() );
92
93 addParameter( new QgsProcessingParameterRasterDestination( u"OUTPUT"_s, QObject::tr( "Aspect" ) ) );
94}
95
96bool QgsAspectAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
97{
98 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u"INPUT"_s, context );
99 if ( !layer )
100 {
101 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT"_s ) );
102 }
103
104 mLayerSource = layer->source();
105 return true;
106}
107
108QVariantMap QgsAspectAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
109{
110 const double zFactor = parameterAsDouble( parameters, u"Z_FACTOR"_s, context );
111 const QString creationOptions = parameterAsString( parameters, u"CREATION_OPTIONS"_s, context ).trimmed();
112 const double outputNodata = parameterAsDouble( parameters, u"NODATA"_s, context );
113
114 const QString outputFile = parameterAsOutputLayer( parameters, u"OUTPUT"_s, context );
115 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT"_s, context );
116
117 QgsAspectFilter aspect( mLayerSource, outputFile, outputFormat );
118 aspect.setZFactor( zFactor );
119 if ( !creationOptions.isEmpty() )
120 {
121 aspect.setCreationOptions( creationOptions.split( '|' ) );
122 }
123 aspect.setOutputNodataValue( outputNodata );
124 aspect.processRaster( feedback );
125
126 QVariantMap outputs;
127 outputs.insert( u"OUTPUT"_s, outputFile );
128 return outputs;
129}
130
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3834
@ Double
Double/float values.
Definition qgis.h:3875
Calculates aspect values in a window of 3x3 cells based on first order derivatives in x- and y- direc...
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.