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