QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
74 "This algorithm takes a polygon layer and creates a new polygon layer in which multipart "
75 "geometries have been removed, leaving only the n largest (in terms of area) parts."
76 );
77}
78
79QString QgsKeepNBiggestPartsAlgorithm::shortDescription() const
80{
81 return QObject::tr(
82 "Creates a polygon layer in which multipart geometries have been removed, "
83 "leaving only the n largest (in terms of area) parts."
84 );
85}
86
87QgsKeepNBiggestPartsAlgorithm *QgsKeepNBiggestPartsAlgorithm::createInstance() const
88{
89 return new QgsKeepNBiggestPartsAlgorithm();
90}
91
92Qgis::ProcessingFeatureSourceFlags QgsKeepNBiggestPartsAlgorithm::sourceFlags() const
93{
94 // skip geometry checks - this algorithm can be used to repair geometries
96}
97
98void QgsKeepNBiggestPartsAlgorithm::initParameters( const QVariantMap & )
99{
100 auto partsToKeep = std::make_unique<QgsProcessingParameterNumber>( u"PARTS"_s, QObject::tr( "Parts to keep" ), Qgis::ProcessingNumberParameterType::Integer, 1.0, false, 1.0 );
101 partsToKeep->setIsDynamic( true );
102 partsToKeep->setDynamicPropertyDefinition( QgsPropertyDefinition( u"PARTS"_s, QObject::tr( "Parts to keep" ), QgsPropertyDefinition::IntegerPositive ) );
103 partsToKeep->setDynamicLayerParameterName( u"POLYGONS"_s );
104 addParameter( partsToKeep.release() );
105}
106
107QString QgsKeepNBiggestPartsAlgorithm::inputParameterName() const
108{
109 return u"POLYGONS"_s;
110}
111
112bool QgsKeepNBiggestPartsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
113{
114 mPartsToKeep = parameterAsInt( parameters, u"PARTS"_s, context );
115 mDynamicPartsToKeep = QgsProcessingParameters::isDynamic( parameters, u"PARTS"_s );
116 if ( mDynamicPartsToKeep )
117 mPartsToKeepProperty = parameters.value( u"PARTS"_s ).value<QgsProperty>();
118
119 return true;
120}
121
122QgsFeatureList QgsKeepNBiggestPartsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
123{
124 QgsFeature f = feature;
125 if ( f.hasGeometry() )
126 {
127 const QgsGeometry geometry = f.geometry();
128
129 QgsGeometry outputGeometry;
130
132 {
133 int nPartsToKeep = mPartsToKeep;
134 if ( mDynamicPartsToKeep )
135 nPartsToKeep = mPartsToKeepProperty.valueAsInt( context.expressionContext(), nPartsToKeep );
136
137 const int numParts = collection->numGeometries();
138 if ( nPartsToKeep >= numParts )
139 {
140 // nothing to do
141 outputGeometry = geometry;
142 }
143 else
144 {
145 struct GreaterThanByArea
146 {
147 bool operator()( const QgsAbstractGeometry *lhs, const QgsAbstractGeometry *rhs ) const { return lhs->area() < rhs->area(); }
148 };
149
150 std::unique_ptr<QgsMultiSurface> res = QgsWkbTypes::isCurvedType( collection->wkbType() ) ? std::make_unique<QgsMultiSurface>() : std::make_unique<QgsMultiPolygon>();
151 std::priority_queue<const QgsAbstractGeometry *, std::vector<const QgsAbstractGeometry *>, GreaterThanByArea> areaQueue;
152 for ( int i = 0; i < numParts; ++i )
153 {
154 areaQueue.push( collection->geometryN( i ) );
155 }
156
157 for ( int i = 0; i < nPartsToKeep; ++i )
158 {
159 const QgsAbstractGeometry *part = areaQueue.top();
160 areaQueue.pop();
161 res->addGeometry( part->clone() );
162 }
163
164 outputGeometry = QgsGeometry( std::move( res ) );
165 }
166 }
167 else
168 {
169 outputGeometry = geometry;
170 outputGeometry.convertToMultiType();
171 }
172
173 f.setGeometry( outputGeometry );
174 }
175 return QgsFeatureList() << f;
176}
177
178
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
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:54
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