QGIS API Documentation 3.43.0-Master (a93bf8b6462)
qgsalgorithmconcavehullbyfeature.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmconcavehullbyfeature.cpp
3 ---------------------
4 begin : May 2025
5 copyright : (C) 2025 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
20
22
23QString QgsConcaveHullByFeatureAlgorithm::name() const
24{
25 return QStringLiteral( "concavehullbyfeature" );
26}
27
28QString QgsConcaveHullByFeatureAlgorithm::displayName() const
29{
30 return QObject::tr( "Concave hull (by feature)" );
31}
32
33QStringList QgsConcaveHullByFeatureAlgorithm::tags() const
34{
35 return QObject::tr( "concave,hull,bounds,bounding,convex" ).split( ',' );
36}
37
38QString QgsConcaveHullByFeatureAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsConcaveHullByFeatureAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsConcaveHullByFeatureAlgorithm::outputName() const
49{
50 return QObject::tr( "Concave hulls" );
51}
52
53QString QgsConcaveHullByFeatureAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm calculates the concave hull for each feature in an input layer." ) + QStringLiteral( "\n\n" )
56 + QObject::tr( "A concave hull is a polygon which contains all the points of the input geometries, but is a better approximation than the convex hull to the area occupied by the input." ) + QStringLiteral( "\n\n" )
57 + QObject::tr( "It is frequently used to convert a multi-point into a polygonal area which contains all the points from the input geometry." ) + QStringLiteral( "\n\n" )
58 + QObject::tr( "See the 'Concave hull (by layer)' algorithm for a concave hull calculation which covers the whole layer or grouped subsets of features." );
59}
60
61QString QgsConcaveHullByFeatureAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Calculates the concave hull for each feature in an input layer." );
64}
65
66QgsConcaveHullByFeatureAlgorithm *QgsConcaveHullByFeatureAlgorithm::createInstance() const
67{
68 return new QgsConcaveHullByFeatureAlgorithm();
69}
70
71void QgsConcaveHullByFeatureAlgorithm::initParameters( const QVariantMap & )
72{
73 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "ALPHA" ), QObject::tr( "Threshold (0-1, where 1 is equivalent with Convex Hull)" ), Qgis::ProcessingNumberParameterType::Double, 0.3, false, 0, 1 ) );
74 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "HOLES" ), QObject::tr( "Allow holes" ), true ) );
75}
76
77QList<int> QgsConcaveHullByFeatureAlgorithm::inputLayerTypes() const
78{
79 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
80}
81
82QgsFields QgsConcaveHullByFeatureAlgorithm::outputFields( const QgsFields &inputFields ) const
83{
84 QgsFields newFields;
85 newFields.append( QgsField( QStringLiteral( "area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
86 newFields.append( QgsField( QStringLiteral( "perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
87 return QgsProcessingUtils::combineFields( inputFields, newFields );
88}
89
90bool QgsConcaveHullByFeatureAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
91{
92#if GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 11
93 throw QgsProcessingException( QObject::tr( "This algorithm requires a QGIS build based on GEOS 3.11 or later" ) );
94#endif
95 mPercentage = parameterAsDouble( parameters, QStringLiteral( "ALPHA" ), context );
96 mAllowHoles = parameterAsBool( parameters, QStringLiteral( "HOLES" ), context );
97 return true;
98}
99
100QgsFeatureList QgsConcaveHullByFeatureAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
101{
102 QgsFeature f = feature;
103 if ( f.hasGeometry() )
104 {
105 QgsGeometry outputGeometry;
106 const QgsAbstractGeometry *inputGeometry = f.geometry().constGet();
107 const QgsGeometryCollection *collection = qgsgeometry_cast< const QgsGeometryCollection * >( inputGeometry );
108 if ( !collection || collection->numGeometries() == 1 )
109 {
110 feedback->reportError( QObject::tr( "Cannot calculate convex hull for a single point feature (%1) (try 'Concave hull (by layer)' algorithm instead)." ).arg( f.id() ) );
111 f.clearGeometry();
112 }
113 else
114 {
115 outputGeometry = f.geometry().concaveHull( mPercentage, mAllowHoles );
116 if ( outputGeometry.isNull() )
117 feedback->reportError( outputGeometry.lastError() );
118 f.setGeometry( outputGeometry );
119 }
120 if ( outputGeometry.type() == Qgis::GeometryType::Polygon )
121 {
122 QgsAttributes attrs = f.attributes();
123 attrs << outputGeometry.constGet()->area()
124 << outputGeometry.constGet()->perimeter();
125 f.setAttributes( attrs );
126 }
127 else
128 {
129 if ( outputGeometry.type() == Qgis::GeometryType::Line )
130 {
131 feedback->pushWarning( QObject::tr( "Concave hull for feature %1 resulted in a linestring, ignoring" ).arg( f.id() ) );
132 }
133 else if ( outputGeometry.type() == Qgis::GeometryType::Point )
134 {
135 feedback->pushWarning( QObject::tr( "Concave hull for feature %1 resulted in a point, ignoring" ).arg( f.id() ) );
136 }
137 QgsAttributes attrs = f.attributes();
138 attrs << QVariant()
139 << QVariant();
140 f.setAttributes( attrs );
141 }
142 }
143 return QgsFeatureList() << f;
144}
145
@ VectorPoint
Vector point layers.
@ Polygon
Polygons.
Abstract base class for all geometries.
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
QgsFeatureId id
Definition qgsfeature.h:66
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
int numGeometries() const
Returns the number of geometries within the collection.
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.
Qgis::GeometryType type
QgsGeometry concaveHull(double targetPercent, bool allowHoles=false) const
Returns a possibly concave polygon that contains all the points in the geometry.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean parameter for processing algorithms.
A numeric parameter for processing algorithms.
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).
QList< QgsFeature > QgsFeatureList