QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsConvexHullAlgorithm::name() const
27{
28 return u"convexhull"_s;
29}
30
31QString QgsConvexHullAlgorithm::displayName() const
32{
33 return QObject::tr( "Convex hull" );
34}
35
36QStringList QgsConvexHullAlgorithm::tags() const
37{
38 return QObject::tr( "convex,hull,bounds,bounding" ).split( ',' );
39}
40
41QString QgsConvexHullAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsConvexHullAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsConvexHullAlgorithm::outputName() const
52{
53 return QObject::tr( "Convex hulls" );
54}
55
56QString QgsConvexHullAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm calculates the convex hull for each feature in an input layer." ) + u"\n\n"_s + QObject::tr( "See the 'Minimum bounding geometry' algorithm for a convex hull calculation which covers the whole layer or grouped subsets of features." );
59}
60
61QString QgsConvexHullAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Calculates the convex hull for each feature in an input layer." );
64}
65
66QgsConvexHullAlgorithm *QgsConvexHullAlgorithm::createInstance() const
67{
68 return new QgsConvexHullAlgorithm();
69}
70
71QgsFields QgsConvexHullAlgorithm::outputFields( const QgsFields &inputFields ) const
72{
73 QgsFields newFields;
74 newFields.append( QgsField( u"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
75 newFields.append( QgsField( u"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
76 return QgsProcessingUtils::combineFields( inputFields, newFields );
77}
78
79QgsFeatureList QgsConvexHullAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
80{
81 QgsFeature f = feature;
82 if ( f.hasGeometry() )
83 {
84 QgsGeometry outputGeometry;
86 {
87 feedback->reportError( QObject::tr( "Cannot calculate convex hull for a single Point feature (try 'Minimum bounding geometry' algorithm instead)." ) );
88 f.clearGeometry();
89 }
90 else
91 {
92 outputGeometry = f.geometry().convexHull();
93 if ( outputGeometry.isNull() )
94 feedback->reportError( outputGeometry.lastError() );
95 f.setGeometry( outputGeometry );
96 }
97 if ( !outputGeometry.isNull() )
98 {
99 QgsAttributes attrs = f.attributes();
100 attrs << outputGeometry.constGet()->area()
101 << outputGeometry.constGet()->perimeter();
102 f.setAttributes( attrs );
103 }
104 else
105 {
106 QgsAttributes attrs = f.attributes();
107 attrs << QVariant()
108 << QVariant();
109 f.setAttributes( attrs );
110 }
111 }
112 return QgsFeatureList() << f;
113}
114
@ Point
Point.
Definition qgis.h:282
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:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
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: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.
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