QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
23
24QString QgsRoundnessAlgorithm::name() const
25{
26 return QStringLiteral( "roundness" );
27}
28
29QString QgsRoundnessAlgorithm::displayName() const
30{
31 return QObject::tr( "Roundness" );
32}
33
34QStringList QgsRoundnessAlgorithm::tags() const
35{
36 return QObject::tr( "roundness,circle" ).split( ',' );
37}
38
39QString QgsRoundnessAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsRoundnessAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsRoundnessAlgorithm::outputName() const
50{
51 return QObject::tr( "Roundness" );
52}
53
54QString QgsRoundnessAlgorithm::shortHelpString() const
55{
56 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"
57 "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." );
58}
59
60QString QgsRoundnessAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Calculates the roundness of polygon features." );
63}
64
65QgsRoundnessAlgorithm *QgsRoundnessAlgorithm::createInstance() const
66{
67 return new QgsRoundnessAlgorithm();
68}
69
70QList<int> QgsRoundnessAlgorithm::inputLayerTypes() const
71{
72 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
73}
74
75Qgis::ProcessingSourceType QgsRoundnessAlgorithm::outputLayerType() const
76{
78}
79
80QgsFields QgsRoundnessAlgorithm::outputFields( const QgsFields &inputFields ) const
81{
82 QgsFields newFields;
83 newFields.append( QgsField( QStringLiteral( "roundness" ), QMetaType::Type::Double ) );
84 return QgsProcessingUtils::combineFields( inputFields, newFields );
85}
86
87QgsFeatureList QgsRoundnessAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
88{
89 QgsFeature f = feature;
90 QgsAttributes attributes = f.attributes();
91 if ( f.hasGeometry() )
92 {
93 QgsGeometry geom = f.geometry();
95 {
96 double roundness = poly->roundness();
97 attributes << QVariant( roundness );
98 }
99 else
100 {
101 attributes << QVariant();
102 }
103 }
104 else
105 {
106 attributes << QVariant();
107 }
108 f.setAttributes( attributes );
109 return QgsFeatureList() << f;
110}
111
ProcessingSourceType
Processing data source types.
Definition qgis.h:3531
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3536
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:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
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:54
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:73
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