Skip to content

Commit

Permalink
Protect against centroid failures
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 28, 2018
1 parent ce541cb commit c20717a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/analysis/processing/qgsalgorithmkmeansclustering.cpp
Expand Up @@ -109,17 +109,20 @@ QVariantMap QgsKMeansClusteringAlgorithm::processAlgorithm( const QVariantMap &p
if ( !feat.hasGeometry() )
continue;

n++;

QgsPointXY point;
if ( QgsWkbTypes::flatType( feat.geometry().wkbType() ) == QgsWkbTypes::Point )
point = QgsPointXY( *qgsgeometry_cast< const QgsPoint * >( feat.geometry().constGet() ) );
else
{
QgsGeometry centroid = feat.geometry().centroid();
if ( !centroid )
continue; // centroid failed, e.g. empty linestring

point = QgsPointXY( *qgsgeometry_cast< const QgsPoint * >( centroid.constGet() ) );
}

n++;

idToObj.insert( feat.id(), clusterFeatures.size() );
clusterFeatures.emplace_back( Feature( point ) );
}
Expand Down

0 comments on commit c20717a

Please sign in to comment.