QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
23 const std::vector< QgsStatisticalSummary::Statistic > STATS
24 {
41 };
42 
43 QString QgsExtractZMValuesAlgorithmBase::group() const
44 {
45  return QObject::tr( "Vector geometry" );
46 }
47 
48 QString QgsExtractZMValuesAlgorithmBase::groupId() const
49 {
50  return QStringLiteral( "vectorgeometry" );
51 }
52 
53 QString QgsExtractZMValuesAlgorithmBase::outputName() const
54 {
55  return QObject::tr( "Extracted" );
56 }
57 
58 QList<int> QgsExtractZMValuesAlgorithmBase::inputLayerTypes() const
59 {
60  return QList<int>() << QgsProcessing::TypeVectorAnyGeometry;
61 }
62 
63 QgsProcessingFeatureSource::Flag QgsExtractZMValuesAlgorithmBase::sourceFlags() const
64 {
66 }
67 
68 void QgsExtractZMValuesAlgorithmBase::initParameters( const QVariantMap & )
69 {
70  QStringList statChoices;
71  statChoices.reserve( STATS.size() );
72  for ( QgsStatisticalSummary::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 
84 QgsFields QgsExtractZMValuesAlgorithmBase::outputFields( const QgsFields &inputFields ) const
85 {
86  return QgsProcessingUtils::combineFields( inputFields, mNewFields );
87 }
88 
89 bool 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 = QgsStatisticalSummary::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 ) == QgsStatisticalSummary::Count || STATS.at( s ) == QgsStatisticalSummary::Variety ? QVariant::Int : QVariant::Double ) );
100  }
101 
102  return true;
103 }
104 
105 QgsFeatureList 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 == QgsStatisticalSummary::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 ( QgsStatisticalSummary::Statistic s : std::as_const( mSelectedStats ) )
131  attrs.append( stat.statistic( s ) );
132  }
133  f.setAttributes( attrs );
134 
135  return QgsFeatureList() << f;
136 }
137 
138 bool QgsExtractZMValuesAlgorithmBase::supportInPlaceEdit( const QgsMapLayer *layer ) const
139 {
140  Q_UNUSED( layer )
141  return false;
142 }
143 
144 //
145 // QgsExtractZValuesAlgorithm
146 //
147 
148 QgsExtractZValuesAlgorithm::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 
161 QString QgsExtractZValuesAlgorithm::name() const
162 {
163  return QStringLiteral( "extractzvalues" );
164 }
165 
166 QString QgsExtractZValuesAlgorithm::displayName() const
167 {
168  return QObject::tr( "Extract Z values" );
169 }
170 
171 QgsProcessingAlgorithm *QgsExtractZValuesAlgorithm::createInstance() const
172 {
173  return new QgsExtractZValuesAlgorithm();
174 }
175 
176 QStringList QgsExtractZValuesAlgorithm::tags() const
177 {
178  return QObject::tr( "add,z,value,elevation,height,attribute,statistics,stats" ).split( ',' );
179 }
180 
181 QString 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 
188 QString 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 
198 QgsExtractMValuesAlgorithm::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 
211 QString QgsExtractMValuesAlgorithm::name() const
212 {
213  return QStringLiteral( "extractmvalues" );
214 }
215 
216 QString QgsExtractMValuesAlgorithm::displayName() const
217 {
218  return QObject::tr( "Extract M values" );
219 }
220 
221 QgsProcessingAlgorithm *QgsExtractMValuesAlgorithm::createInstance() const
222 {
223  return new QgsExtractMValuesAlgorithm();
224 }
225 
226 QStringList QgsExtractMValuesAlgorithm::tags() const
227 {
228  return QObject::tr( "add,m,value,measure,attribute,statistics,stats" ).split( ',' );
229 }
230 
231 QString 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 
238 QString QgsExtractMValuesAlgorithm::shortDescription() const
239 {
240  return QObject::tr( "Extracts m values (or m value statistics) from geometries into feature attributes." );
241 }
242 
243 
245 
QgsStatisticalSummary
Calculator for summary statistics for a list of doubles.
Definition: qgsstatisticalsummary.h:43
qgsfeaturerequest.h
QgsStatisticalSummary::shortName
static QString shortName(QgsStatisticalSummary::Statistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name.
Definition: qgsstatisticalsummary.cpp:343
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsStatisticalSummary::StDev
@ StDev
Standard deviation of values.
Definition: qgsstatisticalsummary.h:55
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsStatisticalSummary::InterQuartileRange
@ InterQuartileRange
Inter quartile range (IQR)
Definition: qgsstatisticalsummary.h:65
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsStatisticalSummary::ThirdQuartile
@ ThirdQuartile
Third quartile.
Definition: qgsstatisticalsummary.h:64
QgsStatisticalSummary::First
@ First
First value (since QGIS 3.6)
Definition: qgsstatisticalsummary.h:66
QgsProcessingUtils::combineFields
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).
Definition: qgsprocessingutils.cpp:1238
QgsStatisticalSummary::Sum
@ Sum
Sum of values.
Definition: qgsstatisticalsummary.h:52
QgsStatisticalSummary::Minority
@ Minority
Minority of values.
Definition: qgsstatisticalsummary.h:60
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessing::TypeVectorAnyGeometry
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:48
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2647
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsWkbTypes::hasM
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1130
QgsStatisticalSummary::Range
@ Range
Range of values (max - min)
Definition: qgsstatisticalsummary.h:59
QgsStatisticalSummary::Majority
@ Majority
Majority of values.
Definition: qgsstatisticalsummary.h:61
QgsGeometry::isEmpty
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Definition: qgsgeometry.cpp:379
QgsStatisticalSummary::displayName
static QString displayName(QgsStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
Definition: qgsstatisticalsummary.cpp:297
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsStatisticalSummary::Statistic
Statistic
Enumeration of flags that specify statistics to be calculated.
Definition: qgsstatisticalsummary.h:48
QgsStatisticalSummary::Median
@ Median
Median of values.
Definition: qgsstatisticalsummary.h:54
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:582
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsStatisticalSummary::Min
@ Min
Min of values.
Definition: qgsstatisticalsummary.h:57
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsStatisticalSummary::FirstQuartile
@ FirstQuartile
First quartile.
Definition: qgsstatisticalsummary.h:63
QgsStatisticalSummary::Mean
@ Mean
Mean of values.
Definition: qgsstatisticalsummary.h:53
QgsWkbTypes::hasZ
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1080
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsStatisticalSummary::Max
@ Max
Max of values.
Definition: qgsstatisticalsummary.h:58
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsStatisticalSummary::Count
@ Count
Count.
Definition: qgsstatisticalsummary.h:50
QgsGeometry::vertices_end
QgsAbstractGeometry::vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry.
Definition: qgsgeometry.cpp:1989
qgsalgorithmextractzmvalues.h
QgsProcessingParameterEnum
An enum based parameter for processing algorithms, allowing for selection from predefined values.
Definition: qgsprocessingparameters.h:2540
QgsStatisticalSummary::Last
@ Last
Last value (since QGIS 3.6)
Definition: qgsstatisticalsummary.h:67
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsGeometry::vertices_begin
QgsAbstractGeometry::vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
Definition: qgsgeometry.cpp:1982
QgsGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Definition: qgsgeometry.cpp:357
QgsStatisticalSummary::Variety
@ Variety
Variety (count of distinct) values.
Definition: qgsstatisticalsummary.h:62
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50