QGIS API Documentation 3.41.0-Master (3440c17df1d)
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" ),
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
81bool QgsSegmentizeByMaximumDistanceAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
82{
83 Q_UNUSED( layer )
84 return false;
85}
86
87bool 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
97QgsFeatureList 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
116QString QgsSegmentizeByMaximumAngleAlgorithm::name() const
117{
118 return QStringLiteral( "segmentizebymaxangle" );
119}
120
121QString QgsSegmentizeByMaximumAngleAlgorithm::displayName() const
122{
123 return QObject::tr( "Segmentize by maximum angle" );
124}
125
126QStringList QgsSegmentizeByMaximumAngleAlgorithm::tags() const
127{
128 return QObject::tr( "straighten,linearize,densify,curves,curved,circular,angle" ).split( ',' );
129}
130
131QString QgsSegmentizeByMaximumAngleAlgorithm::group() const
132{
133 return QObject::tr( "Vector geometry" );
134}
135
136QString QgsSegmentizeByMaximumAngleAlgorithm::groupId() const
137{
138 return QStringLiteral( "vectorgeometry" );
139}
140
141QString QgsSegmentizeByMaximumAngleAlgorithm::outputName() const
142{
143 return QObject::tr( "Segmentized" );
144}
145
146QString 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
155QgsSegmentizeByMaximumAngleAlgorithm *QgsSegmentizeByMaximumAngleAlgorithm::createInstance() const
156{
157 return new QgsSegmentizeByMaximumAngleAlgorithm();
158}
159
160QList<int> QgsSegmentizeByMaximumAngleAlgorithm::inputLayerTypes() const
161{
162 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine ) << static_cast< int >( Qgis::ProcessingSourceType::VectorPolygon );
163}
164
165void 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)" ), Qgis::ProcessingNumberParameterType::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
176bool QgsSegmentizeByMaximumAngleAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
177{
178 Q_UNUSED( layer )
179 return false;
180}
181
182bool 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
192QgsFeatureList 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
@ 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