Skip to content

Commit

Permalink
use default args for Z/M values in QgsGeometry::coerceToType
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Jan 27, 2022
1 parent c716e02 commit da7d78b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion python/core/auto_generated/geometry/qgsgeometry.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand All @@ -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.

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
Expand Down
6 changes: 3 additions & 3 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ json QgsGeometry::asJsonObject( int precision ) const

}

QVector<QgsGeometry> QgsGeometry::coerceToType( const QgsWkbTypes::Type type ) const
QVector<QgsGeometry> QgsGeometry::coerceToType( const QgsWkbTypes::Type type, double defaultZ, double defaultM ) const
{
QVector< QgsGeometry > res;
if ( isNull() )
Expand Down Expand Up @@ -1537,11 +1537,11 @@ QVector<QgsGeometry> 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
Expand Down
6 changes: 5 additions & 1 deletion src/core/geometry/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1956,14 +1956,18 @@ class CORE_EXPORT QgsGeometry
* - single geometries will be upgraded to multi geometries
* - z or m values will be added or dropped as required.
*
* 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
* wherever appropriate.
*
* \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
Expand Down

0 comments on commit da7d78b

Please sign in to comment.