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