QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
19
20#include "qgscurve.h"
22
24
25QString QgsRemovePartsByLengthAlgorithm::name() const
26{
27 return QStringLiteral( "removepartsbylength" );
28}
29
30QString QgsRemovePartsByLengthAlgorithm::displayName() const
31{
32 return QObject::tr( "Remove parts by length" );
33}
34
35QStringList QgsRemovePartsByLengthAlgorithm::tags() const
36{
37 return QObject::tr( "remove,delete,drop,filter,lines,linestring,polyline,size" ).split( ',' );
38}
39
40QString QgsRemovePartsByLengthAlgorithm::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsRemovePartsByLengthAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50QString QgsRemovePartsByLengthAlgorithm::outputName() const
51{
52 return QObject::tr( "Cleaned" );
53}
54
55QList<int> QgsRemovePartsByLengthAlgorithm::inputLayerTypes() const
56{
57 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine );
58}
59
60Qgis::ProcessingSourceType QgsRemovePartsByLengthAlgorithm::outputLayerType() const
61{
63}
64
65QString QgsRemovePartsByLengthAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Removes lines which are shorter than a specified length." );
68}
69
70QString QgsRemovePartsByLengthAlgorithm::shortHelpString() const
71{
72 return QObject::tr( "This algorithm takes a line layer and removes lines which are shorter than a specified length.\n\n"
73 "If the input geometry is a multipart geometry, then the parts will be filtered by their individual lengths. If no parts match the "
74 "required minimum length, 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 "length is below the required size and omitted from the output layer.\n\n"
77 "The length will be calculated using Cartesian calculations in the source layer's coordinate reference system.\n\n"
78 "Attributes are not modified." );
79}
80
81QgsRemovePartsByLengthAlgorithm *QgsRemovePartsByLengthAlgorithm::createInstance() const
82{
83 return new QgsRemovePartsByLengthAlgorithm();
84}
85
86Qgis::ProcessingFeatureSourceFlags QgsRemovePartsByLengthAlgorithm::sourceFlags() const
87{
88 // skip geometry checks - this algorithm can be used to repair geometries
90}
91
92void QgsRemovePartsByLengthAlgorithm::initParameters( const QVariantMap & )
93{
94 auto minLength = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "MIN_LENGTH" ), QObject::tr( "Remove parts with lengths less than" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
95 minLength->setIsDynamic( true );
96 minLength->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "MIN_LENGTH" ), QObject::tr( "Remove parts with length less than" ), QgsPropertyDefinition::DoublePositive ) );
97 minLength->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
98 addParameter( minLength.release() );
99}
100
101bool QgsRemovePartsByLengthAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
102{
103 mMinLength = parameterAsDouble( parameters, QStringLiteral( "MIN_LENGTH" ), context );
104 mDynamicMinLength = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN_LENGTH" ) );
105 if ( mDynamicMinLength )
106 mMinLengthProperty = parameters.value( QStringLiteral( "MIN_LENGTH" ) ).value< QgsProperty >();
107
108 return true;
109}
110
111QgsFeatureList QgsRemovePartsByLengthAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
112{
113 QgsFeature f = feature;
114 if ( f.hasGeometry() )
115 {
116 double minLength = mMinLength;
117 if ( mDynamicMinLength )
118 minLength = mMinLengthProperty.valueAsDouble( context.expressionContext(), minLength );
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 QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( inputCollection->geometryN( i ) ) )
130 {
131 if ( curve->length() >= minLength )
132 {
133 filteredGeometry->addGeometry( curve->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 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:3531
@ VectorLine
Vector line layers.
Definition qgis.h:3535
@ 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
Abstract base class for curved geometry type.
Definition qgscurve.h:36
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.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList