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