Skip to content
Permalink
Browse files

Merge pull request #42381 from agiudiceandrea/fix-centroids-alg

[processing] Fix "Centroids" alg with multipart geometries and fid field in geopackage
  • Loading branch information
nyalldawson committed Mar 22, 2021
2 parents 45001d7 + da66253 commit 97dced969b5e66a327394feed8e8e0b80586c984
Showing with 15 additions and 5 deletions.
  1. +14 −5 src/analysis/processing/qgsalgorithmcentroid.cpp
  2. +1 −0 src/analysis/processing/qgsalgorithmcentroid.h
@@ -50,6 +50,14 @@ QString QgsCentroidAlgorithm::outputName() const
return QObject::tr( "Centroids" );
}

QgsFeatureSink::SinkFlags QgsCentroidAlgorithm::sinkFlags() const
{
if ( mAllParts )
return QgsProcessingFeatureBasedAlgorithm::sinkFlags() | QgsFeatureSink::RegeneratePrimaryKey;
else
return QgsProcessingFeatureBasedAlgorithm::sinkFlags();
}

QString QgsCentroidAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm creates a new point layer, with points representing the centroid of the geometries in an input layer.\n\n"
@@ -87,7 +95,7 @@ QgsFeatureList QgsCentroidAlgorithm::processFeature( const QgsFeature &f, QgsPro
{
QgsFeatureList list;
QgsFeature feature = f;
if ( feature.hasGeometry() )
if ( feature.hasGeometry() && !feature.geometry().isEmpty() )
{
QgsGeometry geom = feature.geometry();

@@ -99,14 +107,15 @@ QgsFeatureList QgsCentroidAlgorithm::processFeature( const QgsFeature &f, QgsPro
{
const QgsGeometryCollection *geomCollection = static_cast<const QgsGeometryCollection *>( geom.constGet() );

list.reserve( geomCollection->partCount() );
for ( int i = 0; i < geomCollection->partCount(); ++i )
const int partCount = geomCollection->partCount();
list.reserve( partCount );
for ( int i = 0; i < partCount; ++i )
{
QgsGeometry partGeometry( geomCollection->geometryN( i )->clone() );
QgsGeometry outputGeometry = partGeometry.centroid();
if ( outputGeometry.isNull() )
{
feedback->pushInfo( QObject::tr( "Error calculating centroid for feature %1 part %2: %3" ).arg( feature.id() ).arg( i ).arg( outputGeometry.lastError() ) );
feedback->reportError( QObject::tr( "Error calculating centroid for feature %1 part %2: %3" ).arg( feature.id() ).arg( i ).arg( outputGeometry.lastError() ) );
}
feature.setGeometry( outputGeometry );
list << feature;
@@ -117,7 +126,7 @@ QgsFeatureList QgsCentroidAlgorithm::processFeature( const QgsFeature &f, QgsPro
QgsGeometry outputGeometry = feature.geometry().centroid();
if ( outputGeometry.isNull() )
{
feedback->pushInfo( QObject::tr( "Error calculating centroid for feature %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
feedback->reportError( QObject::tr( "Error calculating centroid for feature %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
}
feature.setGeometry( outputGeometry );
list << feature;
@@ -51,6 +51,7 @@ class QgsCentroidAlgorithm : public QgsProcessingFeatureBasedAlgorithm
QString outputName() const override;
QgsProcessing::SourceType outputLayerType() const override { return QgsProcessing::TypeVectorPoint; }
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override { Q_UNUSED( inputWkbType ) return QgsWkbTypes::Point; }
QgsFeatureSink::SinkFlags sinkFlags() const override;

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

0 comments on commit 97dced9

Please sign in to comment.