QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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." ).arg( componentString() );
48}
49
50QList<int> QgsFilterVerticesAlgorithmBase::inputLayerTypes() const
51{
53}
54
55void QgsFilterVerticesAlgorithmBase::initParameters( const QVariantMap & )
56{
57 auto min = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MIN" ),
58 QObject::tr( "Minimum" ), QgsProcessingParameterNumber::Double, QVariant(), true );
59 min->setIsDynamic( true );
60 min->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Minimum" ), QObject::tr( "Minimum value" ), QgsPropertyDefinition::Double ) );
61 min->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
62 addParameter( min.release() );
63
64 auto max = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MAX" ),
65 QObject::tr( "Maximum" ), QgsProcessingParameterNumber::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 {
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 {
209 return ( std::isnan( min ) || point.z() >= min )
210 && ( std::isnan( max ) || point.z() <= max );
211 } );
212}
213
215
216
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:233
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
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:73
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...
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:50
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
Definition for a property.
Definition: qgsproperty.h:46
@ Double
Double value (including negative values)
Definition: qgsproperty.h:56
A store for object properties.
Definition: qgsproperty.h:230
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1130
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1080
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:922