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