QGIS API Documentation 3.37.0-Master (08041ddf763)
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
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
63QgsSimplifyAlgorithm *QgsSimplifyAlgorithm::createInstance() const
64{
65 return new QgsSimplifyAlgorithm();
66}
67
68QList<int> QgsSimplifyAlgorithm::inputLayerTypes() const
69{
70 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine ) << static_cast< int >( Qgis::ProcessingSourceType::VectorPolygon );
71}
72
73void QgsSimplifyAlgorithm::initParameters( const QVariantMap & )
74{
75 QStringList methods;
76 methods << QObject::tr( "Distance (Douglas-Peucker)" )
77 << QObject::tr( "Snap to grid" )
78 << QObject::tr( "Area (Visvalingam)" );
79
80 addParameter( new QgsProcessingParameterEnum(
81 QStringLiteral( "METHOD" ),
82 QObject::tr( "Simplification method" ),
83 methods, false, 0 ) );
84 std::unique_ptr< QgsProcessingParameterDistance > tolerance = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "TOLERANCE" ),
85 QObject::tr( "Tolerance" ), 1.0, QStringLiteral( "INPUT" ), false, 0, 10000000.0 );
86 tolerance->setIsDynamic( true );
87 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Tolerance" ), QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
88 tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
89 addParameter( tolerance.release() );
90}
91
92bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
93{
94 mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
95 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "TOLERANCE" ) );
96 if ( mDynamicTolerance )
97 mToleranceProperty = parameters.value( QStringLiteral( "TOLERANCE" ) ).value< QgsProperty >();
98
99 mMethod = static_cast< Qgis::VectorSimplificationAlgorithm >( parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context ) );
101 mSimplifier.reset( new QgsMapToPixelSimplifier( QgsMapToPixelSimplifier::SimplifyGeometry, mTolerance, mMethod ) );
102
103 return true;
104}
105
106QgsFeatureList QgsSimplifyAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
107{
108 QgsFeature f = feature;
109 if ( f.hasGeometry() )
110 {
111 const QgsGeometry inputGeometry = f.geometry();
112 QgsGeometry outputGeometry;
114 {
115 double tolerance = mTolerance;
116 if ( mDynamicTolerance )
117 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
118 outputGeometry = inputGeometry.simplify( tolerance );
119 }
120 else
121 {
122 if ( !mDynamicTolerance )
123 {
124 outputGeometry = mSimplifier->simplify( inputGeometry );
125 }
126 else
127 {
128 const double tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), mTolerance );
129 const QgsMapToPixelSimplifier simplifier( QgsMapToPixelSimplifier::SimplifyGeometry, tolerance, mMethod );
130 outputGeometry = simplifier.simplify( inputGeometry );
131 }
132 }
133 f.setGeometry( outputGeometry );
134 }
135 return QgsFeatureList() << f;
136}
137
138Qgis::ProcessingFeatureSourceFlags QgsSimplifyAlgorithm::sourceFlags() const
139{
141}
142
144
145
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
VectorSimplificationAlgorithm
Simplification algorithms for vector features.
Definition qgis.h:2561
@ 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:3148
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 GeometrySimplifier 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