QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  QString weightFieldName = parameterAsString( parameters, QStringLiteral( "WEIGHT" ), context );
81  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  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  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  double cx = it.value().at( 0 ) / it.value().at( 2 );
201  double cy = it.value().at( 1 ) / it.value().at( 2 );
202 
203  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  sink->addFeature( outFeat, QgsFeatureSink::FastInsert );
213  }
214 
215  QVariantMap outputs;
216  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
217  return outputs;
218 }
219 
220 
222 
223 
QgsGeometry::fromPointXY
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
Definition: qgsgeometry.cpp:164
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:75
QgsProcessingParameterField::Numeric
@ Numeric
Accepts numeric fields.
Definition: qgsprocessingparameters.h:2503
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsFields
Definition: qgsfields.h:44
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:475
QgsProcessingParameterFeatureSource
Definition: qgsprocessingparameters.h:2612
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:48
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsProcessingParameterFeatureSink
Definition: qgsprocessingparameters.h:2773
QgsPoint::y
double y
Definition: qgspoint.h:59
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
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:315
QgsProcessing::TypeVectorAnyGeometry
@ TypeVectorAnyGeometry
Any vector layer with geometry.
Definition: qgsprocessing.h:47
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
qgsalgorithmmeancoordinates.h
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
QgsPointXY
Definition: qgspointxy.h:43
QgsFeedback::isCanceled
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:66
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:373
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1033
QgsAttributes
Definition: qgsattributes.h:57
QgsFeature
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsProcessingParameterField::Any
@ Any
Accepts any field.
Definition: qgsprocessingparameters.h:2502
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsAbstractGeometry::nextVertex
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
QgsProcessingException
Definition: qgsexception.h:82
QgsProcessingParameterField
Definition: qgsprocessingparameters.h:2495
QgsPoint::x
double x
Definition: qgspoint.h:58
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
Definition: qgsfield.h:49