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