QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
59 "This algorithm converts a geometry into its curved geometry equivalent.\n\n"
60 "Already curved geometries will be retained without change."
61 );
62}
63
64QString QgsConvertToCurvesAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Converts a geometry into its curved geometry equivalent." );
67}
68
69QgsConvertToCurvesAlgorithm *QgsConvertToCurvesAlgorithm::createInstance() const
70{
71 return new QgsConvertToCurvesAlgorithm();
72}
73
74QList<int> QgsConvertToCurvesAlgorithm::inputLayerTypes() const
75{
76 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
77}
78
79void QgsConvertToCurvesAlgorithm::initParameters( const QVariantMap & )
80{
81 auto tolerance = std::make_unique<QgsProcessingParameterNumber>( u"DISTANCE"_s, QObject::tr( "Maximum distance tolerance" ), Qgis::ProcessingNumberParameterType::Double, 0.000001, false, 0, 10000000.0 );
82 tolerance->setIsDynamic( true );
83 tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DISTANCE"_s, QObject::tr( "Maximum distance tolerance" ), QgsPropertyDefinition::DoublePositive ) );
84 tolerance->setDynamicLayerParameterName( u"INPUT"_s );
85 addParameter( tolerance.release() );
86
87 auto angleTolerance = std::make_unique<QgsProcessingParameterNumber>( u"ANGLE"_s, QObject::tr( "Maximum angle tolerance" ), Qgis::ProcessingNumberParameterType::Double, 0.000001, false, 0, 45.0 );
88 angleTolerance->setIsDynamic( true );
89 angleTolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"ANGLE"_s, QObject::tr( "Maximum angle tolerance" ), QgsPropertyDefinition::DoublePositive ) );
90 angleTolerance->setDynamicLayerParameterName( u"INPUT"_s );
91 addParameter( angleTolerance.release() );
92}
93
94bool QgsConvertToCurvesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
95{
96 mTolerance = parameterAsDouble( parameters, u"DISTANCE"_s, context );
97 mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, u"DISTANCE"_s );
98 if ( mDynamicTolerance )
99 mToleranceProperty = parameters.value( u"DISTANCE"_s ).value<QgsProperty>();
100
101 mAngleTolerance = parameterAsDouble( parameters, u"ANGLE"_s, context );
102 mDynamicAngleTolerance = QgsProcessingParameters::isDynamic( parameters, u"ANGLE"_s );
103 if ( mDynamicAngleTolerance )
104 mAngleToleranceProperty = parameters.value( u"ANGLE"_s ).value<QgsProperty>();
105
106 return true;
107}
108
109QgsFeatureList QgsConvertToCurvesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
110{
111 QgsFeature f = feature;
112 if ( f.hasGeometry() )
113 {
114 const QgsGeometry geometry = f.geometry();
115 double tolerance = mTolerance;
116 if ( mDynamicTolerance )
117 tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
118 double angleTolerance = mAngleTolerance;
119 if ( mDynamicAngleTolerance )
120 angleTolerance = mAngleToleranceProperty.valueAsDouble( context.expressionContext(), angleTolerance );
121
122 f.setGeometry( geometry.convertToCurves( tolerance, angleTolerance * M_PI / 180.0 ) );
123 }
124 return QgsFeatureList() << f;
125}
126
127Qgis::WkbType QgsConvertToCurvesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
128{
129 if ( QgsWkbTypes::isCurvedType( inputWkbType ) )
130 return inputWkbType;
131
133 switch ( QgsWkbTypes::geometryType( inputWkbType ) )
134 {
138 return inputWkbType;
139
142 break;
143
146 break;
147 }
148
149 if ( QgsWkbTypes::isMultiType( inputWkbType ) )
150 outType = QgsWkbTypes::multiType( outType );
151
152 if ( QgsWkbTypes::hasZ( inputWkbType ) )
153 outType = QgsWkbTypes::addZ( outType );
154
155 if ( QgsWkbTypes::hasM( inputWkbType ) )
156 outType = QgsWkbTypes::addM( outType );
157
158 return outType;
159}
160
161
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ Point
Points.
Definition qgis.h:380
@ Line
Lines.
Definition qgis.h:381
@ Polygon
Polygons.
Definition qgis.h:382
@ Unknown
Unknown types.
Definition qgis.h:383
@ Null
No geometry.
Definition qgis.h:384
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ CompoundCurve
CompoundCurve.
Definition qgis.h:305
@ Unknown
Unknown.
Definition qgis.h:295
@ CurvePolygon
CurvePolygon.
Definition qgis.h:306
@ Double
Double/float values.
Definition qgis.h:3921
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:57
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