QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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 QgsFeature f = feature;
112 if ( f.hasGeometry() )
113 {
114 QgsGeometry geometry = f.geometry();
115 double tolerance = mTolerance;
116 if ( mDynamicTolerance )
117 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
119 f.setGeometry( geometry );
120 }
121 return QgsFeatureList() << f;
122}
123
124
125QString QgsSegmentizeByMaximumAngleAlgorithm::name() const
126{
127 return u"segmentizebymaxangle"_s;
128}
129
130QString QgsSegmentizeByMaximumAngleAlgorithm::displayName() const
131{
132 return QObject::tr( "Segmentize by maximum angle" );
133}
134
135QStringList QgsSegmentizeByMaximumAngleAlgorithm::tags() const
136{
137 return QObject::tr( "straighten,linearize,densify,curves,curved,circular,angle" ).split( ',' );
138}
139
140QString QgsSegmentizeByMaximumAngleAlgorithm::group() const
141{
142 return QObject::tr( "Vector geometry" );
143}
144
145QString QgsSegmentizeByMaximumAngleAlgorithm::groupId() const
146{
147 return u"vectorgeometry"_s;
148}
149
150QString QgsSegmentizeByMaximumAngleAlgorithm::outputName() const
151{
152 return QObject::tr( "Segmentized" );
153}
154
155QString QgsSegmentizeByMaximumAngleAlgorithm::shortHelpString() const
156{
157 return QObject::tr(
158 "This algorithm segmentizes a geometry by converting curved sections to linear sections.\n\n"
159 "The segmentization is performed by specifying the maximum allowed radius angle between vertices "
160 "on the straightened geometry (e.g the angle of the arc created from the original arc center to consecutive "
161 "output vertices on the linearized geometry).\n\n"
162 "Non-curved geometries will be retained without change."
163 );
164}
165
166QString QgsSegmentizeByMaximumAngleAlgorithm::shortDescription() const
167{
168 return QObject::tr(
169 "Segmentizes a geometry by converting curved sections to linear sections, "
170 "given the maximum allowed radius angle between vertices on the straightened geometry."
171 );
172}
173
174QgsSegmentizeByMaximumAngleAlgorithm *QgsSegmentizeByMaximumAngleAlgorithm::createInstance() const
175{
176 return new QgsSegmentizeByMaximumAngleAlgorithm();
177}
178
179QList<int> QgsSegmentizeByMaximumAngleAlgorithm::inputLayerTypes() const
180{
181 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
182}
183
184void QgsSegmentizeByMaximumAngleAlgorithm::initParameters( const QVariantMap & )
185{
186 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 );
187 tolerance->setIsDynamic( true );
188 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ANGLE"_s, QObject::tr( "Maximum angle between vertices (degrees)" ), QgsPropertyDefinition::DoublePositive ) );
189 tolerance->setDynamicLayerParameterName( u"INPUT"_s );
190 addParameter( tolerance.release() );
191}
192
193bool QgsSegmentizeByMaximumAngleAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
194{
195 Q_UNUSED( layer )
196 return false;
197}
198
199bool QgsSegmentizeByMaximumAngleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
200{
201 mTolerance = parameterAsDouble( parameters, u"ANGLE"_s, context );
202 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"ANGLE"_s );
203 if ( mDynamicTolerance )
204 mToleranceProperty = parameters.value( u"ANGLE"_s ).value<QgsProperty>();
205
206 return true;
207}
208
209QgsFeatureList QgsSegmentizeByMaximumAngleAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
210{
211 QgsFeature f = feature;
212 if ( f.hasGeometry() )
213 {
214 QgsGeometry geometry = f.geometry();
215 double tolerance = mTolerance;
216 if ( mDynamicTolerance )
217 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
218 geometry.convertToStraightSegment( M_PI * tolerance / 180.0, QgsAbstractGeometry::MaximumAngle );
219 f.setGeometry( geometry );
220 }
221 return QgsFeatureList() << f;
222}
223
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ Double
Double/float values.
Definition qgis.h:3921
@ 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:57
A store for object properties.
QList< QgsFeature > QgsFeatureList