QGIS API Documentation  3.0.2-Girona (307d082)
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  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "TOLERANCE" ),
82  QObject::tr( "Tolerance" ), QgsProcessingParameterNumber::Double,
83  1.0, false, 0, 10000000.0 ) );
84 }
85 
86 bool QgsSimplifyAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
87 {
88  mTolerance = parameterAsDouble( parameters, QStringLiteral( "TOLERANCE" ), context );
89  mMethod = static_cast< QgsMapToPixelSimplifier::SimplifyAlgorithm >( parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context ) );
90  if ( mMethod != QgsMapToPixelSimplifier::Distance )
91  mSimplifier.reset( new QgsMapToPixelSimplifier( QgsMapToPixelSimplifier::SimplifyGeometry, mTolerance, mMethod ) );
92 
93  return true;
94 }
95 
96 QgsFeatureList QgsSimplifyAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
97 {
98  QgsFeature f = feature;
99  if ( f.hasGeometry() )
100  {
101  QgsGeometry inputGeometry = f.geometry();
102  QgsGeometry outputGeometry;
103  if ( mMethod == QgsMapToPixelSimplifier::Distance )
104  {
105  outputGeometry = inputGeometry.simplify( mTolerance );
106  }
107  else
108  {
109  outputGeometry = mSimplifier->simplify( inputGeometry );
110  }
111  f.setGeometry( outputGeometry );
112  }
113  return QgsFeatureList() << f;
114 }
115 
117 
118 
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
SimplifyAlgorithm
Types of simplification algorithms that can be used.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
An enum based parameter for processing algorithms, allowing for selection from predefined values...
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
Implementation of GeometrySimplifier using the "MapToPixel" algorithm.
A numeric parameter for processing algorithms.
QgsGeometry simplify(double tolerance) const
Returns a simplified version of this geometry using a specified tolerance value.
Vector line layers.
Definition: qgsprocessing.h:50
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
The geometries can be simplified using the current map2pixel context state.
Contains information about the context in which a processing algorithm is executed.
The simplification uses the distance between points to remove duplicate points.