QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
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." )
59 + u"\n\n"_s
60 + QObject::tr( "See the 'Minimum bounding geometry' algorithm for a convex hull calculation which covers the whole layer or grouped subsets of features." );
61}
62
63QString QgsConvexHullAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Calculates the convex hull for each feature in an input layer." );
66}
67
68QgsConvexHullAlgorithm *QgsConvexHullAlgorithm::createInstance() const
69{
70 return new QgsConvexHullAlgorithm();
71}
72
73QgsFields QgsConvexHullAlgorithm::outputFields( const QgsFields &inputFields ) const
74{
75 QgsFields newFields;
76 newFields.append( QgsField( u"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
77 newFields.append( QgsField( u"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
78 return QgsProcessingUtils::combineFields( inputFields, newFields );
79}
80
81QgsFeatureList QgsConvexHullAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
82{
83 QGS_MARK_ALGORITHM_SOURCE
84
85 QgsFeature f = feature;
86 if ( f.hasGeometry() )
87 {
88 QgsGeometry outputGeometry;
89 if ( QgsWkbTypes::flatType( f.geometry().wkbType() ) == Qgis::WkbType::Point )
90 {
91 feedback->reportError( QObject::tr( "Cannot calculate convex hull for a single Point feature (try 'Minimum bounding geometry' algorithm instead)." ) );
92 f.clearGeometry();
93 }
94 else
95 {
96 outputGeometry = f.geometry().convexHull();
97 if ( outputGeometry.isNull() )
98 feedback->reportError( outputGeometry.lastError() );
99 f.setGeometry( outputGeometry );
100 }
101 if ( !outputGeometry.isNull() )
102 {
103 QgsAttributes attrs = f.attributes();
104 attrs << outputGeometry.constGet()->area() << outputGeometry.constGet()->perimeter();
105 f.setAttributes( attrs );
106 }
107 else
108 {
109 QgsAttributes attrs = f.attributes();
110 attrs << QVariant() << QVariant();
111 f.setAttributes( attrs );
112 }
113 }
114 return QgsFeatureList() << f;
115}
116
@ Point
Point.
Definition qgis.h:296
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
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
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.
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