QGIS API Documentation  3.6.0-Noosa (5873452)
qgsalgorithmkmeansclustering.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmkmeansclustering.h
3  ---------------------
4  begin : June 2018
5  copyright : (C) 2018 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 
18 #ifndef QGSALGORITHMKMEANSCLUSTERING_H
19 #define QGSALGORITHMKMEANSCLUSTERING_H
20 
21 #define SIP_NO_FILE
22 
23 #include "qgis_sip.h"
24 #include "qgis_analysis.h"
25 #include "qgsprocessingalgorithm.h"
26 
28 
29 
33 class ANALYSIS_EXPORT QgsKMeansClusteringAlgorithm : public QgsProcessingAlgorithm
34 {
35 
36  public:
37 
38  QgsKMeansClusteringAlgorithm() = default;
39  void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
40  QString name() const override;
41  QString displayName() const override;
42  QStringList tags() const override;
43  QString group() const override;
44  QString groupId() const override;
45  QString shortHelpString() const override;
46  QgsKMeansClusteringAlgorithm *createInstance() const override SIP_FACTORY;
47 
48  protected:
49 
50  QVariantMap processAlgorithm( const QVariantMap &parameters,
51  QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
52 
53  private:
54 
55  struct Feature
56  {
57  Feature( QgsPointXY point )
58  : point( point )
59  {}
60 
61  QgsPointXY point;
62  int cluster = -1;
63  };
64 
65  static void initClusters( std::vector< Feature > &points, std::vector< QgsPointXY > &centers, int k, QgsProcessingFeedback *feedback );
66  static void calculateKMeans( std::vector< Feature > &points, std::vector< QgsPointXY > &centers, int k, QgsProcessingFeedback *feedback );
67  static void findNearest( std::vector< Feature > &points, const std::vector< QgsPointXY > &centers, int k, bool &changed );
68  static void updateMeans( const std::vector< Feature > &points, std::vector< QgsPointXY > &centers, std::vector< uint > &weights, int k );
69 
70  friend class TestQgsProcessingAlgs;
71 };
72 
74 
75 #endif // QGSALGORITHMKMEANSCLUSTERING_H
76 
77 
Base class for providing feedback from a processing algorithm.
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
virtual QString group() const
Returns the name of the group this algorithm belongs to.
A class to represent a 2D point.
Definition: qgspointxy.h:43
virtual QStringList tags() const
Returns a list of tags which relate to the algorithm, and are used to assist users in searching for s...
virtual QString shortHelpString() const
Returns a localised short helper string for the algorithm.
Abstract base class for processing algorithms.
virtual void initAlgorithm(const QVariantMap &configuration=QVariantMap())=0
Initializes the algorithm using the specified configuration.
#define SIP_FACTORY
Definition: qgis_sip.h:69
virtual QgsProcessingAlgorithm * createInstance() const =0
Creates a new instance of the algorithm class.
virtual QVariantMap processAlgorithm(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback) SIP_THROW(QgsProcessingException)=0
Runs the algorithm using the specified parameters.
virtual QString displayName() const =0
Returns the translated algorithm name, which should be used for any user-visible display of the algor...
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
Contains information about the context in which a processing algorithm is executed.