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