27using namespace Qt::StringLiterals;
31QString QgsKeepNBiggestPartsAlgorithm::name()
const
33 return u
"keepnbiggestparts"_s;
36QString QgsKeepNBiggestPartsAlgorithm::displayName()
const
38 return QObject::tr(
"Keep N biggest parts" );
41QStringList QgsKeepNBiggestPartsAlgorithm::tags()
const
43 return QObject::tr(
"remove,delete,drop,largest,area,filter" ).split(
',' );
46QString QgsKeepNBiggestPartsAlgorithm::group()
const
48 return QObject::tr(
"Vector geometry" );
51QString QgsKeepNBiggestPartsAlgorithm::groupId()
const
53 return u
"vectorgeometry"_s;
56QString QgsKeepNBiggestPartsAlgorithm::outputName()
const
58 return QObject::tr(
"Parts" );
61QList<int> QgsKeepNBiggestPartsAlgorithm::inputLayerTypes()
const
71QString QgsKeepNBiggestPartsAlgorithm::shortHelpString()
const
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."
79QString QgsKeepNBiggestPartsAlgorithm::shortDescription()
const
82 "Creates a polygon layer in which multipart geometries have been removed, "
83 "leaving only the n largest (in terms of area) parts."
87QgsKeepNBiggestPartsAlgorithm *QgsKeepNBiggestPartsAlgorithm::createInstance()
const
89 return new QgsKeepNBiggestPartsAlgorithm();
98void QgsKeepNBiggestPartsAlgorithm::initParameters(
const QVariantMap & )
101 partsToKeep->setIsDynamic(
true );
103 partsToKeep->setDynamicLayerParameterName( u
"POLYGONS"_s );
104 addParameter( partsToKeep.release() );
107QString QgsKeepNBiggestPartsAlgorithm::inputParameterName()
const
109 return u
"POLYGONS"_s;
114 mPartsToKeep = parameterAsInt( parameters, u
"PARTS"_s, context );
116 if ( mDynamicPartsToKeep )
117 mPartsToKeepProperty = parameters.value( u
"PARTS"_s ).value<
QgsProperty>();
133 int nPartsToKeep = mPartsToKeep;
134 if ( mDynamicPartsToKeep )
135 nPartsToKeep = mPartsToKeepProperty.valueAsInt( context.
expressionContext(), nPartsToKeep );
137 const int numParts = collection->numGeometries();
138 if ( nPartsToKeep >= numParts )
141 outputGeometry = geometry;
145 struct GreaterThanByArea
147 bool operator()(
const QgsAbstractGeometry *lhs,
const QgsAbstractGeometry *rhs )
const {
return lhs->
area() < rhs->
area(); }
151 std::priority_queue<const QgsAbstractGeometry *, std::vector<const QgsAbstractGeometry *>, GreaterThanByArea> areaQueue;
152 for (
int i = 0; i < numParts; ++i )
154 areaQueue.push( collection->geometryN( i ) );
157 for (
int i = 0; i < nPartsToKeep; ++i )
161 res->addGeometry( part->
clone() );
169 outputGeometry = geometry;
ProcessingSourceType
Processing data source types.
@ 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.
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...
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 ¶meters, 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.
@ IntegerPositive
Positive integer values (including 0).
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