[processing] port terrain analysis algoithms to C++

This commit is contained in:
Alexander Bruy 2019-11-22 13:52:06 +02:00 committed by Nyall Dawson
parent 23ae038da4
commit 06a71403fe
17 changed files with 610 additions and 368 deletions

View File

@ -24,10 +24,6 @@ qgis:aggregate: >
Output attributes are computed depending on each given aggregate definition.
qgis:aspect: >
This algorithm calculates the aspect of the Digital Terrain Model in input.
The final aspect raster layer contains values from 0 to 360 that express the slope direction: starting from North (0°) and continuing clockwise.
qgis:barplot: >
This algorithm creates a bar plot from a category and a layer field.
@ -216,10 +212,6 @@ qgis:generatepointspixelcentroidsinsidepolygons: >
This algorithm generates a point vector layer from an input raster and polygon layer.
The points correspond to the pixel centroids that intersect the polygon layer.
qgis:hillshade: >
This algorithm calculates the hillshade raster layer given a Digital Terrain Model in input.
The shading of the layer is calculated according to the sun position (azimuth and elevation).
qgis:hypsometriccurves: >
This algorithm computes hypsometric curves for an input Digital Elevation Model. Curves are produced as table files in an output folder specified by the user.
@ -459,15 +451,6 @@ qgis:regularpoints: >
qgis:relief: >
This algorithm creates a shaded relief layer from digital elevation data.
qgis:ruggednessindex: >
This algorithm calculates the quantitative measurement of terrain heterogeneity described by Riley et al. (1999).
It is calculated for every location, by summarizing the change in elevation within the 3x3 pixel grid.
Each pixel contains the difference in elevation from a center cell and the 8 cells surrounding it.
qgis:slope: >
This algorithm calculates the angle of inclination of the terrain from an input raster layer. The slope is expressed in degrees.
qgis:removenullgeometries: >
This algorithm removes any features which do not have a geometry from a vector layer. All other features will be copied unchanged.

View File

@ -1,82 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
Aspect.py
---------------------
Date : October 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Alexander Bruy'
__date__ = 'October 2016'
__copyright__ = '(C) 2016, Alexander Bruy'
import os
from qgis.PyQt.QtGui import QIcon
from qgis.analysis import QgsAspectFilter
from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class Aspect(QgisAlgorithm):
INPUT = 'INPUT'
Z_FACTOR = 'Z_FACTOR'
OUTPUT = 'OUTPUT'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))
def group(self):
return self.tr('Raster terrain analysis')
def groupId(self):
return 'rasterterrainanalysis'
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Elevation layer')))
self.addParameter(QgsProcessingParameterNumber(self.Z_FACTOR,
self.tr('Z factor'), QgsProcessingParameterNumber.Double,
1, False, 0.00))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Aspect')))
def name(self):
return 'aspect'
def displayName(self):
return self.tr('Aspect')
def processAlgorithm(self, parameters, context, feedback):
inputFile = self.parameterAsRasterLayer(parameters, self.INPUT, context).source()
zFactor = self.parameterAsDouble(parameters, self.Z_FACTOR, context)
outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])
aspect = QgsAspectFilter(inputFile, outputFile, outputFormat)
aspect.setZFactor(zFactor)
aspect.processRaster(feedback)
return {self.OUTPUT: outputFile}

View File

@ -1,91 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
Hillshade.py
---------------------
Date : October 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Alexander Bruy'
__date__ = 'October 2016'
__copyright__ = '(C) 2016, Alexander Bruy'
import os
from qgis.PyQt.QtGui import QIcon
from qgis.analysis import QgsHillshadeFilter
from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class Hillshade(QgisAlgorithm):
INPUT = 'INPUT'
Z_FACTOR = 'Z_FACTOR'
AZIMUTH = 'AZIMUTH'
V_ANGLE = 'V_ANGLE'
OUTPUT = 'OUTPUT'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))
def group(self):
return self.tr('Raster terrain analysis')
def groupId(self):
return 'rasterterrainanalysis'
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Elevation layer')))
self.addParameter(QgsProcessingParameterNumber(self.Z_FACTOR,
self.tr('Z factor'), QgsProcessingParameterNumber.Double,
1, False, 0.00))
self.addParameter(QgsProcessingParameterNumber(self.AZIMUTH,
self.tr('Azimuth (horizontal angle)'), QgsProcessingParameterNumber.Double,
300, False, 0, 360))
self.addParameter(QgsProcessingParameterNumber(self.V_ANGLE,
self.tr('Vertical angle'), QgsProcessingParameterNumber.Double,
40, False, 1, 90))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Hillshade')))
def name(self):
return 'hillshade'
def displayName(self):
return self.tr('Hillshade')
def processAlgorithm(self, parameters, context, feedback):
inputFile = self.parameterAsRasterLayer(parameters, self.INPUT, context).source()
zFactor = self.parameterAsDouble(parameters, self.Z_FACTOR, context)
azimuth = self.parameterAsDouble(parameters, self.AZIMUTH, context)
vAngle = self.parameterAsDouble(parameters, self.V_ANGLE, context)
outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])
hillshade = QgsHillshadeFilter(inputFile, outputFile, outputFormat, azimuth, vAngle)
hillshade.setZFactor(zFactor)
hillshade.processRaster(feedback)
return {self.OUTPUT: outputFile}

View File

@ -30,7 +30,6 @@ from PyQt5.QtCore import QCoreApplication
from .AddTableField import AddTableField
from .Aggregate import Aggregate
from .Aspect import Aspect
from .BarPlot import BarPlot
from .BasicStatistics import BasicStatisticsForField
from .BoxPlot import BoxPlot
@ -56,7 +55,6 @@ from .FindProjection import FindProjection
from .GeometryConvert import GeometryConvert
from .GeometryByExpression import GeometryByExpression
from .Heatmap import Heatmap
from .Hillshade import Hillshade
from .HubDistanceLines import HubDistanceLines
from .HubDistancePoints import HubDistancePoints
from .HypsometricCurves import HypsometricCurves
@ -98,7 +96,6 @@ from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
from .RegularPoints import RegularPoints
from .Relief import Relief
from .Ruggedness import Ruggedness
from .SelectByAttribute import SelectByAttribute
from .SelectByExpression import SelectByExpression
from .SetMValue import SetMValue
@ -106,7 +103,6 @@ from .SetRasterStyle import SetRasterStyle
from .SetVectorStyle import SetVectorStyle
from .SetZValue import SetZValue
from .SingleSidedBuffer import SingleSidedBuffer
from .Slope import Slope
from .SnapGeometries import SnapGeometriesToLayer
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
from .SpatialIndex import SpatialIndex
@ -140,7 +136,6 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
def getAlgs(self):
algs = [AddTableField(),
Aggregate(),
Aspect(),
BarPlot(),
BasicStatisticsForField(),
BoxPlot(),
@ -166,7 +161,6 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
GeometryByExpression(),
GeometryConvert(),
Heatmap(),
Hillshade(),
HubDistanceLines(),
HubDistancePoints(),
HypsometricCurves(),
@ -208,7 +202,6 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
RectanglesOvalsDiamondsVariable(),
RegularPoints(),
Relief(),
Ruggedness(),
SelectByAttribute(),
SelectByExpression(),
SetMValue(),
@ -216,7 +209,6 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
SetVectorStyle(),
SetZValue(),
SingleSidedBuffer(),
Slope(),
SnapGeometriesToLayer(),
SpatialiteExecuteSQL(),
SpatialIndex(),

View File

@ -1,81 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
Ruggedness.py
---------------------
Date : October 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Alexander Bruy'
__date__ = 'October 2016'
__copyright__ = '(C) 2016, Alexander Bruy'
import os
from qgis.PyQt.QtGui import QIcon
from qgis.analysis import QgsRuggednessFilter
from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class Ruggedness(QgisAlgorithm):
INPUT = 'INPUT'
Z_FACTOR = 'Z_FACTOR'
OUTPUT = 'OUTPUT'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))
def group(self):
return self.tr('Raster terrain analysis')
def groupId(self):
return 'rasterterrainanalysis'
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Elevation layer')))
self.addParameter(QgsProcessingParameterNumber(self.Z_FACTOR,
self.tr('Z factor'),
QgsProcessingParameterNumber.Double,
1, False, 0.00))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Ruggedness')))
def name(self):
return 'ruggednessindex'
def displayName(self):
return self.tr('Ruggedness index')
def processAlgorithm(self, parameters, context, feedback):
inputFile = self.parameterAsRasterLayer(parameters, self.INPUT, context).source()
zFactor = self.parameterAsDouble(parameters, self.Z_FACTOR, context)
outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])
ruggedness = QgsRuggednessFilter(inputFile, outputFile, outputFormat)
ruggedness.setZFactor(zFactor)
ruggedness.processRaster(feedback)
return {self.OUTPUT: outputFile}

View File

@ -1,81 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
Slope.py
---------------------
Date : October 2016
Copyright : (C) 2016 by Alexander Bruy
Email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
__author__ = 'Alexander Bruy'
__date__ = 'October 2016'
__copyright__ = '(C) 2016, Alexander Bruy'
import os
from qgis.PyQt.QtGui import QIcon
from qgis.analysis import QgsSlopeFilter
from qgis.core import (QgsRasterFileWriter,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterRasterDestination)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class Slope(QgisAlgorithm):
INPUT = 'INPUT'
Z_FACTOR = 'Z_FACTOR'
OUTPUT = 'OUTPUT'
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'dem.png'))
def group(self):
return self.tr('Raster terrain analysis')
def groupId(self):
return 'rasterterrainanalysis'
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
self.tr('Elevation layer')))
self.addParameter(QgsProcessingParameterNumber(self.Z_FACTOR,
self.tr('Z factor'), QgsProcessingParameterNumber.Double,
1, False, 0.00))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT, self.tr('Slope')))
def name(self):
return 'slope'
def displayName(self):
return self.tr('Slope')
def processAlgorithm(self, parameters, context, feedback):
inputFile = self.parameterAsRasterLayer(parameters, self.INPUT, context).source()
zFactor = self.parameterAsDouble(parameters, self.Z_FACTOR, context)
outputFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
outputFormat = QgsRasterFileWriter.driverForExtension(os.path.splitext(outputFile)[1])
slope = QgsSlopeFilter(inputFile, outputFile, outputFormat)
slope.setZFactor(zFactor)
slope.processRaster(feedback)
return {self.OUTPUT: outputFile}

View File

@ -1613,7 +1613,7 @@ tests:
name: expected/points_in_polys.gml
type: vector
- algorithm: qgis:aspect
- algorithm: native:aspect
name: Aspect from QGIS analysis library
params:
INPUT:
@ -1627,7 +1627,7 @@ tests:
- f6a8e64647ae93a94f2a4945add8986526a7a07bc85849f3690d15b2
type: rasterhash
- algorithm: qgis:slope
- algorithm: native:slope
name: Slope from QGIS analysis library
params:
INPUT:
@ -1641,7 +1641,7 @@ tests:
- 177475642c57428b395bc0a1e7e86fc1cfd4d86ffc19f31ff8bc964d
type: rasterhash
- algorithm: qgis:ruggednessindex
- algorithm: native:ruggednessindex
name: Ruggedness index from QGIS analysis library
params:
INPUT:
@ -1653,7 +1653,7 @@ tests:
hash: ff630246e8dc19c7217d81261c6b64f965c17fa04d3e41d7979c1f1e
type: rasterhash
- algorithm: qgis:hillshade
- algorithm: native:hillshade
name: Hillshade from QGIS analysis library
params:
AZIMUTH: 300.0

View File

@ -24,6 +24,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmaddincrementalfield.cpp
processing/qgsalgorithmaddxyfields.cpp
processing/qgsalgorithmarraytranslatedfeatures.cpp
processing/qgsalgorithmaspect.cpp
processing/qgsalgorithmassignprojection.cpp
processing/qgsalgorithmboundary.cpp
processing/qgsalgorithmboundingbox.cpp
@ -60,6 +61,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmforcerhr.cpp
processing/qgsalgorithmfuzzifyraster.cpp
processing/qgsalgorithmgrid.cpp
processing/qgsalgorithmhillshade.cpp
processing/qgsalgorithmimportphotos.cpp
processing/qgsalgorithminterpolatepoint.cpp
processing/qgsalgorithmintersection.cpp
@ -100,6 +102,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmrenamelayer.cpp
processing/qgsalgorithmreverselinedirection.cpp
processing/qgsalgorithmrotate.cpp
processing/qgsalgorithmruggedness.cpp
processing/qgsalgorithmsaveselectedfeatures.cpp
processing/qgsalgorithmsegmentize.cpp
processing/qgsalgorithmserviceareafromlayer.cpp
@ -108,6 +111,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmshortestpathpointtolayer.cpp
processing/qgsalgorithmshortestpathpointtopoint.cpp
processing/qgsalgorithmsimplify.cpp
processing/qgsalgorithmslope.cpp
processing/qgsalgorithmsmooth.cpp
processing/qgsalgorithmsnaptogrid.cpp
processing/qgsalgorithmsplitfeaturesbyattributecharacter.cpp

View File

@ -0,0 +1,93 @@
/***************************************************************************
qgsalgorithmaspect.cpp
---------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmaspect.h"
#include "qgsrasterfilewriter.h"
#include "qgsaspectfilter.h"
///@cond PRIVATE
QString QgsAspectAlgorithm::name() const
{
return QStringLiteral( "aspect" );
}
QString QgsAspectAlgorithm::displayName() const
{
return QObject::tr( "Aspect" );
}
QStringList QgsAspectAlgorithm::tags() const
{
return QObject::tr( "dem,aspect,terrain" ).split( ',' );
}
QString QgsAspectAlgorithm::group() const
{
return QObject::tr( "Raster terrain analysis" );
}
QString QgsAspectAlgorithm::groupId() const
{
return QStringLiteral( "rasterterrainanalysis" );
}
QString QgsAspectAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm calculates the aspect of the Digital Terrain Model in input." )
+ QStringLiteral( "\n\n" )
+ QObject::tr( "The final aspect raster layer contains values from 0 to 360 that express "
"the slope direction: starting from North (0°) and continuing clockwise." );
}
QgsAspectAlgorithm *QgsAspectAlgorithm::createInstance() const
{
return new QgsAspectAlgorithm();
}
void QgsAspectAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Elevation layer" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "Z_FACTOR" ), QObject::tr( "Z factor" ),
QgsProcessingParameterNumber::Double, 1, false, 0 ) );
addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Aspect" ) ) );
}
QVariantMap QgsAspectAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
if ( !inputLayer )
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
QgsAspectFilter aspect( inputLayer->source(), outputFile, outputFormat );
aspect.setZFactor( zFactor );
aspect.processRaster( feedback );
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
return outputs;
}
///@endcond

View File

@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmaspect.h
------------------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSALGORITHMASPECT_H
#define QGSALGORITHMASPECT_H
#define SIP_NO_FILE
#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native aspect algorithm.
*/
class QgsAspectAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsAspectAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
QgsAspectAlgorithm *createInstance() const override SIP_FACTORY;
protected:
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};
///@endcond PRIVATE
#endif // QGSALGORITHMASPECT_H

View File

@ -0,0 +1,98 @@
/***************************************************************************
qgsalgorithmhillshade.cpp
---------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmhillshade.h"
#include "qgsrasterfilewriter.h"
#include "qgshillshadefilter.h"
///@cond PRIVATE
QString QgsHillshadeAlgorithm::name() const
{
return QStringLiteral( "hillshade" );
}
QString QgsHillshadeAlgorithm::displayName() const
{
return QObject::tr( "Hillshade" );
}
QStringList QgsHillshadeAlgorithm::tags() const
{
return QObject::tr( "dem,hillshade,terrain" ).split( ',' );
}
QString QgsHillshadeAlgorithm::group() const
{
return QObject::tr( "Raster terrain analysis" );
}
QString QgsHillshadeAlgorithm::groupId() const
{
return QStringLiteral( "rasterterrainanalysis" );
}
QString QgsHillshadeAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm calculates the hillshade of the Digital Terrain Model in input." )
+ QStringLiteral( "\n\n" )
+ QObject::tr( "The shading of the layer is calculated according to the sun position (azimuth and elevation)." );
}
QgsHillshadeAlgorithm *QgsHillshadeAlgorithm::createInstance() const
{
return new QgsHillshadeAlgorithm();
}
void QgsHillshadeAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Elevation layer" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "Z_FACTOR" ), QObject::tr( "Z factor" ),
QgsProcessingParameterNumber::Double, 1, false, 0 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "AZIMUTH" ), QObject::tr( "Azimuth (horizontal angle)" ),
QgsProcessingParameterNumber::Double, 300, false, 0, 360 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "V_ANGLE" ), QObject::tr( "Vertical angle" ),
QgsProcessingParameterNumber::Double, 40, false, 0, 90 ) );
addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Hillshade" ) ) );
}
QVariantMap QgsHillshadeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
if ( !inputLayer )
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
double azimuth = parameterAsDouble( parameters, QStringLiteral( "AZIMUTH" ), context );
double vAngle = parameterAsDouble( parameters, QStringLiteral( "V_ANGLE" ), context );
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
QgsHillshadeFilter hillshade( inputLayer->source(), outputFile, outputFormat, azimuth, vAngle );
hillshade.setZFactor( zFactor );
hillshade.processRaster( feedback );
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
return outputs;
}
///@endcond

View File

@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmhillshade.h
------------------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSALGORITHMHILLSHADE_H
#define QGSALGORITHMHILLSHADE_H
#define SIP_NO_FILE
#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native hillshade algorithm.
*/
class QgsHillshadeAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsHillshadeAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
QgsHillshadeAlgorithm *createInstance() const override SIP_FACTORY;
protected:
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};
///@endcond PRIVATE
#endif // QGSALGORITHMHILLSHADE_H

View File

@ -0,0 +1,95 @@
/***************************************************************************
qgsalgorithmruggedness.cpp
---------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmruggedness.h"
#include "qgsrasterfilewriter.h"
#include "qgsruggednessfilter.h"
///@cond PRIVATE
QString QgsRuggednessAlgorithm::name() const
{
return QStringLiteral( "ruggednessindex" );
}
QString QgsRuggednessAlgorithm::displayName() const
{
return QObject::tr( "Ruggedness index" );
}
QStringList QgsRuggednessAlgorithm::tags() const
{
return QObject::tr( "dem,ruggedness,index,terrain" ).split( ',' );
}
QString QgsRuggednessAlgorithm::group() const
{
return QObject::tr( "Raster terrain analysis" );
}
QString QgsRuggednessAlgorithm::groupId() const
{
return QStringLiteral( "rasterterrainanalysis" );
}
QString QgsRuggednessAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm calculates the quantitative measurement of terrain "
"heterogeneity described by Riley et al. (1999)." )
+ QStringLiteral( "\n\n" )
+ QObject::tr( "It is calculated for every location, by summarizing the change "
"in elevation within the 3x3 pixel grid. Each pixel contains the "
"difference in elevation from a center cell and the 8 cells surrounding it." );
}
QgsRuggednessAlgorithm *QgsRuggednessAlgorithm::createInstance() const
{
return new QgsRuggednessAlgorithm();
}
void QgsRuggednessAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Elevation layer" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "Z_FACTOR" ), QObject::tr( "Z factor" ),
QgsProcessingParameterNumber::Double, 1, false, 0 ) );
addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Ruggedness" ) ) );
}
QVariantMap QgsRuggednessAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
if ( !inputLayer )
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
QgsRuggednessFilter ruggedness( inputLayer->source(), outputFile, outputFormat );
ruggedness.setZFactor( zFactor );
ruggedness.processRaster( feedback );
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
return outputs;
}
///@endcond

View File

@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmruggedness.h
------------------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSALGORITHMRUGGEDNESS_H
#define QGSALGORITHMRUGGENDESS_H
#define SIP_NO_FILE
#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native ruggedness index algorithm.
*/
class QgsRuggednessAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsRuggednessAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
QgsRuggednessAlgorithm *createInstance() const override SIP_FACTORY;
protected:
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};
///@endcond PRIVATE
#endif // QGSALGORITHMRUGGEDNESS_H

View File

@ -0,0 +1,92 @@
/***************************************************************************
qgsalgorithmslope.cpp
---------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmslope.h"
#include "qgsrasterfilewriter.h"
#include "qgsslopefilter.h"
///@cond PRIVATE
QString QgsSlopeAlgorithm::name() const
{
return QStringLiteral( "slope" );
}
QString QgsSlopeAlgorithm::displayName() const
{
return QObject::tr( "Slope" );
}
QStringList QgsSlopeAlgorithm::tags() const
{
return QObject::tr( "dem,slope,terrain" ).split( ',' );
}
QString QgsSlopeAlgorithm::group() const
{
return QObject::tr( "Raster terrain analysis" );
}
QString QgsSlopeAlgorithm::groupId() const
{
return QStringLiteral( "rasterterrainanalysis" );
}
QString QgsSlopeAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm calculates the angle of inclination "
"of the terrain from an input raster layer. The slope "
"is expressed in degrees." );
}
QgsSlopeAlgorithm *QgsSlopeAlgorithm::createInstance() const
{
return new QgsSlopeAlgorithm();
}
void QgsSlopeAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Elevation layer" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "Z_FACTOR" ), QObject::tr( "Z factor" ),
QgsProcessingParameterNumber::Double, 1, false, 0 ) );
addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Slope" ) ) );
}
QVariantMap QgsSlopeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
if ( !inputLayer )
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
double zFactor = parameterAsDouble( parameters, QStringLiteral( "Z_FACTOR" ), context );
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
QgsSlopeFilter slope( inputLayer->source(), outputFile, outputFormat );
slope.setZFactor( zFactor );
slope.processRaster( feedback );
QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
return outputs;
}
///@endcond

View File

@ -0,0 +1,54 @@
/***************************************************************************
qgsalgorithmslope.h
------------------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSALGORITHMSLOPE_H
#define QGSALGORITHMSLOPE_H
#define SIP_NO_FILE
#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native slope algorithm.
*/
class QgsSlopeAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsSlopeAlgorithm() = default;
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
QgsSlopeAlgorithm *createInstance() const override SIP_FACTORY;
protected:
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
};
///@endcond PRIVATE
#endif // QGSALGORITHMSLOPE_H

View File

@ -19,6 +19,7 @@
#include "qgsalgorithmaddincrementalfield.h"
#include "qgsalgorithmaddxyfields.h"
#include "qgsalgorithmarraytranslatedfeatures.h"
#include "qgsalgorithmaspect.h"
#include "qgsalgorithmassignprojection.h"
#include "qgsalgorithmboundary.h"
#include "qgsalgorithmboundingbox.h"
@ -55,6 +56,7 @@
#include "qgsalgorithmforcerhr.h"
#include "qgsalgorithmfuzzifyraster.h"
#include "qgsalgorithmgrid.h"
#include "qgsalgorithmhillshade.h"
#include "qgsalgorithmjoinbyattribute.h"
#include "qgsalgorithmjoinbynearest.h"
#include "qgsalgorithmjoinwithlines.h"
@ -94,6 +96,7 @@
#include "qgsalgorithmrenamelayer.h"
#include "qgsalgorithmreverselinedirection.h"
#include "qgsalgorithmrotate.h"
#include "qgsalgorithmruggedness.h"
#include "qgsalgorithmsaveselectedfeatures.h"
#include "qgsalgorithmsegmentize.h"
#include "qgsalgorithmserviceareafromlayer.h"
@ -102,6 +105,7 @@
#include "qgsalgorithmshortestpathpointtolayer.h"
#include "qgsalgorithmshortestpathpointtopoint.h"
#include "qgsalgorithmsimplify.h"
#include "qgsalgorithmslope.h"
#include "qgsalgorithmsmooth.h"
#include "qgsalgorithmsnaptogrid.h"
#include "qgsalgorithmsplitlineantimeridian.h"
@ -168,6 +172,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsAddXYFieldsAlgorithm() );
addAlgorithm( new QgsAddUniqueValueIndexAlgorithm() );
addAlgorithm( new QgsArrayTranslatedFeaturesAlgorithm() );
addAlgorithm( new QgsAspectAlgorithm() );
addAlgorithm( new QgsAssignProjectionAlgorithm() );
addAlgorithm( new QgsBookmarksToLayerAlgorithm() );
addAlgorithm( new QgsBoundaryAlgorithm() );
@ -212,6 +217,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsFuzzifyRasterGaussianMembershipAlgorithm() );
addAlgorithm( new QgsFuzzifyRasterNearMembershipAlgorithm() );
addAlgorithm( new QgsGridAlgorithm() );
addAlgorithm( new QgsHillshadeAlgorithm() );
addAlgorithm( new QgsImportPhotosAlgorithm() );
addAlgorithm( new QgsInterpolatePointAlgorithm() );
addAlgorithm( new QgsIntersectionAlgorithm() );
@ -257,6 +263,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsRenameLayerAlgorithm() );
addAlgorithm( new QgsReverseLineDirectionAlgorithm() );
addAlgorithm( new QgsRotateFeaturesAlgorithm() );
addAlgorithm( new QgsRuggednessAlgorithm() );
addAlgorithm( new QgsSaveSelectedFeatures() );
addAlgorithm( new QgsSegmentizeByMaximumAngleAlgorithm() );
addAlgorithm( new QgsSegmentizeByMaximumDistanceAlgorithm() );
@ -267,6 +274,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsShortestPathPointToLayerAlgorithm() );
addAlgorithm( new QgsShortestPathPointToPointAlgorithm() );
addAlgorithm( new QgsSimplifyAlgorithm() );
addAlgorithm( new QgsSlopeAlgorithm() );
addAlgorithm( new QgsSmoothAlgorithm() );
addAlgorithm( new QgsSnapToGridAlgorithm() );
addAlgorithm( new QgsSplitFeaturesByAttributeCharacterAlgorithm() );
@ -291,8 +299,4 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsDensifyGeometriesByCountAlgorithm() );
}
///@endcond