QGIS API Documentation 3.39.0-Master (67e056379ed)
Loading...
Searching...
No Matches
qgsalgorithmhillshade.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmhillshade.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#include "qgsrasterfilewriter.h"
20#include "qgshillshadefilter.h"
21
23
24QString QgsHillshadeAlgorithm::name() const
25{
26 return QStringLiteral( "hillshade" );
27}
28
29QString QgsHillshadeAlgorithm::displayName() const
30{
31 return QObject::tr( "Hillshade" );
32}
33
34QStringList QgsHillshadeAlgorithm::tags() const
35{
36 return QObject::tr( "dem,hillshade,terrain" ).split( ',' );
37}
38
39QString QgsHillshadeAlgorithm::group() const
40{
41 return QObject::tr( "Raster terrain analysis" );
42}
43
44QString QgsHillshadeAlgorithm::groupId() const
45{
46 return QStringLiteral( "rasterterrainanalysis" );
47}
48
49QString QgsHillshadeAlgorithm::shortHelpString() const
50{
51 return QObject::tr( "This algorithm calculates the hillshade of the Digital Terrain Model in input." )
52 + QStringLiteral( "\n\n" )
53 + QObject::tr( "The shading of the layer is calculated according to the sun position (azimuth and elevation)." );
54}
55
56QgsHillshadeAlgorithm *QgsHillshadeAlgorithm::createInstance() const
57{
58 return new QgsHillshadeAlgorithm();
59}
60
61void QgsHillshadeAlgorithm::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" ),
66 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "AZIMUTH" ), QObject::tr( "Azimuth (horizontal angle)" ),
67 Qgis::ProcessingNumberParameterType::Double, 300, false, 0, 360 ) );
68 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "V_ANGLE" ), QObject::tr( "Vertical angle" ),
70
71 addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Hillshade" ) ) );
72}
73
74QVariantMap QgsHillshadeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75{
76 QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
77
78 if ( !inputLayer )
79 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
80
81 const double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
82 const double azimuth = parameterAsDouble( parameters, QStringLiteral( "AZIMUTH" ), context );
83 const double vAngle = parameterAsDouble( parameters, QStringLiteral( "V_ANGLE" ), context );
84
85 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
86 const QFileInfo fi( outputFile );
87 const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
88
89 QgsHillshadeFilter hillshade( inputLayer->source(), outputFile, outputFormat, azimuth, vAngle );
90 hillshade.setZFactor( zFactor );
91 hillshade.processRaster( feedback );
92
93 QVariantMap outputs;
94 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
95 return outputs;
96}
97
A hillshade filter.
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 numeric parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Represents a raster layer.