QGIS API Documentation  3.0.2-Girona (307d082)
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( QObject::tr( "Could not load source layer for 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( QObject::tr( "Could not create destination layer for 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 
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
double y
Definition: qgspoint.h:42
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...
A vector layer or feature source field parameter for processing algorithms.
A class to represent a 2D point.
Definition: qgspointxy.h:43
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
Container of fields for a vector layer.
Definition: qgsfields.h:42
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:62
virtual bool nextVertex(QgsVertexId &id, QgsPoint &vertex) const =0
Returns next vertex id and coordinates.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:251
A feature sink output for processing algorithms.
Utility class for identifying a unique vertex within a geometry.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
Abstract base class for all geometries.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
Vector point layers.
Definition: qgsprocessing.h:49
An input feature source (such as vector layers) parameter for processing algorithms.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
QList< int > QgsAttributeList
Definition: qgsfield.h:27
bool nextFeature(QgsFeature &f)
A vector of attributes.
Definition: qgsattributes.h:58
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:255
Contains information about the context in which a processing algorithm is executed.
Any vector layer with geometry.
Definition: qgsprocessing.h:48
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
double x
Definition: qgspoint.h:41