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