QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
69 "This algorithm takes a polygon layer and removes holes in polygons. It creates a new vector "
70 "layer in which polygons with holes have been replaced by polygons with only their external ring. "
71 "Attributes are not modified.\n\n"
72 "An optional minimum area parameter allows removing only holes which are smaller than a specified "
73 "area threshold. Leaving this parameter as 0.0 results in all holes being removed."
74 );
75}
76
77QString QgsRemoveHolesAlgorithm::shortDescription() const
78{
79 return QObject::tr( "Creates a vector layer in which polygons with holes are replaced by polygons with only their external ring." );
80}
81
82QgsRemoveHolesAlgorithm *QgsRemoveHolesAlgorithm::createInstance() const
83{
84 return new QgsRemoveHolesAlgorithm();
85}
86
87Qgis::ProcessingFeatureSourceFlags QgsRemoveHolesAlgorithm::sourceFlags() const
88{
89 // skip geometry checks - this algorithm can be used to repair geometries
91}
92
93void QgsRemoveHolesAlgorithm::initParameters( const QVariantMap & )
94{
95 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 );
96 minArea->setIsDynamic( true );
97 minArea->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MIN_AREA"_s, QObject::tr( "Remove holes with area less than" ), QgsPropertyDefinition::DoublePositive ) );
98 minArea->setDynamicLayerParameterName( u"INPUT"_s );
99 addParameter( minArea.release() );
100}
101
102bool QgsRemoveHolesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
103{
104 mMinArea = parameterAsDouble( parameters, u"MIN_AREA"_s, context );
105 mDynamicMinArea = QgsProcessingParameters::isDynamic( parameters, u"MIN_AREA"_s );
106 if ( mDynamicMinArea )
107 mMinAreaProperty = parameters.value( u"MIN_AREA"_s ).value<QgsProperty>();
108
109 return true;
110}
111
112QgsFeatureList QgsRemoveHolesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
113{
114 QgsFeature f = feature;
115 if ( f.hasGeometry() )
116 {
117 const QgsGeometry geometry = f.geometry();
118
119 double minArea = mMinArea;
120 if ( mDynamicMinArea )
121 minArea = mMinAreaProperty.valueAsDouble( context.expressionContext(), minArea );
122
123 f.setGeometry( geometry.removeInteriorRings( minArea > 0 ? minArea : -1 ) );
124 }
125 return QgsFeatureList() << f;
126}
127
128
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
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:57
A store for object properties.
QList< QgsFeature > QgsFeatureList