Skip to content

Commit

Permalink
Correctly handle inverse transforms with reversed axis CRS in proj 6.…
Browse files Browse the repository at this point in the history
…0 builds

This logic is too complex and fragile -- we should drop support for proj 6.0
as soon as we can, so that we can the inbuilt handling which
is enabled for proj >= 6.1 only.
  • Loading branch information
nyalldawson committed Apr 5, 2019
1 parent 2e11c34 commit af22863
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/core/qgscoordinatetransform.cpp
Expand Up @@ -648,16 +648,16 @@ void QgsCoordinateTransform::transformCoords( int numPoints, double *x, double *

int projResult = 0;
#if PROJ_VERSION_MAJOR>=6
const bool sourceAxisOrderSwapped = d->mSourceAxisOrderSwapped;
const bool destinationAxisOrderSwapped = d->mDestAxisOrderSwapped;
const bool sourceAxisOrderSwapped = direction == ForwardTransform ? d->mSourceAxisOrderSwapped : d->mDestAxisOrderSwapped;

proj_trans_generic( projData, direction == ForwardTransform ? PJ_FWD : PJ_INV,
!sourceAxisOrderSwapped ? x : y, sizeof( double ), numPoints,
!sourceAxisOrderSwapped ? y : x, sizeof( double ), numPoints,
z, sizeof( double ), numPoints,
nullptr, sizeof( double ), 0 );
projResult = proj_errno( projData );
if ( projResult == 0 && destinationAxisOrderSwapped )
// ewww - this logic is gross. We should drop support for PROJ 6.0 as quickly as possible and dump this code (in favour of built in methods used for >=6.1 builds)
if ( projResult == 0 && ( d->mSourceAxisOrderSwapped != d->mDestAxisOrderSwapped ) )
{
size_t size = sizeof( double ) * numPoints;
void *tmp = malloc( size );
Expand Down
16 changes: 13 additions & 3 deletions tests/src/core/testqgscoordinatetransform.cpp
Expand Up @@ -260,18 +260,27 @@ void TestQgsCoordinateTransform::transform_data()
QTest::addColumn<QgsCoordinateReferenceSystem>( "destCrs" );
QTest::addColumn<double>( "x" );
QTest::addColumn<double>( "y" );
QTest::addColumn<int>( "direction" );
QTest::addColumn<double>( "outX" );
QTest::addColumn<double>( "outY" );
QTest::addColumn<double>( "precision" );

QTest::newRow( "To geographic" )
<< QgsCoordinateReferenceSystem::fromEpsgId( 3111 )
<< QgsCoordinateReferenceSystem::fromEpsgId( 4326 )
<< 2545059.0 << 2393190.0 << 145.512750 << -37.961375 << 0.000001;
<< 2545059.0 << 2393190.0 << static_cast< int >( QgsCoordinateTransform::ForwardTransform ) << 145.512750 << -37.961375 << 0.000001;
QTest::newRow( "From geographic" )
<< QgsCoordinateReferenceSystem::fromEpsgId( 4326 )
<< QgsCoordinateReferenceSystem::fromEpsgId( 3111 )
<< 145.512750 << -37.961375 << 2545059.0 << 2393190.0 << 0.1;
<< 145.512750 << -37.961375 << static_cast< int >( QgsCoordinateTransform::ForwardTransform ) << 2545059.0 << 2393190.0 << 0.1;
QTest::newRow( "To geographic (reverse)" )
<< QgsCoordinateReferenceSystem::fromEpsgId( 3111 )
<< QgsCoordinateReferenceSystem::fromEpsgId( 4326 )
<< 145.512750 << -37.961375 << static_cast< int >( QgsCoordinateTransform::ReverseTransform ) << 2545059.0 << 2393190.0 << 0.1;
QTest::newRow( "From geographic (reverse)" )
<< QgsCoordinateReferenceSystem::fromEpsgId( 4326 )
<< QgsCoordinateReferenceSystem::fromEpsgId( 3111 )
<< 2545058.9675128171 << 2393190.0509782173 << static_cast< int >( QgsCoordinateTransform::ReverseTransform ) << 145.512750 << -37.961375 << 0.000001;
}

void TestQgsCoordinateTransform::transform()
Expand All @@ -280,14 +289,15 @@ void TestQgsCoordinateTransform::transform()
QFETCH( QgsCoordinateReferenceSystem, destCrs );
QFETCH( double, x );
QFETCH( double, y );
QFETCH( int, direction );
QFETCH( double, outX );
QFETCH( double, outY );
QFETCH( double, precision );

double z = 0;
QgsCoordinateTransform ct( sourceCrs, destCrs, QgsProject::instance() );

ct.transformInPlace( x, y, z );
ct.transformInPlace( x, y, z, static_cast< QgsCoordinateTransform::TransformDirection >( direction ) );
QGSCOMPARENEAR( x, outX, precision );
QGSCOMPARENEAR( y, outY, precision );
}
Expand Down

0 comments on commit af22863

Please sign in to comment.