QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmconverttocurves.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmconverttocurves.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 QgsConvertToCurvesAlgorithm::name() const
27{
28 return u"converttocurves"_s;
29}
30
31QString QgsConvertToCurvesAlgorithm::displayName() const
32{
33 return QObject::tr( "Convert to curved geometries" );
34}
35
36QStringList QgsConvertToCurvesAlgorithm::tags() const
37{
38 return QObject::tr( "straight,segmentize,curves,curved,circular" ).split( ',' );
39}
40
41QString QgsConvertToCurvesAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsConvertToCurvesAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsConvertToCurvesAlgorithm::outputName() const
52{
53 return QObject::tr( "Curves" );
54}
55
56QString QgsConvertToCurvesAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm converts a geometry into its curved geometry equivalent.\n\n"
59 "Already curved geometries will be retained without change." );
60}
61
62QString QgsConvertToCurvesAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Converts a geometry into its curved geometry equivalent." );
65}
66
67QgsConvertToCurvesAlgorithm *QgsConvertToCurvesAlgorithm::createInstance() const
68{
69 return new QgsConvertToCurvesAlgorithm();
70}
71
72QList<int> QgsConvertToCurvesAlgorithm::inputLayerTypes() const
73{
74 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
75}
76
77void QgsConvertToCurvesAlgorithm::initParameters( const QVariantMap & )
78{
79 auto tolerance = std::make_unique<QgsProcessingParameterNumber>( u"DISTANCE"_s, QObject::tr( "Maximum distance tolerance" ), Qgis::ProcessingNumberParameterType::Double, 0.000001, false, 0, 10000000.0 );
80 tolerance->setIsDynamic( true );
81 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DISTANCE"_s, QObject::tr( "Maximum distance tolerance" ), QgsPropertyDefinition::DoublePositive ) );
82 tolerance->setDynamicLayerParameterName( u"INPUT"_s );
83 addParameter( tolerance.release() );
84
85 auto angleTolerance = std::make_unique<QgsProcessingParameterNumber>( u"ANGLE"_s, QObject::tr( "Maximum angle tolerance" ), Qgis::ProcessingNumberParameterType::Double, 0.000001, false, 0, 45.0 );
86 angleTolerance->setIsDynamic( true );
87 angleTolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ANGLE"_s, QObject::tr( "Maximum angle tolerance" ), QgsPropertyDefinition::DoublePositive ) );
88 angleTolerance->setDynamicLayerParameterName( u"INPUT"_s );
89 addParameter( angleTolerance.release() );
90}
91
92bool QgsConvertToCurvesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
93{
94 mTolerance = parameterAsDouble( parameters, u"DISTANCE"_s, context );
95 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"DISTANCE"_s );
96 if ( mDynamicTolerance )
97 mToleranceProperty = parameters.value( u"DISTANCE"_s ).value<QgsProperty>();
98
99 mAngleTolerance = parameterAsDouble( parameters, u"ANGLE"_s, context );
100 mDynamicAngleTolerance = QgsProcessingParameters::isDynamic( parameters, u"ANGLE"_s );
101 if ( mDynamicAngleTolerance )
102 mAngleToleranceProperty = parameters.value( u"ANGLE"_s ).value<QgsProperty>();
103
104 return true;
105}
106
107QgsFeatureList QgsConvertToCurvesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
108{
109 QgsFeature f = feature;
110 if ( f.hasGeometry() )
111 {
112 const QgsGeometry geometry = f.geometry();
113 double tolerance = mTolerance;
114 if ( mDynamicTolerance )
115 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
116 double angleTolerance = mAngleTolerance;
117 if ( mDynamicAngleTolerance )
118 angleTolerance = mAngleToleranceProperty.valueAsDouble( context.expressionContext(), angleTolerance );
119
120 f.setGeometry( geometry.convertToCurves( tolerance, angleTolerance * M_PI / 180.0 ) );
121 }
122 return QgsFeatureList() << f;
123}
124
125Qgis::WkbType QgsConvertToCurvesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
126{
127 if ( QgsWkbTypes::isCurvedType( inputWkbType ) )
128 return inputWkbType;
129
131 switch ( QgsWkbTypes::geometryType( inputWkbType ) )
132 {
136 return inputWkbType;
137
140 break;
141
144 break;
145 }
146
147 if ( QgsWkbTypes::isMultiType( inputWkbType ) )
148 outType = QgsWkbTypes::multiType( outType );
149
150 if ( QgsWkbTypes::hasZ( inputWkbType ) )
151 outType = QgsWkbTypes::addZ( outType );
152
153 if ( QgsWkbTypes::hasM( inputWkbType ) )
154 outType = QgsWkbTypes::addM( outType );
155
156 return outType;
157}
158
159
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
@ Point
Points.
Definition qgis.h:366
@ Line
Lines.
Definition qgis.h:367
@ Polygon
Polygons.
Definition qgis.h:368
@ Unknown
Unknown types.
Definition qgis.h:369
@ Null
No geometry.
Definition qgis.h:370
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ CompoundCurve
CompoundCurve.
Definition qgis.h:291
@ Unknown
Unknown.
Definition qgis.h:281
@ CurvePolygon
CurvePolygon.
Definition qgis.h:292
@ Double
Double/float values.
Definition qgis.h:3875
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.
QgsGeometry convertToCurves(double distanceTolerance=1e-8, double angleTolerance=1e-8) const
Attempts to convert a non-curved geometry into a curved geometry type (e.g.
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.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Q_INVOKABLE bool isCurvedType(Qgis::WkbType type)
Returns true if the WKB type is a curved type or can contain curved geometries.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
QList< QgsFeature > QgsFeatureList