QGIS API Documentation 3.99.0-Master (f78f5286a64)
qgsalgorithmremovepartsbyarea.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmremovepartsbyarea.cpp
3 ---------------------
4 begin : July 2024
5 copyright : (C) 2024 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
20#include "qgssurface.h"
21
23
24QString QgsRemovePartsByAreaAlgorithm::name() const
25{
26 return QStringLiteral( "removepartsbyarea" );
27}
28
29QString QgsRemovePartsByAreaAlgorithm::displayName() const
30{
31 return QObject::tr( "Remove parts by area" );
32}
33
34QStringList QgsRemovePartsByAreaAlgorithm::tags() const
35{
36 return QObject::tr( "remove,delete,drop,filter,polygon,size" ).split( ',' );
37}
38
39QString QgsRemovePartsByAreaAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsRemovePartsByAreaAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsRemovePartsByAreaAlgorithm::outputName() const
50{
51 return QObject::tr( "Cleaned" );
52}
53
54QList<int> QgsRemovePartsByAreaAlgorithm::inputLayerTypes() const
55{
56 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorPolygon );
57}
58
59Qgis::ProcessingSourceType QgsRemovePartsByAreaAlgorithm::outputLayerType() const
60{
62}
63
64QString QgsRemovePartsByAreaAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Removes polygons which are smaller than a specified area." );
67}
68
69QString QgsRemovePartsByAreaAlgorithm::shortHelpString() const
70{
71 return QObject::tr( "This algorithm takes a polygon layer and removes polygons which are smaller than a specified area.\n\n"
72 "If the input geometry is a multipart geometry, then the parts will be filtered by their individual areas. If no parts match the "
73 "required minimum area, then the feature will be skipped and omitted from the output layer.\n\n"
74 "If the input geometry is a singlepart geometry, then the feature will be skipped if the geometry's "
75 "area is below the required size and omitted from the output layer.\n\n"
76 "The area will be calculated using Cartesian calculations in the source layer's coordinate reference system.\n\n"
77 "Attributes are not modified." );
78}
79
80QgsRemovePartsByAreaAlgorithm *QgsRemovePartsByAreaAlgorithm::createInstance() const
81{
82 return new QgsRemovePartsByAreaAlgorithm();
83}
84
85Qgis::ProcessingFeatureSourceFlags QgsRemovePartsByAreaAlgorithm::sourceFlags() const
86{
87 // skip geometry checks - this algorithm can be used to repair geometries
89}
90
91void QgsRemovePartsByAreaAlgorithm::initParameters( const QVariantMap & )
92{
93 std::unique_ptr< QgsProcessingParameterArea > minArea = std::make_unique< QgsProcessingParameterArea >( QStringLiteral( "MIN_AREA" ), QObject::tr( "Remove parts with area less than" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
94 minArea->setIsDynamic( true );
95 minArea->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MIN_AREA" ), QObject::tr( "Remove parts with area less than" ), QgsPropertyDefinition::DoublePositive ) );
96 minArea->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
97 addParameter( minArea.release() );
98}
99
100bool QgsRemovePartsByAreaAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mMinArea = parameterAsDouble( parameters, QStringLiteral( "MIN_AREA" ), context );
103 mDynamicMinArea = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN_AREA" ) );
104 if ( mDynamicMinArea )
105 mMinAreaProperty = parameters.value( QStringLiteral( "MIN_AREA" ) ).value< QgsProperty >();
106
107 return true;
108}
109
110QgsFeatureList QgsRemovePartsByAreaAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
111{
112 QgsFeature f = feature;
113 if ( f.hasGeometry() )
114 {
115 double minArea = mMinArea;
116 if ( mDynamicMinArea )
117 minArea = mMinAreaProperty.valueAsDouble( context.expressionContext(), minArea );
118
119 const QgsGeometry geometry = f.geometry();
120 QgsGeometry outputGeometry;
121 if ( const QgsGeometryCollection *inputCollection = qgsgeometry_cast< const QgsGeometryCollection * >( geometry.constGet() ) )
122 {
123 std::unique_ptr< QgsAbstractGeometry> filteredGeometry( geometry.constGet()->createEmptyWithSameType() );
124 QgsGeometryCollection *collection = qgsgeometry_cast< QgsGeometryCollection * >( filteredGeometry.get() );
125 const int size = inputCollection->numGeometries();
126 collection->reserve( size );
127 for ( int i = 0; i < size; ++i )
128 {
129 if ( const QgsSurface *surface = qgsgeometry_cast< const QgsSurface * >( inputCollection->geometryN( i ) ) )
130 {
131 if ( surface->area() >= minArea )
132 {
133 collection->addGeometry( surface->clone() );
134 }
135 }
136 }
137 if ( collection->numGeometries() == 0 )
138 {
139 // skip empty features
140 return {};
141 }
142 outputGeometry = QgsGeometry( std::move( filteredGeometry ) );
143 f.setGeometry( outputGeometry );
144 }
145 else if ( const QgsSurface *surface = qgsgeometry_cast< const QgsSurface * >( geometry.constGet() ) )
146 {
147 if ( surface->area() < minArea )
148 {
149 return {};
150 }
151 }
152 else
153 {
154 return {};
155 }
156 }
157 return { f };
158}
159
160
ProcessingSourceType
Processing data source types.
Definition qgis.h:3434
@ VectorPolygon
Vector polygon layers.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3623
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
void reserve(int size)
Attempts to allocate memory for at least size geometries.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
int numGeometries() const
Returns the number of geometries within the collection.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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:45
@ DoublePositive
Positive double value (including 0)
Definition qgsproperty.h:56
A store for object properties.
Surface geometry type.
Definition qgssurface.h:34
QList< QgsFeature > QgsFeatureList