QGIS API Documentation 3.43.0-Master (6c62b930b02)
qgsalgorithmkeepnbiggestparts.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmkeepnbiggestparts.cpp
3 ---------------------
4 begin : July 2023
5 copyright : (C) 2023 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 "qgsmultisurface.h"
21#include "qgsmultipolygon.h"
22#include <queue>
23
25
26QString QgsKeepNBiggestPartsAlgorithm::name() const
27{
28 return QStringLiteral( "keepnbiggestparts" );
29}
30
31QString QgsKeepNBiggestPartsAlgorithm::displayName() const
32{
33 return QObject::tr( "Keep N biggest parts" );
34}
35
36QStringList QgsKeepNBiggestPartsAlgorithm::tags() const
37{
38 return QObject::tr( "remove,delete,drop,largest,area,filter" ).split( ',' );
39}
40
41QString QgsKeepNBiggestPartsAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsKeepNBiggestPartsAlgorithm::groupId() const
47{
48 return QStringLiteral( "vectorgeometry" );
49}
50
51QString QgsKeepNBiggestPartsAlgorithm::outputName() const
52{
53 return QObject::tr( "Parts" );
54}
55
56QList<int> QgsKeepNBiggestPartsAlgorithm::inputLayerTypes() const
57{
58 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
59}
60
61Qgis::ProcessingSourceType QgsKeepNBiggestPartsAlgorithm::outputLayerType() const
62{
64}
65
66QString QgsKeepNBiggestPartsAlgorithm::shortHelpString() const
67{
68 return QObject::tr( "This algorithm takes a polygon layer and creates a new polygon layer in which multipart "
69 "geometries have been removed, leaving only the n largest (in terms of area) parts." );
70}
71
72QString QgsKeepNBiggestPartsAlgorithm::shortDescription() const
73{
74 return QObject::tr( "Creates a polygon layer in which multipart geometries have been removed, "
75 "leaving only the n largest (in terms of area) parts." );
76}
77
78QgsKeepNBiggestPartsAlgorithm *QgsKeepNBiggestPartsAlgorithm::createInstance() const
79{
80 return new QgsKeepNBiggestPartsAlgorithm();
81}
82
83Qgis::ProcessingFeatureSourceFlags QgsKeepNBiggestPartsAlgorithm::sourceFlags() const
84{
85 // skip geometry checks - this algorithm can be used to repair geometries
87}
88
89void QgsKeepNBiggestPartsAlgorithm::initParameters( const QVariantMap & )
90{
91 auto partsToKeep = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "PARTS" ), QObject::tr( "Parts to keep" ), Qgis::ProcessingNumberParameterType::Integer, 1.0, false, 1.0 );
92 partsToKeep->setIsDynamic( true );
93 partsToKeep->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "PARTS" ), QObject::tr( "Parts to keep" ), QgsPropertyDefinition::IntegerPositive ) );
94 partsToKeep->setDynamicLayerParameterName( QStringLiteral( "POLYGONS" ) );
95 addParameter( partsToKeep.release() );
96}
97
98QString QgsKeepNBiggestPartsAlgorithm::inputParameterName() const
99{
100 return QStringLiteral( "POLYGONS" );
101}
102
103bool QgsKeepNBiggestPartsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
104{
105 mPartsToKeep = parameterAsInt( parameters, QStringLiteral( "PARTS" ), context );
106 mDynamicPartsToKeep = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "PARTS" ) );
107 if ( mDynamicPartsToKeep )
108 mPartsToKeepProperty = parameters.value( QStringLiteral( "PARTS" ) ).value<QgsProperty>();
109
110 return true;
111}
112
113QgsFeatureList QgsKeepNBiggestPartsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
114{
115 QgsFeature f = feature;
116 if ( f.hasGeometry() )
117 {
118 const QgsGeometry geometry = f.geometry();
119
120 QgsGeometry outputGeometry;
121
122 if ( const QgsGeometryCollection *collection = qgsgeometry_cast<const QgsGeometryCollection *>( geometry.constGet() ) )
123 {
124 int nPartsToKeep = mPartsToKeep;
125 if ( mDynamicPartsToKeep )
126 nPartsToKeep = mPartsToKeepProperty.valueAsInt( context.expressionContext(), nPartsToKeep );
127
128 const int numParts = collection->numGeometries();
129 if ( nPartsToKeep >= numParts )
130 {
131 // nothing to do
132 outputGeometry = geometry;
133 }
134 else
135 {
136 struct GreaterThanByArea
137 {
138 bool operator()( const QgsAbstractGeometry *lhs, const QgsAbstractGeometry *rhs ) const
139 {
140 return lhs->area() < rhs->area();
141 }
142 };
143
144 std::unique_ptr<QgsMultiSurface> res = QgsWkbTypes::isCurvedType( collection->wkbType() ) ? std::make_unique<QgsMultiSurface>() : std::make_unique<QgsMultiPolygon>();
145 std::priority_queue<const QgsAbstractGeometry *, std::vector<const QgsAbstractGeometry *>, GreaterThanByArea> areaQueue;
146 for ( int i = 0; i < numParts; ++i )
147 {
148 areaQueue.push( collection->geometryN( i ) );
149 }
150
151 for ( int i = 0; i < nPartsToKeep; ++i )
152 {
153 const QgsAbstractGeometry *part = areaQueue.top();
154 areaQueue.pop();
155 res->addGeometry( part->clone() );
156 }
157
158 outputGeometry = QgsGeometry( std::move( res ) );
159 }
160 }
161 else
162 {
163 outputGeometry = geometry;
164 outputGeometry.convertToMultiType();
165 }
166
167 f.setGeometry( outputGeometry );
168 }
169 return QgsFeatureList() << f;
170}
171
172
ProcessingSourceType
Processing data source types.
Definition qgis.h:3415
@ 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:3604
Abstract base class for all geometries.
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
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.
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.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
Multi polygon geometry collection.
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
@ IntegerPositive
Positive integer values (including 0)
Definition qgsproperty.h:53
A store for object properties.
static bool isCurvedType(Qgis::WkbType type)
Returns true if the WKB type is a curved type or can contain curved geometries.
QList< QgsFeature > QgsFeatureList