QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
19#include <vector>
20
21#include "qgsfeaturerequest.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29const std::vector<Qgis::Statistic> STATS {
46};
47
48QString QgsExtractZMValuesAlgorithmBase::group() const
49{
50 return QObject::tr( "Vector geometry" );
51}
52
53QString QgsExtractZMValuesAlgorithmBase::groupId() const
54{
55 return u"vectorgeometry"_s;
56}
57
58QString QgsExtractZMValuesAlgorithmBase::outputName() const
59{
60 return QObject::tr( "Extracted" );
61}
62
63QList<int> QgsExtractZMValuesAlgorithmBase::inputLayerTypes() const
64{
65 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorAnyGeometry );
66}
67
68Qgis::ProcessingFeatureSourceFlags QgsExtractZMValuesAlgorithmBase::sourceFlags() const
69{
71}
72
73void QgsExtractZMValuesAlgorithmBase::initParameters( const QVariantMap & )
74{
75 QStringList statChoices;
76 statChoices.reserve( STATS.size() );
77 for ( Qgis::Statistic stat : STATS )
78 {
79 statChoices << QgsStatisticalSummary::displayName( stat );
80 }
81
82 addParameter( new QgsProcessingParameterEnum( u"SUMMARIES"_s, QObject::tr( "Summaries to calculate" ), statChoices, true, QVariantList() << 0 ) );
83
84 addParameter( new QgsProcessingParameterString( u"COLUMN_PREFIX"_s, QObject::tr( "Output column prefix" ), mDefaultFieldPrefix, false, true ) );
85}
86
87QgsFields QgsExtractZMValuesAlgorithmBase::outputFields( const QgsFields &inputFields ) const
88{
89 return QgsProcessingUtils::combineFields( inputFields, mNewFields );
90}
91
92bool QgsExtractZMValuesAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
93{
94 mPrefix = parameterAsString( parameters, u"COLUMN_PREFIX"_s, context );
95
96 const QList<int> stats = parameterAsEnums( parameters, u"SUMMARIES"_s, context );
97 mStats = Qgis::Statistics();
98 for ( int s : stats )
99 {
100 mStats |= STATS.at( s );
101 mSelectedStats << STATS.at( s );
102 mNewFields.append( QgsField( mPrefix + QgsStatisticalSummary::shortName( STATS.at( s ) ), STATS.at( s ) == Qgis::Statistic::Count || STATS.at( s ) == Qgis::Statistic::Variety ? QMetaType::Type::Int : QMetaType::Type::Double ) );
103 }
104
105 return true;
106}
107
108QgsFeatureList QgsExtractZMValuesAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
109{
110 QgsFeature f = feature;
111 QgsAttributes attrs = f.attributes();
112 attrs.reserve( attrs.count() + mSelectedStats.count() );
113 if ( !f.hasGeometry() || f.geometry().isEmpty() || !mTestGeomFunc( f.geometry() ) )
114 {
115 attrs.resize( attrs.count() + mNewFields.size() );
116 }
117 else
118 {
119 const QgsGeometry g = f.geometry();
120 QgsStatisticalSummary stat( mStats );
121 for ( auto it = g.vertices_begin(); it != g.vertices_end(); ++it )
122 {
123 stat.addValue( mExtractValFunc( *it ) );
124 if ( mStats == Qgis::Statistics( Qgis::Statistic::First ) )
125 {
126 // only retrieving first vertex info (default behavior), so short cut and
127 // don't iterate remaining vertices
128 break;
129 }
130 }
131 stat.finalize();
132
133 for ( Qgis::Statistic s : std::as_const( mSelectedStats ) )
134 attrs.append( stat.statistic( s ) );
135 }
136 f.setAttributes( attrs );
137
138 return QgsFeatureList() << f;
139}
140
141bool QgsExtractZMValuesAlgorithmBase::supportInPlaceEdit( const QgsMapLayer *layer ) const
142{
143 Q_UNUSED( layer )
144 return false;
145}
146
147//
148// QgsExtractZValuesAlgorithm
149//
150
151QgsExtractZValuesAlgorithm::QgsExtractZValuesAlgorithm()
152{
153 mExtractValFunc = []( const QgsPoint &p ) -> double {
154 return p.z();
155 };
156 mTestGeomFunc = []( const QgsGeometry &g ) -> bool {
157 return QgsWkbTypes::hasZ( g.wkbType() );
158 };
159 mDefaultFieldPrefix = u"z_"_s;
160}
161
162QString QgsExtractZValuesAlgorithm::name() const
163{
164 return u"extractzvalues"_s;
165}
166
167QString QgsExtractZValuesAlgorithm::displayName() const
168{
169 return QObject::tr( "Extract Z values" );
170}
171
172QgsProcessingAlgorithm *QgsExtractZValuesAlgorithm::createInstance() const
173{
174 return new QgsExtractZValuesAlgorithm();
175}
176
177QStringList QgsExtractZValuesAlgorithm::tags() const
178{
179 return QObject::tr( "add,z,value,elevation,height,attribute,statistics,stats" ).split( ',' );
180}
181
182QString QgsExtractZValuesAlgorithm::shortHelpString() const
183{
184 return QObject::tr( "Extracts z values from geometries into feature attributes.\n\n"
185 "By default only the z value from the first vertex of each feature is extracted, however the algorithm "
186 "can optionally calculate statistics on all of the geometry's z values, including sums, means, and minimums and maximums" );
187}
188
189QString QgsExtractZValuesAlgorithm::shortDescription() const
190{
191 return QObject::tr( "Extracts z values (or z value statistics) from geometries into feature attributes." );
192}
193
194
195//
196// QgsExtractMValuesAlgorithm
197//
198
199QgsExtractMValuesAlgorithm::QgsExtractMValuesAlgorithm()
200{
201 mExtractValFunc = []( const QgsPoint &p ) -> double {
202 return p.m();
203 };
204 mTestGeomFunc = []( const QgsGeometry &g ) -> bool {
205 return QgsWkbTypes::hasM( g.wkbType() );
206 };
207 mDefaultFieldPrefix = u"m_"_s;
208}
209
210QString QgsExtractMValuesAlgorithm::name() const
211{
212 return u"extractmvalues"_s;
213}
214
215QString QgsExtractMValuesAlgorithm::displayName() const
216{
217 return QObject::tr( "Extract M values" );
218}
219
220QgsProcessingAlgorithm *QgsExtractMValuesAlgorithm::createInstance() const
221{
222 return new QgsExtractMValuesAlgorithm();
223}
224
225QStringList QgsExtractMValuesAlgorithm::tags() const
226{
227 return QObject::tr( "add,m,value,measure,attribute,statistics,stats" ).split( ',' );
228}
229
230QString QgsExtractMValuesAlgorithm::shortHelpString() const
231{
232 return QObject::tr( "Extracts m values from geometries into feature attributes.\n\n"
233 "By default only the m value from the first vertex of each feature is extracted, however the algorithm "
234 "can optionally calculate statistics on all of the geometry's m values, including sums, means, and minimums and maximums" );
235}
236
237QString QgsExtractMValuesAlgorithm::shortDescription() const
238{
239 return QObject::tr( "Extracts m values (or m value statistics) from geometries into feature attributes." );
240}
241
242
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3604
Statistic
Available generic statistics.
Definition qgis.h:6149
@ StDev
Standard deviation of values.
Definition qgis.h:6155
@ FirstQuartile
First quartile.
Definition qgis.h:6163
@ Mean
Mean of values.
Definition qgis.h:6153
@ Median
Median of values.
Definition qgis.h:6154
@ Max
Max of values.
Definition qgis.h:6158
@ Min
Min of values.
Definition qgis.h:6157
@ First
First value.
Definition qgis.h:6166
@ Range
Range of values (max - min).
Definition qgis.h:6159
@ Sum
Sum of values.
Definition qgis.h:6152
@ Minority
Minority of values.
Definition qgis.h:6160
@ Majority
Majority of values.
Definition qgis.h:6161
@ Variety
Variety (count of distinct) values.
Definition qgis.h:6162
@ Last
Last value.
Definition qgis.h:6167
@ Count
Count.
Definition qgis.h:6150
@ ThirdQuartile
Third quartile.
Definition qgis.h:6164
@ InterQuartileRange
Inter quartile range (IQR).
Definition qgis.h:6165
QFlags< Statistic > Statistics
Statistics to be calculated for generic values.
Definition qgis.h:6177
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
A geometry is the spatial representation of a feature.
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.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
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:83
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
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).
Calculator for summary statistics for a list of doubles.
static QString displayName(Qgis::Statistic statistic)
Returns the friendly display name for a statistic.
static QString shortName(Qgis::Statistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
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