QGIS API Documentation 3.99.0-Master (357b655ed83)
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsSmoothAlgorithm::name() const
27{
28 return u"smoothgeometry"_s;
29}
30
31QString QgsSmoothAlgorithm::displayName() const
32{
33 return QObject::tr( "Smooth" );
34}
35
36QStringList QgsSmoothAlgorithm::tags() const
37{
38 return QObject::tr( "smooth,curve,generalize,round,bend,corners" ).split( ',' );
39}
40
41QString QgsSmoothAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsSmoothAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsSmoothAlgorithm::outputName() const
52{
53 return QObject::tr( "Smoothed" );
54}
55
56QString QgsSmoothAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm smooths the geometries in a line or polygon layer. It creates a new layer with the "
59 "same features as the ones in the input layer, but with geometries containing a higher number of vertices "
60 "and corners in the geometries smoothed out.\n\n"
61 "The iterations parameter dictates how many smoothing iterations will be applied to each "
62 "geometry. A higher number of iterations results in smoother geometries with the cost of "
63 "greater number of nodes in the geometries.\n\n"
64 "The offset parameter controls how \"tightly\" the smoothed geometries follow the original geometries. "
65 "Smaller values results in a tighter fit, and larger values will create a looser fit.\n\n"
66 "The maximum angle parameter can be used to prevent smoothing of "
67 "nodes with large angles. Any node where the angle of the segments to either "
68 "side is larger than this will not be smoothed. For example, setting the maximum "
69 "angle to 90 degrees or lower would preserve right angles in the geometry.\n\n"
70 "If input geometries contain Z or M values, these will also be smoothed and the output "
71 "geometry will retain the same dimensionality as the input geometry." );
72}
73
74QString QgsSmoothAlgorithm::shortDescription() const
75{
76 return QObject::tr( "Smooths the geometries in a line or polygon layer by adding vertices and rounding corners." );
77}
78
79QgsSmoothAlgorithm *QgsSmoothAlgorithm::createInstance() const
80{
81 return new QgsSmoothAlgorithm();
82}
83
84QList<int> QgsSmoothAlgorithm::inputLayerTypes() const
85{
86 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
87}
88
89void QgsSmoothAlgorithm::initParameters( const QVariantMap & )
90{
91 auto iterations = std::make_unique<QgsProcessingParameterNumber>( u"ITERATIONS"_s, QObject::tr( "Iterations" ), Qgis::ProcessingNumberParameterType::Integer, 1, false, 1, 10 );
92 iterations->setIsDynamic( true );
93 iterations->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ITERATIONS"_s, QObject::tr( "Iterations" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
94 iterations->setDynamicLayerParameterName( u"INPUT"_s );
95 addParameter( iterations.release() );
96
97 auto offset = std::make_unique<QgsProcessingParameterNumber>( u"OFFSET"_s, QObject::tr( "Offset" ), Qgis::ProcessingNumberParameterType::Double, 0.25, false, 0.0, 0.5 );
98 offset->setIsDynamic( true );
99 offset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"OFFSET"_s, QObject::tr( "Offset" ), QgsPropertyDefinition::Double0To1 ) );
100 offset->setDynamicLayerParameterName( u"INPUT"_s );
101 addParameter( offset.release() );
102
103 auto maxAngle = std::make_unique<QgsProcessingParameterNumber>( u"MAX_ANGLE"_s, QObject::tr( "Maximum node angle to smooth" ), Qgis::ProcessingNumberParameterType::Double, 180.0, false, 0.0, 180.0 );
104 maxAngle->setIsDynamic( true );
105 maxAngle->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MAX_ANGLE"_s, QObject::tr( "Maximum node angle to smooth" ), QgsPropertyDefinition::Rotation ) );
106 maxAngle->setDynamicLayerParameterName( u"INPUT"_s );
107 addParameter( maxAngle.release() );
108}
109
110bool QgsSmoothAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
111{
112 mIterations = parameterAsInt( parameters, u"ITERATIONS"_s, context );
113 mDynamicIterations = QgsProcessingParameters::isDynamic( parameters, u"ITERATIONS"_s );
114 if ( mDynamicIterations )
115 mIterationsProperty = parameters.value( u"ITERATIONS"_s ).value<QgsProperty>();
116
117 mOffset = parameterAsDouble( parameters, u"OFFSET"_s, context );
118 mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, u"OFFSET"_s );
119 if ( mDynamicOffset )
120 mOffsetProperty = parameters.value( u"OFFSET"_s ).value<QgsProperty>();
121
122 mMaxAngle = parameterAsDouble( parameters, u"MAX_ANGLE"_s, context );
123 mDynamicMaxAngle = QgsProcessingParameters::isDynamic( parameters, u"MAX_ANGLE"_s );
124 if ( mDynamicMaxAngle )
125 mMaxAngleProperty = parameters.value( u"MAX_ANGLE"_s ).value<QgsProperty>();
126
127 return true;
128}
129
130QgsFeatureList QgsSmoothAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
131{
132 QgsFeature f = feature;
133 if ( f.hasGeometry() )
134 {
135 int iterations = mIterations;
136 if ( mDynamicIterations )
137 iterations = mIterationsProperty.valueAsInt( context.expressionContext(), iterations );
138
139 double offset = mOffset;
140 if ( mDynamicOffset )
141 offset = mOffsetProperty.valueAsDouble( context.expressionContext(), offset );
142
143 double maxAngle = mMaxAngle;
144 if ( mDynamicMaxAngle )
145 maxAngle = mMaxAngleProperty.valueAsDouble( context.expressionContext(), maxAngle );
146
147 const QgsGeometry outputGeometry = f.geometry().smooth( iterations, offset, -1, maxAngle );
148 if ( outputGeometry.isNull() )
149 {
150 feedback->reportError( QObject::tr( "Error smoothing geometry %1" ).arg( feature.id() ) );
151 }
152 f.setGeometry( outputGeometry );
153 }
154 return QgsFeatureList() << f;
155}
156
157Qgis::ProcessingFeatureSourceFlags QgsSmoothAlgorithm::sourceFlags() const
158{
160}
161
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
@ Double
Double/float values.
Definition qgis.h:3875
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
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:47
@ Double0To1
Double value between 0-1 (inclusive).
Definition qgsproperty.h:59
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition qgsproperty.h:56
@ Rotation
Rotation (value between 0-360 degrees).
Definition qgsproperty.h:60
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