QGIS API Documentation  3.0.2-Girona (307d082)
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 
22 QString QgsSmoothAlgorithm::name() const
23 {
24  return QStringLiteral( "smoothgeometry" );
25 }
26 
27 QString QgsSmoothAlgorithm::displayName() const
28 {
29  return QObject::tr( "Smooth" );
30 }
31 
32 QStringList QgsSmoothAlgorithm::tags() const
33 {
34  return QObject::tr( "smooth,curve,generalize,round,bend,corners" ).split( ',' );
35 }
36 
37 QString QgsSmoothAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsSmoothAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsSmoothAlgorithm::outputName() const
48 {
49  return QObject::tr( "Smoothed" );
50 }
51 
52 QgsProcessing::SourceType QgsSmoothAlgorithm::outputLayerType() const
53 {
55 }
56 
57 QString QgsSmoothAlgorithm::shortHelpString() const
58 {
59  return QObject::tr( "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." );
71 }
72 
73 QgsSmoothAlgorithm *QgsSmoothAlgorithm::createInstance() const
74 {
75  return new QgsSmoothAlgorithm();
76 }
77 
78 QList<int> QgsSmoothAlgorithm::inputLayerTypes() const
79 {
81 }
82 
83 void QgsSmoothAlgorithm::initParameters( const QVariantMap & )
84 {
85  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "ITERATIONS" ),
86  QObject::tr( "Iterations" ), QgsProcessingParameterNumber::Integer,
87  1, false, 1, 10 ) );
88  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "OFFSET" ),
89  QObject::tr( "Offset" ), QgsProcessingParameterNumber::Double,
90  0.25, false, 0.0, 0.5 ) );
91  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MAX_ANGLE" ),
92  QObject::tr( "Maximum node angle to smooth" ), QgsProcessingParameterNumber::Double,
93  180.0, false, 0.0, 180.0 ) );
94 }
95 
96 bool QgsSmoothAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
97 {
98  mIterations = parameterAsInt( parameters, QStringLiteral( "ITERATIONS" ), context );
99  mOffset = parameterAsDouble( parameters, QStringLiteral( "OFFSET" ), context );
100  mMaxAngle = parameterAsDouble( parameters, QStringLiteral( "MAX_ANGLE" ), context );
101  return true;
102 }
103 
104 QgsFeatureList QgsSmoothAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
105 {
106  QgsFeature f = feature;
107  if ( f.hasGeometry() )
108  {
109  QgsGeometry outputGeometry = f.geometry().smooth( mIterations, mOffset, -1, mMaxAngle );
110  if ( !outputGeometry )
111  {
112  feedback->reportError( QObject::tr( "Error smoothing geometry %1" ).arg( feature.id() ) );
113  }
114  f.setGeometry( outputGeometry );
115  }
116  return QgsFeatureList() << f;
117 }
118 
120 
121 
QgsFeatureId id
Definition: qgsfeature.h:71
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A numeric parameter for processing algorithms.
Vector line layers.
Definition: qgsprocessing.h:50
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
Contains information about the context in which a processing algorithm is executed.
QgsGeometry smooth(const unsigned int iterations=1, const double offset=0.25, double minimumDistance=-1.0, double maxAngle=180.0) const
Smooths a geometry by rounding off corners using the Chaikin algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.