Skip to content
Permalink
Browse files
Don't crash when calculating centroid of empty geometry
Refs #38983
  • Loading branch information
nyalldawson committed Jun 10, 2021
1 parent 73a4948 commit 9059cda9b391d3553b4a2f3c889532ebe4851c42
Showing with 9 additions and 2 deletions.
  1. +3 −0 src/core/geometry/qgsabstractgeometry.cpp
  2. +6 −2 tests/src/core/testqgsgeometry.cpp
@@ -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

@@ -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 ) );
@@ -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 ) );
@@ -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()

0 comments on commit 9059cda

Please sign in to comment.