Skip to content

Commit

Permalink
QgsCoordinateTransform::transformCoords(): with PROJ 6, do no raise o…
Browse files Browse the repository at this point in the history
…n partial failure of multiple point transformation

Fixes #32973 or at least improve it significantly

Currently with PROJ 6 transformCoords() will raise an exception as soon
as a single point fails to transform. With PROJ < 6, this (generally)
did not occur. Exceptions were raised only (mostly) on failures on
single point.
Imitate that
  • Loading branch information
rouault authored and nyalldawson committed Nov 23, 2019
1 parent a485b47 commit ed7d13d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/core/qgscoordinatetransform.cpp
Expand Up @@ -662,7 +662,18 @@ void QgsCoordinateTransform::transformCoords( int numPoints, double *x, double *
y, sizeof( double ), numPoints,
z, sizeof( double ), numPoints,
nullptr, sizeof( double ), 0 );
projResult = proj_errno( projData );
// Try to - approximatively - emulate the behavior of pj_transform()...
// In the case of a single point transform, and a transformation error occurs,
// pj_transform() would return the errno. In cases of multiple point transform,
// it would continue (for non-transient errors, that is pipeline definition
// errors) and just set the resulting x,y to infinity. This is in fact a
// bit more subtle than that, and I'm not completely sure the logic in
// pj_transform() was really sane & fully bullet proof
// So here just check proj_errno() for single point transform
if ( numPoints == 1 )
{
projResult = proj_errno( projData );
}
#else
bool sourceIsLatLong = false;
bool destIsLatLong = false;
Expand Down
39 changes: 39 additions & 0 deletions tests/src/core/testqgscoordinatetransform.cpp
Expand Up @@ -42,6 +42,8 @@ class TestQgsCoordinateTransform: public QObject
void transform();
void transformLKS();
void transformContextNormalize();
void transformErrorMultiplePoints();
void transformErrorOnePoint();
};


Expand Down Expand Up @@ -493,5 +495,42 @@ void TestQgsCoordinateTransform::transformContextNormalize()
#endif
}


void TestQgsCoordinateTransform::transformErrorMultiplePoints()
{
// Check that we don't throw an exception when transforming multiple
// points and at least one fails.
QgsCoordinateTransformContext context;
QgsCoordinateTransform ct( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QgsCoordinateReferenceSystem::fromEpsgId( 3857 ), context );
QVERIFY( ct.isValid() );
double x[] = { 0, -1000 };
double y[] = { 0, 0 };
double z[] = { 0, 0 };
ct.transformCoords( 2, x, y, z );
QGSCOMPARENEAR( x[0], 0, 0.01 );
QGSCOMPARENEAR( y[0], 0, 0.01 );
QVERIFY( !std::isfinite( x[1] ) );
QVERIFY( !std::isfinite( y[1] ) );
}


void TestQgsCoordinateTransform::transformErrorOnePoint()
{
QgsCoordinateTransformContext context;
QgsCoordinateTransform ct( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ), QgsCoordinateReferenceSystem::fromEpsgId( 3857 ), context );
QVERIFY( ct.isValid() );
double x[] = { -1000 };
double y[] = { 0 };
double z[] = { 0 };
try
{
ct.transformCoords( 1, x, y, z );
QVERIFY( false );
}
catch ( QgsCsException & )
{
}
}

QGSTEST_MAIN( TestQgsCoordinateTransform )
#include "testqgscoordinatetransform.moc"

0 comments on commit ed7d13d

Please sign in to comment.