@@ -90,6 +90,7 @@ class TestQgsCoordinateReferenceSystem: public QObject
90
90
void geographicCrsAuthId ();
91
91
void noProj ();
92
92
void customProjString ();
93
+ void recentProjections ();
93
94
94
95
private:
95
96
void debugPrint ( QgsCoordinateReferenceSystem &crs );
@@ -136,8 +137,15 @@ void TestQgsCoordinateReferenceSystem::initTestCase()
136
137
QgsApplication::init ( mTempFolder );
137
138
QgsApplication::createDatabase ();
138
139
QgsApplication::initQgis ();
140
+ QCoreApplication::setOrganizationName ( QStringLiteral ( " QGIS" ) );
141
+ QCoreApplication::setOrganizationDomain ( QStringLiteral ( " qgis.org" ) );
142
+ QCoreApplication::setApplicationName ( QStringLiteral ( " QGIS-TEST" ) );
143
+
144
+ QSettings ().clear ();
145
+
139
146
QgsApplication::showSettings ();
140
147
148
+
141
149
QgsDebugMsg ( QStringLiteral ( " Custom srs database: %1" ).arg ( QgsApplication::qgisUserDatabaseFilePath () ) );
142
150
143
151
qDebug () << " geoProj4() constant: " << geoProj4 ();
@@ -179,9 +187,10 @@ void TestQgsCoordinateReferenceSystem::wktCtor()
179
187
}
180
188
void TestQgsCoordinateReferenceSystem::idCtor ()
181
189
{
190
+ Q_NOWARN_DEPRECATED_PUSH
182
191
QgsCoordinateReferenceSystem myCrs ( GEOSRID,
183
192
QgsCoordinateReferenceSystem::EpsgCrsId );
184
- debugPrint ( myCrs );
193
+ Q_NOWARN_DEPRECATED_POP
185
194
QVERIFY ( myCrs.isValid () );
186
195
187
196
#if PROJ_VERSION_MAJOR>=6
@@ -192,15 +201,14 @@ void TestQgsCoordinateReferenceSystem::idCtor()
192
201
}
193
202
void TestQgsCoordinateReferenceSystem::copyCtor ()
194
203
{
195
- QgsCoordinateReferenceSystem myCrs ( GEOSRID,
196
- QgsCoordinateReferenceSystem::EpsgCrsId );
204
+ QgsCoordinateReferenceSystem myCrs ( QStringLiteral ( " EPSG:4326" ) );
197
205
QgsCoordinateReferenceSystem myCrs2 ( myCrs );
198
206
debugPrint ( myCrs2 );
199
207
QVERIFY ( myCrs2.isValid () );
200
208
QCOMPARE ( myCrs2.authid (), QStringLiteral ( " EPSG:4326" ) );
201
209
202
210
// test implicit sharing detachment - modify original
203
- myCrs.createFromString ( QStringLiteral ( " EPSG:3111" ) ) ) ;
211
+ myCrs.createFromString ( QStringLiteral ( " EPSG:3111" ) );
204
212
QVERIFY ( myCrs.isValid () );
205
213
QCOMPARE ( myCrs.authid (), QStringLiteral ( " EPSG:3111" ) );
206
214
QVERIFY ( myCrs2.isValid () );
@@ -209,8 +217,7 @@ void TestQgsCoordinateReferenceSystem::copyCtor()
209
217
210
218
void TestQgsCoordinateReferenceSystem::assignmentCtor ()
211
219
{
212
- QgsCoordinateReferenceSystem myCrs ( GEOSRID,
213
- QgsCoordinateReferenceSystem::EpsgCrsId );
220
+ QgsCoordinateReferenceSystem myCrs ( QStringLiteral ( " EPSG:4326" ) );
214
221
QgsCoordinateReferenceSystem myCrs2 = myCrs;
215
222
debugPrint ( myCrs2 );
216
223
QVERIFY ( myCrs2.isValid () );
@@ -227,8 +234,10 @@ void TestQgsCoordinateReferenceSystem::assignmentCtor()
227
234
void TestQgsCoordinateReferenceSystem::createFromId ()
228
235
{
229
236
QgsCoordinateReferenceSystem myCrs;
237
+ Q_NOWARN_DEPRECATED_PUSH
230
238
myCrs.createFromId ( GEO_EPSG_CRS_ID,
231
239
QgsCoordinateReferenceSystem::EpsgCrsId );
240
+ Q_NOWARN_DEPRECATED_POP
232
241
debugPrint ( myCrs );
233
242
QVERIFY ( myCrs.isValid () );
234
243
QCOMPARE ( myCrs.srsid (), GEOCRS_ID );
@@ -423,6 +432,7 @@ void TestQgsCoordinateReferenceSystem::createFromWktUnknown()
423
432
crs.createFromWkt ( wkt );
424
433
QVERIFY ( crs.isValid () );
425
434
QgsDebugMsg ( crs.toWkt () );
435
+ crs.saveAsUserCrs ( QStringLiteral ( " Test CRS" ) );
426
436
QCOMPARE ( crs.toWkt (), expectedWkt );
427
437
QCOMPARE ( crs.srsid (), static_cast < long >( USER_CRS_START_ID + 1 ) );
428
438
QCOMPARE ( crs.authid (), QStringLiteral ( " USER:100001" ) );
@@ -895,11 +905,13 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
895
905
// valid CRS from proj string
896
906
QgsCoordinateReferenceSystem myCrs8;
897
907
myCrs8.createFromProj ( QStringLiteral ( " +proj=aea +lat_1=20 +lat_2=-23 +lat_0=4 +lon_0=29 +x_0=10.123 +y_0=3 +datum=WGS84 +units=m +no_defs" ) );
908
+ myCrs8.saveAsUserCrs ( QStringLiteral ( " test" ) );
898
909
node = document.createElement ( QStringLiteral ( " crs" ) );
899
910
document.appendChild ( node );
900
911
QVERIFY ( myCrs8.writeXml ( node, document ) );
901
912
QgsCoordinateReferenceSystem myCrs9;
902
913
QVERIFY ( myCrs9.readXml ( node ) );
914
+ myCrs9.saveAsUserCrs ( QStringLiteral ( " test2" ) );
903
915
904
916
#if PROJ_VERSION_MAJOR>=6
905
917
QCOMPARE ( myCrs9.toProj (), QStringLiteral ( " +proj=aea +lat_0=4 +lon_0=29 +lat_1=20 +lat_2=-23 +x_0=10.123 +y_0=3 +datum=WGS84 +units=m +no_defs +type=crs" ) );
@@ -914,11 +926,13 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
914
926
// valid CRS from WKT string
915
927
QgsCoordinateReferenceSystem myCrs10;
916
928
myCrs10.createFromWkt ( QStringLiteral ( R"""( PROJCS["xxx",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[1,2,3,4,5,6,7],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4283"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",-36],PARAMETER["standard_parallel_2",-38],PARAMETER["latitude_of_origin",-37.2],PARAMETER["central_meridian",145.1],PARAMETER["false_easting",2510000],PARAMETER["false_northing",2520000],AXIS["Easting",EAST],AXIS["Northing",NORTH]])""" ) );
929
+ myCrs10.saveAsUserCrs ( QStringLiteral ( " test3" ) );
917
930
node = document.createElement ( QStringLiteral ( " crs" ) );
918
931
document.appendChild ( node );
919
932
QVERIFY ( myCrs10.writeXml ( node, document ) );
920
933
QgsCoordinateReferenceSystem myCrs11;
921
934
QVERIFY ( myCrs11.readXml ( node ) );
935
+ myCrs11.saveAsUserCrs ( QStringLiteral ( " test4" ) );
922
936
#if PROJ_VERSION_MAJOR>=6
923
937
QCOMPARE ( myCrs11.authid (), QStringLiteral ( " USER:100005" ) );
924
938
QCOMPARE ( myCrs11.toProj (), QStringLiteral ( " +proj=lcc +lat_0=-37.2 +lon_0=145.1 +lat_1=-36 +lat_2=-38 +x_0=2510000 +y_0=2520000 +ellps=GRS80 +towgs84=1,2,3,4,5,6,7 +units=m +no_defs +type=crs" ) );
@@ -932,8 +946,9 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
932
946
// try reloading, make sure it gets the same user crs assigned
933
947
QgsCoordinateReferenceSystem myCrs11b;
934
948
QVERIFY ( myCrs11b.readXml ( node ) );
949
+ myCrs11b.saveAsUserCrs ( QStringLiteral ( " test4" ) );
935
950
#if PROJ_VERSION_MAJOR>=6
936
- QCOMPARE ( myCrs11b.authid (), QStringLiteral ( " USER:100005 " ) );
951
+ QCOMPARE ( myCrs11b.authid (), QStringLiteral ( " USER:100006 " ) );
937
952
QCOMPARE ( myCrs11b.toProj (), QStringLiteral ( " +proj=lcc +lat_0=-37.2 +lon_0=145.1 +lat_1=-36 +lat_2=-38 +x_0=2510000 +y_0=2520000 +ellps=GRS80 +towgs84=1,2,3,4,5,6,7 +units=m +no_defs +type=crs" ) );
938
953
QCOMPARE ( myCrs11b.toWkt (), QStringLiteral ( R"""( PROJCS["xxx",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[1,2,3,4,5,6,7],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4283"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",-36],PARAMETER["standard_parallel_2",-38],PARAMETER["latitude_of_origin",-37.2],PARAMETER["central_meridian",145.1],PARAMETER["false_easting",2510000],PARAMETER["false_northing",2520000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]])""" ) );
939
954
#else
@@ -952,6 +967,7 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
952
967
nodeList.at ( 0 ).parentNode ().removeChild ( nodeList.at ( 0 ) );
953
968
QgsCoordinateReferenceSystem myCrs13;
954
969
QVERIFY ( myCrs13.readXml ( node ) );
970
+ myCrs13.saveAsUserCrs ( QStringLiteral ( " test6" ) );
955
971
#if PROJ_VERSION_MAJOR>=6
956
972
QCOMPARE ( myCrs13.authid (), QStringLiteral ( " USER:100007" ) );
957
973
QCOMPARE ( myCrs13.toProj (), QStringLiteral ( " +proj=lcc +lat_0=-37.2 +lon_0=145.1 +lat_1=-36 +lat_2=-38 +x_0=2510000 +y_0=2520000 +ellps=GRS80 +towgs84=1,2,3,4,5,6,7 +units=m +no_defs" ) );
@@ -965,6 +981,7 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
965
981
// fudge a dom element with conflicting proj and wkt, wkt should be preferred
966
982
QgsCoordinateReferenceSystem myCrs14;
967
983
myCrs14.createFromWkt ( QStringLiteral ( R"""( PROJCS["xxx",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[1,2,3,4,5,6,7],AUTHORITY["EPSG","6283"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4283"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",-36],PARAMETER["standard_parallel_2",-38],PARAMETER["latitude_of_origin",-37.2],PARAMETER["central_meridian",145.1],PARAMETER["false_easting",2510000],PARAMETER["false_northing",2520000],AXIS["Easting",EAST],AXIS["Northing",NORTH]])""" ) );
984
+ myCrs14.saveAsUserCrs ( QStringLiteral ( " test5" ) );
968
985
node = document.createElement ( QStringLiteral ( " crs" ) );
969
986
document.appendChild ( node );
970
987
QVERIFY ( myCrs14.writeXml ( node, document ) );
@@ -975,6 +992,7 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
975
992
node.toElement ().elementsByTagName ( QStringLiteral ( " spatialrefsys" ) ).at ( 0 ).toElement ().appendChild ( proj4Element );
976
993
QgsCoordinateReferenceSystem myCrs15;
977
994
QVERIFY ( myCrs15.readXml ( node ) );
995
+ myCrs15.saveAsUserCrs ( QStringLiteral ( " test6" ) );
978
996
#if PROJ_VERSION_MAJOR>=6
979
997
QCOMPARE ( myCrs15.authid (), QStringLiteral ( " USER:100009" ) );
980
998
QCOMPARE ( myCrs15.toProj (), QStringLiteral ( " +proj=lcc +lat_0=-37.2 +lon_0=145.1 +lat_1=-36 +lat_2=-38 +x_0=2510000 +y_0=2520000 +ellps=GRS80 +towgs84=1,2,3,4,5,6,7 +units=m +no_defs +type=crs" ) );
@@ -1356,5 +1374,67 @@ void TestQgsCoordinateReferenceSystem::customProjString()
1356
1374
#endif
1357
1375
}
1358
1376
1377
+ void TestQgsCoordinateReferenceSystem::recentProjections ()
1378
+ {
1379
+ QList< QgsCoordinateReferenceSystem > recent = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems ();
1380
+ QVERIFY ( recent.isEmpty () );
1381
+
1382
+ QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem ( QgsCoordinateReferenceSystem ( QStringLiteral ( " EPSG:3111" ) ) );
1383
+ recent = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems ();
1384
+ QCOMPARE ( recent.size (), 1 );
1385
+ QCOMPARE ( recent.at ( 0 ).authid (), QStringLiteral ( " EPSG:3111" ) );
1386
+
1387
+ QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem ( QgsCoordinateReferenceSystem::fromProj ( geoProj4 () ) );
1388
+ recent = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems ();
1389
+ QCOMPARE ( recent.size (), 2 );
1390
+ QCOMPARE ( recent.at ( 0 ).authid (), QStringLiteral ( " EPSG:4326" ) );
1391
+ QCOMPARE ( recent.at ( 1 ).authid (), QStringLiteral ( " EPSG:3111" ) );
1392
+
1393
+ // push back to top of list
1394
+ QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem ( QgsCoordinateReferenceSystem ( QStringLiteral ( " EPSG:3111" ) ) );
1395
+ recent = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems ();
1396
+ QCOMPARE ( recent.size (), 2 );
1397
+ QCOMPARE ( recent.at ( 0 ).authid (), QStringLiteral ( " EPSG:3111" ) );
1398
+ QCOMPARE ( recent.at ( 1 ).authid (), QStringLiteral ( " EPSG:4326" ) );
1399
+
1400
+ // no invalid CRSes in list:
1401
+ QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem ( QgsCoordinateReferenceSystem () );
1402
+ QCOMPARE ( recent.size (), 2 );
1403
+ QCOMPARE ( recent.at ( 0 ).authid (), QStringLiteral ( " EPSG:3111" ) );
1404
+ QCOMPARE ( recent.at ( 1 ).authid (), QStringLiteral ( " EPSG:4326" ) );
1405
+
1406
+ // no custom CRSes in list:
1407
+ QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem ( QgsCoordinateReferenceSystem::fromProj ( 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" ) ) );
1408
+ QCOMPARE ( recent.size (), 2 );
1409
+ QCOMPARE ( recent.at ( 0 ).authid (), QStringLiteral ( " EPSG:3111" ) );
1410
+ QCOMPARE ( recent.at ( 1 ).authid (), QStringLiteral ( " EPSG:4326" ) );
1411
+
1412
+ // user CRSes ARE allowed
1413
+ QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem ( QgsCoordinateReferenceSystem ( QStringLiteral ( " USER:100001" ) ) );
1414
+ recent = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems ();
1415
+ QCOMPARE ( recent.size (), 3 );
1416
+ QCOMPARE ( recent.at ( 0 ).authid (), QStringLiteral ( " USER:100001" ) );
1417
+ QCOMPARE ( recent.at ( 1 ).authid (), QStringLiteral ( " EPSG:3111" ) );
1418
+ QCOMPARE ( recent.at ( 2 ).authid (), QStringLiteral ( " EPSG:4326" ) );
1419
+
1420
+ // list should be truncated after 10 entries
1421
+ for ( int i = 32510 ; i < 32550 ; ++i )
1422
+ {
1423
+ QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem ( QgsCoordinateReferenceSystem ( QStringLiteral ( " EPSG:%1" ).arg ( i ) ) );
1424
+ }
1425
+ recent = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems ();
1426
+ QCOMPARE ( recent.size (), 10 );
1427
+ QCOMPARE ( recent.at ( 0 ).authid (), QStringLiteral ( " EPSG:32549" ) );
1428
+ QCOMPARE ( recent.at ( 1 ).authid (), QStringLiteral ( " EPSG:32548" ) );
1429
+ QCOMPARE ( recent.at ( 2 ).authid (), QStringLiteral ( " EPSG:32547" ) );
1430
+ QCOMPARE ( recent.at ( 3 ).authid (), QStringLiteral ( " EPSG:32546" ) );
1431
+ QCOMPARE ( recent.at ( 4 ).authid (), QStringLiteral ( " EPSG:32545" ) );
1432
+ QCOMPARE ( recent.at ( 5 ).authid (), QStringLiteral ( " EPSG:32544" ) );
1433
+ QCOMPARE ( recent.at ( 6 ).authid (), QStringLiteral ( " EPSG:32543" ) );
1434
+ QCOMPARE ( recent.at ( 7 ).authid (), QStringLiteral ( " EPSG:32542" ) );
1435
+ QCOMPARE ( recent.at ( 8 ).authid (), QStringLiteral ( " EPSG:32541" ) );
1436
+ QCOMPARE ( recent.at ( 9 ).authid (), QStringLiteral ( " EPSG:32540" ) );
1437
+ }
1438
+
1359
1439
QGSTEST_MAIN ( TestQgsCoordinateReferenceSystem )
1360
1440
#include " testqgscoordinatereferencesystem.moc"
0 commit comments