QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
19
21
23
24QString QgsConcaveHullByFeatureAlgorithm::name() const
25{
26 return QStringLiteral( "concavehullbyfeature" );
27}
28
29QString QgsConcaveHullByFeatureAlgorithm::displayName() const
30{
31 return QObject::tr( "Concave hull (by feature)" );
32}
33
34QStringList QgsConcaveHullByFeatureAlgorithm::tags() const
35{
36 return QObject::tr( "concave,hull,bounds,bounding,convex" ).split( ',' );
37}
38
39QString QgsConcaveHullByFeatureAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsConcaveHullByFeatureAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsConcaveHullByFeatureAlgorithm::outputName() const
50{
51 return QObject::tr( "Concave hulls" );
52}
53
54QString QgsConcaveHullByFeatureAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm calculates the concave hull for each feature in an input layer." ) + QStringLiteral( "\n\n" )
57 + 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" )
58 + 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" )
59 + QObject::tr( "See the 'Concave hull (by layer)' algorithm for a concave hull calculation which covers the whole layer or grouped subsets of features." );
60}
61
62QString QgsConcaveHullByFeatureAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Calculates the concave hull for each feature in an input layer." );
65}
66
67QgsConcaveHullByFeatureAlgorithm *QgsConcaveHullByFeatureAlgorithm::createInstance() const
68{
69 return new QgsConcaveHullByFeatureAlgorithm();
70}
71
72void QgsConcaveHullByFeatureAlgorithm::initParameters( const QVariantMap & )
73{
74 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 ) );
75 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "HOLES" ), QObject::tr( "Allow holes" ), true ) );
76}
77
78QList<int> QgsConcaveHullByFeatureAlgorithm::inputLayerTypes() const
79{
80 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
81}
82
83QgsFields QgsConcaveHullByFeatureAlgorithm::outputFields( const QgsFields &inputFields ) const
84{
85 QgsFields newFields;
86 newFields.append( QgsField( QStringLiteral( "area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
87 newFields.append( QgsField( QStringLiteral( "perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
88 return QgsProcessingUtils::combineFields( inputFields, newFields );
89}
90
91bool QgsConcaveHullByFeatureAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
92{
93#if GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 11
94 throw QgsProcessingException( QObject::tr( "This algorithm requires a QGIS build based on GEOS 3.11 or later" ) );
95#endif
96 mPercentage = parameterAsDouble( parameters, QStringLiteral( "ALPHA" ), context );
97 mAllowHoles = parameterAsBool( parameters, QStringLiteral( "HOLES" ), context );
98 return true;
99}
100
101QgsFeatureList QgsConcaveHullByFeatureAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
102{
103 QgsFeature f = feature;
104 if ( f.hasGeometry() )
105 {
106 QgsGeometry outputGeometry;
107 const QgsAbstractGeometry *inputGeometry = f.geometry().constGet();
109 if ( !collection || collection->numGeometries() == 1 )
110 {
111 feedback->reportError( QObject::tr( "Cannot calculate convex hull for a single point feature (%1) (try 'Concave hull (by layer)' algorithm instead)." ).arg( f.id() ) );
112 f.clearGeometry();
113 }
114 else
115 {
116 outputGeometry = f.geometry().concaveHull( mPercentage, mAllowHoles );
117 if ( outputGeometry.isNull() )
118 feedback->reportError( outputGeometry.lastError() );
119 f.setGeometry( outputGeometry );
120 }
121 if ( outputGeometry.type() == Qgis::GeometryType::Polygon )
122 {
123 QgsAttributes attrs = f.attributes();
124 attrs << outputGeometry.constGet()->area()
125 << outputGeometry.constGet()->perimeter();
126 f.setAttributes( attrs );
127 }
128 else
129 {
130 if ( outputGeometry.type() == Qgis::GeometryType::Line )
131 {
132 feedback->pushWarning( QObject::tr( "Concave hull for feature %1 resulted in a linestring, ignoring" ).arg( f.id() ) );
133 }
134 else if ( outputGeometry.type() == Qgis::GeometryType::Point )
135 {
136 feedback->pushWarning( QObject::tr( "Concave hull for feature %1 resulted in a point, ignoring" ).arg( f.id() ) );
137 }
138 QgsAttributes attrs = f.attributes();
139 attrs << QVariant()
140 << QVariant();
141 f.setAttributes( attrs );
142 }
143 }
144 return QgsFeatureList() << f;
145}
146
@ VectorPoint
Vector point layers.
Definition qgis.h:3534
@ Point
Points.
Definition qgis.h:359
@ Line
Lines.
Definition qgis.h:360
@ Polygon
Polygons.
Definition qgis.h:361
@ Double
Double/float values.
Definition qgis.h:3804
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:54
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:73
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).
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList