QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
22 QString QgsConvertToCurvesAlgorithm::name() const
23 {
24  return QStringLiteral( "converttocurves" );
25 }
26 
27 QString QgsConvertToCurvesAlgorithm::displayName() const
28 {
29  return QObject::tr( "Convert to curved geometries" );
30 }
31 
32 QStringList QgsConvertToCurvesAlgorithm::tags() const
33 {
34  return QObject::tr( "straight,segmentize,curves,curved,circular" ).split( ',' );
35 }
36 
37 QString QgsConvertToCurvesAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsConvertToCurvesAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsConvertToCurvesAlgorithm::outputName() const
48 {
49  return QObject::tr( "Curves" );
50 }
51 
52 QString 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 
58 QgsConvertToCurvesAlgorithm *QgsConvertToCurvesAlgorithm::createInstance() const
59 {
60  return new QgsConvertToCurvesAlgorithm();
61 }
62 
63 QList<int> QgsConvertToCurvesAlgorithm::inputLayerTypes() const
64 {
66 }
67 
68 void QgsConvertToCurvesAlgorithm::initParameters( const QVariantMap & )
69 {
70  std::unique_ptr< QgsProcessingParameterNumber > tolerance = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DISTANCE" ),
71  QObject::tr( "Maximum distance tolerance" ), QgsProcessingParameterNumber::Double,
72  0.000001, false, 0, 10000000.0 );
73  tolerance->setIsDynamic( true );
74  tolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Maximum distance tolerance" ), QgsPropertyDefinition::DoublePositive ) );
75  tolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
76  addParameter( tolerance.release() );
77 
78  std::unique_ptr< QgsProcessingParameterNumber > angleTolerance = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "ANGLE" ),
79  QObject::tr( "Maximum angle tolerance" ), QgsProcessingParameterNumber::Double,
80  0.000001, false, 0, 45.0 );
81  angleTolerance->setIsDynamic( true );
82  angleTolerance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ANGLE" ), QObject::tr( "Maximum angle tolerance" ), QgsPropertyDefinition::DoublePositive ) );
83  angleTolerance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
84  addParameter( angleTolerance.release() );
85 }
86 
87 bool QgsConvertToCurvesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
88 {
89  mTolerance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
90  mDynamicTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
91  if ( mDynamicTolerance )
92  mToleranceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
93 
94  mAngleTolerance = parameterAsDouble( parameters, QStringLiteral( "ANGLE" ), context );
95  mDynamicAngleTolerance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ANGLE" ) );
96  if ( mDynamicAngleTolerance )
97  mAngleToleranceProperty = parameters.value( QStringLiteral( "ANGLE" ) ).value< QgsProperty >();
98 
99  return true;
100 }
101 
102 QgsFeatureList QgsConvertToCurvesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
103 {
104  QgsFeature f = feature;
105  if ( f.hasGeometry() )
106  {
107  QgsGeometry geometry = f.geometry();
108  double tolerance = mTolerance;
109  if ( mDynamicTolerance )
110  tolerance = mToleranceProperty.valueAsDouble( context.expressionContext(), tolerance );
111  double angleTolerance = mAngleTolerance;
112  if ( mDynamicAngleTolerance )
113  angleTolerance = mAngleToleranceProperty.valueAsDouble( context.expressionContext(), angleTolerance );
114 
115  f.setGeometry( geometry.convertToCurves( tolerance, angleTolerance * M_PI / 180.0 ) );
116  }
117  return QgsFeatureList() << f;
118 }
119 
120 QgsWkbTypes::Type QgsConvertToCurvesAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
121 {
122  if ( QgsWkbTypes::isCurvedType( inputWkbType ) )
123  return inputWkbType;
124 
126  switch ( QgsWkbTypes::geometryType( inputWkbType ) )
127  {
131  return inputWkbType;
132 
134  outType = QgsWkbTypes::CompoundCurve;
135  break;
136 
138  outType = QgsWkbTypes::CurvePolygon;
139  break;
140  }
141 
142  if ( QgsWkbTypes::isMultiType( inputWkbType ) )
143  outType = QgsWkbTypes::multiType( outType );
144 
145  if ( QgsWkbTypes::hasZ( inputWkbType ) )
146  outType = QgsWkbTypes::addZ( outType );
147 
148  if ( QgsWkbTypes::hasM( inputWkbType ) )
149  outType = QgsWkbTypes::addM( outType );
150 
151  return outType;
152 }
153 
154 
156 
157 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:1967
QgsProperty
A store for object properties.
Definition: qgsproperty.h:232
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:111
QgsWkbTypes::NullGeometry
@ NullGeometry
Definition: qgswkbtypes.h:146
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:38
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:50
QgsWkbTypes::addZ
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1139
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:67
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
QgsWkbTypes::multiType
static Type multiType(Type type) SIP_HOLDGIL
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:302
QgsWkbTypes::PolygonGeometry
@ PolygonGeometry
Definition: qgswkbtypes.h:144
QgsWkbTypes::addM
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1164
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:71
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:139
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
QgsWkbTypes::CurvePolygon
@ CurvePolygon
Definition: qgswkbtypes.h:82
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
QgsWkbTypes::hasM
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1093
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:48
QgsGeometry::convertToCurves
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.
Definition: qgsgeometry.cpp:2131
QgsWkbTypes::isCurvedType
static bool isCurvedType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a curved type or can contain curved geometries.
Definition: qgswkbtypes.h:881
QgsWkbTypes::LineGeometry
@ LineGeometry
Definition: qgswkbtypes.h:143
QgsWkbTypes::PointGeometry
@ PointGeometry
Definition: qgswkbtypes.h:142
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:199
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:133
qgsalgorithmconverttocurves.h
QgsWkbTypes::UnknownGeometry
@ UnknownGeometry
Definition: qgswkbtypes.h:145
QgsProperty::valueAsDouble
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.
Definition: qgsproperty.cpp:620
QgsWkbTypes::geometryType
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:938
QgsWkbTypes::hasZ
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1043
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsWkbTypes::isMultiType
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:832
QgsWkbTypes::CompoundCurve
@ CompoundCurve
Definition: qgswkbtypes.h:81
QgsPropertyDefinition::DoublePositive
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59