QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsRemovePartsByAreaAlgorithm::name() const
30{
31 return u"removepartsbyarea"_s;
32}
33
34QString QgsRemovePartsByAreaAlgorithm::displayName() const
35{
36 return QObject::tr( "Remove parts by area" );
37}
38
39QStringList QgsRemovePartsByAreaAlgorithm::tags() const
40{
41 return QObject::tr( "remove,delete,drop,filter,polygon,size" ).split( ',' );
42}
43
44QString QgsRemovePartsByAreaAlgorithm::group() const
45{
46 return QObject::tr( "Vector geometry" );
47}
48
49QString QgsRemovePartsByAreaAlgorithm::groupId() const
50{
51 return u"vectorgeometry"_s;
52}
53
54QString QgsRemovePartsByAreaAlgorithm::outputName() const
55{
56 return QObject::tr( "Cleaned" );
57}
58
59QList<int> QgsRemovePartsByAreaAlgorithm::inputLayerTypes() const
60{
61 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorPolygon );
62}
63
64Qgis::ProcessingSourceType QgsRemovePartsByAreaAlgorithm::outputLayerType() const
65{
67}
68
69QString QgsRemovePartsByAreaAlgorithm::shortDescription() const
70{
71 return QObject::tr( "Removes polygons which are smaller than a specified area." );
72}
73
74QString QgsRemovePartsByAreaAlgorithm::shortHelpString() const
75{
76 return QObject::tr( "This algorithm takes a polygon layer and removes polygons which are smaller than a specified area.\n\n"
77 "If the input geometry is a multipart geometry, then the parts will be filtered by their individual areas. If no parts match the "
78 "required minimum area, then the feature will be skipped and omitted from the output layer.\n\n"
79 "If the input geometry is a singlepart geometry, then the feature will be skipped if the geometry's "
80 "area is below the required size and omitted from the output layer.\n\n"
81 "The area will be calculated using Cartesian calculations in the source layer's coordinate reference system.\n\n"
82 "Attributes are not modified." );
83}
84
85QgsRemovePartsByAreaAlgorithm *QgsRemovePartsByAreaAlgorithm::createInstance() const
86{
87 return new QgsRemovePartsByAreaAlgorithm();
88}
89
90Qgis::ProcessingFeatureSourceFlags QgsRemovePartsByAreaAlgorithm::sourceFlags() const
91{
92 // skip geometry checks - this algorithm can be used to repair geometries
94}
95
96void QgsRemovePartsByAreaAlgorithm::initParameters( const QVariantMap & )
97{
98 auto minArea = std::make_unique< QgsProcessingParameterArea >( u"MIN_AREA"_s, QObject::tr( "Remove parts with area less than" ), 0.0, u"INPUT"_s, false, 0 );
99 minArea->setIsDynamic( true );
100 minArea->setDynamicPropertyDefinition( QgsPropertyDefinition( u"MIN_AREA"_s, QObject::tr( "Remove parts with area less than" ), QgsPropertyDefinition::DoublePositive ) );
101 minArea->setDynamicLayerParameterName( u"INPUT"_s );
102 addParameter( minArea.release() );
103}
104
105bool QgsRemovePartsByAreaAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
106{
107 mMinArea = parameterAsDouble( parameters, u"MIN_AREA"_s, context );
108 mDynamicMinArea = QgsProcessingParameters::isDynamic( parameters, u"MIN_AREA"_s );
109 if ( mDynamicMinArea )
110 mMinAreaProperty = parameters.value( u"MIN_AREA"_s ).value< QgsProperty >();
111
112 return true;
113}
114
115QgsFeatureList QgsRemovePartsByAreaAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
116{
117 QgsFeature f = feature;
118 if ( f.hasGeometry() )
119 {
120 double minArea = mMinArea;
121 if ( mDynamicMinArea )
122 minArea = mMinAreaProperty.valueAsDouble( context.expressionContext(), minArea );
123
124 const QgsGeometry geometry = f.geometry();
125 QgsGeometry outputGeometry;
126 if ( const QgsGeometryCollection *inputCollection = qgsgeometry_cast< const QgsGeometryCollection * >( geometry.constGet() ) )
127 {
128 std::unique_ptr< QgsGeometryCollection> filteredGeometry( inputCollection->createEmptyWithSameType() );
129 const int size = inputCollection->numGeometries();
130 filteredGeometry->reserve( size );
131 for ( int i = 0; i < size; ++i )
132 {
133 if ( const QgsSurface *surface = qgsgeometry_cast< const QgsSurface * >( inputCollection->geometryN( i ) ) )
134 {
135 if ( surface->area() >= minArea )
136 {
137 filteredGeometry->addGeometry( surface->clone() );
138 }
139 }
140 }
141 if ( filteredGeometry->numGeometries() == 0 )
142 {
143 // skip empty features
144 return {};
145 }
146 outputGeometry = QgsGeometry( std::move( filteredGeometry ) );
147 f.setGeometry( outputGeometry );
148 }
149 else if ( const QgsSurface *surface = qgsgeometry_cast< const QgsSurface * >( geometry.constGet() ) )
150 {
151 if ( surface->area() < minArea )
152 {
153 return {};
154 }
155 }
156 else
157 {
158 return {};
159 }
160 }
161 return { f };
162}
163
164
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
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.
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
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:58
A store for object properties.
Surface geometry type.
Definition qgssurface.h:34
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList