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