16
16
***************************************************************************/
17
17
18
18
#include " qgsalgorithmcentroid.h"
19
+ #include " qgsgeometrycollection.h"
19
20
20
21
// /@cond PRIVATE
21
22
@@ -49,12 +50,6 @@ QString QgsCentroidAlgorithm::outputName() const
49
50
return QObject::tr ( " Centroids" );
50
51
}
51
52
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
53
QString QgsCentroidAlgorithm::shortHelpString () const
59
54
{
60
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 "
@@ -66,18 +61,72 @@ QgsCentroidAlgorithm *QgsCentroidAlgorithm::createInstance() const
66
61
return new QgsCentroidAlgorithm ();
67
62
}
68
63
69
- QgsFeatureList QgsCentroidAlgorithm::processFeature ( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
64
+ void QgsCentroidAlgorithm::initParameters ( const QVariantMap & )
70
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 ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
77
+ {
78
+ mAllParts = parameterAsBool ( 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;
71
89
QgsFeature feature = f;
72
90
if ( feature.hasGeometry () )
73
91
{
74
- feature.setGeometry ( feature.geometry ().centroid () );
75
- if ( !feature.geometry () )
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
+ for ( int i = 0 ; i < geomCollection->partCount (); ++i )
103
+ {
104
+ QgsGeometry partGeometry ( geomCollection->geometryN ( i )->clone () );
105
+ QgsGeometry outputGeometry = partGeometry.centroid ();
106
+ if ( !outputGeometry )
107
+ {
108
+ feedback->pushInfo ( QObject::tr ( " Error calculating centroid for feature %1 part %2: %3" ).arg ( feature.id () ).arg ( i ).arg ( outputGeometry.lastError () ) );
109
+ }
110
+ feature.setGeometry ( outputGeometry );
111
+ list << feature;
112
+ }
113
+ }
114
+ else
76
115
{
77
- feedback->pushInfo ( QObject::tr ( " Error calculating centroid for feature %1" ).arg ( feature.id () ) );
116
+ QgsGeometry outputGeometry = feature.geometry ().centroid ();
117
+ if ( !outputGeometry )
118
+ {
119
+ feedback->pushInfo ( QObject::tr ( " Error calculating centroid for feature %1: %2" ).arg ( feature.id () ).arg ( outputGeometry.lastError () ) );
120
+ }
121
+ feature.setGeometry ( outputGeometry );
122
+ list << feature;
78
123
}
79
124
}
80
- return QgsFeatureList () << feature;
125
+ else
126
+ {
127
+ list << feature;
128
+ }
129
+ return list;
81
130
}
82
131
83
132
// /@endcond
0 commit comments