QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
25
26QgsSimplifyAlgorithm::~QgsSimplifyAlgorithm() = default;
27
28QString QgsSimplifyAlgorithm::name() const
29{
30 return QStringLiteral( "simplifygeometries" );
31}
32
33QString QgsSimplifyAlgorithm::displayName() const
34{
35 return QObject::tr( "Simplify" );
36}
37
38QStringList QgsSimplifyAlgorithm::tags() const
39{
40 return QObject::tr( "simplify,generalize,douglas,peucker,visvalingam" ).split( ',' );
41}
42
43QString QgsSimplifyAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsSimplifyAlgorithm::groupId() const
49{
50 return QStringLiteral( "vectorgeometry" );
51}
52
53QString QgsSimplifyAlgorithm::outputName() const
54{
55 return QObject::tr( "Simplified" );
56}
57
58QString QgsSimplifyAlgorithm::shortHelpString() const
59{
60 return QObject::tr( "This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer "
61 "with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.\n\n"
62 "The algorithm gives a choice of simplification methods, including distance based "
63 "(the \"Douglas-Peucker\" algorithm), area based (\"Visvalingam\" algorithm) and snapping geometries to a grid." );
64}
65
66QString QgsSimplifyAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Simplifies the geometries in a line or polygon layer by removing a number of vertices." );
69}
70
71QgsSimplifyAlgorithm *QgsSimplifyAlgorithm::createInstance() const
72{
73 return new QgsSimplifyAlgorithm();
74}
75
76QList<int> QgsSimplifyAlgorithm::inputLayerTypes() const
77{
78 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
79}
80
81void QgsSimplifyAlgorithm::initParameters( const QVariantMap & )
82{
83 QStringList methods;
84 methods << QObject::tr( "Distance (Douglas-Peucker)" )
85 << QObject::tr( "Snap to grid" )
86 << QObject::tr( "Area (Visvalingam)" );
87
88 addParameter( new QgsProcessingParameterEnum(
89 QStringLiteral( "METHOD" ),
90 QObject::tr( "Simplification method" ),
91 methods, false, 0
92 ) );
93 auto tolerance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "TOLERANCE" ), QObject::tr( "Tolerance" ), 1.0, QStringLiteral( "INPUT" ), false, 0, 10000000.0 );
94 tolerance->setIsDynamic( true );
95 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Tolerance" ), QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
96 tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
97 addParameter( tolerance.release() );
98}
99
100bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
103 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "TOLERANCE" ) );
104 if ( mDynamicTolerance )
105 mToleranceProperty = parameters.value( QStringLiteral( "TOLERANCE" ) ).value<QgsProperty>();
106
107 mMethod = static_cast<Qgis::VectorSimplificationAlgorithm>( parameterAsEnum( parameters, QStringLiteral( "METHOD" ), 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:3536
@ VectorLine
Vector line layers.
Definition qgis.h:3535
VectorSimplificationAlgorithm
Simplification algorithms for vector features.
Definition qgis.h:3013
@ Distance
The simplification uses the distance between points to remove duplicate points.
Definition qgis.h:3014
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
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