QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
18 #include "qgsalgorithmsimplify.h"
19 
21 
22 QString QgsSimplifyAlgorithm::name() const
23 {
24  return QStringLiteral( "simplifygeometries" );
25 }
26 
27 QString QgsSimplifyAlgorithm::displayName() const
28 {
29  return QObject::tr( "Simplify" );
30 }
31 
32 QStringList QgsSimplifyAlgorithm::tags() const
33 {
34  return QObject::tr( "simplify,generalize,douglas,peucker,visvalingam" ).split( ',' );
35 }
36 
37 QString QgsSimplifyAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsSimplifyAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsSimplifyAlgorithm::outputName() const
48 {
49  return QObject::tr( "Simplified" );
50 }
51 
52 QString QgsSimplifyAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm simplifies the geometries in a line or polygon layer. It creates a new layer "
55  "with the same features as the ones in the input layer, but with geometries containing a lower number of vertices.\n\n"
56  "The algorithm gives a choice of simplification methods, including distance based "
57  "(the \"Douglas-Peucker\" algorithm), area based (\"Visvalingam\" algorithm) and snapping geometries to a grid." );
58 }
59 
60 QgsSimplifyAlgorithm *QgsSimplifyAlgorithm::createInstance() const
61 {
62  return new QgsSimplifyAlgorithm();
63 }
64 
65 QList<int> QgsSimplifyAlgorithm::inputLayerTypes() const
66 {
68 }
69 
70 void QgsSimplifyAlgorithm::initParameters( const QVariantMap & )
71 {
72  QStringList methods;
73  methods << QObject::tr( "Distance (Douglas-Peucker)" )
74  << QObject::tr( "Snap to grid" )
75  << QObject::tr( "Area (Visvalingam)" );
76 
77  addParameter( new QgsProcessingParameterEnum(
78  QStringLiteral( "METHOD" ),
79  QObject::tr( "Simplification method" ),
80  methods, false, 0 ) );
81  std::unique_ptr< QgsProcessingParameterDistance > tolerance = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "TOLERANCE" ),
82  QObject::tr( "Tolerance" ), 1.0, QStringLiteral( "INPUT" ), false, 0, 10000000.0 );
83  tolerance->setIsDynamic( true );
84  tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Tolerance" ), QObject::tr( "Tolerance distance" ), QgsPropertyDefinition::DoublePositive ) );
85  tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
86  addParameter( tolerance.release() );
87 }
88 
89 bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
90 {
91  mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
92  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "TOLERANCE" ) );
93  if ( mDynamicTolerance )
94  mToleranceProperty = parameters.value( QStringLiteral( "TOLERANCE" ) ).value< QgsProperty >();
95 
96  mMethod = static_cast< QgsMapToPixelSimplifier::SimplifyAlgorithm >( parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context ) );
97  if ( mMethod != QgsMapToPixelSimplifier::Distance )
98  mSimplifier.reset( new QgsMapToPixelSimplifier( QgsMapToPixelSimplifier::SimplifyGeometry, mTolerance, mMethod ) );
99 
100  return true;
101 }
102 
103 QgsFeatureList QgsSimplifyAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
104 {
105  QgsFeature f = feature;
106  if ( f.hasGeometry() )
107  {
108  const QgsGeometry inputGeometry = f.geometry();
109  QgsGeometry outputGeometry;
110  if ( mMethod == QgsMapToPixelSimplifier::Distance )
111  {
112  double tolerance = mTolerance;
113  if ( mDynamicTolerance )
114  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
115  outputGeometry = inputGeometry.simplify( tolerance );
116  }
117  else
118  {
119  if ( !mDynamicTolerance )
120  {
121  outputGeometry = mSimplifier->simplify( inputGeometry );
122  }
123  else
124  {
125  const double tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), mTolerance );
126  const QgsMapToPixelSimplifier simplifier( QgsMapToPixelSimplifier::SimplifyGeometry, tolerance, mMethod );
127  outputGeometry = simplifier.simplify( inputGeometry );
128  }
129  }
130  f.setGeometry( outputGeometry );
131  }
132  return QgsFeatureList() << f;
133 }
134 
135 QgsProcessingFeatureSource::Flag QgsSimplifyAlgorithm::sourceFlags() const
136 {
138 }
139 
141 
142 
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:114
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:50
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsMapToPixelSimplifier::Distance
@ Distance
The simplification uses the distance between points to remove duplicate points.
Definition: qgsmaptopixelgeometrysimplifier.h:44
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsMapToPixelSimplifier::SimplifyAlgorithm
SimplifyAlgorithm
Types of simplification algorithms that can be used.
Definition: qgsmaptopixelgeometrysimplifier.h:42
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
qgsalgorithmsimplify.h
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
QgsMapToPixelSimplifier::SimplifyGeometry
@ SimplifyGeometry
The geometries can be simplified using the current map2pixel context state.
Definition: qgsmaptopixelgeometrysimplifier.h:57
QgsGeometry::simplify
QgsGeometry simplify(double tolerance) const
Returns a simplified version of this geometry using a specified tolerance value.
Definition: qgsgeometry.cpp:2244
QgsMapToPixelSimplifier
Implementation of GeometrySimplifier using the "MapToPixel" algorithm.
Definition: qgsmaptopixelgeometrysimplifier.h:38
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:582
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:149
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsProcessingParameterEnum
An enum based parameter for processing algorithms, allowing for selection from predefined values.
Definition: qgsprocessingparameters.h:2540
QgsPropertyDefinition::DoublePositive
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:58