QGIS API Documentation 3.99.0-Master (f78f5286a64)
qgsalgorithmremovepartsbylength.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmremovepartsbylength.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
20#include "qgscurve.h"
21
23
24QString QgsRemovePartsByLengthAlgorithm::name() const
25{
26 return QStringLiteral( "removepartsbylength" );
27}
28
29QString QgsRemovePartsByLengthAlgorithm::displayName() const
30{
31 return QObject::tr( "Remove parts by length" );
32}
33
34QStringList QgsRemovePartsByLengthAlgorithm::tags() const
35{
36 return QObject::tr( "remove,delete,drop,filter,lines,linestring,polyline,size" ).split( ',' );
37}
38
39QString QgsRemovePartsByLengthAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsRemovePartsByLengthAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsRemovePartsByLengthAlgorithm::outputName() const
50{
51 return QObject::tr( "Cleaned" );
52}
53
54QList<int> QgsRemovePartsByLengthAlgorithm::inputLayerTypes() const
55{
56 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine );
57}
58
59Qgis::ProcessingSourceType QgsRemovePartsByLengthAlgorithm::outputLayerType() const
60{
62}
63
64QString QgsRemovePartsByLengthAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Removes lines which are shorter than a specified length." );
67}
68
69QString QgsRemovePartsByLengthAlgorithm::shortHelpString() const
70{
71 return QObject::tr( "This algorithm takes a line layer and removes lines which are shorter than a specified length.\n\n"
72 "If the input geometry is a multipart geometry, then the parts will be filtered by their individual lengths. If no parts match the "
73 "required minimum length, then the feature will be skipped and omitted from the output layer.\n\n"
74 "If the input geometry is a singlepart geometry, then the feature will be skipped if the geometry's "
75 "length is below the required size and omitted from the output layer.\n\n"
76 "The length will be calculated using Cartesian calculations in the source layer's coordinate reference system.\n\n"
77 "Attributes are not modified." );
78}
79
80QgsRemovePartsByLengthAlgorithm *QgsRemovePartsByLengthAlgorithm::createInstance() const
81{
82 return new QgsRemovePartsByLengthAlgorithm();
83}
84
85Qgis::ProcessingFeatureSourceFlags QgsRemovePartsByLengthAlgorithm::sourceFlags() const
86{
87 // skip geometry checks - this algorithm can be used to repair geometries
89}
90
91void QgsRemovePartsByLengthAlgorithm::initParameters( const QVariantMap & )
92{
93 std::unique_ptr< QgsProcessingParameterDistance > minLength = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "MIN_LENGTH" ), QObject::tr( "Remove parts with lengths less than" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
94 minLength->setIsDynamic( true );
95 minLength->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MIN_LENGTH" ), QObject::tr( "Remove parts with length less than" ), QgsPropertyDefinition::DoublePositive ) );
96 minLength->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
97 addParameter( minLength.release() );
98}
99
100bool QgsRemovePartsByLengthAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mMinLength = parameterAsDouble( parameters, QStringLiteral( "MIN_LENGTH" ), context );
103 mDynamicMinLength = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN_LENGTH" ) );
104 if ( mDynamicMinLength )
105 mMinLengthProperty = parameters.value( QStringLiteral( "MIN_LENGTH" ) ).value< QgsProperty >();
106
107 return true;
108}
109
110QgsFeatureList QgsRemovePartsByLengthAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
111{
112 QgsFeature f = feature;
113 if ( f.hasGeometry() )
114 {
115 double minLength = mMinLength;
116 if ( mDynamicMinLength )
117 minLength = mMinLengthProperty.valueAsDouble( context.expressionContext(), minLength );
118
119 const QgsGeometry geometry = f.geometry();
120 QgsGeometry outputGeometry;
121 if ( const QgsGeometryCollection *inputCollection = qgsgeometry_cast< const QgsGeometryCollection * >( geometry.constGet() ) )
122 {
123 std::unique_ptr< QgsAbstractGeometry> filteredGeometry( geometry.constGet()->createEmptyWithSameType() );
124 QgsGeometryCollection *collection = qgsgeometry_cast< QgsGeometryCollection * >( filteredGeometry.get() );
125 const int size = inputCollection->numGeometries();
126 collection->reserve( size );
127 for ( int i = 0; i < size; ++i )
128 {
129 if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( inputCollection->geometryN( i ) ) )
130 {
131 if ( curve->length() >= minLength )
132 {
133 collection->addGeometry( curve->clone() );
134 }
135 }
136 }
137 if ( collection->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 QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geometry.constGet() ) )
146 {
147 if ( curve->length() < minLength )
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:3434
@ VectorLine
Vector line 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.
Definition qgis.h:3623
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
Abstract base class for curved geometry type.
Definition qgscurve.h:35
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.
void reserve(int size)
Attempts to allocate memory for at least size geometries.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
int numGeometries() const
Returns the number of geometries within the collection.
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.
QList< QgsFeature > QgsFeatureList