Skip to content

Commit

Permalink
[proj6] Always normalize operations, even when we are using predefined
Browse files Browse the repository at this point in the history
proj coordinate operation (e.g. those stored in a project)

Fixes #30643
  • Loading branch information
nyalldawson committed Jul 26, 2019
1 parent ca13055 commit f2d3bdd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/core/qgscoordinatetransform_p.cpp
Expand Up @@ -351,6 +351,17 @@ ProjData QgsCoordinateTransformPrivate::threadLocalProjData()

transform.reset();
}
else
{
// transform may have either the source or destination CRS using swapped axis order. For QGIS, we ALWAYS need regular x/y axis order
transform.reset( proj_normalize_for_visualization( context, transform.get() ) );
if ( !transform )
{
const QString err = QObject::tr( "Cannot normalize transform between %1 and %2" ).arg( mSourceCRS.authid(),
mDestCRS.authid() );
QgsMessageLog::logMessage( err, QString(), Qgis::Critical );
}
}
}

QString nonAvailableError;
Expand Down
38 changes: 38 additions & 0 deletions tests/src/core/testqgscoordinatetransform.cpp
Expand Up @@ -41,6 +41,7 @@ class TestQgsCoordinateTransform: public QObject
void transform_data();
void transform();
void transformLKS();
void transformContextNormalize();
};


Expand Down Expand Up @@ -346,6 +347,10 @@ void TestQgsCoordinateTransform::transform_data()
<< QgsCoordinateReferenceSystem::fromEpsgId( 27700 )
<< QgsCoordinateReferenceSystem::fromEpsgId( 4326 )
<< 7467023.96 << -5527971.74 << static_cast< int >( QgsCoordinateTransform::ForwardTransform ) << 51.400222 << 0.000025 << 0.4;
QTest::newRow( "From BNG to WGS84 2" )
<< QgsCoordinateReferenceSystem::fromEpsgId( 27700 )
<< QgsCoordinateReferenceSystem::fromEpsgId( 4326 )
<< 246909.0 << 54108.0 << static_cast< int >( QgsCoordinateTransform::ForwardTransform ) << -4.153951 << 50.366908 << 0.4;
QTest::newRow( "From BNG to WGS84 (reverse)" )
<< QgsCoordinateReferenceSystem::fromEpsgId( 27700 )
<< QgsCoordinateReferenceSystem::fromEpsgId( 4326 )
Expand Down Expand Up @@ -455,5 +460,38 @@ void TestQgsCoordinateTransform::transformLKS()
QGSCOMPARENEAR( sPoly.at( 2 ).y(), 6333650.333, 0.001 );
}

void TestQgsCoordinateTransform::transformContextNormalize()
{
#if PROJ_VERSION_MAJOR >= 6
// coordinate operation for WGS84 to 27700
const QString coordOperation = QStringLiteral( "+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=push +v_3 +step +proj=cart +ellps=WGS84 +step +inv +proj=helmert +x=446.448 +y=-125.157 +z=542.06 +rx=0.15 +ry=0.247 +rz=0.842 +s=-20.489 +convention=position_vector +step +inv +proj=cart +ellps=airy +step +proj=pop +v_3 +step +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy" );
QgsCoordinateTransformContext context;
context.addCoordinateOperation( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QgsCoordinateReferenceSystem::fromEpsgId( 27700 ), coordOperation );

QgsCoordinateTransform ct( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QgsCoordinateReferenceSystem::fromEpsgId( 27700 ), context );
QVERIFY( ct.isValid() );
QgsPointXY p( -4.17477, 50.3657 );
QgsPointXY p2 = ct.transform( p );
QGSCOMPARENEAR( p2.x(), 245424.604645, 0.01 );
QGSCOMPARENEAR( p2.y(), 54016.813093, 0.01 );

p = ct.transform( p2, QgsCoordinateTransform::ReverseTransform );
QGSCOMPARENEAR( p.x(), -4.17477, 0.01 );
QGSCOMPARENEAR( p.y(), 50.3657, 0.01 );

QgsCoordinateTransform ct2( QgsCoordinateReferenceSystem::fromEpsgId( 27700 ), QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), context );
QVERIFY( ct2.isValid() );
p = QgsPointXY( 245424.604645, 54016.813093 );
p2 = ct2.transform( p );
QGSCOMPARENEAR( p2.x(), -4.17477, 0.01 );
QGSCOMPARENEAR( p2.y(), 50.3657, 0.01 );

p = ct2.transform( p2, QgsCoordinateTransform::ReverseTransform );
QGSCOMPARENEAR( p.x(), 245424.604645, 0.01 );
QGSCOMPARENEAR( p.y(), 54016.813093, 0.01 );

#endif
}

QGSTEST_MAIN( TestQgsCoordinateTransform )
#include "testqgscoordinatetransform.moc"

0 comments on commit f2d3bdd

Please sign in to comment.