QGIS API Documentation 3.41.0-Master (cea29feecf2)
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
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
57QgsConvexHullAlgorithm *QgsConvexHullAlgorithm::createInstance() const
58{
59 return new QgsConvexHullAlgorithm();
60}
61
62QgsFields QgsConvexHullAlgorithm::outputFields( const QgsFields &inputFields ) const
63{
64 QgsFields fields = inputFields;
65 fields.append( QgsField( QStringLiteral( "area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
66 fields.append( QgsField( QStringLiteral( "perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
67 return fields;
68}
69
70QgsFeatureList QgsConvexHullAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
71{
72 QgsFeature f = feature;
73 if ( f.hasGeometry() )
74 {
75 QgsGeometry outputGeometry;
77 {
78 feedback->reportError( QObject::tr( "Cannot calculate convex hull for a single Point feature (try 'Minimum bounding geometry' algorithm instead)." ) );
79 f.clearGeometry();
80 }
81 else
82 {
83 outputGeometry = f.geometry().convexHull();
84 if ( outputGeometry.isNull() )
85 feedback->reportError( outputGeometry.lastError() );
86 f.setGeometry( outputGeometry );
87 }
88 if ( !outputGeometry.isNull() )
89 {
90 QgsAttributes attrs = f.attributes();
91 attrs << outputGeometry.constGet()->area()
92 << outputGeometry.constGet()->perimeter();
93 f.setAttributes( attrs );
94 }
95 else
96 {
97 QgsAttributes attrs = f.attributes();
98 attrs << QVariant()
99 << QVariant();
100 f.setAttributes( attrs );
101 }
102 }
103 return QgsFeatureList() << f;
104}
105
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 Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
QList< QgsFeature > QgsFeatureList