QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsalgorithmextractzmvalues.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextractzmvalues.cpp
3 ---------------------------------
4 begin : January 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18#include "qgsfeaturerequest.h"
19#include <vector>
20
22
23const std::vector< QgsStatisticalSummary::Statistic > STATS
24{
41};
42
43QString QgsExtractZMValuesAlgorithmBase::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsExtractZMValuesAlgorithmBase::groupId() const
49{
50 return QStringLiteral( "vectorgeometry" );
51}
52
53QString QgsExtractZMValuesAlgorithmBase::outputName() const
54{
55 return QObject::tr( "Extracted" );
56}
57
58QList<int> QgsExtractZMValuesAlgorithmBase::inputLayerTypes() const
59{
60 return QList<int>() << QgsProcessing::TypeVectorAnyGeometry;
61}
62
63QgsProcessingFeatureSource::Flag QgsExtractZMValuesAlgorithmBase::sourceFlags() const
64{
66}
67
68void QgsExtractZMValuesAlgorithmBase::initParameters( const QVariantMap & )
69{
70 QStringList statChoices;
71 statChoices.reserve( STATS.size() );
72 for ( QgsStatisticalSummary::Statistic stat : STATS )
73 {
74 statChoices << QgsStatisticalSummary::displayName( stat );
75 }
76
77 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "SUMMARIES" ),
78 QObject::tr( "Summaries to calculate" ),
79 statChoices, true, QVariantList() << 0 ) );
80
81 addParameter( new QgsProcessingParameterString( QStringLiteral( "COLUMN_PREFIX" ), QObject::tr( "Output column prefix" ), mDefaultFieldPrefix, false, true ) );
82}
83
84QgsFields QgsExtractZMValuesAlgorithmBase::outputFields( const QgsFields &inputFields ) const
85{
86 return QgsProcessingUtils::combineFields( inputFields, mNewFields );
87}
88
89bool QgsExtractZMValuesAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
90{
91 mPrefix = parameterAsString( parameters, QStringLiteral( "COLUMN_PREFIX" ), context );
92
93 const QList< int > stats = parameterAsEnums( parameters, QStringLiteral( "SUMMARIES" ), context );
94 mStats = QgsStatisticalSummary::Statistics();
95 for ( int s : stats )
96 {
97 mStats |= STATS.at( s );
98 mSelectedStats << STATS.at( s );
99 mNewFields.append( QgsField( mPrefix + QgsStatisticalSummary::shortName( STATS.at( s ) ), STATS.at( s ) == QgsStatisticalSummary::Count || STATS.at( s ) == QgsStatisticalSummary::Variety ? QVariant::Int : QVariant::Double ) );
100 }
101
102 return true;
103}
104
105QgsFeatureList QgsExtractZMValuesAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
106{
107 QgsFeature f = feature;
108 QgsAttributes attrs = f.attributes();
109 attrs.reserve( attrs.count() + mSelectedStats.count() );
110 if ( !f.hasGeometry() || f.geometry().isEmpty() || !mTestGeomFunc( f.geometry() ) )
111 {
112 attrs.resize( attrs.count() + mNewFields.size() );
113 }
114 else
115 {
116 const QgsGeometry g = f.geometry();
117 QgsStatisticalSummary stat( mStats );
118 for ( auto it = g.vertices_begin(); it != g.vertices_end(); ++it )
119 {
120 stat.addValue( mExtractValFunc( *it ) );
121 if ( mStats == QgsStatisticalSummary::First )
122 {
123 // only retrieving first vertex info (default behavior), so short cut and
124 // don't iterate remaining vertices
125 break;
126 }
127 }
128 stat.finalize();
129
130 for ( QgsStatisticalSummary::Statistic s : std::as_const( mSelectedStats ) )
131 attrs.append( stat.statistic( s ) );
132 }
133 f.setAttributes( attrs );
134
135 return QgsFeatureList() << f;
136}
137
138bool QgsExtractZMValuesAlgorithmBase::supportInPlaceEdit( const QgsMapLayer *layer ) const
139{
140 Q_UNUSED( layer )
141 return false;
142}
143
144//
145// QgsExtractZValuesAlgorithm
146//
147
148QgsExtractZValuesAlgorithm::QgsExtractZValuesAlgorithm()
149{
150 mExtractValFunc = []( const QgsPoint & p ) -> double
151 {
152 return p.z();
153 };
154 mTestGeomFunc = []( const QgsGeometry & g ) -> bool
155 {
156 return QgsWkbTypes::hasZ( g.wkbType() );
157 };
158 mDefaultFieldPrefix = QStringLiteral( "z_" );
159}
160
161QString QgsExtractZValuesAlgorithm::name() const
162{
163 return QStringLiteral( "extractzvalues" );
164}
165
166QString QgsExtractZValuesAlgorithm::displayName() const
167{
168 return QObject::tr( "Extract Z values" );
169}
170
171QgsProcessingAlgorithm *QgsExtractZValuesAlgorithm::createInstance() const
172{
173 return new QgsExtractZValuesAlgorithm();
174}
175
176QStringList QgsExtractZValuesAlgorithm::tags() const
177{
178 return QObject::tr( "add,z,value,elevation,height,attribute,statistics,stats" ).split( ',' );
179}
180
181QString QgsExtractZValuesAlgorithm::shortHelpString() const
182{
183 return QObject::tr( "Extracts z values from geometries into feature attributes.\n\n"
184 "By default only the z value from the first vertex of each feature is extracted, however the algorithm "
185 "can optionally calculate statistics on all of the geometry's z values, including sums, means, and minimums and maximums" );
186}
187
188QString QgsExtractZValuesAlgorithm::shortDescription() const
189{
190 return QObject::tr( "Extracts z values (or z value statistics) from geometries into feature attributes." );
191}
192
193
194//
195// QgsExtractMValuesAlgorithm
196//
197
198QgsExtractMValuesAlgorithm::QgsExtractMValuesAlgorithm()
199{
200 mExtractValFunc = []( const QgsPoint & p ) -> double
201 {
202 return p.m();
203 };
204 mTestGeomFunc = []( const QgsGeometry & g ) -> bool
205 {
206 return QgsWkbTypes::hasM( g.wkbType() );
207 };
208 mDefaultFieldPrefix = QStringLiteral( "m_" );
209}
210
211QString QgsExtractMValuesAlgorithm::name() const
212{
213 return QStringLiteral( "extractmvalues" );
214}
215
216QString QgsExtractMValuesAlgorithm::displayName() const
217{
218 return QObject::tr( "Extract M values" );
219}
220
221QgsProcessingAlgorithm *QgsExtractMValuesAlgorithm::createInstance() const
222{
223 return new QgsExtractMValuesAlgorithm();
224}
225
226QStringList QgsExtractMValuesAlgorithm::tags() const
227{
228 return QObject::tr( "add,m,value,measure,attribute,statistics,stats" ).split( ',' );
229}
230
231QString QgsExtractMValuesAlgorithm::shortHelpString() const
232{
233 return QObject::tr( "Extracts m values from geometries into feature attributes.\n\n"
234 "By default only the m value from the first vertex of each feature is extracted, however the algorithm "
235 "can optionally calculate statistics on all of the geometry's m values, including sums, means, and minimums and maximums" );
236}
237
238QString QgsExtractMValuesAlgorithm::shortDescription() const
239{
240 return QObject::tr( "Extracts m values (or m value statistics) from geometries into feature attributes." );
241}
242
243
245
A vector of attributes.
Definition: qgsattributes.h:59
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsAttributes attributes
Definition: qgsfeature.h:65
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:233
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsAbstractGeometry::vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
QgsAbstractGeometry::vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A string parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:48
Calculator for summary statistics for a list of doubles.
Statistic
Enumeration of flags that specify statistics to be calculated.
@ InterQuartileRange
Inter quartile range (IQR)
@ Median
Median of values.
@ Variety
Variety (count of distinct) values.
@ First
First value (since QGIS 3.6)
@ Last
Last value (since QGIS 3.6)
@ Minority
Minority of values.
@ ThirdQuartile
Third quartile.
@ Majority
Majority of values.
@ Range
Range of values (max - min)
@ FirstQuartile
First quartile.
@ StDev
Standard deviation of values.
static QString shortName(QgsStatisticalSummary::Statistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
static QString displayName(QgsStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1130
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1080
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:922