QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
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
20#include "qgscurvepolygon.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsRoundnessAlgorithm::name() const
29{
30 return u"roundness"_s;
31}
32
33QString QgsRoundnessAlgorithm::displayName() const
34{
35 return QObject::tr( "Roundness" );
36}
37
38QStringList QgsRoundnessAlgorithm::tags() const
39{
40 return QObject::tr( "roundness,circle" ).split( ',' );
41}
42
43QString QgsRoundnessAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsRoundnessAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsRoundnessAlgorithm::outputName() const
54{
55 return QObject::tr( "Roundness" );
56}
57
58QString QgsRoundnessAlgorithm::shortHelpString() const
59{
60 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"
61 "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." );
62}
63
64QString QgsRoundnessAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Calculates the roundness of polygon features." );
67}
68
69QgsRoundnessAlgorithm *QgsRoundnessAlgorithm::createInstance() const
70{
71 return new QgsRoundnessAlgorithm();
72}
73
74QList<int> QgsRoundnessAlgorithm::inputLayerTypes() const
75{
76 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
77}
78
79Qgis::ProcessingSourceType QgsRoundnessAlgorithm::outputLayerType() const
80{
82}
83
84QgsFields QgsRoundnessAlgorithm::outputFields( const QgsFields &inputFields ) const
85{
86 QgsFields newFields;
87 newFields.append( QgsField( u"roundness"_s, QMetaType::Type::Double ) );
88 return QgsProcessingUtils::combineFields( inputFields, newFields );
89}
90
91QgsFeatureList QgsRoundnessAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
92{
93 QgsFeature f = feature;
94 QgsAttributes attributes = f.attributes();
95 if ( f.hasGeometry() )
96 {
97 QgsGeometry geom = f.geometry();
99 {
100 double roundness = poly->roundness();
101 attributes << QVariant( roundness );
102 }
103 else
104 {
105 attributes << QVariant();
106 }
107 }
108 else
109 {
110 attributes << QVariant();
111 }
112 f.setAttributes( attributes );
113 return QgsFeatureList() << f;
114}
115
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
virtual const QgsAbstractGeometry * simplifiedTypeRef() const
Returns a reference to the simplest lossless representation of this geometry, e.g.
A vector of attributes.
Curve polygon geometry type.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:76
A geometry is the spatial representation of a feature.
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.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList