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