Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
|
@@ -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 ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; |
|
|
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; |
|
|