QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #include "qgsgeometrycollection.h"
20 
22 
23 QString QgsCentroidAlgorithm::name() const
24 {
25  return QStringLiteral( "centroids" );
26 }
27 
28 QString QgsCentroidAlgorithm::displayName() const
29 {
30  return QObject::tr( "Centroids" );
31 }
32 
33 QStringList QgsCentroidAlgorithm::tags() const
34 {
35  return QObject::tr( "centroid,center,average,point,middle" ).split( ',' );
36 }
37 
38 QString QgsCentroidAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsCentroidAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsCentroidAlgorithm::outputName() const
49 {
50  return QObject::tr( "Centroids" );
51 }
52 
53 QString QgsCentroidAlgorithm::shortHelpString() const
54 {
55  return QObject::tr( "This algorithm creates a new point layer, with points representing the centroid of the geometries in an input layer.\n\n"
56  "The attributes associated to each point in the output layer are the same ones associated to the original features." );
57 }
58 
59 QgsCentroidAlgorithm *QgsCentroidAlgorithm::createInstance() const
60 {
61  return new QgsCentroidAlgorithm();
62 }
63 
64 void QgsCentroidAlgorithm::initParameters( const QVariantMap & )
65 {
66  std::unique_ptr< QgsProcessingParameterBoolean> allParts = qgis::make_unique< QgsProcessingParameterBoolean >(
67  QStringLiteral( "ALL_PARTS" ),
68  QObject::tr( "Create point on surface for each part" ),
69  false );
70  allParts->setIsDynamic( true );
71  allParts->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "All parts" ), QObject::tr( "Create point on surface for each part" ), QgsPropertyDefinition::Boolean ) );
72  allParts->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
73  addParameter( allParts.release() );
74 }
75 
76 bool QgsCentroidAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
77 {
78  mAllParts = parameterAsBoolean( parameters, QStringLiteral( "ALL_PARTS" ), context );
79  mDynamicAllParts = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ALL_PARTS" ) );
80  if ( mDynamicAllParts )
81  mAllPartsProperty = parameters.value( QStringLiteral( "ALL_PARTS" ) ).value< QgsProperty >();
82 
83  return true;
84 }
85 
86 QgsFeatureList QgsCentroidAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
87 {
88  QgsFeatureList list;
89  QgsFeature feature = f;
90  if ( feature.hasGeometry() )
91  {
92  QgsGeometry geom = feature.geometry();
93 
94  bool allParts = mAllParts;
95  if ( mDynamicAllParts )
96  allParts = mAllPartsProperty.valueAsBool( context.expressionContext(), allParts );
97 
98  if ( allParts && geom.isMultipart() )
99  {
100  const QgsGeometryCollection *geomCollection = static_cast<const QgsGeometryCollection *>( geom.constGet() );
101 
102  list.reserve( geomCollection->partCount() );
103  for ( int i = 0; i < geomCollection->partCount(); ++i )
104  {
105  QgsGeometry partGeometry( geomCollection->geometryN( i )->clone() );
106  QgsGeometry outputGeometry = partGeometry.centroid();
107  if ( outputGeometry.isNull() )
108  {
109  feedback->pushInfo( QObject::tr( "Error calculating centroid for feature %1 part %2: %3" ).arg( feature.id() ).arg( i ).arg( outputGeometry.lastError() ) );
110  }
111  feature.setGeometry( outputGeometry );
112  list << feature;
113  }
114  }
115  else
116  {
117  QgsGeometry outputGeometry = feature.geometry().centroid();
118  if ( outputGeometry.isNull() )
119  {
120  feedback->pushInfo( QObject::tr( "Error calculating centroid for feature %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
121  }
122  feature.setGeometry( outputGeometry );
123  list << feature;
124  }
125  }
126  else
127  {
128  list << feature;
129  }
130  return list;
131 }
132 
QgsFeatureId id
Definition: qgsfeature.h:64
Base class for providing feedback from a processing algorithm.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
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:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
Geometry collection.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A store for object properties.
Definition: qgsproperty.h:229
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
int partCount() const override
Returns count of parts contained in the geometry.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
QgsGeometry geometry
Definition: qgsfeature.h:67
Contains information about the context in which a processing algorithm is executed.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...