QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
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
19
21
22QString QgsSegmentizeByMaximumDistanceAlgorithm::name() const
23{
24 return QStringLiteral( "segmentizebymaxdistance" );
25}
26
27QString QgsSegmentizeByMaximumDistanceAlgorithm::displayName() const
28{
29 return QObject::tr( "Segmentize by maximum distance" );
30}
31
32QStringList QgsSegmentizeByMaximumDistanceAlgorithm::tags() const
33{
34 return QObject::tr( "straighten,linearize,densify,curves,curved,circular" ).split( ',' );
35}
36
37QString QgsSegmentizeByMaximumDistanceAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsSegmentizeByMaximumDistanceAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsSegmentizeByMaximumDistanceAlgorithm::outputName() const
48{
49 return QObject::tr( "Segmentized" );
50}
51
52QString 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
60QString QgsSegmentizeByMaximumDistanceAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Segmentizes a geometry by converting curved sections to linear sections, "
63 "given the maximum allowed offset distance between the original curve and the segmentized representation." );
64}
65
66QgsSegmentizeByMaximumDistanceAlgorithm *QgsSegmentizeByMaximumDistanceAlgorithm::createInstance() const
67{
68 return new QgsSegmentizeByMaximumDistanceAlgorithm();
69}
70
71QList<int> QgsSegmentizeByMaximumDistanceAlgorithm::inputLayerTypes() const
72{
73 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
74}
75
76void QgsSegmentizeByMaximumDistanceAlgorithm::initParameters( const QVariantMap & )
77{
78 auto tolerance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum offset distance" ), 1.0, QStringLiteral( "INPUT" ), false, 0, 10000000.0 );
79 tolerance->setIsDynamic( true );
80 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum offset distance" ), QgsPropertyDefinition::DoublePositive ) );
81 tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
82 addParameter( tolerance.release() );
83}
84
85bool QgsSegmentizeByMaximumDistanceAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
86{
87 Q_UNUSED( layer )
88 return false;
89}
90
91bool QgsSegmentizeByMaximumDistanceAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
92{
93 mTolerance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
94 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
95 if ( mDynamicTolerance )
96 mToleranceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value<QgsProperty>();
97
98 return true;
99}
100
101QgsFeatureList QgsSegmentizeByMaximumDistanceAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
102{
103 QgsFeature f = feature;
104 if ( f.hasGeometry() )
105 {
106 QgsGeometry geometry = f.geometry();
107 double tolerance = mTolerance;
108 if ( mDynamicTolerance )
109 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
111 f.setGeometry( geometry );
112 }
113 return QgsFeatureList() << f;
114}
115
116
117QString QgsSegmentizeByMaximumAngleAlgorithm::name() const
118{
119 return QStringLiteral( "segmentizebymaxangle" );
120}
121
122QString QgsSegmentizeByMaximumAngleAlgorithm::displayName() const
123{
124 return QObject::tr( "Segmentize by maximum angle" );
125}
126
127QStringList QgsSegmentizeByMaximumAngleAlgorithm::tags() const
128{
129 return QObject::tr( "straighten,linearize,densify,curves,curved,circular,angle" ).split( ',' );
130}
131
132QString QgsSegmentizeByMaximumAngleAlgorithm::group() const
133{
134 return QObject::tr( "Vector geometry" );
135}
136
137QString QgsSegmentizeByMaximumAngleAlgorithm::groupId() const
138{
139 return QStringLiteral( "vectorgeometry" );
140}
141
142QString QgsSegmentizeByMaximumAngleAlgorithm::outputName() const
143{
144 return QObject::tr( "Segmentized" );
145}
146
147QString QgsSegmentizeByMaximumAngleAlgorithm::shortHelpString() const
148{
149 return QObject::tr( "This algorithm segmentizes a geometry by converting curved sections to linear sections.\n\n"
150 "The segmentization is performed by specifying the maximum allowed radius angle between vertices "
151 "on the straightened geometry (e.g the angle of the arc created from the original arc center to consecutive "
152 "output vertices on the linearized geometry).\n\n"
153 "Non-curved geometries will be retained without change." );
154}
155
156QString QgsSegmentizeByMaximumAngleAlgorithm::shortDescription() const
157{
158 return QObject::tr( "Segmentizes a geometry by converting curved sections to linear sections, "
159 "given the maximum allowed radius angle between vertices on the straightened geometry." );
160}
161
162QgsSegmentizeByMaximumAngleAlgorithm *QgsSegmentizeByMaximumAngleAlgorithm::createInstance() const
163{
164 return new QgsSegmentizeByMaximumAngleAlgorithm();
165}
166
167QList<int> QgsSegmentizeByMaximumAngleAlgorithm::inputLayerTypes() const
168{
169 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
170}
171
172void QgsSegmentizeByMaximumAngleAlgorithm::initParameters( const QVariantMap & )
173{
174 auto tolerance = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "ANGLE" ), QObject::tr( "Maximum angle between vertices (degrees)" ), Qgis::ProcessingNumberParameterType::Double, 5.0, false, 0, 360.0 );
175 tolerance->setIsDynamic( true );
176 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ANGLE" ), QObject::tr( "Maximum angle between vertices (degrees)" ), QgsPropertyDefinition::DoublePositive ) );
177 tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
178 addParameter( tolerance.release() );
179}
180
181bool QgsSegmentizeByMaximumAngleAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
182{
183 Q_UNUSED( layer )
184 return false;
185}
186
187bool QgsSegmentizeByMaximumAngleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
188{
189 mTolerance = parameterAsDouble( parameters, QStringLiteral( "ANGLE" ), context );
190 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE" ) );
191 if ( mDynamicTolerance )
192 mToleranceProperty = parameters.value( QStringLiteral( "ANGLE" ) ).value<QgsProperty>();
193
194 return true;
195}
196
197QgsFeatureList QgsSegmentizeByMaximumAngleAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
198{
199 QgsFeature f = feature;
200 if ( f.hasGeometry() )
201 {
202 QgsGeometry geometry = f.geometry();
203 double tolerance = mTolerance;
204 if ( mDynamicTolerance )
205 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
206 geometry.convertToStraightSegment( M_PI * tolerance / 180.0, QgsAbstractGeometry::MaximumAngle );
207 f.setGeometry( geometry );
208 }
209 return QgsFeatureList() << f;
210}
211
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ 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: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.
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:77
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...
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