QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmsimplify.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsimplify.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
19
20#include <memory>
21
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30QgsSimplifyAlgorithm::~QgsSimplifyAlgorithm() = default;
31
32QString QgsSimplifyAlgorithm::name() const
33{
34 return u"simplifygeometries"_s;
35}
36
37QString QgsSimplifyAlgorithm::displayName() const
38{
39 return QObject::tr( "Simplify" );
40}
41
42QStringList QgsSimplifyAlgorithm::tags() const
43{
44 return QObject::tr( "simplify,generalize,douglas,peucker,visvalingam" ).split( ',' );
45}
46
47QString QgsSimplifyAlgorithm::group() const
48{
49 return QObject::tr( "Vector geometry" );
50}
51
52QString QgsSimplifyAlgorithm::groupId() const
53{
54 return u"vectorgeometry"_s;
55}
56
57QString QgsSimplifyAlgorithm::outputName() const
58{
59 return QObject::tr( "Simplified" );
60}
61
62QString QgsSimplifyAlgorithm::shortHelpString() const
63{
64 return QObject::tr(
65 "This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer "
66 "with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.\n\n"
67 "The algorithm gives a choice of simplification methods, including distance based "
68 "(the \"Douglas-Peucker\" algorithm), area based (\"Visvalingam\" algorithm) and snapping geometries to a grid."
69 );
70}
71
72QString QgsSimplifyAlgorithm::shortDescription() const
73{
74 return QObject::tr( "Simplifies the geometries in a line or polygon layer by removing a number of vertices." );
75}
76
77QgsSimplifyAlgorithm *QgsSimplifyAlgorithm::createInstance() const
78{
79 return new QgsSimplifyAlgorithm();
80}
81
82QList<int> QgsSimplifyAlgorithm::inputLayerTypes() const
83{
84 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
85}
86
87void QgsSimplifyAlgorithm::initParameters( const QVariantMap & )
88{
89 QStringList methods;
90 methods << QObject::tr( "Distance (Douglas-Peucker)" ) << QObject::tr( "Snap to grid" ) << QObject::tr( "Area (Visvalingam)" );
91
92 addParameter( new QgsProcessingParameterEnum( u"METHOD"_s, QObject::tr( "Simplification method" ), methods, false, 0 ) );
93 auto tolerance = std::make_unique<QgsProcessingParameterDistance>( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), 1.0, u"INPUT"_s, false, 0, 10000000.0 );
94 tolerance->setIsDynamic( true );
95 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Tolerance"_s, QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
96 tolerance->setDynamicLayerParameterName( u"INPUT"_s );
97 addParameter( tolerance.release() );
98}
99
100bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mTolerance = parameterAsDouble( parameters, u"TOLERANCE"_s, context );
103 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"TOLERANCE"_s );
104 if ( mDynamicTolerance )
105 mToleranceProperty = parameters.value( u"TOLERANCE"_s ).value<QgsProperty>();
106
107 mMethod = static_cast<Qgis::VectorSimplificationAlgorithm>( parameterAsEnum( parameters, u"METHOD"_s, context ) );
109 mSimplifier = std::make_unique<QgsMapToPixelSimplifier>( QgsMapToPixelSimplifier::SimplifyGeometry, mTolerance, mMethod );
110
111 return true;
112}
113
114QgsFeatureList QgsSimplifyAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
115{
116 QgsFeature f = feature;
117 if ( f.hasGeometry() )
118 {
119 const QgsGeometry inputGeometry = f.geometry();
120 QgsGeometry outputGeometry;
122 {
123 double tolerance = mTolerance;
124 if ( mDynamicTolerance )
125 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
126 outputGeometry = inputGeometry.simplify( tolerance );
127 }
128 else
129 {
130 if ( !mDynamicTolerance )
131 {
132 outputGeometry = mSimplifier->simplify( inputGeometry );
133 }
134 else
135 {
136 const double tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), mTolerance );
137 const QgsMapToPixelSimplifier simplifier( QgsMapToPixelSimplifier::SimplifyGeometry, tolerance, mMethod );
138 outputGeometry = simplifier.simplify( inputGeometry );
139 }
140 }
141 f.setGeometry( outputGeometry );
142 }
143 return QgsFeatureList() << f;
144}
145
146Qgis::ProcessingFeatureSourceFlags QgsSimplifyAlgorithm::sourceFlags() const
147{
149}
150
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ VectorLine
Vector line layers.
Definition qgis.h:3649
VectorSimplificationAlgorithm
Simplification algorithms for vector features.
Definition qgis.h:3114
@ Distance
The simplification uses the distance between points to remove duplicate points.
Definition qgis.h:3115
@ 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
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
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 simplify(double tolerance) const
Returns a simplified version of this geometry using a specified tolerance value.
Implementation of a geometry simplifier using the "MapToPixel" algorithm.
@ SimplifyGeometry
The geometries can be simplified using the current map2pixel context state.
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.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
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
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:57
A store for object properties.
QList< QgsFeature > QgsFeatureList