QGIS API Documentation 3.43.0-Master (261ee7da134)
qgsalgorithmfiltervertices.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmfiltervertices.cpp
3 ------------------------------
4 begin : July 2018
5 copyright : (C) 2018 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#include "qgsvectorlayer.h"
20
22
23
24QString QgsFilterVerticesAlgorithmBase::group() const
25{
26 return QObject::tr( "Vector geometry" );
27}
28
29QString QgsFilterVerticesAlgorithmBase::groupId() const
30{
31 return QStringLiteral( "vectorgeometry" );
32}
33
34QString QgsFilterVerticesAlgorithmBase::outputName() const
35{
36 return QObject::tr( "Filtered" );
37}
38
39QString QgsFilterVerticesAlgorithmBase::shortHelpString() const
40{
41 return QObject::tr( "Filters away vertices based on their %1, returning geometries with only "
42 "vertex points that have a %1 ≥ the specified minimum value and ≤ "
43 "the maximum value.\n\n"
44 "If the minimum value is not specified then only the maximum value is tested, "
45 "and similarly if the maximum value is not specified then only the minimum value is tested.\n\n"
46 "Depending on the input geometry attributes and the filters used, "
47 "the resultant geometries created by this algorithm may no longer be valid." )
48 .arg( componentString() );
49}
50
51QString QgsFilterVerticesAlgorithmBase::shortDescription() const
52{
53 return QObject::tr( "Filters away vertices based on their %1 value." )
54 .arg( componentString() );
55}
56
57void QgsFilterVerticesAlgorithmBase::initParameters( const QVariantMap & )
58{
59 auto min = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MIN" ), QObject::tr( "Minimum" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
60 min->setIsDynamic( true );
61 min->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Minimum" ), QObject::tr( "Minimum value" ), QgsPropertyDefinition::Double ) );
62 min->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
63 addParameter( min.release() );
64
65 auto max = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MAX" ), QObject::tr( "Maximum" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
66 max->setIsDynamic( true );
67 max->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Maximum" ), QObject::tr( "Maximum value" ), QgsPropertyDefinition::Double ) );
68 max->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
69 addParameter( max.release() );
70}
71
72bool QgsFilterVerticesAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
73{
74 if ( parameters.contains( QStringLiteral( "MIN" ) ) && parameters.value( QStringLiteral( "MIN" ) ).isValid() )
75 mMin = parameterAsDouble( parameters, QStringLiteral( "MIN" ), context );
76 else
77 mMin = std::numeric_limits<double>::quiet_NaN();
78
79 mDynamicMin = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN" ) );
80 if ( mDynamicMin )
81 mMinProperty = parameters.value( QStringLiteral( "MIN" ) ).value<QgsProperty>();
82
83 if ( parameters.contains( QStringLiteral( "MAX" ) ) && parameters.value( QStringLiteral( "MAX" ) ).isValid() )
84 mMax = parameterAsDouble( parameters, QStringLiteral( "MAX" ), context );
85 else
86 mMax = std::numeric_limits<double>::quiet_NaN();
87
88 mDynamicMax = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MAX" ) );
89 if ( mDynamicMax )
90 mMaxProperty = parameters.value( QStringLiteral( "MAX" ) ).value<QgsProperty>();
91
92 return true;
93}
94
95QgsFeatureList QgsFilterVerticesAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
96{
97 QgsFeature f = feature;
98 if ( f.hasGeometry() )
99 {
100 QgsGeometry geometry = f.geometry();
101 double min = mMin;
102 if ( mDynamicMin )
103 min = mMinProperty.valueAsDouble( context.expressionContext(), std::numeric_limits<double>::quiet_NaN() );
104
105 double max = mMax;
106 if ( mDynamicMax )
107 max = mMaxProperty.valueAsDouble( context.expressionContext(), std::numeric_limits<double>::quiet_NaN() );
108
109 filter( geometry, min, max );
110 f.setGeometry( geometry );
111 }
112 return QgsFeatureList() << f;
113}
114
115//
116// QgsFilterPointsByM
117//
118
119QString QgsFilterVerticesByM::name() const
120{
121 return QStringLiteral( "filterverticesbym" );
122}
123
124QString QgsFilterVerticesByM::displayName() const
125{
126 return QObject::tr( "Filter vertices by M value" );
127}
128
129QStringList QgsFilterVerticesByM::tags() const
130{
131 return QObject::tr( "filter,points,vertex,m" ).split( ',' );
132}
133
134QgsFilterVerticesByM *QgsFilterVerticesByM::createInstance() const
135{
136 return new QgsFilterVerticesByM();
137}
138
139bool QgsFilterVerticesByM::supportInPlaceEdit( const QgsMapLayer *l ) const
140{
141 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
142 if ( !layer )
143 return false;
144
145 if ( !QgsFilterVerticesAlgorithmBase::supportInPlaceEdit( layer ) )
146 return false;
147 return QgsWkbTypes::hasM( layer->wkbType() );
148}
149
150QString QgsFilterVerticesByM::componentString() const
151{
152 return QObject::tr( "m-value" );
153}
154
155void QgsFilterVerticesByM::filter( QgsGeometry &geometry, double min, double max ) const
156{
157 geometry.filterVertices( [min, max]( const QgsPoint &point ) -> bool {
158 return ( std::isnan( min ) || point.m() >= min )
159 && ( std::isnan( max ) || point.m() <= max );
160 } );
161}
162
163
164//
165// QgsFilterPointsByZ
166//
167
168QString QgsFilterVerticesByZ::name() const
169{
170 return QStringLiteral( "filterverticesbyz" );
171}
172
173QString QgsFilterVerticesByZ::displayName() const
174{
175 return QObject::tr( "Filter vertices by Z value" );
176}
177
178QStringList QgsFilterVerticesByZ::tags() const
179{
180 return QObject::tr( "filter,points,vertex,z" ).split( ',' );
181}
182
183QgsFilterVerticesByZ *QgsFilterVerticesByZ::createInstance() const
184{
185 return new QgsFilterVerticesByZ();
186}
187
188bool QgsFilterVerticesByZ::supportInPlaceEdit( const QgsMapLayer *l ) const
189{
190 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
191 if ( !layer )
192 return false;
193
194 if ( !QgsFilterVerticesAlgorithmBase::supportInPlaceEdit( layer ) )
195 return false;
196 return QgsWkbTypes::hasZ( layer->wkbType() );
197}
198
199QString QgsFilterVerticesByZ::componentString() const
200{
201 return QObject::tr( "z-value" );
202}
203
204void QgsFilterVerticesByZ::filter( QgsGeometry &geometry, double min, double max ) const
205{
206 geometry.filterVertices( [min, max]( const QgsPoint &point ) -> bool {
207 return ( std::isnan( min ) || point.z() >= min )
208 && ( std::isnan( max ) || point.z() <= max );
209 } );
210}
211
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.
void filterVertices(const std::function< bool(const QgsPoint &) > &filter)
Filters the vertices from the geometry in place, removing any which do not return true for the filter...
Base class for all map layer types.
Definition qgsmaplayer.h:77
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
double z
Definition qgspoint.h:54
double m
Definition qgspoint.h:55
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
@ Double
Double value (including negative values)
Definition qgsproperty.h:55
A store for object properties.
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
QList< QgsFeature > QgsFeatureList