diff --git a/src/core/geometry/qgsabstractgeometry.cpp b/src/core/geometry/qgsabstractgeometry.cpp index 3d7f0f0364a6..9b9cc36c3ea2 100644 --- a/src/core/geometry/qgsabstractgeometry.cpp +++ b/src/core/geometry/qgsabstractgeometry.cpp @@ -166,6 +166,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 diff --git a/tests/src/core/testqgsgeometry.cpp b/tests/src/core/testqgsgeometry.cpp index 03d71d94756c..ecec6381d89e 100644 --- a/tests/src/core/testqgsgeometry.cpp +++ b/tests/src/core/testqgsgeometry.cpp @@ -2465,7 +2465,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 ) ); @@ -11394,7 +11394,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 ) ); @@ -15185,6 +15185,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()