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