QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmsmooth.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsmooth.cpp
3 ---------------------
4 begin : April 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson 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 "qgsalgorithmsmooth.h"
19
21
22QString QgsSmoothAlgorithm::name() const
23{
24 return QStringLiteral( "smoothgeometry" );
25}
26
27QString QgsSmoothAlgorithm::displayName() const
28{
29 return QObject::tr( "Smooth" );
30}
31
32QStringList QgsSmoothAlgorithm::tags() const
33{
34 return QObject::tr( "smooth,curve,generalize,round,bend,corners" ).split( ',' );
35}
36
37QString QgsSmoothAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsSmoothAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsSmoothAlgorithm::outputName() const
48{
49 return QObject::tr( "Smoothed" );
50}
51
52QString QgsSmoothAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm smooths the geometries in a line or polygon layer. It creates a new layer with the "
55 "same features as the ones in the input layer, but with geometries containing a higher number of vertices "
56 "and corners in the geometries smoothed out.\n\n"
57 "The iterations parameter dictates how many smoothing iterations will be applied to each "
58 "geometry. A higher number of iterations results in smoother geometries with the cost of "
59 "greater number of nodes in the geometries.\n\n"
60 "The offset parameter controls how \"tightly\" the smoothed geometries follow the original geometries. "
61 "Smaller values results in a tighter fit, and larger values will create a looser fit.\n\n"
62 "The maximum angle parameter can be used to prevent smoothing of "
63 "nodes with large angles. Any node where the angle of the segments to either "
64 "side is larger than this will not be smoothed. For example, setting the maximum "
65 "angle to 90 degrees or lower would preserve right angles in the geometry.\n\n"
66 "If input geometries contain Z or M values, these will also be smoothed and the output "
67 "geometry will retain the same dimensionality as the input geometry." );
68}
69
70QgsSmoothAlgorithm *QgsSmoothAlgorithm::createInstance() const
71{
72 return new QgsSmoothAlgorithm();
73}
74
75QList<int> QgsSmoothAlgorithm::inputLayerTypes() const
76{
77 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
78}
79
80void QgsSmoothAlgorithm::initParameters( const QVariantMap & )
81{
82 std::unique_ptr<QgsProcessingParameterNumber> iterations = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "ITERATIONS" ), QObject::tr( "Iterations" ), Qgis::ProcessingNumberParameterType::Integer, 1, false, 1, 10 );
83 iterations->setIsDynamic( true );
84 iterations->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ITERATIONS" ), QObject::tr( "Iterations" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
85 iterations->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
86 addParameter( iterations.release() );
87
88 std::unique_ptr<QgsProcessingParameterNumber> offset = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "OFFSET" ), QObject::tr( "Offset" ), Qgis::ProcessingNumberParameterType::Double, 0.25, false, 0.0, 0.5 );
89 offset->setIsDynamic( true );
90 offset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "OFFSET" ), QObject::tr( "Offset" ), QgsPropertyDefinition::Double0To1 ) );
91 offset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
92 addParameter( offset.release() );
93
94 std::unique_ptr<QgsProcessingParameterNumber> maxAngle = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MAX_ANGLE" ), QObject::tr( "Maximum node angle to smooth" ), Qgis::ProcessingNumberParameterType::Double, 180.0, false, 0.0, 180.0 );
95 maxAngle->setIsDynamic( true );
96 maxAngle->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MAX_ANGLE" ), QObject::tr( "Maximum node angle to smooth" ), QgsPropertyDefinition::Rotation ) );
97 maxAngle->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
98 addParameter( maxAngle.release() );
99}
100
101bool QgsSmoothAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
102{
103 mIterations = parameterAsInt( parameters, QStringLiteral( "ITERATIONS" ), context );
104 mDynamicIterations = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ITERATIONS" ) );
105 if ( mDynamicIterations )
106 mIterationsProperty = parameters.value( QStringLiteral( "ITERATIONS" ) ).value<QgsProperty>();
107
108 mOffset = parameterAsDouble( parameters, QStringLiteral( "OFFSET" ), context );
109 mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "OFFSET" ) );
110 if ( mDynamicOffset )
111 mOffsetProperty = parameters.value( QStringLiteral( "OFFSET" ) ).value<QgsProperty>();
112
113 mMaxAngle = parameterAsDouble( parameters, QStringLiteral( "MAX_ANGLE" ), context );
114 mDynamicMaxAngle = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MAX_ANGLE" ) );
115 if ( mDynamicMaxAngle )
116 mMaxAngleProperty = parameters.value( QStringLiteral( "MAX_ANGLE" ) ).value<QgsProperty>();
117
118 return true;
119}
120
121QgsFeatureList QgsSmoothAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
122{
123 QgsFeature f = feature;
124 if ( f.hasGeometry() )
125 {
126 int iterations = mIterations;
127 if ( mDynamicIterations )
128 iterations = mIterationsProperty.valueAsInt( context.expressionContext(), iterations );
129
130 double offset = mOffset;
131 if ( mDynamicOffset )
132 offset = mOffsetProperty.valueAsDouble( context.expressionContext(), offset );
133
134 double maxAngle = mMaxAngle;
135 if ( mDynamicMaxAngle )
136 maxAngle = mMaxAngleProperty.valueAsDouble( context.expressionContext(), maxAngle );
137
138 const QgsGeometry outputGeometry = f.geometry().smooth( iterations, offset, -1, maxAngle );
139 if ( outputGeometry.isNull() )
140 {
141 feedback->reportError( QObject::tr( "Error smoothing geometry %1" ).arg( feature.id() ) );
142 }
143 f.setGeometry( outputGeometry );
144 }
145 return QgsFeatureList() << f;
146}
147
148Qgis::ProcessingFeatureSourceFlags QgsSmoothAlgorithm::sourceFlags() const
149{
151}
152
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3507
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
QgsGeometry smooth(unsigned int iterations=1, double offset=0.25, double minimumDistance=-1.0, double maxAngle=180.0) const
Smooths a geometry by rounding off corners using the Chaikin algorithm.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:45
@ Double0To1
Double value between 0-1 (inclusive)
Definition qgsproperty.h:57
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition qgsproperty.h:54
@ Rotation
Rotation (value between 0-360 degrees)
Definition qgsproperty.h:58
A store for object properties.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
QList< QgsFeature > QgsFeatureList