QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
59 "This algorithm smooths the geometries in a line or polygon layer. It creates a new layer with the "
60 "same features as the ones in the input layer, but with geometries containing a higher number of vertices "
61 "and corners in the geometries smoothed out.\n\n"
62 "The iterations parameter dictates how many smoothing iterations will be applied to each "
63 "geometry. A higher number of iterations results in smoother geometries with the cost of "
64 "greater number of nodes in the geometries.\n\n"
65 "The offset parameter controls how \"tightly\" the smoothed geometries follow the original geometries. "
66 "Smaller values results in a tighter fit, and larger values will create a looser fit.\n\n"
67 "The maximum angle parameter can be used to prevent smoothing of "
68 "nodes with large angles. Any node where the angle of the segments to either "
69 "side is larger than this will not be smoothed. For example, setting the maximum "
70 "angle to 90 degrees or lower would preserve right angles in the geometry.\n\n"
71 "If input geometries contain Z or M values, these will also be smoothed and the output "
72 "geometry will retain the same dimensionality as the input geometry."
73 );
74}
75
76QString QgsSmoothAlgorithm::shortDescription() const
77{
78 return QObject::tr( "Smooths the geometries in a line or polygon layer by adding vertices and rounding corners." );
79}
80
81QgsSmoothAlgorithm *QgsSmoothAlgorithm::createInstance() const
82{
83 return new QgsSmoothAlgorithm();
84}
85
86QList<int> QgsSmoothAlgorithm::inputLayerTypes() const
87{
88 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
89}
90
91void QgsSmoothAlgorithm::initParameters( const QVariantMap & )
92{
93 auto iterations = std::make_unique<QgsProcessingParameterNumber>( u"ITERATIONS"_s, QObject::tr( "Iterations" ), Qgis::ProcessingNumberParameterType::Integer, 1, false, 1, 10 );
94 iterations->setIsDynamic( true );
95 iterations->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ITERATIONS"_s, QObject::tr( "Iterations" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
96 iterations->setDynamicLayerParameterName( u"INPUT"_s );
97 addParameter( iterations.release() );
98
99 auto offset = std::make_unique<QgsProcessingParameterNumber>( u"OFFSET"_s, QObject::tr( "Offset" ), Qgis::ProcessingNumberParameterType::Double, 0.25, false, 0.0, 0.5 );
100 offset->setIsDynamic( true );
101 offset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"OFFSET"_s, QObject::tr( "Offset" ), QgsPropertyDefinition::Double0To1 ) );
102 offset->setDynamicLayerParameterName( u"INPUT"_s );
103 addParameter( offset.release() );
104
105 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 );
106 maxAngle->setIsDynamic( true );
107 maxAngle->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MAX_ANGLE"_s, QObject::tr( "Maximum node angle to smooth" ), QgsPropertyDefinition::Rotation ) );
108 maxAngle->setDynamicLayerParameterName( u"INPUT"_s );
109 addParameter( maxAngle.release() );
110}
111
112bool QgsSmoothAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
113{
114 mIterations = parameterAsInt( parameters, u"ITERATIONS"_s, context );
115 mDynamicIterations = QgsProcessingParameters::isDynamic( parameters, u"ITERATIONS"_s );
116 if ( mDynamicIterations )
117 mIterationsProperty = parameters.value( u"ITERATIONS"_s ).value<QgsProperty>();
118
119 mOffset = parameterAsDouble( parameters, u"OFFSET"_s, context );
120 mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, u"OFFSET"_s );
121 if ( mDynamicOffset )
122 mOffsetProperty = parameters.value( u"OFFSET"_s ).value<QgsProperty>();
123
124 mMaxAngle = parameterAsDouble( parameters, u"MAX_ANGLE"_s, context );
125 mDynamicMaxAngle = QgsProcessingParameters::isDynamic( parameters, u"MAX_ANGLE"_s );
126 if ( mDynamicMaxAngle )
127 mMaxAngleProperty = parameters.value( u"MAX_ANGLE"_s ).value<QgsProperty>();
128
129 return true;
130}
131
132QgsFeatureList QgsSmoothAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
133{
134 QgsFeature f = feature;
135 if ( f.hasGeometry() )
136 {
137 int iterations = mIterations;
138 if ( mDynamicIterations )
139 iterations = mIterationsProperty.valueAsInt( context.expressionContext(), iterations );
140
141 double offset = mOffset;
142 if ( mDynamicOffset )
143 offset = mOffsetProperty.valueAsDouble( context.expressionContext(), offset );
144
145 double maxAngle = mMaxAngle;
146 if ( mDynamicMaxAngle )
147 maxAngle = mMaxAngleProperty.valueAsDouble( context.expressionContext(), maxAngle );
148
149 const QgsGeometry outputGeometry = f.geometry().smooth( iterations, offset, -1, maxAngle );
150 if ( outputGeometry.isNull() )
151 {
152 feedback->reportError( QObject::tr( "Error smoothing geometry %1" ).arg( feature.id() ) );
153 }
154 f.setGeometry( outputGeometry );
155 }
156 return QgsFeatureList() << f;
157}
158
159Qgis::ProcessingFeatureSourceFlags QgsSmoothAlgorithm::sourceFlags() const
160{
162}
163
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
@ Double
Double/float values.
Definition qgis.h:3921
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:58
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition qgsproperty.h:55
@ Rotation
Rotation (value between 0-360 degrees).
Definition qgsproperty.h:59
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