QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
19
21#include "qgssurface.h"
22
24
25QString QgsRemovePartsByAreaAlgorithm::name() const
26{
27 return QStringLiteral( "removepartsbyarea" );
28}
29
30QString QgsRemovePartsByAreaAlgorithm::displayName() const
31{
32 return QObject::tr( "Remove parts by area" );
33}
34
35QStringList QgsRemovePartsByAreaAlgorithm::tags() const
36{
37 return QObject::tr( "remove,delete,drop,filter,polygon,size" ).split( ',' );
38}
39
40QString QgsRemovePartsByAreaAlgorithm::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsRemovePartsByAreaAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50QString QgsRemovePartsByAreaAlgorithm::outputName() const
51{
52 return QObject::tr( "Cleaned" );
53}
54
55QList<int> QgsRemovePartsByAreaAlgorithm::inputLayerTypes() const
56{
57 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorPolygon );
58}
59
60Qgis::ProcessingSourceType QgsRemovePartsByAreaAlgorithm::outputLayerType() const
61{
63}
64
65QString QgsRemovePartsByAreaAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Removes polygons which are smaller than a specified area." );
68}
69
70QString QgsRemovePartsByAreaAlgorithm::shortHelpString() const
71{
72 return QObject::tr( "This algorithm takes a polygon layer and removes polygons which are smaller than a specified area.\n\n"
73 "If the input geometry is a multipart geometry, then the parts will be filtered by their individual areas. If no parts match the "
74 "required minimum area, then the feature will be skipped and omitted from the output layer.\n\n"
75 "If the input geometry is a singlepart geometry, then the feature will be skipped if the geometry's "
76 "area is below the required size and omitted from the output layer.\n\n"
77 "The area will be calculated using Cartesian calculations in the source layer's coordinate reference system.\n\n"
78 "Attributes are not modified." );
79}
80
81QgsRemovePartsByAreaAlgorithm *QgsRemovePartsByAreaAlgorithm::createInstance() const
82{
83 return new QgsRemovePartsByAreaAlgorithm();
84}
85
86Qgis::ProcessingFeatureSourceFlags QgsRemovePartsByAreaAlgorithm::sourceFlags() const
87{
88 // skip geometry checks - this algorithm can be used to repair geometries
90}
91
92void QgsRemovePartsByAreaAlgorithm::initParameters( const QVariantMap & )
93{
94 auto minArea = std::make_unique< QgsProcessingParameterArea >( QStringLiteral( "MIN_AREA" ), QObject::tr( "Remove parts with area less than" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
95 minArea->setIsDynamic( true );
96 minArea->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MIN_AREA" ), QObject::tr( "Remove parts with area less than" ), QgsPropertyDefinition::DoublePositive ) );
97 minArea->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
98 addParameter( minArea.release() );
99}
100
101bool QgsRemovePartsByAreaAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
102{
103 mMinArea = parameterAsDouble( parameters, QStringLiteral( "MIN_AREA" ), context );
104 mDynamicMinArea = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN_AREA" ) );
105 if ( mDynamicMinArea )
106 mMinAreaProperty = parameters.value( QStringLiteral( "MIN_AREA" ) ).value< QgsProperty >();
107
108 return true;
109}
110
111QgsFeatureList QgsRemovePartsByAreaAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
112{
113 QgsFeature f = feature;
114 if ( f.hasGeometry() )
115 {
116 double minArea = mMinArea;
117 if ( mDynamicMinArea )
118 minArea = mMinAreaProperty.valueAsDouble( context.expressionContext(), minArea );
119
120 const QgsGeometry geometry = f.geometry();
121 QgsGeometry outputGeometry;
122 if ( const QgsGeometryCollection *inputCollection = qgsgeometry_cast< const QgsGeometryCollection * >( geometry.constGet() ) )
123 {
124 std::unique_ptr< QgsGeometryCollection> filteredGeometry( inputCollection->createEmptyWithSameType() );
125 const int size = inputCollection->numGeometries();
126 filteredGeometry->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 filteredGeometry->addGeometry( surface->clone() );
134 }
135 }
136 }
137 if ( filteredGeometry->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:3531
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3536
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
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.
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
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList