QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgsalgorithmsegmentize.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmsegmentize.cpp
3  ---------------------
4  begin : March 2018
5  copyright : (C) 2018 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 "qgsalgorithmsegmentize.h"
19 
21 
22 QString QgsSegmentizeByMaximumDistanceAlgorithm::name() const
23 {
24  return QStringLiteral( "segmentizebymaxdistance" );
25 }
26 
27 QString QgsSegmentizeByMaximumDistanceAlgorithm::displayName() const
28 {
29  return QObject::tr( "Segmentize by maximum distance" );
30 }
31 
32 QStringList QgsSegmentizeByMaximumDistanceAlgorithm::tags() const
33 {
34  return QObject::tr( "straighten,linearize,densify,curves,curved,circular" ).split( ',' );
35 }
36 
37 QString QgsSegmentizeByMaximumDistanceAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsSegmentizeByMaximumDistanceAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsSegmentizeByMaximumDistanceAlgorithm::outputName() const
48 {
49  return QObject::tr( "Segmentized" );
50 }
51 
52 QString QgsSegmentizeByMaximumDistanceAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm segmentizes a geometry by converting curved sections to linear sections.\n\n"
55  "The segmentization is performed by specifying the maximum allowed offset distance between the original"
56  "curve and the segmentized representation.\n\n"
57  "Non-curved geometries will be retained without change." );
58 }
59 
60 QgsSegmentizeByMaximumDistanceAlgorithm *QgsSegmentizeByMaximumDistanceAlgorithm::createInstance() const
61 {
62  return new QgsSegmentizeByMaximumDistanceAlgorithm();
63 }
64 
65 QList<int> QgsSegmentizeByMaximumDistanceAlgorithm::inputLayerTypes() const
66 {
68 }
69 
70 void QgsSegmentizeByMaximumDistanceAlgorithm::initParameters( const QVariantMap & )
71 {
72  std::unique_ptr< QgsProcessingParameterDistance > tolerance = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ),
73  QObject::tr( "Maximum offset distance" ),
74  1.0, QStringLiteral( "INPUT" ), false, 0, 10000000.0 );
75  tolerance->setIsDynamic( true );
76  tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum offset distance" ), QgsPropertyDefinition::DoublePositive ) );
77  tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
78  addParameter( tolerance.release() );
79 }
80 
81 bool QgsSegmentizeByMaximumDistanceAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
82 {
83  Q_UNUSED( layer )
84  return false;
85 }
86 
87 bool QgsSegmentizeByMaximumDistanceAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
88 {
89  mTolerance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
90  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
91  if ( mDynamicTolerance )
92  mToleranceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
93 
94  return true;
95 }
96 
97 QgsFeatureList QgsSegmentizeByMaximumDistanceAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
98 {
99  QgsFeature f = feature;
100  if ( f.hasGeometry() )
101  {
102  QgsGeometry geometry = f.geometry();
103  double tolerance = mTolerance;
104  if ( mDynamicTolerance )
105  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
107  f.setGeometry( geometry );
108  }
109  return QgsFeatureList() << f;
110 }
111 
112 
113 
114 
115 
116 QString QgsSegmentizeByMaximumAngleAlgorithm::name() const
117 {
118  return QStringLiteral( "segmentizebymaxangle" );
119 }
120 
121 QString QgsSegmentizeByMaximumAngleAlgorithm::displayName() const
122 {
123  return QObject::tr( "Segmentize by maximum angle" );
124 }
125 
126 QStringList QgsSegmentizeByMaximumAngleAlgorithm::tags() const
127 {
128  return QObject::tr( "straighten,linearize,densify,curves,curved,circular,angle" ).split( ',' );
129 }
130 
131 QString QgsSegmentizeByMaximumAngleAlgorithm::group() const
132 {
133  return QObject::tr( "Vector geometry" );
134 }
135 
136 QString QgsSegmentizeByMaximumAngleAlgorithm::groupId() const
137 {
138  return QStringLiteral( "vectorgeometry" );
139 }
140 
141 QString QgsSegmentizeByMaximumAngleAlgorithm::outputName() const
142 {
143  return QObject::tr( "Segmentized" );
144 }
145 
146 QString QgsSegmentizeByMaximumAngleAlgorithm::shortHelpString() const
147 {
148  return QObject::tr( "This algorithm segmentizes a geometry by converting curved sections to linear sections.\n\n"
149  "The segmentization is performed by specifying the maximum allowed radius angle between vertices "
150  "on the straightened geometry (e.g the angle of the arc created from the original arc center to consecutive "
151  "output vertices on the linearized geometry).\n\n"
152  "Non-curved geometries will be retained without change." );
153 }
154 
155 QgsSegmentizeByMaximumAngleAlgorithm *QgsSegmentizeByMaximumAngleAlgorithm::createInstance() const
156 {
157  return new QgsSegmentizeByMaximumAngleAlgorithm();
158 }
159 
160 QList<int> QgsSegmentizeByMaximumAngleAlgorithm::inputLayerTypes() const
161 {
163 }
164 
165 void QgsSegmentizeByMaximumAngleAlgorithm::initParameters( const QVariantMap & )
166 {
167  std::unique_ptr< QgsProcessingParameterNumber > tolerance = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "ANGLE" ),
168  QObject::tr( "Maximum angle between vertices (degrees)" ), QgsProcessingParameterNumber::Double,
169  5.0, false, 0, 360.0 );
170  tolerance->setIsDynamic( true );
171  tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ANGLE" ), QObject::tr( "Maximum angle between vertices (degrees)" ), QgsPropertyDefinition::DoublePositive ) );
172  tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
173  addParameter( tolerance.release() );
174 }
175 
176 bool QgsSegmentizeByMaximumAngleAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
177 {
178  Q_UNUSED( layer )
179  return false;
180 }
181 
182 bool QgsSegmentizeByMaximumAngleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
183 {
184  mTolerance = parameterAsDouble( parameters, QStringLiteral( "ANGLE" ), context );
185  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE" ) );
186  if ( mDynamicTolerance )
187  mToleranceProperty = parameters.value( QStringLiteral( "ANGLE" ) ).value< QgsProperty >();
188 
189  return true;
190 }
191 
192 QgsFeatureList QgsSegmentizeByMaximumAngleAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
193 {
194  QgsFeature f = feature;
195  if ( f.hasGeometry() )
196  {
197  QgsGeometry geometry = f.geometry();
198  double tolerance = mTolerance;
199  if ( mDynamicTolerance )
200  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
201  geometry.convertToStraightSegment( M_PI * tolerance / 180.0, QgsAbstractGeometry::MaximumAngle );
202  f.setGeometry( geometry );
203  }
204  return QgsFeatureList() << f;
205 }
206 
208 
209 
@ MaximumDifference
Maximum distance between an arbitrary point on the original curve and closest point on its approximat...
@ MaximumAngle
Maximum angle between generating radii (lines from arc center to output vertices)
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:223
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:163
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
void convertToStraightSegment(double tolerance=M_PI/180., QgsAbstractGeometry::SegmentationToleranceType toleranceType=QgsAbstractGeometry::MaximumAngle)
Converts the geometry to straight line segments, if it is a curved geometry type.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
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.
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...
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:50
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
Definition for a property.
Definition: qgsproperty.h:47
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:58
A store for object properties.
Definition: qgsproperty.h:231
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882