QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 = nullptr;
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 : qgis::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 
Base class for all map layer types.
Definition: qgsmaplayer.h:79
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Statistic
Enumeration of flags that specify statistics to be calculated.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Variety (count of distinct) values.
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
Abstract base class for processing algorithms.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:917
An enum based parameter for processing algorithms, allowing for selection from predefined values...
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
First value (since QGIS 3.6)
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
Last value (since QGIS 3.6)
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
static QString shortName(QgsStatisticalSummary::Statistic statistic)
Returns a short, friendly display name for a statistic, suitable for use in a field name...
static QString displayName(QgsStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:967
Standard deviation of values.
QgsGeometry geometry
Definition: qgsfeature.h:67
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).
A vector of attributes.
Definition: qgsattributes.h:57
Calculator for summary statistics for a list of doubles.
QgsAbstractGeometry::vertex_iterator vertices_end() const
Returns STL-style iterator pointing to the imaginary vertex after the last vertex of the geometry...
Contains information about the context in which a processing algorithm is executed.
QgsAbstractGeometry::vertex_iterator vertices_begin() const
Returns STL-style iterator pointing to the first vertex of the geometry.
A string parameter for processing algorithms.
Any vector layer with geometry.
Definition: qgsprocessing.h:47
QgsAttributes attributes
Definition: qgsfeature.h:65
Range of values (max - min)