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