QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmconvexhull.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmconvexhull.cpp
3 ---------------------
4 begin : April 2017
5 copyright : (C) 2017 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
22QString QgsConvexHullAlgorithm::name() const
23{
24 return QStringLiteral( "convexhull" );
25}
26
27QString QgsConvexHullAlgorithm::displayName() const
28{
29 return QObject::tr( "Convex hull" );
30}
31
32QStringList QgsConvexHullAlgorithm::tags() const
33{
34 return QObject::tr( "convex,hull,bounds,bounding" ).split( ',' );
35}
36
37QString QgsConvexHullAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsConvexHullAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsConvexHullAlgorithm::outputName() const
48{
49 return QObject::tr( "Convex hulls" );
50}
51
52QString QgsConvexHullAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm calculates the convex hull for each feature in an input layer." ) + QStringLiteral( "\n\n" ) + QObject::tr( "See the 'Minimum bounding geometry' algorithm for a convex hull calculation which covers the whole layer or grouped subsets of features." );
55}
56
57QString QgsConvexHullAlgorithm::shortDescription() const
58{
59 return QObject::tr( "Calculates the convex hull for each feature in an input layer." );
60}
61
62QgsConvexHullAlgorithm *QgsConvexHullAlgorithm::createInstance() const
63{
64 return new QgsConvexHullAlgorithm();
65}
66
67QgsFields QgsConvexHullAlgorithm::outputFields( const QgsFields &inputFields ) const
68{
69 QgsFields newFields;
70 newFields.append( QgsField( QStringLiteral( "area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
71 newFields.append( QgsField( QStringLiteral( "perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
72 return QgsProcessingUtils::combineFields( inputFields, newFields );
73}
74
75QgsFeatureList QgsConvexHullAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
76{
77 QgsFeature f = feature;
78 if ( f.hasGeometry() )
79 {
80 QgsGeometry outputGeometry;
82 {
83 feedback->reportError( QObject::tr( "Cannot calculate convex hull for a single Point feature (try 'Minimum bounding geometry' algorithm instead)." ) );
84 f.clearGeometry();
85 }
86 else
87 {
88 outputGeometry = f.geometry().convexHull();
89 if ( outputGeometry.isNull() )
90 feedback->reportError( outputGeometry.lastError() );
91 f.setGeometry( outputGeometry );
92 }
93 if ( !outputGeometry.isNull() )
94 {
95 QgsAttributes attrs = f.attributes();
96 attrs << outputGeometry.constGet()->area()
97 << outputGeometry.constGet()->perimeter();
98 f.setAttributes( attrs );
99 }
100 else
101 {
102 QgsAttributes attrs = f.attributes();
103 attrs << QVariant()
104 << QVariant();
105 f.setAttributes( attrs );
106 }
107 }
108 return QgsFeatureList() << f;
109}
110
virtual double perimeter() const
Returns the planar, 2-dimensional perimeter of the geometry.
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
A vector of attributes.
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
void clearGeometry()
Removes any geometry associated with the feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
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:70
A geometry is the spatial representation of a feature.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsGeometry convexHull() const
Returns the smallest convex polygon that contains all the points in the geometry.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
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).
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
QList< QgsFeature > QgsFeatureList