QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmremoveholes.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmremoveholes.cpp
3 ---------------------
4 begin : March 2018
5 copyright : (C) 2018 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 QgsRemoveHolesAlgorithm::name() const
27{
28 return u"deleteholes"_s;
29}
30
31QString QgsRemoveHolesAlgorithm::displayName() const
32{
33 return QObject::tr( "Delete holes" );
34}
35
36QStringList QgsRemoveHolesAlgorithm::tags() const
37{
38 return QObject::tr( "remove,delete,drop,holes,rings,fill" ).split( ',' );
39}
40
41QString QgsRemoveHolesAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsRemoveHolesAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsRemoveHolesAlgorithm::outputName() const
52{
53 return QObject::tr( "Cleaned" );
54}
55
56QList<int> QgsRemoveHolesAlgorithm::inputLayerTypes() const
57{
58 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
59}
60
61Qgis::ProcessingSourceType QgsRemoveHolesAlgorithm::outputLayerType() const
62{
64}
65
66QString QgsRemoveHolesAlgorithm::shortHelpString() const
67{
68 return QObject::tr( "This algorithm takes a polygon layer and removes holes in polygons. It creates a new vector "
69 "layer in which polygons with holes have been replaced by polygons with only their external ring. "
70 "Attributes are not modified.\n\n"
71 "An optional minimum area parameter allows removing only holes which are smaller than a specified "
72 "area threshold. Leaving this parameter as 0.0 results in all holes being removed." );
73}
74
75QString QgsRemoveHolesAlgorithm::shortDescription() const
76{
77 return QObject::tr( "Creates a vector layer in which polygons with holes are replaced by polygons with only their external ring." );
78}
79
80QgsRemoveHolesAlgorithm *QgsRemoveHolesAlgorithm::createInstance() const
81{
82 return new QgsRemoveHolesAlgorithm();
83}
84
85Qgis::ProcessingFeatureSourceFlags QgsRemoveHolesAlgorithm::sourceFlags() const
86{
87 // skip geometry checks - this algorithm can be used to repair geometries
89}
90
91void QgsRemoveHolesAlgorithm::initParameters( const QVariantMap & )
92{
93 auto minArea = std::make_unique<QgsProcessingParameterArea>( u"MIN_AREA"_s, QObject::tr( "Remove holes with area less than" ), 0.0, u"INPUT"_s, false, 0 );
94 minArea->setIsDynamic( true );
95 minArea->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MIN_AREA"_s, QObject::tr( "Remove holes with area less than" ), QgsPropertyDefinition::DoublePositive ) );
96 minArea->setDynamicLayerParameterName( u"INPUT"_s );
97 addParameter( minArea.release() );
98}
99
100bool QgsRemoveHolesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mMinArea = parameterAsDouble( parameters, u"MIN_AREA"_s, context );
103 mDynamicMinArea = QgsProcessingParameters::isDynamic( parameters, u"MIN_AREA"_s );
104 if ( mDynamicMinArea )
105 mMinAreaProperty = parameters.value( u"MIN_AREA"_s ).value<QgsProperty>();
106
107 return true;
108}
109
110QgsFeatureList QgsRemoveHolesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
111{
112 QgsFeature f = feature;
113 if ( f.hasGeometry() )
114 {
115 const QgsGeometry geometry = f.geometry();
116
117 double minArea = mMinArea;
118 if ( mDynamicMinArea )
119 minArea = mMinAreaProperty.valueAsDouble( context.expressionContext(), minArea );
120
121 f.setGeometry( geometry.removeInteriorRings( minArea > 0 ? minArea : -1 ) );
122 }
123 return QgsFeatureList() << f;
124}
125
126
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
QgsGeometry removeInteriorRings(double minimumAllowedArea=-1) const
Removes the interior rings from a (multi)polygon geometry.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:47
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:58
A store for object properties.
QList< QgsFeature > QgsFeatureList