Skip to content
Permalink
Browse files

Fix transform differences vs proj < 6 when using custom CRS via proj …

…definition

By being more selective about when we force convert to a single crs

Fixes #32928

(cherry picked from commit 05eaa65)
  • Loading branch information
nyalldawson committed Dec 13, 2019
1 parent 183bcc4 commit 10850d7ac8323741897b3bc938010c3d972a8d28
@@ -1307,8 +1307,7 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString &proj4String )
PJ_CONTEXT *ctx = QgsProjContext::get();

{
QgsProjUtils::proj_pj_unique_ptr crs( proj_create( ctx, trimmed.toLatin1().constData() ) );
d->mPj = QgsProjUtils::crsToSingleCrs( crs.get() );
d->mPj.reset( proj_create( ctx, trimmed.toLatin1().constData() ) );
}

if ( !d->mPj )
@@ -1370,8 +1369,7 @@ bool QgsCoordinateReferenceSystem::setWktString( const QString &wkt, bool allowP
PROJ_STRING_LIST warnings = nullptr;
PROJ_STRING_LIST grammerErrors = nullptr;
{
QgsProjUtils::proj_pj_unique_ptr crs( proj_create_from_wkt( QgsProjContext::get(), wkt.toLatin1().constData(), nullptr, &warnings, &grammerErrors ) );
d->mPj = QgsProjUtils::crsToSingleCrs( crs.get() );
d->mPj.reset( proj_create_from_wkt( QgsProjContext::get(), wkt.toLatin1().constData(), nullptr, &warnings, &grammerErrors ) );
}

res = static_cast< bool >( d->mPj );
@@ -89,6 +89,7 @@ class TestQgsCoordinateReferenceSystem: public QObject
void geoCcsDescription();
void geographicCrsAuthId();
void noProj();
void customProjString();

private:
void debugPrint( QgsCoordinateReferenceSystem &crs );
@@ -1275,7 +1276,7 @@ void TestQgsCoordinateReferenceSystem::noProj()
{
#if PROJ_VERSION_MAJOR>=6
// test a crs which cannot be represented by a proj string
QgsCoordinateReferenceSystem crs( "EPSG:2218" );
QgsCoordinateReferenceSystem crs( QStringLiteral( "EPSG:2218" ) );
QCOMPARE( crs.authid(), QStringLiteral( "EPSG:2218" ) );
QVERIFY( crs.isValid() );
QCOMPARE( crs.toWkt(), QStringLiteral( "PROJCS[\"Scoresbysund 1952 / Greenland zone 5 east\",GEOGCS[\"Scoresbysund 1952\",DATUM[\"Scoresbysund_1952\",SPHEROID[\"International 1924\",6378388,297,AUTHORITY[\"EPSG\",\"7022\"]],AUTHORITY[\"EPSG\",\"6195\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4195\"]],PROJECTION[\"Lambert_Conic_Conformal_(West_Orientated)\"],PARAMETER[\"Latitude of natural origin\",70.5],PARAMETER[\"Longitude of natural origin\",-24],PARAMETER[\"Scale factor at natural origin\",1],PARAMETER[\"False easting\",0],PARAMETER[\"False northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AUTHORITY[\"EPSG\",\"2218\"]]" ) );
@@ -1290,5 +1291,16 @@ void TestQgsCoordinateReferenceSystem::noProj()
#endif

}

void TestQgsCoordinateReferenceSystem::customProjString()
{
#if PROJ_VERSION_MAJOR>=6
QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromProj4( QStringLiteral( "+proj=sterea +lat_0=47.4860018439082 +lon_0=19.0491441390302 +k=1 +x_0=500000 +y_0=500000 +ellps=bessel +towgs84=595.75,121.09,515.50,8.2270,-1.5193,5.5971,-2.6729 +units=m +vunits=m +no_defs" ) );
QVERIFY( crs.isValid() );
QCOMPARE( crs.toProj4(), QStringLiteral( "+proj=sterea +lat_0=47.4860018439082 +lon_0=19.0491441390302 +k=1 +x_0=500000 +y_0=500000 +ellps=bessel +towgs84=595.75,121.09,515.50,8.2270,-1.5193,5.5971,-2.6729 +units=m +vunits=m +no_defs" ) );
QCOMPARE( crs.toWkt(), QStringLiteral( R"""(COMPD_CS["unknown",PROJCS["unknown",GEOGCS["unknown",DATUM["Unknown_based_on_Bessel_1841_ellipsoid",SPHEROID["Bessel 1841",6377397.155,299.1528128],TOWGS84[595.75,121.09,515.5,8.227,-1.5193,5.5971,-2.6729]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Oblique_Stereographic"],PARAMETER["latitude_of_origin",47.4860018439082],PARAMETER["central_meridian",19.0491441390302],PARAMETER["scale_factor",1],PARAMETER["false_easting",500000],PARAMETER["false_northing",500000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]],VERT_CS["unknown",VERT_DATUM["unknown",2005],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Gravity-related height",UP]]])""" ) );
#endif
}

QGSTEST_MAIN( TestQgsCoordinateReferenceSystem )
#include "testqgscoordinatereferencesystem.moc"
@@ -46,6 +46,7 @@ class TestQgsCoordinateTransform: public QObject
void transformErrorMultiplePoints();
void transformErrorOnePoint();
void testDeprecated4240to4326();
void testCustomProjTransform();
};


@@ -607,5 +608,25 @@ void TestQgsCoordinateTransform::testDeprecated4240to4326()
#endif
}

void TestQgsCoordinateTransform::testCustomProjTransform()
{
#if PROJ_VERSION_MAJOR >= 6
// test custom proj string
// refs https://github.com/qgis/QGIS/issues/32928
QgsCoordinateReferenceSystem ss( QgsCoordinateReferenceSystem::fromProj4( QStringLiteral( "+proj=sterea +lat_0=47.4860018439082 +lon_0=19.0491441390302 +k=1 +x_0=500000 +y_0=500000 +ellps=bessel +towgs84=595.75,121.09,515.50,8.2270,-1.5193,5.5971,-2.6729 +units=m +vunits=m +no_defs" ) ) );
QgsCoordinateReferenceSystem dd( QStringLiteral( "EPSG:23700" ) );
QgsCoordinateTransform ct( ss, dd, QgsCoordinateTransformContext() );
QVERIFY( ct.isValid() );
QgsDebugMsg( ct.instantiatedCoordinateOperationDetails().proj );
QCOMPARE( ct.instantiatedCoordinateOperationDetails().proj, QStringLiteral( "+proj=pipeline +step +inv +proj=sterea +lat_0=47.4860018439082 +lon_0=19.0491441390302 +k=1 +x_0=500000 +y_0=500000 +ellps=bessel +step +proj=push +v_3 +step +proj=cart +ellps=bessel +step +proj=helmert +x=595.75 +y=121.09 +z=515.5 +rx=8.227 +ry=-1.5193 +rz=5.5971 +s=-2.6729 +convention=position_vector +step +inv +proj=helmert +x=52.684 +y=-71.194 +z=-13.975 +rx=0.312 +ry=0.1063 +rz=0.3729 +s=1.0191 +convention=coordinate_frame +step +inv +proj=cart +ellps=GRS67 +step +proj=pop +v_3 +step +proj=somerc +lat_0=47.1443937222222 +lon_0=19.0485717777778 +k_0=0.99993 +x_0=650000 +y_0=200000 +ellps=GRS67" ) );

QgsPointXY pp( 529127, 479348 );
pp = ct.transform( pp );
QGSCOMPARENEAR( pp.x(), 679125.816475, 0.00001 );
QGSCOMPARENEAR( pp.y(), 217454.893093, 0.00001 );
#endif
}


QGSTEST_MAIN( TestQgsCoordinateTransform )
#include "testqgscoordinatetransform.moc"

0 comments on commit 10850d7

Please sign in to comment.
You can’t perform that action at this time.