From ca2fabdba8426c62fbed6a33b464b97d10566ca4 Mon Sep 17 00:00:00 2001 From: bdm-oslandia Date: Mon, 26 May 2025 16:33:47 +0200 Subject: [PATCH] fix(wktReadBlock): improve support of EMPTY for geometry collection --- src/core/geometry/qgsgeometryutils.cpp | 8 ++- .../geometry/testqgsgeometrycollection.cpp | 55 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/core/geometry/qgsgeometryutils.cpp b/src/core/geometry/qgsgeometryutils.cpp index ee85d22a135..4c3f6d67055 100644 --- a/src/core/geometry/qgsgeometryutils.cpp +++ b/src/core/geometry/qgsgeometryutils.cpp @@ -1019,6 +1019,7 @@ QPair QgsGeometryUtils::wktReadBlock( const QString &wkt { QString wktParsed = wkt; QString contents; + bool isEmpty = false; const QLatin1String empty { "EMPTY" }; if ( wkt.contains( empty, Qt::CaseInsensitive ) ) { @@ -1032,9 +1033,14 @@ QPair QgsGeometryUtils::wktReadBlock( const QString &wkt // Extract the part of the QString to the left of "EMPTY" wktParsed = wktParsed.left( index ); contents = empty; + isEmpty = true; + } + else + { + wktParsed = wkt; // reset to original content } } - else + if ( !isEmpty ) { const int openedParenthesisCount = wktParsed.count( '(' ); const int closedParenthesisCount = wktParsed.count( ')' ); diff --git a/tests/src/core/geometry/testqgsgeometrycollection.cpp b/tests/src/core/geometry/testqgsgeometrycollection.cpp index db0e2f51c2f..158ebdd985a 100644 --- a/tests/src/core/geometry/testqgsgeometrycollection.cpp +++ b/tests/src/core/geometry/testqgsgeometrycollection.cpp @@ -294,6 +294,7 @@ void TestQgsGeometryCollection::geometryCollection() QVERIFY( !c17.fromWkb( wkbPointPtr ) ); QCOMPARE( c17.wkbType(), Qgis::WkbType::GeometryCollection ); + //as JSON QgsGeometryCollection exportC; part.setPoints( QgsPointSequence() << QgsPoint( Qgis::WkbType::Point, 0, 0 ) << QgsPoint( Qgis::WkbType::Point, 0, 10 ) << QgsPoint( Qgis::WkbType::Point, 10, 10 ) << QgsPoint( Qgis::WkbType::Point, 10, 0 ) << QgsPoint( Qgis::WkbType::Point, 0, 0 ) ); @@ -1475,6 +1476,60 @@ void TestQgsGeometryCollection::testfromToWkt() "PolyhedralSurface Z (((0 0 1, 0 10 1, 10 10 1, 10 0 1, 0 0 1)))," "TIN Z (((0.1 0.1 10.2, 1.13 0.2 10.3, 0.4 2.3 10.4, 0.1 0.1 10.2)))" ")" ); + + QgsGeometryCollection c22; + QVERIFY( c22.fromWkt( wkt21 ) ); + QCOMPARE( c21.numGeometries(), c22.numGeometries() ); + for ( int i = 0; i < c21.numGeometries(); i++ ) + { + QCOMPARE( *c21.geometryN( 0 ), *c22.geometryN( 0 ) ); + } + + // mixed type all EMPTY + QString emptyWkt = QString( "GeometryCollection (" + "Point EMPTY," + "LineString EMPTY," + "Polygon EMPTY," + "CircularString EMPTY," + "CompoundCurve EMPTY," + "CurvePolygon EMPTY," + "MultiPoint EMPTY," + "MultiLineString EMPTY," + "MultiPolygon EMPTY," + "GeometryCollection EMPTY," + "MultiCurve EMPTY," + "MultiSurface EMPTY," + "Triangle EMPTY," + "PolyhedralSurface EMPTY," + "TIN EMPTY" + ")" ); + + QgsGeometryCollection c23; + QVERIFY( c23.fromWkt( emptyWkt ) ); + QCOMPARE( c23.numGeometries(), 15 ); + + // mixed type partial EMPTY + QString partialWkt = QString( "GeometryCollection (" + "Point Z (0 0 1)," + "LineString EMPTY," + "Polygon EMPTY," + "CircularString EMPTY," + "CompoundCurve EMPTY," + "CurvePolygon EMPTY," + "MultiPoint EMPTY," + "MultiLineString EMPTY," + "MultiPolygon EMPTY," + "GeometryCollection EMPTY," + "MultiCurve EMPTY," + "MultiSurface EMPTY," + "Triangle EMPTY," + "PolyhedralSurface EMPTY," + "TIN EMPTY" + ")" ); + + QgsGeometryCollection c24; + QVERIFY( c24.fromWkt( partialWkt ) ); + QCOMPARE( c24.numGeometries(), 15 ); } void TestQgsGeometryCollection::testCopyConstructor()