Skip to content

Commit

Permalink
Don't crash when calculating centroid of empty geometry
Browse files Browse the repository at this point in the history
Refs #38983
  • Loading branch information
nyalldawson committed Jun 10, 2021
1 parent 73a4948 commit 9059cda
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core/geometry/qgsabstractgeometry.cpp
Expand Up @@ -199,6 +199,9 @@ json QgsAbstractGeometry::asJsonObject( int precision ) const

QgsPoint QgsAbstractGeometry::centroid() const
{
if ( isEmpty() )
return QgsPoint();

// http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
// Pick the first ring of first part for the moment

Expand Down
8 changes: 6 additions & 2 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -2589,7 +2589,7 @@ void TestQgsGeometry::circularString()

//centroid
QgsCircularString l34;
QCOMPARE( l34.centroid(), QgsPoint( 0, 0 ) );
QCOMPARE( l34.centroid(), QgsPoint() );
l34.setPoints( QgsPointSequence() << QgsPoint( 5, 10 ) );
QCOMPARE( l34.centroid(), QgsPoint( 5, 10 ) );
l34.setPoints( QgsPointSequence() << QgsPoint( 0, 0 ) << QgsPoint( 20, 10 ) << QgsPoint( 2, 9 ) );
Expand Down Expand Up @@ -11995,7 +11995,7 @@ void TestQgsGeometry::compoundCurve()
//centroid
QgsCircularString l34;
QgsCompoundCurve c34;
QCOMPARE( c34.centroid(), QgsPoint( 0, 0 ) );
QCOMPARE( c34.centroid(), QgsPoint() );
l34.setPoints( QgsPointSequence() << QgsPoint( 5, 10 ) );
c34.addCurve( l34.clone() );
QCOMPARE( c34.centroid(), QgsPoint( 5, 10 ) );
Expand Down Expand Up @@ -15829,6 +15829,10 @@ void TestQgsGeometry::multiPolygon()
QVERIFY( dn1.removeDuplicateNodes( 0.001, false ) );
QVERIFY( !dn1.removeDuplicateNodes( 0.001, false ) );
QCOMPARE( dn1.asWkt(), QStringLiteral( "MultiPolygonZ (((0 0 0, 10 10 0, 11 9 0, 9 8 0, 1 -1 0, 0 0 0)),((7 -1 0, 12 7 0, 13 6 0, 8 -3 0, 7 -1 0)))" ) );

// test centroid of empty multipolygon
QgsMultiPolygon empty;
QCOMPARE( empty.centroid().asWkt(), QStringLiteral( "Point EMPTY" ) );
}

void TestQgsGeometry::geometryCollection()
Expand Down

0 comments on commit 9059cda

Please sign in to comment.