Add method to determine whether a QgsCoordinateTransform has a vertical

component
This commit is contained in:
Nyall Dawson 2024-07-12 13:30:42 +10:00
parent 9dd1ba2caa
commit 45090576b2
7 changed files with 82 additions and 0 deletions

View File

@ -349,6 +349,14 @@ otherwise points are transformed from destination to source CRS.
bool isShortCircuited() const;
%Docstring
Returns ``True`` if the transform short circuits because the source and destination are equivalent.
%End
bool hasVerticalComponent() const;
%Docstring
Returns ``True`` if the transform includes a vertical component, i.e. if both the :py:func:`~QgsCoordinateTransform.sourceCrs`
and :py:func:`~QgsCoordinateTransform.destinationCrs` have a vertical axis.
.. versionadded:: 3.40
%End
QString coordinateOperation() const;

View File

@ -349,6 +349,14 @@ otherwise points are transformed from destination to source CRS.
bool isShortCircuited() const;
%Docstring
Returns ``True`` if the transform short circuits because the source and destination are equivalent.
%End
bool hasVerticalComponent() const;
%Docstring
Returns ``True`` if the transform includes a vertical component, i.e. if both the :py:func:`~QgsCoordinateTransform.sourceCrs`
and :py:func:`~QgsCoordinateTransform.destinationCrs` have a vertical axis.
.. versionadded:: 3.40
%End
QString coordinateOperation() const;

View File

@ -935,6 +935,11 @@ bool QgsCoordinateTransform::isShortCircuited() const
return !d->mIsValid || d->mShortCircuit;
}
bool QgsCoordinateTransform::hasVerticalComponent() const
{
return d->mIsValid && d->mHasVerticalComponent;
}
QString QgsCoordinateTransform::coordinateOperation() const
{
return d->mProjCoordinateOperation;

View File

@ -381,6 +381,14 @@ class CORE_EXPORT QgsCoordinateTransform
*/
bool isShortCircuited() const;
/**
* Returns TRUE if the transform includes a vertical component, i.e. if both the sourceCrs()
* and destinationCrs() have a vertical axis.
*
* \since QGIS 3.40
*/
bool hasVerticalComponent() const;
/**
* Returns a Proj string representing the coordinate operation which will be used to transform
* coordinates.

View File

@ -84,6 +84,7 @@ QgsCoordinateTransformPrivate::QgsCoordinateTransformPrivate( const QgsCoordinat
, mIsValid( other.mIsValid )
, mShortCircuit( other.mShortCircuit )
, mGeographicToWebMercator( other.mGeographicToWebMercator )
, mHasVerticalComponent( other.mHasVerticalComponent )
, mSourceCRS( other.mSourceCRS )
, mDestCRS( other.mDestCRS )
, mSourceDatumTransform( other.mSourceDatumTransform )
@ -163,6 +164,8 @@ bool QgsCoordinateTransformPrivate::initialize()
mSourceCRS.isGeographic() &&
mDestCRS.authid() == QLatin1String( "EPSG:3857" );
mHasVerticalComponent = mSourceCRS.hasVerticalAxis() && mDestCRS.hasVerticalAxis();
mSourceIsDynamic = mSourceCRS.isDynamic();
mSourceCoordinateEpoch = mSourceCRS.coordinateEpoch();
mDestIsDynamic = mDestCRS.isDynamic();

View File

@ -91,6 +91,9 @@ class QgsCoordinateTransformPrivate : public QSharedData
//! Flag to indicate EPSG:4326 to EPSG:3857 reprojection
bool mGeographicToWebMercator = false;
//! Flag to indicate whether the transform has a vertical component
bool mHasVerticalComponent = false;
//! QgsCoordinateReferenceSystem of the source (layer) coordinate system
QgsCoordinateReferenceSystem mSourceCRS;

View File

@ -161,6 +161,53 @@ class TestQgsCoordinateTransform(QgisTestCase):
self.assertAlmostEqual(transformedExtent.xMaximum(), 20037508.343, delta=1e-3)
self.assertAlmostEqual(transformedExtent.yMaximum(), 44927335.427, delta=1e-3)
def test_has_vertical_component(self):
transform = QgsCoordinateTransform()
self.assertFalse(transform.hasVerticalComponent())
# 2d to 2d
transform = QgsCoordinateTransform(
QgsCoordinateReferenceSystem('EPSG:4326'),
QgsCoordinateReferenceSystem('EPSG:3857'),
QgsCoordinateTransformContext()
)
self.assertFalse(transform.hasVerticalComponent())
# 2d to 3d
transform = QgsCoordinateTransform(
QgsCoordinateReferenceSystem('EPSG:4326'),
QgsCoordinateReferenceSystem('EPSG:7843'),
QgsCoordinateTransformContext()
)
self.assertFalse(transform.hasVerticalComponent())
# 3d to 2d
transform = QgsCoordinateTransform(
QgsCoordinateReferenceSystem('EPSG:7843'),
QgsCoordinateReferenceSystem('EPSG:4326'),
QgsCoordinateTransformContext()
)
self.assertFalse(transform.hasVerticalComponent())
# 3d to 3d
transform = QgsCoordinateTransform(
QgsCoordinateReferenceSystem('EPSG:7843'),
QgsCoordinateReferenceSystem.createCompoundCrs(
QgsCoordinateReferenceSystem('EPSG:7844'),
QgsCoordinateReferenceSystem('EPSG:9458'))[0],
QgsCoordinateTransformContext()
)
self.assertTrue(transform.hasVerticalComponent())
transform = QgsCoordinateTransform(
QgsCoordinateReferenceSystem.createCompoundCrs(
QgsCoordinateReferenceSystem('EPSG:7844'),
QgsCoordinateReferenceSystem('EPSG:5711'))[0],
QgsCoordinateReferenceSystem('EPSG:7843'),
QgsCoordinateTransformContext()
)
self.assertTrue(transform.hasVerticalComponent())
if __name__ == '__main__':
unittest.main()