QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
50void QgsFilterVerticesAlgorithmBase::initParameters( const QVariantMap & )
51{
52 auto min = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MIN" ),
53 QObject::tr( "Minimum" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
54 min->setIsDynamic( true );
55 min->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Minimum" ), QObject::tr( "Minimum value" ), QgsPropertyDefinition::Double ) );
56 min->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
57 addParameter( min.release() );
58
59 auto max = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MAX" ),
60 QObject::tr( "Maximum" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true );
61 max->setIsDynamic( true );
62 max->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Maximum" ), QObject::tr( "Maximum value" ), QgsPropertyDefinition::Double ) );
63 max->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
64 addParameter( max.release() );
65}
66
67bool QgsFilterVerticesAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
68{
69 if ( parameters.contains( QStringLiteral( "MIN" ) ) && parameters.value( QStringLiteral( "MIN" ) ).isValid() )
70 mMin = parameterAsDouble( parameters, QStringLiteral( "MIN" ), context );
71 else
72 mMin = std::numeric_limits<double>::quiet_NaN();
73
74 mDynamicMin = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MIN" ) );
75 if ( mDynamicMin )
76 mMinProperty = parameters.value( QStringLiteral( "MIN" ) ).value< QgsProperty >();
77
78 if ( parameters.contains( QStringLiteral( "MAX" ) ) && parameters.value( QStringLiteral( "MAX" ) ).isValid() )
79 mMax = parameterAsDouble( parameters, QStringLiteral( "MAX" ), context );
80 else
81 mMax = std::numeric_limits<double>::quiet_NaN();
82
83 mDynamicMax = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "MAX" ) );
84 if ( mDynamicMax )
85 mMaxProperty = parameters.value( QStringLiteral( "MAX" ) ).value< QgsProperty >();
86
87 return true;
88}
89
90QgsFeatureList QgsFilterVerticesAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
91{
92 QgsFeature f = feature;
93 if ( f.hasGeometry() )
94 {
95 QgsGeometry geometry = f.geometry();
96 double min = mMin;
97 if ( mDynamicMin )
98 min = mMinProperty.valueAsDouble( context.expressionContext(), std::numeric_limits<double>::quiet_NaN() );
99
100 double max = mMax;
101 if ( mDynamicMax )
102 max = mMaxProperty.valueAsDouble( context.expressionContext(), std::numeric_limits<double>::quiet_NaN() );
103
104 filter( geometry, min, max );
105 f.setGeometry( geometry );
106 }
107 return QgsFeatureList() << f;
108}
109
110//
111// QgsFilterPointsByM
112//
113
114QString QgsFilterVerticesByM::name() const
115{
116 return QStringLiteral( "filterverticesbym" );
117}
118
119QString QgsFilterVerticesByM::displayName() const
120{
121 return QObject::tr( "Filter vertices by M value" );
122}
123
124QStringList QgsFilterVerticesByM::tags() const
125{
126 return QObject::tr( "filter,points,vertex,m" ).split( ',' );
127}
128
129QgsFilterVerticesByM *QgsFilterVerticesByM::createInstance() const
130{
131 return new QgsFilterVerticesByM();
132}
133
134bool QgsFilterVerticesByM::supportInPlaceEdit( const QgsMapLayer *l ) const
135{
136 const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
137 if ( !layer )
138 return false;
139
140 if ( ! QgsFilterVerticesAlgorithmBase::supportInPlaceEdit( layer ) )
141 return false;
142 return QgsWkbTypes::hasM( layer->wkbType() );
143}
144
145QString QgsFilterVerticesByM::componentString() const
146{
147 return QObject::tr( "m-value" );
148}
149
150void QgsFilterVerticesByM::filter( QgsGeometry &geometry, double min, double max ) const
151{
152 geometry.filterVertices( [min, max]( const QgsPoint & point )->bool
153 {
154 return ( std::isnan( min ) || point.m() >= min )
155 && ( std::isnan( max ) || point.m() <= max );
156 } );
157}
158
159
160//
161// QgsFilterPointsByZ
162//
163
164QString QgsFilterVerticesByZ::name() const
165{
166 return QStringLiteral( "filterverticesbyz" );
167}
168
169QString QgsFilterVerticesByZ::displayName() const
170{
171 return QObject::tr( "Filter vertices by Z value" );
172}
173
174QStringList QgsFilterVerticesByZ::tags() const
175{
176 return QObject::tr( "filter,points,vertex,z" ).split( ',' );
177}
178
179QgsFilterVerticesByZ *QgsFilterVerticesByZ::createInstance() const
180{
181 return new QgsFilterVerticesByZ();
182}
183
184bool QgsFilterVerticesByZ::supportInPlaceEdit( const QgsMapLayer *l ) const
185{
186 const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
187 if ( !layer )
188 return false;
189
190 if ( ! QgsFilterVerticesAlgorithmBase::supportInPlaceEdit( layer ) )
191 return false;
192 return QgsWkbTypes::hasZ( layer->wkbType() );
193}
194
195QString QgsFilterVerticesByZ::componentString() const
196{
197 return QObject::tr( "z-value" );
198}
199
200void QgsFilterVerticesByZ::filter( QgsGeometry &geometry, double min, double max ) const
201{
202 geometry.filterVertices( [min, max]( const QgsPoint & point )->bool
203 {
204 return ( std::isnan( min ) || point.z() >= min )
205 && ( std::isnan( max ) || point.z() <= max );
206 } );
207}
208
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:230
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:167
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
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:75
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.
Definition: qgsproperty.h:228
Represents a vector layer which manages a vector based data sets.
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.
Definition: qgswkbtypes.h:973
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1023
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:917