Skip to content

Commit

Permalink
Add method to convert QgsCoordinateReferenceSystem to geodetic crs
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 11, 2022
1 parent e504be4 commit 1f240c0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/core/proj/qgscoordinatereferencesystem.cpp
Expand Up @@ -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() )
Expand Down
11 changes: 11 additions & 0 deletions src/core/proj/qgscoordinatereferencesystem.h
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 1f240c0

Please sign in to comment.