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