QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
18 #include "qgsalgorithmroundness.h"
19 #include "qgscurvepolygon.h"
20 
22 
23 QString QgsRoundnessAlgorithm::name() const
24 {
25  return QStringLiteral( "roundness" );
26 }
27 
28 QString QgsRoundnessAlgorithm::displayName() const
29 {
30  return QObject::tr( "Roundness" );
31 }
32 
33 QStringList QgsRoundnessAlgorithm::tags() const
34 {
35  return QObject::tr( "roundness,circle" ).split( ',' );
36 }
37 
38 QString QgsRoundnessAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsRoundnessAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsRoundnessAlgorithm::outputName() const
49 {
50  return QObject::tr( "Roundness" );
51 }
52 
53 QString 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 
59 QString QgsRoundnessAlgorithm::shortDescription() const
60 {
61  return QObject::tr( "Calculates the roundness of polygon features." );
62 }
63 
64 QgsRoundnessAlgorithm *QgsRoundnessAlgorithm::createInstance() const
65 {
66  return new QgsRoundnessAlgorithm();
67 }
68 
69 QList<int> QgsRoundnessAlgorithm::inputLayerTypes() const
70 {
71  return QList<int>() << QgsProcessing::TypeVectorPolygon;
72 }
73 
74 QgsProcessing::SourceType QgsRoundnessAlgorithm::outputLayerType() const
75 {
77 }
78 
79 QgsFields QgsRoundnessAlgorithm::outputFields( const QgsFields &inputFields ) const
80 {
81  QgsFields outputFields = inputFields;
82  outputFields.append( QgsField( QStringLiteral( "roundness" ), QVariant::Double ) );
83  return outputFields;
84 }
85 
86 QgsFeatureList 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 
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsCurvePolygon
Curve polygon geometry type.
Definition: qgscurvepolygon.h:34
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsFields::append
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
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsalgorithmroundness.h
QgsAbstractGeometry::simplifiedTypeRef
virtual const QgsAbstractGeometry * simplifiedTypeRef() const SIP_HOLDGIL
Returns a reference to the simplest lossless representation of this geometry, e.g.
Definition: qgsabstractgeometry.cpp:287
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
qgscurvepolygon.h
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
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:230
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50