QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
103 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 )
104 );
105 }
106
107 return true;
108}
109
110QgsFeatureList QgsExtractZMValuesAlgorithmBase::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
111{
112 QgsFeature f = feature;
113 QgsAttributes attrs = f.attributes();
114 attrs.reserve( attrs.count() + mSelectedStats.count() );
115 if ( !f.hasGeometry() || f.geometry().isEmpty() || !mTestGeomFunc( f.geometry() ) )
116 {
117 attrs.resize( attrs.count() + mNewFields.size() );
118 }
119 else
120 {
121 const QgsGeometry g = f.geometry();
122 QgsStatisticalSummary stat( mStats );
123 for ( auto it = g.vertices_begin(); it != g.vertices_end(); ++it )
124 {
125 stat.addValue( mExtractValFunc( *it ) );
126 if ( mStats == Qgis::Statistics( Qgis::Statistic::First ) )
127 {
128 // only retrieving first vertex info (default behavior), so short cut and
129 // don't iterate remaining vertices
130 break;
131 }
132 }
133 stat.finalize();
134
135 for ( Qgis::Statistic s : std::as_const( mSelectedStats ) )
136 attrs.append( stat.statistic( s ) );
137 }
138 f.setAttributes( attrs );
139
140 return QgsFeatureList() << f;
141}
142
143bool QgsExtractZMValuesAlgorithmBase::supportInPlaceEdit( const QgsMapLayer *layer ) const
144{
145 Q_UNUSED( layer )
146 return false;
147}
148
149//
150// QgsExtractZValuesAlgorithm
151//
152
153QgsExtractZValuesAlgorithm::QgsExtractZValuesAlgorithm()
154{
155 mExtractValFunc = []( const QgsPoint &p ) -> double { return p.z(); };
156 mTestGeomFunc = []( const QgsGeometry &g ) -> bool { return QgsWkbTypes::hasZ( g.wkbType() ); };
157 mDefaultFieldPrefix = u"z_"_s;
158}
159
160QString QgsExtractZValuesAlgorithm::name() const
161{
162 return u"extractzvalues"_s;
163}
164
165QString QgsExtractZValuesAlgorithm::displayName() const
166{
167 return QObject::tr( "Extract Z values" );
168}
169
170QgsProcessingAlgorithm *QgsExtractZValuesAlgorithm::createInstance() const
171{
172 return new QgsExtractZValuesAlgorithm();
173}
174
175QStringList QgsExtractZValuesAlgorithm::tags() const
176{
177 return QObject::tr( "add,z,value,elevation,height,attribute,statistics,stats" ).split( ',' );
178}
179
180QString QgsExtractZValuesAlgorithm::shortHelpString() const
181{
182 return QObject::tr(
183 "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}
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 { return p.m(); };
202 mTestGeomFunc = []( const QgsGeometry &g ) -> bool { return QgsWkbTypes::hasM( g.wkbType() ); };
203 mDefaultFieldPrefix = u"m_"_s;
204}
205
206QString QgsExtractMValuesAlgorithm::name() const
207{
208 return u"extractmvalues"_s;
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(
229 "Extracts m values from geometries into feature attributes.\n\n"
230 "By default only the m value from the first vertex of each feature is extracted, however the algorithm "
231 "can optionally calculate statistics on all of the geometry's m values, including sums, means, and minimums and maximums"
232 );
233}
234
235QString QgsExtractMValuesAlgorithm::shortDescription() const
236{
237 return QObject::tr( "Extracts m values (or m value statistics) from geometries into feature attributes." );
238}
239
240
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3647
Statistic
Available generic statistics.
Definition qgis.h:6206
@ StDev
Standard deviation of values.
Definition qgis.h:6212
@ FirstQuartile
First quartile.
Definition qgis.h:6220
@ Mean
Mean of values.
Definition qgis.h:6210
@ Median
Median of values.
Definition qgis.h:6211
@ Max
Max of values.
Definition qgis.h:6215
@ Min
Min of values.
Definition qgis.h:6214
@ First
First value.
Definition qgis.h:6223
@ Range
Range of values (max - min).
Definition qgis.h:6216
@ Sum
Sum of values.
Definition qgis.h:6209
@ Minority
Minority of values.
Definition qgis.h:6217
@ Majority
Majority of values.
Definition qgis.h:6218
@ Variety
Variety (count of distinct) values.
Definition qgis.h:6219
@ Last
Last value.
Definition qgis.h:6224
@ Count
Count.
Definition qgis.h:6207
@ ThirdQuartile
Third quartile.
Definition qgis.h:6221
@ InterQuartileRange
Inter quartile range (IQR).
Definition qgis.h:6222
QFlags< Statistic > Statistics
Statistics to be calculated for generic values.
Definition qgis.h:6234
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
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