QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
18 #include "qgsalgorithmconvexhull.h"
19 
21 
22 QString QgsConvexHullAlgorithm::name() const
23 {
24  return QStringLiteral( "convexhull" );
25 }
26 
27 QString QgsConvexHullAlgorithm::displayName() const
28 {
29  return QObject::tr( "Convex hull" );
30 }
31 
32 QStringList QgsConvexHullAlgorithm::tags() const
33 {
34  return QObject::tr( "convex,hull,bounds,bounding" ).split( ',' );
35 }
36 
37 QString QgsConvexHullAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsConvexHullAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsConvexHullAlgorithm::outputName() const
48 {
49  return QObject::tr( "Convex hulls" );
50 }
51 
52 QString QgsConvexHullAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm calculates the convex hull for each feature in an input layer." ) +
55  QStringLiteral( "\n\n" ) +
56  QObject::tr( "See the 'Minimum bounding geometry' algorithm for a convex hull calculation which covers the whole layer or grouped subsets of features." );
57 }
58 
59 QgsConvexHullAlgorithm *QgsConvexHullAlgorithm::createInstance() const
60 {
61  return new QgsConvexHullAlgorithm();
62 }
63 
64 QgsFields QgsConvexHullAlgorithm::outputFields( const QgsFields &inputFields ) const
65 {
66  QgsFields fields = inputFields;
67  fields.append( QgsField( QStringLiteral( "area" ), QVariant::Double, QString(), 20, 6 ) );
68  fields.append( QgsField( QStringLiteral( "perimeter" ), QVariant::Double, QString(), 20, 6 ) );
69  return fields;
70 }
71 
72 QgsFeatureList QgsConvexHullAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
73 {
74  QgsFeature f = feature;
75  if ( f.hasGeometry() )
76  {
77  QgsGeometry outputGeometry;
79  {
80  feedback->reportError( QObject::tr( "Cannot calculate convex hull for a single Point feature (try 'Minimum bounding geometry' algorithm instead)." ) );
81  f.clearGeometry();
82  }
83  else
84  {
85  outputGeometry = f.geometry().convexHull();
86  if ( outputGeometry.isNull() )
87  feedback->reportError( outputGeometry.lastError() );
88  f.setGeometry( outputGeometry );
89  }
90  if ( !outputGeometry.isNull() )
91  {
92  QgsAttributes attrs = f.attributes();
93  attrs << outputGeometry.constGet()->area()
94  << outputGeometry.constGet()->perimeter();
95  f.setAttributes( attrs );
96  }
97  else
98  {
99  QgsAttributes attrs = f.attributes();
100  attrs << QVariant()
101  << QVariant();
102  f.setAttributes( attrs );
103  }
104  }
105  return QgsFeatureList() << f;
106 }
107 
109 
QgsGeometry::lastError
QString lastError() const SIP_HOLDGIL
Returns an error string referring to the last error encountered either when this geometry was created...
Definition: qgsgeometry.cpp:3308
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:72
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsWkbTypes::flatType
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:732
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:59
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsFeature::clearGeometry
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:184
QgsAbstractGeometry::area
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
Definition: qgsabstractgeometry.cpp:176
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsGeometry::convexHull
QgsGeometry convexHull() const
Returns the smallest convex polygon that contains all the points in the geometry.
Definition: qgsgeometry.cpp:2388
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:127
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsAbstractGeometry::perimeter
virtual double perimeter() const
Returns the planar, 2-dimensional perimeter of the geometry.
Definition: qgsabstractgeometry.cpp:171
qgsalgorithmconvexhull.h
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Definition: qgsgeometry.cpp:357
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50