Skip to content

Commit

Permalink
[processing] Centroids: optimize for multipart and better error repor…
Browse files Browse the repository at this point in the history
…ting

See 954ad35 #42363

(cherry picked from commit da66253)
  • Loading branch information
agiudiceandrea authored and nyalldawson committed Apr 17, 2021
1 parent ab5e990 commit 671c433
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/analysis/processing/qgsalgorithmcentroid.cpp
Expand Up @@ -95,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();

Expand All @@ -107,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;
Expand All @@ -125,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;
Expand Down

0 comments on commit 671c433

Please sign in to comment.