QGIS API Documentation 3.99.0-Master (357b655ed83)
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( "This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer "
65 "with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.\n\n"
66 "The algorithm gives a choice of simplification methods, including distance based "
67 "(the \"Douglas-Peucker\" algorithm), area based (\"Visvalingam\" algorithm) and snapping geometries to a grid." );
68}
69
70QString QgsSimplifyAlgorithm::shortDescription() const
71{
72 return QObject::tr( "Simplifies the geometries in a line or polygon layer by removing a number of vertices." );
73}
74
75QgsSimplifyAlgorithm *QgsSimplifyAlgorithm::createInstance() const
76{
77 return new QgsSimplifyAlgorithm();
78}
79
80QList<int> QgsSimplifyAlgorithm::inputLayerTypes() const
81{
82 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
83}
84
85void QgsSimplifyAlgorithm::initParameters( const QVariantMap & )
86{
87 QStringList methods;
88 methods << QObject::tr( "Distance (Douglas-Peucker)" )
89 << QObject::tr( "Snap to grid" )
90 << QObject::tr( "Area (Visvalingam)" );
91
92 addParameter( new QgsProcessingParameterEnum(
93 u"METHOD"_s,
94 QObject::tr( "Simplification method" ),
95 methods, false, 0
96 ) );
97 auto tolerance = std::make_unique<QgsProcessingParameterDistance>( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), 1.0, u"INPUT"_s, false, 0, 10000000.0 );
98 tolerance->setIsDynamic( true );
99 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Tolerance"_s, QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
100 tolerance->setDynamicLayerParameterName( u"INPUT"_s );
101 addParameter( tolerance.release() );
102}
103
104bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
105{
106 mTolerance = parameterAsDouble( parameters, u"TOLERANCE"_s, context );
107 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"TOLERANCE"_s );
108 if ( mDynamicTolerance )
109 mToleranceProperty = parameters.value( u"TOLERANCE"_s ).value<QgsProperty>();
110
111 mMethod = static_cast<Qgis::VectorSimplificationAlgorithm>( parameterAsEnum( parameters, u"METHOD"_s, context ) );
113 mSimplifier = std::make_unique<QgsMapToPixelSimplifier>( QgsMapToPixelSimplifier::SimplifyGeometry, mTolerance, mMethod );
114
115 return true;
116}
117
118QgsFeatureList QgsSimplifyAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
119{
120 QgsFeature f = feature;
121 if ( f.hasGeometry() )
122 {
123 const QgsGeometry inputGeometry = f.geometry();
124 QgsGeometry outputGeometry;
126 {
127 double tolerance = mTolerance;
128 if ( mDynamicTolerance )
129 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
130 outputGeometry = inputGeometry.simplify( tolerance );
131 }
132 else
133 {
134 if ( !mDynamicTolerance )
135 {
136 outputGeometry = mSimplifier->simplify( inputGeometry );
137 }
138 else
139 {
140 const double tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), mTolerance );
141 const QgsMapToPixelSimplifier simplifier( QgsMapToPixelSimplifier::SimplifyGeometry, tolerance, mMethod );
142 outputGeometry = simplifier.simplify( inputGeometry );
143 }
144 }
145 f.setGeometry( outputGeometry );
146 }
147 return QgsFeatureList() << f;
148}
149
150Qgis::ProcessingFeatureSourceFlags QgsSimplifyAlgorithm::sourceFlags() const
151{
153}
154
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
VectorSimplificationAlgorithm
Simplification algorithms for vector features.
Definition qgis.h:3071
@ Distance
The simplification uses the distance between points to remove duplicate points.
Definition qgis.h:3072
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
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:58
A store for object properties.
QList< QgsFeature > QgsFeatureList