1 /***************************************************************************
2     testqgsdatumtransformdialog.cpp
3      --------------------------------------
4     Date                 : March 2019
5     Copyright            : (C) 2019 Nyall Dawson
6     Email                : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 
16 
17 #include "qgstest.h"
18 
19 #include "qgsdatumtransformdialog.h"
20 #include "qgssettings.h"
21 #include "qgsproject.h"
22 
23 class TestQgsDatumTransformDialog: public QObject
24 {
25     Q_OBJECT
26   private slots:
27     void initTestCase(); // will be called before the first testfunction is executed.
28     void cleanupTestCase(); // will be called after the last testfunction was executed.
29     void init(); // will be called before each testfunction is executed.
30     void cleanup(); // will be called after every testfunction.
31 
32     void defaultTransform();
33     void fallback();
34     void shouldAskUser();
35     void applyDefaultTransform();
36     void runDialog();
37 
38   private:
39 
40 };
41 
initTestCase()42 void TestQgsDatumTransformDialog::initTestCase()
43 {
44   // initialize with test settings directory so we don't mess with user's stuff
45   QgsApplication::init( QDir::tempPath() + "/dot-qgis" );
46   QgsApplication::initQgis();
47   QgsApplication::createDatabase();
48   // output test environment
49   QgsApplication::showSettings();
50 
51   // Set up the QgsSettings environment
52   QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
53   QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
54   QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
55 }
56 
cleanupTestCase()57 void TestQgsDatumTransformDialog::cleanupTestCase()
58 {
59 }
60 
init()61 void TestQgsDatumTransformDialog::init()
62 {
63 }
64 
cleanup()65 void TestQgsDatumTransformDialog::cleanup()
66 {
67 }
68 
defaultTransform()69 void TestQgsDatumTransformDialog::defaultTransform()
70 {
71   QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7844" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4283" ) ) );
72 
73   QgsDatumTransformDialog::TransformInfo def = dlg.defaultDatumTransform();
74   QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:7844" ) );
75   QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:4283" ) );
76   QCOMPARE( def.proj, QStringLiteral( "+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=push +v_3 +step +proj=cart +ellps=GRS80 +step +inv +proj=helmert +x=0.06155 +y=-0.01087 +z=-0.04019 +rx=-0.0394924 +ry=-0.0327221 +rz=-0.0328979 +s=-0.009994 +convention=coordinate_frame +step +inv +proj=cart +ellps=GRS80 +step +proj=pop +v_3 +step +proj=unitconvert +xy_in=rad +xy_out=deg" ) );
77   QVERIFY( def.allowFallback );
78 
79   // default should be initially selected
80   def = dlg.selectedDatumTransform();
81   QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:7844" ) );
82   QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:4283" ) );
83   QCOMPARE( def.proj, QStringLiteral( "+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=push +v_3 +step +proj=cart +ellps=GRS80 +step +inv +proj=helmert +x=0.06155 +y=-0.01087 +z=-0.04019 +rx=-0.0394924 +ry=-0.0327221 +rz=-0.0328979 +s=-0.009994 +convention=coordinate_frame +step +inv +proj=cart +ellps=GRS80 +step +proj=pop +v_3 +step +proj=unitconvert +xy_in=rad +xy_out=deg" ) );
84 
85   const QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ) );
86   def = dlg2.defaultDatumTransform();
87   QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:4326" ) );
88   QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:26742" ) );
89   if ( def.proj == QLatin1String( "+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +inv +proj=hgridshift +grids=conus +step +proj=lcc +lat_0=37.6666666666667 +lon_0=-122 +lat_1=39.8333333333333 +lat_2=38.3333333333333 +x_0=609601.219202438 +y_0=0 +ellps=clrk66 +step +proj=unitconvert +xy_in=m +xy_out=us-ft" ) )
90   {
91     QCOMPARE( def.proj, QStringLiteral( "+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +inv +proj=hgridshift +grids=conus +step +proj=lcc +lat_0=37.6666666666667 +lon_0=-122 +lat_1=39.8333333333333 +lat_2=38.3333333333333 +x_0=609601.219202438 +y_0=0 +ellps=clrk66 +step +proj=unitconvert +xy_in=m +xy_out=us-ft" ) );
92   }
93   else
94   {
95     QCOMPARE( def.proj, QStringLiteral( "+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=push +v_3 +step +proj=cart +ellps=WGS84 +step +proj=helmert +x=8 +y=-159 +z=-175 +step +inv +proj=cart +ellps=clrk66 +step +proj=pop +v_3 +step +proj=lcc +lat_0=37.6666666666667 +lon_0=-122 +lat_1=39.8333333333333 +lat_2=38.3333333333333 +x_0=609601.219202438 +y_0=0 +ellps=clrk66 +step +proj=unitconvert +xy_in=m +xy_out=us-ft" ) );
96   }
97 }
98 
fallback()99 void TestQgsDatumTransformDialog::fallback()
100 {
101   // don't default to allow fallback
102   QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7844" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4283" ) ), false, true, false, qMakePair( -1, -1 ), nullptr, Qt::WindowFlags(), QString(), nullptr, false );
103 
104   const QgsDatumTransformDialog::TransformInfo def = dlg.selectedDatumTransform();
105   QCOMPARE( def.sourceCrs.authid(), QStringLiteral( "EPSG:7844" ) );
106   QCOMPARE( def.destinationCrs.authid(), QStringLiteral( "EPSG:4283" ) );
107   QCOMPARE( def.proj, QStringLiteral( "+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=push +v_3 +step +proj=cart +ellps=GRS80 +step +inv +proj=helmert +x=0.06155 +y=-0.01087 +z=-0.04019 +rx=-0.0394924 +ry=-0.0327221 +rz=-0.0328979 +s=-0.009994 +convention=coordinate_frame +step +inv +proj=cart +ellps=GRS80 +step +proj=pop +v_3 +step +proj=unitconvert +xy_in=rad +xy_out=deg" ) );
108   QVERIFY( !def.allowFallback );
109 }
110 
shouldAskUser()111 void TestQgsDatumTransformDialog::shouldAskUser()
112 {
113   // no prompts!
114   QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );
115   const QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
116   QVERIFY( !dlg.shouldAskUserForSelection() );
117   const QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
118   QVERIFY( !dlg2.shouldAskUserForSelection() );
119 
120 //prompts
121   QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), true, QgsSettings::App );
122   const QgsDatumTransformDialog dlg3( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
123   QVERIFY( dlg3.shouldAskUserForSelection() );
124   const QgsDatumTransformDialog dlg4( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
125   QVERIFY( !dlg4.shouldAskUserForSelection() );
126 }
127 
applyDefaultTransform()128 void TestQgsDatumTransformDialog::applyDefaultTransform()
129 {
130   QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );
131 
132   Q_NOWARN_DEPRECATED_PUSH
133   QgsDatumTransformDialog dlg( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) );
134   dlg.applyDefaultTransform();
135   QVERIFY( QgsProject::instance()->transformContext().sourceDestinationDatumTransforms().isEmpty() );
136   QVERIFY( QgsProject::instance()->transformContext().coordinateOperations().isEmpty() );
137   QgsProject::instance()->transformContext().addCoordinateOperation( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ), QStringLiteral( "some proj" ) );
138 
139   QgsDatumTransformDialog dlg2( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
140   dlg2.applyDefaultTransform();
141 
142   // a default transform means there should be no entry in the context
143   QVERIFY( QgsProject::instance()->transformContext().coordinateOperations().isEmpty() );
144   Q_NOWARN_DEPRECATED_POP
145   QgsProject::instance()->clear();
146 }
147 
runDialog()148 void TestQgsDatumTransformDialog::runDialog()
149 {
150   QgsSettings().setValue( QStringLiteral( "/projections/promptWhenMultipleTransformsExist" ), false, QgsSettings::App );
151   QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7406" ) ) ) );
152   QVERIFY( QgsProject::instance()->transformContext().coordinateOperations().isEmpty() );
153 
154   QVERIFY( QgsDatumTransformDialog::run( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:26742" ) ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) ) );
155   QVERIFY( QgsProject::instance()->transformContext().coordinateOperations().isEmpty() );
156   QgsProject::instance()->clear();
157 }
158 
159 
160 QGSTEST_MAIN( TestQgsDatumTransformDialog )
161 #include "testqgsdatumtransformdialog.moc"
162