QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "qgsfeaturerequest.h"
19#include <vector>
20
22
23const std::vector< Qgis::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>() << static_cast< int >( Qgis::ProcessingSourceType::VectorAnyGeometry );
61}
62
63Qgis::ProcessingFeatureSourceFlags QgsExtractZMValuesAlgorithmBase::sourceFlags() const
64{
66}
67
68void QgsExtractZMValuesAlgorithmBase::initParameters( const QVariantMap & )
69{
70 QStringList statChoices;
71 statChoices.reserve( STATS.size() );
72 for ( Qgis::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 = Qgis::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 ) == Qgis::Statistic::Count || STATS.at( s ) == Qgis::Statistic::Variety ? QMetaType::Type::Int : QMetaType::Type::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 == Qgis::Statistics( Qgis::Statistic::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 ( Qgis::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
@ VectorAnyGeometry
Any vector layer with geometry.
Statistic
Available generic statistics.
Definition qgis.h:5446
@ StDev
Standard deviation of values.
@ FirstQuartile
First quartile.
@ Mean
Mean of values.
@ Median
Median of values.
@ Max
Max of values.
@ Min
Min of values.
@ First
First value.
@ Range
Range of values (max - min)
@ Sum
Sum of values.
@ Minority
Minority of values.
@ Majority
Majority of values.
@ Variety
Variety (count of distinct) values.
@ Last
Last value.
@ ThirdQuartile
Third quartile.
@ InterQuartileRange
Inter quartile range (IQR)
QFlags< Statistic > Statistics
Statistics to be calculated for generic values.
Definition qgis.h:5474
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3444
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:53
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:76
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 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