Skip to content
Permalink
Browse files
Add method to convert QgsCoordinateReferenceSystem to geodetic crs
  • Loading branch information
nyalldawson committed Jan 11, 2022
1 parent e504be4 commit 1f240c0196bd2b719ccaa5ec9ab2169f79ec4ec2
@@ -995,6 +995,17 @@ definition. FormatWkt is recommended as it is a lossless format.
Since QGIS 3.18, internally this calls :py:func:`QgsCoordinateReferenceSystemRegistry.addUserCrs()`.
%End

QgsCoordinateReferenceSystem toGeodeticCrs() const;
%Docstring
Returns the geodetic CRS associated with this CRS object.

May return an invalid CRS if the geodetic CRS could not be determined.

This method will always return a longitude, latitude ordered CRS.

.. versionadded:: 3.24
%End

QString geographicCrsAuthId() const;
%Docstring
Returns auth id of related geographic CRS
@@ -2608,6 +2608,35 @@ const QHash<long, QgsCoordinateReferenceSystem> &QgsCoordinateReferenceSystem::s
return *sSrsIdCache();
}

QgsCoordinateReferenceSystem QgsCoordinateReferenceSystem::toGeodeticCrs() const
{
if ( isGeographic() )
{
return *this;
}

if ( PJ *obj = d->threadLocalProjObject() )
{
PJ_CONTEXT *pjContext = QgsProjContext::get();
QgsProjUtils::proj_pj_unique_ptr geoCrs( proj_crs_get_geodetic_crs( pjContext, obj ) );
if ( !geoCrs )
return QgsCoordinateReferenceSystem();

if ( !testIsGeographic( geoCrs.get() ) )
return QgsCoordinateReferenceSystem();

QgsProjUtils::proj_pj_unique_ptr normalized( proj_normalize_for_visualization( pjContext, geoCrs.get() ) );
if ( !normalized )
return QgsCoordinateReferenceSystem();

return QgsCoordinateReferenceSystem::fromProjObject( normalized.get() );
}
else
{
return QgsCoordinateReferenceSystem();
}
}

QString QgsCoordinateReferenceSystem::geographicCrsAuthId() const
{
if ( isGeographic() )
@@ -907,6 +907,17 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
*/
long saveAsUserCrs( const QString &name, Qgis::CrsDefinitionFormat nativeFormat = Qgis::CrsDefinitionFormat::Wkt );

/**
* Returns the geodetic CRS associated with this CRS object.
*
* May return an invalid CRS if the geodetic CRS could not be determined.
*
* This method will always return a longitude, latitude ordered CRS.
*
* \since QGIS 3.24
*/
QgsCoordinateReferenceSystem toGeodeticCrs() const;

//! Returns auth id of related geographic CRS
QString geographicCrsAuthId() const;

@@ -1693,9 +1693,13 @@ void TestQgsCoordinateReferenceSystem::geographicCrsAuthId()
crs.createFromString( QStringLiteral( "EPSG:4326" ) );
QCOMPARE( crs.authid(), QStringLiteral( "EPSG:4326" ) );
QCOMPARE( crs.geographicCrsAuthId(), QStringLiteral( "EPSG:4326" ) );
QCOMPARE( crs.toGeodeticCrs(), crs );

crs.createFromString( QStringLiteral( "EPSG:3825" ) );
QCOMPARE( crs.authid(), QStringLiteral( "EPSG:3825" ) );
QCOMPARE( crs.geographicCrsAuthId(), QStringLiteral( "EPSG:3824" ) );
QCOMPARE( crs.toGeodeticCrs().toProj().replace( QStringLiteral( "+towgs84=0,0,0,0,0,0,0 " ), QString() ).replace( QStringLiteral( " +type=crs" ), QString() ),
QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3824" ) ).toProj().replace( QStringLiteral( "+towgs84=0,0,0,0,0,0,0 " ), QString() ).replace( QStringLiteral( " +type=crs" ), QString() ) );
}

void TestQgsCoordinateReferenceSystem::noProj()

0 comments on commit 1f240c0

Please sign in to comment.