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