QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsalgorithmroundness.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmroundness.cpp
3 ---------------------
4 begin : September 2021
5 copyright : (C) 2021 by Antoine Facchini
6 email : antoine dot facchini @oslandia 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#include "qgscurvepolygon.h"
20
22
23QString QgsRoundnessAlgorithm::name() const
24{
25 return QStringLiteral( "roundness" );
26}
27
28QString QgsRoundnessAlgorithm::displayName() const
29{
30 return QObject::tr( "Roundness" );
31}
32
33QStringList QgsRoundnessAlgorithm::tags() const
34{
35 return QObject::tr( "roundness,circle" ).split( ',' );
36}
37
38QString QgsRoundnessAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsRoundnessAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsRoundnessAlgorithm::outputName() const
49{
50 return QObject::tr( "Roundness" );
51}
52
53QString QgsRoundnessAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "Calculates the roundness of each feature and stores it as a new field. The input vector layer must contain polygons.\n\n"
56 "The roundness of a polygon is defined as 4π × polygon area / perimeter². The roundness value varies between 0 and 1. A perfect circle has a roundness of 1, while a completely flat polygon has a roundness of 0." );
57}
58
59QString QgsRoundnessAlgorithm::shortDescription() const
60{
61 return QObject::tr( "Calculates the roundness of polygon features." );
62}
63
64QgsRoundnessAlgorithm *QgsRoundnessAlgorithm::createInstance() const
65{
66 return new QgsRoundnessAlgorithm();
67}
68
69QList<int> QgsRoundnessAlgorithm::inputLayerTypes() const
70{
71 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorPolygon );
72}
73
74Qgis::ProcessingSourceType QgsRoundnessAlgorithm::outputLayerType() const
75{
77}
78
79QgsFields QgsRoundnessAlgorithm::outputFields( const QgsFields &inputFields ) const
80{
81 QgsFields outputFields = inputFields;
82 outputFields.append( QgsField( QStringLiteral( "roundness" ), QVariant::Double ) );
83 return outputFields;
84}
85
86QgsFeatureList QgsRoundnessAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
87{
88 QgsFeature f = feature;
89 QgsAttributes attributes = f.attributes();
90 if ( f.hasGeometry() )
91 {
92 QgsGeometry geom = f.geometry();
93 if ( const QgsCurvePolygon *poly = qgsgeometry_cast< const QgsCurvePolygon * >( geom.constGet()->simplifiedTypeRef() ) )
94 {
95 double roundness = poly->roundness();
96 attributes << QVariant( roundness );
97 }
98 else
99 {
100 attributes << QVariant();
101 }
102 }
103 else
104 {
105 attributes << QVariant();
106 }
107 f.setAttributes( attributes );
108 return QgsFeatureList() << f;
109}
110
112
113
ProcessingSourceType
Processing data source types.
Definition: qgis.h:2858
@ VectorPolygon
Vector polygon layers.
virtual const QgsAbstractGeometry * simplifiedTypeRef() const
Returns a reference to the simplest lossless representation of this geometry, e.g.
A vector of attributes.
Definition: qgsattributes.h:59
Curve polygon geometry type.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:53
Container of fields for a vector layer.
Definition: qgsfields.h:45
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:917