QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsalgorithmmeancoordinates.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmmeancoordinates.cpp
3  ---------------------
4  begin : April 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 
21 
22 QString QgsMeanCoordinatesAlgorithm::name() const
23 {
24  return QStringLiteral( "meancoordinates" );
25 }
26 
27 QString QgsMeanCoordinatesAlgorithm::displayName() const
28 {
29  return QObject::tr( "Mean coordinate(s)" );
30 }
31 
32 QStringList QgsMeanCoordinatesAlgorithm::tags() const
33 {
34  return QObject::tr( "mean,average,coordinate" ).split( ',' );
35 }
36 
37 QString QgsMeanCoordinatesAlgorithm::group() const
38 {
39  return QObject::tr( "Vector analysis" );
40 }
41 
42 QString QgsMeanCoordinatesAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectoranalysis" );
45 }
46 
47 void QgsMeanCoordinatesAlgorithm::initAlgorithm( const QVariantMap & )
48 {
49  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ),
50  QObject::tr( "Input layer" ), QList< int >() << QgsProcessing::TypeVectorAnyGeometry ) );
51  addParameter( new QgsProcessingParameterField( QStringLiteral( "WEIGHT" ), QObject::tr( "Weight field" ),
52  QVariant(), QStringLiteral( "INPUT" ),
53  QgsProcessingParameterField::Numeric, false, true ) );
54  addParameter( new QgsProcessingParameterField( QStringLiteral( "UID" ),
55  QObject::tr( "Unique ID field" ), QVariant(),
56  QStringLiteral( "INPUT" ), QgsProcessingParameterField::Any, false, true ) );
57  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Mean coordinates" ), QgsProcessing::TypeVectorPoint ) );
58 }
59 
60 QString QgsMeanCoordinatesAlgorithm::shortHelpString() const
61 {
62  return QObject::tr( "This algorithm computes a point layer with the center of mass of geometries in an input layer.\n\n"
63  "An attribute can be specified as containing weights to be applied to each feature when computing the center of mass.\n\n"
64  "If an attribute is selected in the <Unique ID field> parameter, features will be grouped according "
65  "to values in this field. Instead of a single point with the center of mass of the whole layer, "
66  "the output layer will contain a center of mass for the features in each category." );
67 }
68 
69 QgsMeanCoordinatesAlgorithm *QgsMeanCoordinatesAlgorithm::createInstance() const
70 {
71  return new QgsMeanCoordinatesAlgorithm();
72 }
73 
74 QVariantMap QgsMeanCoordinatesAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
75 {
76  std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
77  if ( !source )
78  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
79 
80  const QString weightFieldName = parameterAsString( parameters, QStringLiteral( "WEIGHT" ), context );
81  const QString uniqueFieldName = parameterAsString( parameters, QStringLiteral( "UID" ), context );
82 
83  QgsAttributeList attributes;
84  int weightIndex = -1;
85  if ( !weightFieldName.isEmpty() )
86  {
87  weightIndex = source->fields().lookupField( weightFieldName );
88  if ( weightIndex >= 0 )
89  attributes.append( weightIndex );
90  }
91 
92  int uniqueFieldIndex = -1;
93  if ( !uniqueFieldName.isEmpty() )
94  {
95  uniqueFieldIndex = source->fields().lookupField( uniqueFieldName );
96  if ( uniqueFieldIndex >= 0 )
97  attributes.append( uniqueFieldIndex );
98  }
99 
100  QgsFields fields;
101  fields.append( QgsField( QStringLiteral( "MEAN_X" ), QVariant::Double, QString(), 24, 15 ) );
102  fields.append( QgsField( QStringLiteral( "MEAN_Y" ), QVariant::Double, QString(), 24, 15 ) );
103  if ( uniqueFieldIndex >= 0 )
104  {
105  const QgsField uniqueField = source->fields().at( uniqueFieldIndex );
106  fields.append( uniqueField );
107  }
108 
109  QString dest;
110  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields,
111  QgsWkbTypes::Point, source->sourceCrs() ) );
112  if ( !sink )
113  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
114 
115  QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( attributes ), QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks );
116 
117  double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
118  int i = 0;
119  QgsFeature feat;
120 
121  QHash< QVariant, QList< double > > means;
122  while ( features.nextFeature( feat ) )
123  {
124  i++;
125  if ( feedback->isCanceled() )
126  {
127  break;
128  }
129 
130  feedback->setProgress( i * step );
131  if ( !feat.hasGeometry() )
132  continue;
133 
134 
135  QVariant featureClass;
136  if ( uniqueFieldIndex >= 0 )
137  {
138  featureClass = feat.attribute( uniqueFieldIndex );
139  }
140  else
141  {
142  featureClass = QStringLiteral( "#####singleclass#####" );
143  }
144 
145  double weight = 1;
146  if ( weightIndex >= 0 )
147  {
148  bool ok = false;
149  weight = feat.attribute( weightIndex ).toDouble( &ok );
150  if ( !ok )
151  weight = 1.0;
152  }
153 
154  if ( weight < 0 )
155  {
156  throw QgsProcessingException( QObject::tr( "Negative weight value found. Please fix your data and try again." ) );
157  }
158 
159  const QList< double > values = means.value( featureClass );
160  double cx = 0;
161  double cy = 0;
162  double totalWeight = 0;
163  if ( !values.empty() )
164  {
165  cx = values.at( 0 );
166  cy = values.at( 1 );
167  totalWeight = values.at( 2 );
168  }
169 
170  QgsVertexId vid;
171  QgsPoint pt;
172  const QgsAbstractGeometry *g = feat.geometry().constGet();
173  // NOTE - should this be including the duplicate nodes for closed rings? currently it is,
174  // but I suspect that the expected behavior would be to NOT include these
175  while ( g->nextVertex( vid, pt ) )
176  {
177  cx += pt.x() * weight;
178  cy += pt.y() * weight;
179  totalWeight += weight;
180  }
181 
182  means[featureClass] = QList< double >() << cx << cy << totalWeight;
183  }
184 
185  i = 0;
186  step = !means.empty() ? 50.0 / means.count() : 1;
187  for ( auto it = means.constBegin(); it != means.constEnd(); ++it )
188  {
189  i++;
190  if ( feedback->isCanceled() )
191  {
192  break;
193  }
194 
195  feedback->setProgress( 50 + i * step );
196  if ( qgsDoubleNear( it.value().at( 2 ), 0 ) )
197  continue;
198 
199  QgsFeature outFeat;
200  const double cx = it.value().at( 0 ) / it.value().at( 2 );
201  const double cy = it.value().at( 1 ) / it.value().at( 2 );
202 
203  const QgsPointXY meanPoint( cx, cy );
204  outFeat.setGeometry( QgsGeometry::fromPointXY( meanPoint ) );
205 
206  QgsAttributes attributes;
207  attributes << cx << cy;
208  if ( uniqueFieldIndex >= 0 )
209  attributes.append( it.key() );
210 
211  outFeat.setAttributes( attributes );
212  if ( !sink->addFeature( outFeat, QgsFeatureSink::FastInsert ) )
213  throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
214  }
215 
216  QVariantMap outputs;
217  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
218  return outputs;
219 }
220 
221 
223 
224 
Abstract base class for all geometries.
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
A vector of attributes.
Definition: qgsattributes.h:58
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:153
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:223
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:320
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:163
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static QgsGeometry fromPointXY(const QgsPointXY &point) SIP_HOLDGIL
Creates a new geometry from a QgsPointXY object.
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Q_GADGET double x
Definition: qgspoint.h:52
double y
Definition: qgspoint.h:53
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
@ Numeric
Accepts numeric fields.
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:49
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:48
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:1246
QList< int > QgsAttributeList
Definition: qgsfield.h:26
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:31