From 2783ec8724bec51b8a63239bf598ffa9c6f25fd8 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Tue, 4 Jan 2022 16:12:25 +0100 Subject: [PATCH] use default args for Z/M values in QgsGeometry::coerceToType --- python/core/auto_generated/geometry/qgsgeometry.sip.in | 6 +++++- src/core/geometry/qgsgeometry.cpp | 6 +++--- src/core/geometry/qgsgeometry.h | 6 +++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/python/core/auto_generated/geometry/qgsgeometry.sip.in b/python/core/auto_generated/geometry/qgsgeometry.sip.in index f0728a64d66b..a063ede0f009 100644 --- a/python/core/auto_generated/geometry/qgsgeometry.sip.in +++ b/python/core/auto_generated/geometry/qgsgeometry.sip.in @@ -1934,7 +1934,7 @@ Exports the geometry to a GeoJSON string. %End - QVector< QgsGeometry > coerceToType( QgsWkbTypes::Type type ) const; + QVector< QgsGeometry > coerceToType( QgsWkbTypes::Type type, double defaultZ = 0, double defaultM = 0 ) const; %Docstring Attempts to coerce this geometry into the specified destination ``type``. @@ -1950,6 +1950,10 @@ This method will do anything possible to force the current geometry into the spe - single geometries will be upgraded to multi geometries - z or m values will be added or dropped as required. +Since QGIS 3.24, the parameters ``defaultZ`` and ``defaultM`` control the dimension value added when promoting geometries +to Z, M or ZM versions. +By default 0.0 is used for Z and M. + .. note:: This method is much stricter than :py:func:`~QgsGeometry.convertToType`, as it considers the exact WKB type diff --git a/src/core/geometry/qgsgeometry.cpp b/src/core/geometry/qgsgeometry.cpp index 07f7df3f0dfa..282eb53bc94c 100644 --- a/src/core/geometry/qgsgeometry.cpp +++ b/src/core/geometry/qgsgeometry.cpp @@ -1439,7 +1439,7 @@ json QgsGeometry::asJsonObject( int precision ) const } -QVector QgsGeometry::coerceToType( const QgsWkbTypes::Type type ) const +QVector QgsGeometry::coerceToType( const QgsWkbTypes::Type type, double defaultZ, double defaultM ) const { QVector< QgsGeometry > res; if ( isNull() ) @@ -1537,11 +1537,11 @@ QVector QgsGeometry::coerceToType( const QgsWkbTypes::Type type ) c // Add Z/M back, set to 0 if ( ! newGeom.constGet()->is3D() && QgsWkbTypes::hasZ( type ) ) { - newGeom.get()->addZValue( 0.0 ); + newGeom.get()->addZValue( defaultZ ); } if ( ! newGeom.constGet()->isMeasure() && QgsWkbTypes::hasM( type ) ) { - newGeom.get()->addMValue( 0.0 ); + newGeom.get()->addMValue( defaultM ); } // Multi -> single diff --git a/src/core/geometry/qgsgeometry.h b/src/core/geometry/qgsgeometry.h index b563c94e8dce..86f1eec4e41d 100644 --- a/src/core/geometry/qgsgeometry.h +++ b/src/core/geometry/qgsgeometry.h @@ -1956,6 +1956,10 @@ class CORE_EXPORT QgsGeometry * - single geometries will be upgraded to multi geometries * - z or m values will be added or dropped as required. * + * Since QGIS 3.24, the parameters \a defaultZ and \a defaultM control the dimension value added when promoting geometries + * to Z, M or ZM versions. + * By default 0.0 is used for Z and M. + * * \note This method is much stricter than convertToType(), as it considers the exact WKB type * of geometries instead of the geometry family (point/line/polygon), and tries more exhaustively * to coerce geometries to the desired \a type. It also correctly maintains curves and z/m values @@ -1963,7 +1967,7 @@ class CORE_EXPORT QgsGeometry * * \since QGIS 3.14 */ - QVector< QgsGeometry > coerceToType( QgsWkbTypes::Type type ) const; + QVector< QgsGeometry > coerceToType( QgsWkbTypes::Type type, double defaultZ = 0, double defaultM = 0 ) const; /** * Try to convert the geometry to the requested type