QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:76
QgsProcessingParameterField::Numeric
@ Numeric
Accepts numeric fields.
Definition: qgsprocessingparameters.h:2948
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:72
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
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
QgsGeometry::fromPointXY
static QgsGeometry fromPointXY(const QgsPointXY &point) SIP_HOLDGIL
Creates a new geometry from a QgsPointXY object.
Definition: qgsgeometry.cpp:176
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsProcessingParameterFeatureSource
An input feature source (such as vector layers) parameter for processing algorithms.
Definition: qgsprocessingparameters.h:3057
QgsFields::append
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
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:49
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsProcessingParameterFeatureSink
A feature sink output for processing algorithms.
Definition: qgsprocessingparameters.h:3219
QgsPoint::y
double y
Definition: qgspoint.h:70
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsProcessing::TypeVectorAnyGeometry
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:48
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:327
qgsalgorithmmeancoordinates.h
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:79
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:30
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsProcessingParameterField::Any
@ Any
Accepts any field.
Definition: qgsprocessingparameters.h:2947
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsAbstractGeometry::nextVertex
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsProcessingParameterField
A vector layer or feature source field parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2940
QgsPoint::x
double x
Definition: qgspoint.h:69
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50