QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
24
25const std::vector<Qgis::Statistic> STATS {
42};
43
44QString QgsExtractZMValuesAlgorithmBase::group() const
45{
46 return QObject::tr( "Vector geometry" );
47}
48
49QString QgsExtractZMValuesAlgorithmBase::groupId() const
50{
51 return QStringLiteral( "vectorgeometry" );
52}
53
54QString QgsExtractZMValuesAlgorithmBase::outputName() const
55{
56 return QObject::tr( "Extracted" );
57}
58
59QList<int> QgsExtractZMValuesAlgorithmBase::inputLayerTypes() const
60{
61 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorAnyGeometry );
62}
63
64Qgis::ProcessingFeatureSourceFlags QgsExtractZMValuesAlgorithmBase::sourceFlags() const
65{
67}
68
69void QgsExtractZMValuesAlgorithmBase::initParameters( const QVariantMap & )
70{
71 QStringList statChoices;
72 statChoices.reserve( STATS.size() );
73 for ( Qgis::Statistic stat : STATS )
74 {
75 statChoices << QgsStatisticalSummary::displayName( stat );
76 }
77
78 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "SUMMARIES" ), QObject::tr( "Summaries to calculate" ), statChoices, true, QVariantList() << 0 ) );
79
80 addParameter( new QgsProcessingParameterString( QStringLiteral( "COLUMN_PREFIX" ), QObject::tr( "Output column prefix" ), mDefaultFieldPrefix, false, true ) );
81}
82
83QgsFields QgsExtractZMValuesAlgorithmBase::outputFields( const QgsFields &inputFields ) const
84{
85 return QgsProcessingUtils::combineFields( inputFields, mNewFields );
86}
87
88bool QgsExtractZMValuesAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
89{
90 mPrefix = parameterAsString( parameters, QStringLiteral( "COLUMN_PREFIX" ), context );
91
92 const QList<int> stats = parameterAsEnums( parameters, QStringLiteral( "SUMMARIES" ), context );
93 mStats = Qgis::Statistics();
94 for ( int s : stats )
95 {
96 mStats |= STATS.at( s );
97 mSelectedStats << STATS.at( s );
98 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 ) );
99 }
100
101 return true;
102}
103
104QgsFeatureList QgsExtractZMValuesAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
105{
106 QgsFeature f = feature;
107 QgsAttributes attrs = f.attributes();
108 attrs.reserve( attrs.count() + mSelectedStats.count() );
109 if ( !f.hasGeometry() || f.geometry().isEmpty() || !mTestGeomFunc( f.geometry() ) )
110 {
111 attrs.resize( attrs.count() + mNewFields.size() );
112 }
113 else
114 {
115 const QgsGeometry g = f.geometry();
116 QgsStatisticalSummary stat( mStats );
117 for ( auto it = g.vertices_begin(); it != g.vertices_end(); ++it )
118 {
119 stat.addValue( mExtractValFunc( *it ) );
120 if ( mStats == Qgis::Statistics( Qgis::Statistic::First ) )
121 {
122 // only retrieving first vertex info (default behavior), so short cut and
123 // don't iterate remaining vertices
124 break;
125 }
126 }
127 stat.finalize();
128
129 for ( Qgis::Statistic s : std::as_const( mSelectedStats ) )
130 attrs.append( stat.statistic( s ) );
131 }
132 f.setAttributes( attrs );
133
134 return QgsFeatureList() << f;
135}
136
137bool QgsExtractZMValuesAlgorithmBase::supportInPlaceEdit( const QgsMapLayer *layer ) const
138{
139 Q_UNUSED( layer )
140 return false;
141}
142
143//
144// QgsExtractZValuesAlgorithm
145//
146
147QgsExtractZValuesAlgorithm::QgsExtractZValuesAlgorithm()
148{
149 mExtractValFunc = []( const QgsPoint &p ) -> double {
150 return p.z();
151 };
152 mTestGeomFunc = []( const QgsGeometry &g ) -> bool {
153 return QgsWkbTypes::hasZ( g.wkbType() );
154 };
155 mDefaultFieldPrefix = QStringLiteral( "z_" );
156}
157
158QString QgsExtractZValuesAlgorithm::name() const
159{
160 return QStringLiteral( "extractzvalues" );
161}
162
163QString QgsExtractZValuesAlgorithm::displayName() const
164{
165 return QObject::tr( "Extract Z values" );
166}
167
168QgsProcessingAlgorithm *QgsExtractZValuesAlgorithm::createInstance() const
169{
170 return new QgsExtractZValuesAlgorithm();
171}
172
173QStringList QgsExtractZValuesAlgorithm::tags() const
174{
175 return QObject::tr( "add,z,value,elevation,height,attribute,statistics,stats" ).split( ',' );
176}
177
178QString QgsExtractZValuesAlgorithm::shortHelpString() const
179{
180 return QObject::tr( "Extracts z values from geometries into feature attributes.\n\n"
181 "By default only the z value from the first vertex of each feature is extracted, however the algorithm "
182 "can optionally calculate statistics on all of the geometry's z values, including sums, means, and minimums and maximums" );
183}
184
185QString QgsExtractZValuesAlgorithm::shortDescription() const
186{
187 return QObject::tr( "Extracts z values (or z value statistics) from geometries into feature attributes." );
188}
189
190
191//
192// QgsExtractMValuesAlgorithm
193//
194
195QgsExtractMValuesAlgorithm::QgsExtractMValuesAlgorithm()
196{
197 mExtractValFunc = []( const QgsPoint &p ) -> double {
198 return p.m();
199 };
200 mTestGeomFunc = []( const QgsGeometry &g ) -> bool {
201 return QgsWkbTypes::hasM( g.wkbType() );
202 };
203 mDefaultFieldPrefix = QStringLiteral( "m_" );
204}
205
206QString QgsExtractMValuesAlgorithm::name() const
207{
208 return QStringLiteral( "extractmvalues" );
209}
210
211QString QgsExtractMValuesAlgorithm::displayName() const
212{
213 return QObject::tr( "Extract M values" );
214}
215
216QgsProcessingAlgorithm *QgsExtractMValuesAlgorithm::createInstance() const
217{
218 return new QgsExtractMValuesAlgorithm();
219}
220
221QStringList QgsExtractMValuesAlgorithm::tags() const
222{
223 return QObject::tr( "add,m,value,measure,attribute,statistics,stats" ).split( ',' );
224}
225
226QString QgsExtractMValuesAlgorithm::shortHelpString() const
227{
228 return QObject::tr( "Extracts m values from geometries into feature attributes.\n\n"
229 "By default only the m value from the first vertex of each feature is extracted, however the algorithm "
230 "can optionally calculate statistics on all of the geometry's m values, including sums, means, and minimums and maximums" );
231}
232
233QString QgsExtractMValuesAlgorithm::shortDescription() const
234{
235 return QObject::tr( "Extracts m values (or m value statistics) from geometries into feature attributes." );
236}
237
238
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3533
Statistic
Available generic statistics.
Definition qgis.h:5848
@ StDev
Standard deviation of values.
Definition qgis.h:5854
@ FirstQuartile
First quartile.
Definition qgis.h:5862
@ Mean
Mean of values.
Definition qgis.h:5852
@ Median
Median of values.
Definition qgis.h:5853
@ Max
Max of values.
Definition qgis.h:5857
@ Min
Min of values.
Definition qgis.h:5856
@ First
First value.
Definition qgis.h:5865
@ Range
Range of values (max - min).
Definition qgis.h:5858
@ Sum
Sum of values.
Definition qgis.h:5851
@ Minority
Minority of values.
Definition qgis.h:5859
@ Majority
Majority of values.
Definition qgis.h:5860
@ Variety
Variety (count of distinct) values.
Definition qgis.h:5861
@ Last
Last value.
Definition qgis.h:5866
@ Count
Count.
Definition qgis.h:5849
@ ThirdQuartile
Third quartile.
Definition qgis.h:5863
@ InterQuartileRange
Inter quartile range (IQR).
Definition qgis.h:5864
QFlags< Statistic > Statistics
Statistics to be calculated for generic values.
Definition qgis.h:5876
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
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:54
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:80
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.
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