From a0ddf6fbc995ca72551ea3cdbab57c2a017ae6c8 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Thu, 4 Feb 2021 18:52:38 +0100 Subject: [PATCH] Fix fromGeos returning 0 for x/y on empty point --- src/core/geometry/qgsgeos.cpp | 2 ++ tests/src/core/testqgsgeometry.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/core/geometry/qgsgeos.cpp b/src/core/geometry/qgsgeos.cpp index cd38ad33cb56..290febcdd376 100644 --- a/src/core/geometry/qgsgeos.cpp +++ b/src/core/geometry/qgsgeos.cpp @@ -1111,6 +1111,8 @@ std::unique_ptr QgsGeos::fromGeos( const GEOSGeometry *geos { case GEOS_POINT: // a point { + if ( GEOSisEmpty_r( geosinit()->ctxt, geos ) ) + return qgis::make_unique< QgsPoint >(); const GEOSCoordSequence *cs = GEOSGeom_getCoordSeq_r( geosinit()->ctxt, geos ); unsigned int nPoints = 0; GEOSCoordSeq_getSize_r( geosinit()->ctxt, cs, &nPoints ); diff --git a/tests/src/core/testqgsgeometry.cpp b/tests/src/core/testqgsgeometry.cpp index 379bd506934f..c15bae6cd0f0 100644 --- a/tests/src/core/testqgsgeometry.cpp +++ b/tests/src/core/testqgsgeometry.cpp @@ -613,6 +613,12 @@ void TestQgsGeometry::geos() QCOMPARE( GEOSGetNumGeometries_r( QgsGeos::getGEOSHandler(), asGeos.get() ), 2 ); res = QgsGeometry( QgsGeos::fromGeos( asGeos.get() ) ); QCOMPARE( res.asWkt(), QStringLiteral( "MultiPolygon (((0 0, 0 1, 1 1, 0 0)),((10 0, 10 1, 11 1, 10 0)))" ) ); + + // Empty geometry + QgsPoint point; + asGeos = QgsGeos::asGeos( &point ); + res = QgsGeometry( QgsGeos::fromGeos( asGeos.get() ) ); + QCOMPARE( res.asWkt(), QgsPoint().asWkt( ) ); } void TestQgsGeometry::point()