QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
21 
22 QString QgsRemoveHolesAlgorithm::name() const
23 {
24  return QStringLiteral( "deleteholes" );
25 }
26 
27 QString QgsRemoveHolesAlgorithm::displayName() const
28 {
29  return QObject::tr( "Delete holes" );
30 }
31 
32 QStringList QgsRemoveHolesAlgorithm::tags() const
33 {
34  return QObject::tr( "remove,delete,drop,holes,rings,fill" ).split( ',' );
35 }
36 
37 QString QgsRemoveHolesAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsRemoveHolesAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsRemoveHolesAlgorithm::outputName() const
48 {
49  return QObject::tr( "Cleaned" );
50 }
51 
52 QList<int> QgsRemoveHolesAlgorithm::inputLayerTypes() const
53 {
54  return QList<int>() << QgsProcessing::TypeVectorPolygon;
55 }
56 
57 QgsProcessing::SourceType QgsRemoveHolesAlgorithm::outputLayerType() const
58 {
60 }
61 
62 QString QgsRemoveHolesAlgorithm::shortHelpString() const
63 {
64  return QObject::tr( "This algorithm takes a polygon layer and removes holes in polygons. It creates a new vector "
65  "layer in which polygons with holes have been replaced by polygons with only their external ring. "
66  "Attributes are not modified.\n\n"
67  "An optional minimum area parameter allows removing only holes which are smaller than a specified "
68  "area threshold. Leaving this parameter as 0.0 results in all holes being removed." );
69 }
70 
71 QgsRemoveHolesAlgorithm *QgsRemoveHolesAlgorithm::createInstance() const
72 {
73  return new QgsRemoveHolesAlgorithm();
74 }
75 
76 QgsProcessingFeatureSource::Flag QgsRemoveHolesAlgorithm::sourceFlags() const
77 {
78  // skip geometry checks - this algorithm can be used to repair geometries
80 }
81 
82 void QgsRemoveHolesAlgorithm::initParameters( const QVariantMap & )
83 {
84  std::unique_ptr< QgsProcessingParameterNumber > minArea = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MIN_AREA" ),
85  QObject::tr( "Remove holes with area less than" ), QgsProcessingParameterNumber::Double,
86  0.0, false, 0 );
87  minArea->setIsDynamic( true );
88  minArea->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MIN_AREA" ), QObject::tr( "Remove holes with area less than" ), QgsPropertyDefinition::DoublePositive ) );
89  minArea->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
90  addParameter( minArea.release() );
91 }
92 
93 bool QgsRemoveHolesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
94 {
95  mMinArea = parameterAsDouble( parameters, QStringLiteral( "MIN_AREA" ), context );
96  mDynamicMinArea = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN_AREA" ) );
97  if ( mDynamicMinArea )
98  mMinAreaProperty = parameters.value( QStringLiteral( "MIN_AREA" ) ).value< QgsProperty >();
99 
100  return true;
101 }
102 
103 QgsFeatureList QgsRemoveHolesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
104 {
105  QgsFeature f = feature;
106  if ( f.hasGeometry() )
107  {
108  const QgsGeometry geometry = f.geometry();
109 
110  double minArea = mMinArea;
111  if ( mDynamicMinArea )
112  minArea = mMinAreaProperty.valueAsDouble( context.expressionContext(), minArea );
113 
114  f.setGeometry( geometry.removeInteriorRings( minArea > 0 ? minArea : -1 ) );
115  }
116  return QgsFeatureList() << f;
117 }
118 
119 
121 
122 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:2187
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:114
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsGeometry::removeInteriorRings
QgsGeometry removeInteriorRings(double minimumAllowedArea=-1) const
Removes the interior rings from a (multi)polygon geometry.
Definition: qgsgeometry.cpp:861
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
qgsalgorithmremoveholes.h
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:582
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:149
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
QgsPropertyDefinition::DoublePositive
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:58