QGIS API Documentation 4.3.0-Master (52aa6503bed)
Loading...
Searching...
No Matches
qgsalgorithmconcavehullofpolygons.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmconcavehullofpolygons.cpp
3 ---------------------
4 begin : March 2026
5 copyright : (C) 2026 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 "qgsgeos.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsConcaveHullOfPolygonsAlgorithm::name() const
29{
30 return u"tightconcavehullofpolygons"_s;
31}
32
33QString QgsConcaveHullOfPolygonsAlgorithm::displayName() const
34{
35 return QObject::tr( "Fill gaps between polygons" );
36}
37
38QStringList QgsConcaveHullOfPolygonsAlgorithm::tags() const
39{
40 return QObject::tr( "concave,hull,bounds,bounding,convex,multipolygons,boundary,fill,holes,between,space,tight,strict" ).split( ',' );
41}
42
43QString QgsConcaveHullOfPolygonsAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsConcaveHullOfPolygonsAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsConcaveHullOfPolygonsAlgorithm::outputName() const
54{
55 return QObject::tr( "Tight concave hulls" );
56}
57
58QString QgsConcaveHullOfPolygonsAlgorithm::shortHelpString() const
59{
60 return QObject::tr( "This algorithm calculates a concave hull for each multi-polygon feature in an input layer." )
61 + u"\n\n"_s
62 + QObject::tr(
63 "Unlike the standard Concave Hull algorithm, a tight concave hull is a (possibly) non-convex polygon containing all the input polygons. The computed hull fills the gaps between the "
64 "polygons without intersecting their interiors. It strictly follows the outer boundaries of the input polygons, allowing you to fill gaps between them without distorting their original "
65 "shapes."
66 )
67 + u"\n\n"_s
68 + QObject::tr(
69 "It is particularly useful for cases such as generalizing groups of building outlines, creating 'district' polygons around blocks, or removing gaps and joining disjoint sets of polygons."
70 )
71 + u"\n\n"_s
72 + QObject::tr( "The algorithm works by creating a constrained Delaunay Triangulation of the space between the polygons and removing the longest outer edges until a target criterion is reached." )
73 + u"\n\n"_s
74 + QObject::tr( "The input geometry must be a valid Polygon or MultiPolygon (i.e., the individual polygons must not overlap)." );
75}
76
77QString QgsConcaveHullOfPolygonsAlgorithm::shortDescription() const
78{
79 return QObject::tr( "Constructs a tight concave hull for a set of polygons, filling gaps between them while strictly preserving their original outer boundaries." );
80}
81
82QgsConcaveHullOfPolygonsAlgorithm *QgsConcaveHullOfPolygonsAlgorithm::createInstance() const
83{
84 return new QgsConcaveHullOfPolygonsAlgorithm();
85}
86
87void QgsConcaveHullOfPolygonsAlgorithm::initParameters( const QVariantMap & )
88{
89 auto ratioParam
90 = std::make_unique<QgsProcessingParameterNumber>( u"RATIO"_s, QObject::tr( "Ratio (0-1, where 1 is equivalent with Convex Hull)" ), Qgis::ProcessingNumberParameterType::Double, 0.3, false, 0, 1 );
91 ratioParam->setHelp(
92 QObject::tr(
93 "Specifies the maximum edge length as a fraction of the difference between the longest and shortest edge lengths between the polygons. This normalizes the maximum edge length to be scale-free. "
94 "A value of 1 produces the convex hull; a value of 0 produces the original polygons."
95 )
96 );
97 addParameter( ratioParam.release() );
98
99 auto holesParam = std::make_unique<QgsProcessingParameterBoolean>( u"HOLES"_s, QObject::tr( "Allow holes" ), true );
100 holesParam->setHelp( QObject::tr( "Controls whether the computed concave hull is allowed to contain holes." ) );
101 addParameter( holesParam.release() );
102}
103
104QList<int> QgsConcaveHullOfPolygonsAlgorithm::inputLayerTypes() const
105{
106 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
107}
108
109QgsFields QgsConcaveHullOfPolygonsAlgorithm::outputFields( const QgsFields &inputFields ) const
110{
111 QgsFields newFields;
112 newFields.append( QgsField( u"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
113 newFields.append( QgsField( u"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
114 return QgsProcessingUtils::combineFields( inputFields, newFields );
115}
116
117bool QgsConcaveHullOfPolygonsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
118{
119#if GEOS_VERSION_NUM < GEOS_COMPUTE_VERSION( 3, 11, 0 )
120 throw QgsProcessingException( QObject::tr( "This algorithm requires a QGIS build based on GEOS 3.11 or later" ) );
121#endif
122 mPercentage = parameterAsDouble( parameters, u"RATIO"_s, context );
123 mAllowHoles = parameterAsBool( parameters, u"HOLES"_s, context );
124 return true;
125}
126
127QgsFeatureList QgsConcaveHullOfPolygonsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback )
128{
129 QGS_MARK_ALGORITHM_SOURCE
130
131 QgsFeature f = feature;
132 if ( f.hasGeometry() )
133 {
134 QgsGeometry outputGeometry;
135 outputGeometry = f.geometry().concaveHullOfPolygons( mPercentage, mAllowHoles, true );
136 if ( outputGeometry.isNull() && !outputGeometry.lastError().isEmpty() )
137 feedback->reportError( outputGeometry.lastError() );
138 f.setGeometry( outputGeometry );
139
140 if ( outputGeometry.type() == Qgis::GeometryType::Polygon )
141 {
142 QgsAttributes attrs = f.attributes();
143 attrs << outputGeometry.constGet()->area() << outputGeometry.constGet()->perimeter();
144 f.setAttributes( attrs );
145 }
146 else
147 {
148 if ( outputGeometry.type() == Qgis::GeometryType::Line )
149 {
150 feedback->pushWarning( QObject::tr( "Concave hull for feature %1 resulted in a linestring, ignoring" ).arg( f.id() ) );
151 }
152 else if ( outputGeometry.type() == Qgis::GeometryType::Point )
153 {
154 feedback->pushWarning( QObject::tr( "Concave hull for feature %1 resulted in a point, ignoring" ).arg( f.id() ) );
155 }
156 QgsAttributes attrs = f.attributes();
157 attrs << QVariant() << QVariant();
158 f.setAttributes( attrs );
159 }
160 }
161 return QgsFeatureList() << f;
162}
163
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3774
@ Point
Points.
Definition qgis.h:400
@ Line
Lines.
Definition qgis.h:401
@ Polygon
Polygons.
Definition qgis.h:402
@ Double
Double/float values.
Definition qgis.h:4067
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.
QgsGeometry concaveHullOfPolygons(double lengthRatio, bool allowHoles=false, bool isTight=false, QgsFeedback *feedback=nullptr) const
Constructs a concave hull of a set of polygons, respecting the polygons as constraints.
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
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.
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