QGIS API Documentation  3.0.2-Girona (307d082)
qgsalgorithmcentroid.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmcentroid.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 
18 #include "qgsalgorithmcentroid.h"
19 
21 
22 QString QgsCentroidAlgorithm::name() const
23 {
24  return QStringLiteral( "centroids" );
25 }
26 
27 QString QgsCentroidAlgorithm::displayName() const
28 {
29  return QObject::tr( "Centroids" );
30 }
31 
32 QStringList QgsCentroidAlgorithm::tags() const
33 {
34  return QObject::tr( "centroid,center,average,point,middle" ).split( ',' );
35 }
36 
37 QString QgsCentroidAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsCentroidAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsCentroidAlgorithm::outputName() const
48 {
49  return QObject::tr( "Centroids" );
50 }
51 
52 void QgsCentroidAlgorithm::initAlgorithm( const QVariantMap & )
53 {
54  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
55  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Centroids" ), QgsProcessing::TypeVectorPoint ) );
56 }
57 
58 QString QgsCentroidAlgorithm::shortHelpString() const
59 {
60  return QObject::tr( "This algorithm creates a new point layer, with points representing the centroid of the geometries in an input layer.\n\n"
61  "The attributes associated to each point in the output layer are the same ones associated to the original features." );
62 }
63 
64 QgsCentroidAlgorithm *QgsCentroidAlgorithm::createInstance() const
65 {
66  return new QgsCentroidAlgorithm();
67 }
68 
69 QgsFeatureList QgsCentroidAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
70 {
71  QgsFeature feature = f;
72  if ( feature.hasGeometry() )
73  {
74  feature.setGeometry( feature.geometry().centroid() );
75  if ( !feature.geometry() )
76  {
77  feedback->pushInfo( QObject::tr( "Error calculating centroid for feature %1" ).arg( feature.id() ) );
78  }
79  }
80  return QgsFeatureList() << feature;
81 }
82 
QgsFeatureId id
Definition: qgsfeature.h:71
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
QgsGeometry centroid() const
Returns the center of mass of a geometry.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
A feature sink output for processing algorithms.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
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
Contains information about the context in which a processing algorithm is executed.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.