QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
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
21
22QString QgsConvertToCurvesAlgorithm::name() const
23{
24 return QStringLiteral( "converttocurves" );
25}
26
27QString QgsConvertToCurvesAlgorithm::displayName() const
28{
29 return QObject::tr( "Convert to curved geometries" );
30}
31
32QStringList QgsConvertToCurvesAlgorithm::tags() const
33{
34 return QObject::tr( "straight,segmentize,curves,curved,circular" ).split( ',' );
35}
36
37QString QgsConvertToCurvesAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsConvertToCurvesAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsConvertToCurvesAlgorithm::outputName() const
48{
49 return QObject::tr( "Curves" );
50}
51
52QString QgsConvertToCurvesAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm converts a geometry into its curved geometry equivalent.\n\n"
55 "Already curved geometries will be retained without change." );
56}
57
58QString QgsConvertToCurvesAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Converts a geometry into its curved geometry equivalent." );
61}
62
63QgsConvertToCurvesAlgorithm *QgsConvertToCurvesAlgorithm::createInstance() const
64{
65 return new QgsConvertToCurvesAlgorithm();
66}
67
68QList<int> QgsConvertToCurvesAlgorithm::inputLayerTypes() const
69{
70 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
71}
72
73void QgsConvertToCurvesAlgorithm::initParameters( const QVariantMap & )
74{
75 auto tolerance = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum distance tolerance" ), Qgis::ProcessingNumberParameterType::Double, 0.000001, false, 0, 10000000.0 );
76 tolerance->setIsDynamic( true );
77 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum distance tolerance" ), QgsPropertyDefinition::DoublePositive ) );
78 tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
79 addParameter( tolerance.release() );
80
81 auto angleTolerance = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "ANGLE" ), QObject::tr( "Maximum angle tolerance" ), Qgis::ProcessingNumberParameterType::Double, 0.000001, false, 0, 45.0 );
82 angleTolerance->setIsDynamic( true );
83 angleTolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ANGLE" ), QObject::tr( "Maximum angle tolerance" ), QgsPropertyDefinition::DoublePositive ) );
84 angleTolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
85 addParameter( angleTolerance.release() );
86}
87
88bool QgsConvertToCurvesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
89{
90 mTolerance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
91 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
92 if ( mDynamicTolerance )
93 mToleranceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value<QgsProperty>();
94
95 mAngleTolerance = parameterAsDouble( parameters, QStringLiteral( "ANGLE" ), context );
96 mDynamicAngleTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE" ) );
97 if ( mDynamicAngleTolerance )
98 mAngleToleranceProperty = parameters.value( QStringLiteral( "ANGLE" ) ).value<QgsProperty>();
99
100 return true;
101}
102
103QgsFeatureList QgsConvertToCurvesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
104{
105 QgsFeature f = feature;
106 if ( f.hasGeometry() )
107 {
108 const QgsGeometry geometry = f.geometry();
109 double tolerance = mTolerance;
110 if ( mDynamicTolerance )
111 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
112 double angleTolerance = mAngleTolerance;
113 if ( mDynamicAngleTolerance )
114 angleTolerance = mAngleToleranceProperty.valueAsDouble( context.expressionContext(), angleTolerance );
115
116 f.setGeometry( geometry.convertToCurves( tolerance, angleTolerance * M_PI / 180.0 ) );
117 }
118 return QgsFeatureList() << f;
119}
120
121Qgis::WkbType QgsConvertToCurvesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
122{
123 if ( QgsWkbTypes::isCurvedType( inputWkbType ) )
124 return inputWkbType;
125
127 switch ( QgsWkbTypes::geometryType( inputWkbType ) )
128 {
132 return inputWkbType;
133
136 break;
137
140 break;
141 }
142
143 if ( QgsWkbTypes::isMultiType( inputWkbType ) )
144 outType = QgsWkbTypes::multiType( outType );
145
146 if ( QgsWkbTypes::hasZ( inputWkbType ) )
147 outType = QgsWkbTypes::addZ( outType );
148
149 if ( QgsWkbTypes::hasM( inputWkbType ) )
150 outType = QgsWkbTypes::addM( outType );
151
152 return outType;
153}
154
155
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ Polygon
Polygons.
@ Unknown
Unknown types.
@ Null
No geometry.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ CompoundCurve
CompoundCurve.
@ Unknown
Unknown.
@ CurvePolygon
CurvePolygon.
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.
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:45
@ DoublePositive
Positive double value (including 0)
Definition qgsproperty.h:56
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 bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
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 bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.
static bool isCurvedType(Qgis::WkbType type)
Returns true if the WKB type is a curved type or can contain curved geometries.
QList< QgsFeature > QgsFeatureList