Skip to content

Commit e152685

Browse files
committedDec 20, 2019
Cleanup handling of recent CRS list, add tests
1 parent 908684c commit e152685

12 files changed

+205
-110
lines changed
 

‎python/core/auto_generated/qgscoordinatereferencesystem.sip.in

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,13 +827,28 @@ Returns auth id of related geographic CRS
827827
%End
828828

829829

830-
static QStringList recentProjections();
830+
static QStringList recentProjections() /Deprecated/;
831831
%Docstring
832832
Returns a list of recently used projections
833833

834834
:return: list of srsid for recently used projections
835835

836-
.. versionadded:: 2.7
836+
.. deprecated::
837+
use recentCoordinateReferenceSystems() instead.
838+
%End
839+
840+
static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems();
841+
%Docstring
842+
Returns a list of recently used CRS.
843+
844+
.. versionadded:: 3.10.3
845+
%End
846+
847+
static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs );
848+
%Docstring
849+
Pushes a recently used CRS to the top of the recent CRS list.
850+
851+
.. versionadded:: 3.10.3
837852
%End
838853

839854

‎src/app/dwg/qgsdwgimportdialog.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ QgsDwgImportDialog::QgsDwgImportDialog( QWidget *parent, Qt::WindowFlags f )
8181

8282
int crsid = s.value( QStringLiteral( "/DwgImport/lastCrs" ), QString::number( QgsProject::instance()->crs().srsid() ) ).toInt();
8383

84-
QgsCoordinateReferenceSystem crs( crsid, QgsCoordinateReferenceSystem::InternalCrsId );
84+
QgsCoordinateReferenceSystem crs;
85+
crs.createFromSrsId( crsid );
8586
mCrsSelector->setCrs( crs );
8687
mCrsSelector->setLayerCrs( crs );
8788
mCrsSelector->setMessage( tr( "Select the coordinate reference system for the dxf file. "
@@ -162,7 +163,8 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
162163
{
163164
leDrawing->setText( f.attribute( idxPath ).toString() );
164165

165-
QgsCoordinateReferenceSystem crs( f.attribute( idxCrs ).toInt(), QgsCoordinateReferenceSystem::InternalCrsId );
166+
QgsCoordinateReferenceSystem crs;
167+
crs.createFromSrsId( f.attribute( idxCrs ).toInt() );
166168
mCrsSelector->setCrs( crs );
167169
mCrsSelector->setLayerCrs( crs );
168170

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,18 +2200,6 @@ long QgsCoordinateReferenceSystem::saveAsUserCrs( const QString &name, Format na
22002200
if ( authid().isEmpty() )
22012201
setAuthId( QStringLiteral( "USER:%1" ).arg( returnId ) );
22022202
setDescription( name );
2203-
2204-
//We add the just created user CRS to the list of recently used CRS
2205-
QgsSettings settings;
2206-
//QStringList recentProjections = settings.value( "/UI/recentProjections" ).toStringList();
2207-
QStringList projectionsProj4 = settings.value( QStringLiteral( "UI/recentProjectionsProj4" ) ).toStringList();
2208-
QStringList projectionsAuthId = settings.value( QStringLiteral( "UI/recentProjectionsAuthId" ) ).toStringList();
2209-
//recentProjections.append();
2210-
//settings.setValue( "/UI/recentProjections", recentProjections );
2211-
projectionsProj4.append( toProj() );
2212-
projectionsAuthId.append( authid() );
2213-
settings.setValue( QStringLiteral( "UI/recentProjectionsProj4" ), projectionsProj4 );
2214-
settings.setValue( QStringLiteral( "UI/recentProjectionsAuthId" ), projectionsAuthId );
22152203
}
22162204

22172205
invalidateCache();
@@ -3294,43 +3282,75 @@ PJ *QgsCoordinateReferenceSystem::projObject() const
32943282
QStringList QgsCoordinateReferenceSystem::recentProjections()
32953283
{
32963284
QStringList projections;
3285+
const QList<QgsCoordinateReferenceSystem> res = recentCoordinateReferenceSystems();
3286+
projections.reserve( res.size() );
3287+
for ( const QgsCoordinateReferenceSystem &crs : res )
3288+
{
3289+
projections << QString::number( crs.srsid() );
3290+
}
3291+
return projections;
3292+
}
3293+
3294+
QList<QgsCoordinateReferenceSystem> QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems()
3295+
{
3296+
QList<QgsCoordinateReferenceSystem> res;
32973297

32983298
// Read settings from persistent storage
32993299
QgsSettings settings;
3300-
projections = settings.value( QStringLiteral( "UI/recentProjections" ) ).toStringList();
3301-
/*** The reading (above) of internal id from persistent storage should be removed sometime in the future */
3302-
/*** This is kept now for backwards compatibility */
3303-
33043300
QStringList projectionsProj4 = settings.value( QStringLiteral( "UI/recentProjectionsProj4" ) ).toStringList();
3301+
QStringList projectionsWkt = settings.value( QStringLiteral( "UI/recentProjectionsWkt" ) ).toStringList();
33053302
QStringList projectionsAuthId = settings.value( QStringLiteral( "UI/recentProjectionsAuthId" ) ).toStringList();
3306-
if ( projectionsAuthId.size() >= projections.size() )
3303+
int max = std::max( projectionsAuthId.size(), std::max( projectionsProj4.size(), projectionsWkt.size() ) );
3304+
res.reserve( max );
3305+
for ( int i = 0; i < max; ++i )
33073306
{
3308-
// We had saved state with AuthId and Proj4. Use that instead
3309-
// to find out the crs id
3310-
projections.clear();
3311-
for ( int i = 0; i < projectionsAuthId.size(); i++ )
3312-
{
3313-
// Create a crs from the EPSG
3314-
QgsCoordinateReferenceSystem crs;
3315-
crs.createFromOgcWmsCrs( projectionsAuthId.at( i ) );
3316-
if ( ! crs.isValid() )
3317-
{
3318-
// Couldn't create from EPSG, try the Proj4 string instead
3319-
if ( i >= projectionsProj4.size() || !crs.createFromProj( projectionsProj4.at( i ) ) )
3320-
{
3321-
// No? Skip this entry
3322-
continue;
3323-
}
3324-
//If the CRS can be created but do not correspond to a CRS in the database, skip it (for example a deleted custom CRS)
3325-
if ( crs.srsid() == 0 )
3326-
{
3327-
continue;
3328-
}
3329-
}
3330-
projections << QString::number( crs.srsid() );
3331-
}
3307+
const QString proj = projectionsProj4.value( i );
3308+
const QString wkt = projectionsWkt.value( i );
3309+
const QString authid = projectionsAuthId.value( i );
3310+
3311+
QgsCoordinateReferenceSystem crs;
3312+
if ( !authid.isEmpty() )
3313+
crs = QgsCoordinateReferenceSystem( authid );
3314+
if ( !crs.isValid() && !wkt.isEmpty() )
3315+
crs.createFromWkt( wkt );
3316+
if ( !crs.isValid() && !proj.isEmpty() )
3317+
crs.createFromProj( wkt );
3318+
3319+
if ( crs.isValid() )
3320+
res << crs;
33323321
}
3333-
return projections;
3322+
return res;
3323+
}
3324+
3325+
void QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs )
3326+
{
3327+
// we only want saved and standard CRSes in the recent list
3328+
if ( crs.srsid() == 0 || !crs.isValid() )
3329+
return;
3330+
3331+
QList<QgsCoordinateReferenceSystem> recent = recentCoordinateReferenceSystems();
3332+
recent.removeAll( crs );
3333+
recent.insert( 0, crs );
3334+
3335+
// trim to max 10 items
3336+
recent = recent.mid( 0, 10 );
3337+
QStringList authids;
3338+
authids.reserve( recent.size() );
3339+
QStringList proj;
3340+
proj.reserve( recent.size() );
3341+
QStringList wkt;
3342+
wkt.reserve( recent.size() );
3343+
for ( const QgsCoordinateReferenceSystem &c : qgis::as_const( recent ) )
3344+
{
3345+
authids << c.authid();
3346+
proj << c.toProj();
3347+
wkt << c.toWkt();
3348+
}
3349+
3350+
QgsSettings settings;
3351+
settings.setValue( QStringLiteral( "UI/recentProjectionsAuthId" ), authids );
3352+
settings.setValue( QStringLiteral( "UI/recentProjectionsWkt" ), wkt );
3353+
settings.setValue( QStringLiteral( "UI/recentProjectionsProj4" ), proj );
33343354
}
33353355

33363356
void QgsCoordinateReferenceSystem::invalidateCache( bool disableCache )

‎src/core/qgscoordinatereferencesystem.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,21 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
770770
/**
771771
* Returns a list of recently used projections
772772
* \returns list of srsid for recently used projections
773-
* \since QGIS 2.7
773+
* \deprecated use recentCoordinateReferenceSystems() instead.
774774
*/
775-
static QStringList recentProjections();
775+
Q_DECL_DEPRECATED static QStringList recentProjections() SIP_DEPRECATED;
776+
777+
/**
778+
* Returns a list of recently used CRS.
779+
* \since QGIS 3.10.3
780+
*/
781+
static QList< QgsCoordinateReferenceSystem > recentCoordinateReferenceSystems();
782+
783+
/**
784+
* Pushes a recently used CRS to the top of the recent CRS list.
785+
* \since QGIS 3.10.3
786+
*/
787+
static void pushRecentCoordinateReferenceSystem( const QgsCoordinateReferenceSystem &crs );
776788

777789
#ifndef SIP_RUN
778790

‎src/gui/qgscoordinateboundspreviewmapwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ QgsCoordinateBoundsPreviewMapWidget::QgsCoordinateBoundsPreviewMapWidget( QWidge
3131
mCanvasCenterMarker->setColor( QColor( 185, 84, 210 ) );
3232
mCanvasCenterMarker->setPenWidth( 3 );
3333

34-
QgsCoordinateReferenceSystem srs( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );
34+
QgsCoordinateReferenceSystem srs( QStringLiteral( "EPSG:4326" ) );
3535
setDestinationCrs( srs );
3636

3737
QString layerPath = QgsApplication::pkgDataPath() + QStringLiteral( "/resources/data/world_map.gpkg|layername=countries" );

‎src/gui/qgsprojectionselectiontreewidget.cpp

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ QgsProjectionSelectionTreeWidget::QgsProjectionSelectionTreeWidget( QWidget *par
7171
// Hide (internal) ID column
7272
lstRecent->setColumnHidden( QgisCrsIdColumn, true );
7373

74-
mRecentProjections = QgsCoordinateReferenceSystem::recentProjections();
74+
mRecentProjections = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems();
7575

7676
mCheckBoxNoProjection->setHidden( true );
7777
mCheckBoxNoProjection->setEnabled( false );
@@ -101,37 +101,7 @@ QgsProjectionSelectionTreeWidget::~QgsProjectionSelectionTreeWidget()
101101
if ( crsId == 0 )
102102
return;
103103

104-
// Save persistent list of projects
105-
mRecentProjections.removeAll( QString::number( crsId ) );
106-
mRecentProjections.prepend( QString::number( crsId ) );
107-
// Prune size of list
108-
while ( mRecentProjections.size() > 8 )
109-
{
110-
mRecentProjections.removeLast();
111-
}
112-
113-
// Save to file *** Should be removed sometime in the future ***
114-
QgsSettings settings;
115-
settings.setValue( QStringLiteral( "/UI/recentProjections" ), mRecentProjections );
116-
117-
// Convert to EPSG and proj4, and save those values also
118-
119-
QStringList projectionsProj4;
120-
QStringList projectionsAuthId;
121-
for ( int i = 0; i < mRecentProjections.size(); i++ )
122-
{
123-
// Create a crs from the crsId
124-
QgsCoordinateReferenceSystem crs( mRecentProjections.at( i ).toLong(), QgsCoordinateReferenceSystem::InternalCrsId );
125-
if ( ! crs.isValid() )
126-
{
127-
// No? Skip this entry
128-
continue;
129-
}
130-
projectionsProj4 << crs.toProj();
131-
projectionsAuthId << crs.authid();
132-
}
133-
settings.setValue( QStringLiteral( "/UI/recentProjectionsProj4" ), projectionsProj4 );
134-
settings.setValue( QStringLiteral( "/UI/recentProjectionsAuthId" ), projectionsAuthId );
104+
QgsCoordinateReferenceSystem::pushRecentCoordinateReferenceSystem( crs() );
135105
}
136106

137107
void QgsProjectionSelectionTreeWidget::resizeEvent( QResizeEvent *event )
@@ -157,8 +127,8 @@ void QgsProjectionSelectionTreeWidget::showEvent( QShowEvent *event )
157127

158128
if ( !mRecentProjListDone )
159129
{
160-
for ( int i = mRecentProjections.size() - 1; i >= 0; i-- )
161-
insertRecent( mRecentProjections.at( i ).toLong() );
130+
for ( const QgsCoordinateReferenceSystem &crs : qgis::as_const( mRecentProjections ) )
131+
insertRecent( crs );
162132
mRecentProjListDone = true;
163133
}
164134

@@ -272,12 +242,12 @@ void QgsProjectionSelectionTreeWidget::applySelection( int column, QString value
272242
}
273243
}
274244

275-
void QgsProjectionSelectionTreeWidget::insertRecent( long crsId )
245+
void QgsProjectionSelectionTreeWidget::insertRecent( const QgsCoordinateReferenceSystem &crs )
276246
{
277247
if ( !mProjListDone || !mUserProjListDone )
278248
return;
279249

280-
QList<QTreeWidgetItem *> nodes = lstCoordinateSystems->findItems( QString::number( crsId ), Qt::MatchExactly | Qt::MatchRecursive, QgisCrsIdColumn );
250+
QList<QTreeWidgetItem *> nodes = lstCoordinateSystems->findItems( QString::number( crs.srsid() ), Qt::MatchExactly | Qt::MatchRecursive, QgisCrsIdColumn );
281251
if ( nodes.isEmpty() )
282252
return;
283253

‎src/gui/qgsprojectionselectiontreewidget.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,14 @@ class GUI_EXPORT QgsProjectionSelectionTreeWidget : public QWidget, private Ui::
260260
long getLargestCrsIdMatch( const QString &sql );
261261

262262
//! add recently used CRS
263-
void insertRecent( long crsId );
263+
void insertRecent( const QgsCoordinateReferenceSystem &crs );
264264

265265
//! Has the Projection List been populated?
266266
bool mProjListDone = false;
267267

268268
//! Has the User Projection List been populated?
269269
bool mUserProjListDone = false;
270270

271-
272271
//! Has the Recent Projection List been populated?
273272
bool mRecentProjListDone = false;
274273

@@ -281,8 +280,8 @@ class GUI_EXPORT QgsProjectionSelectionTreeWidget : public QWidget, private Ui::
281280
//! The set of OGC WMS CRSs that want to be applied to this widget
282281
QSet<QString> mCrsFilter;
283282

284-
//! Most recently used projections (trimmed at 25 entries)
285-
QStringList mRecentProjections;
283+
//! Most recently used projections
284+
QList< QgsCoordinateReferenceSystem > mRecentProjections;
286285

287286
//! Hide deprecated CRSes
288287
void hideDeprecated( QTreeWidgetItem *item );

‎src/gui/qgsprojectionselectionwidget.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,31 +319,22 @@ QString QgsProjectionSelectionWidget::crsOptionText( const QgsCoordinateReferenc
319319

320320
void QgsProjectionSelectionWidget::addRecentCrs()
321321
{
322-
QStringList recentProjections = QgsCoordinateReferenceSystem::recentProjections();
323-
int i = 0;
324-
const auto constRecentProjections = recentProjections;
325-
for ( const QString &projection : constRecentProjections )
322+
const QList< QgsCoordinateReferenceSystem> recentProjections = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems();
323+
for ( const QgsCoordinateReferenceSystem &crs : recentProjections )
326324
{
327-
long srsid = projection.toLong();
325+
long srsid = crs.srsid();
328326

329327
//check if already shown
330328
if ( crsIsShown( srsid ) )
331329
{
332330
continue;
333331
}
334332

335-
i++;
336-
QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromSrsId( srsid );
337333
if ( crs.isValid() )
338334
{
339335
mCrsComboBox->addItem( tr( "%1 - %2" ).arg( crs.authid(), crs.description() ), QgsProjectionSelectionWidget::RecentCrs );
340336
mCrsComboBox->setItemData( mCrsComboBox->count() - 1, QVariant( ( long long )srsid ), Qt::UserRole + 1 );
341337
}
342-
if ( i >= 4 )
343-
{
344-
//limit to 4 recent projections to avoid clutter
345-
break;
346-
}
347338
}
348339
}
349340

‎src/providers/virtual/qgsvirtuallayerprovider.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ QgsVirtualLayerProvider::QgsVirtualLayerProvider( QString const &uri, const QgsD
9090

9191
if ( mDefinition.geometrySrid() != -1 )
9292
{
93+
Q_NOWARN_DEPRECATED_PUSH
9394
mCrs = QgsCoordinateReferenceSystem( mDefinition.geometrySrid() );
95+
Q_NOWARN_DEPRECATED_POP
9496
}
9597
}
9698

‎src/providers/virtual/qgsvirtuallayersourceselect.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ void QgsVirtualLayerSourceSelect::layerComboChanged( int idx )
128128
{
129129
mGeometryRadio->setChecked( true );
130130
mSrid = def.geometrySrid();
131+
Q_NOWARN_DEPRECATED_PUSH
131132
QgsCoordinateReferenceSystem crs( def.geometrySrid() );
133+
Q_NOWARN_DEPRECATED_POP
132134
mCRS->setText( crs.authid() );
133135
mGeometryType->setCurrentIndex( static_cast<long>( def.geometryWkbType() ) - 1 );
134136
mGeometryField->setText( def.geometryField() );
@@ -150,7 +152,9 @@ void QgsVirtualLayerSourceSelect::layerComboChanged( int idx )
150152
void QgsVirtualLayerSourceSelect::browseCRS()
151153
{
152154
QgsProjectionSelectionDialog crsSelector( this );
155+
Q_NOWARN_DEPRECATED_PUSH
153156
QgsCoordinateReferenceSystem crs( mSrid );
157+
Q_NOWARN_DEPRECATED_POP
154158
crsSelector.setCrs( crs );
155159
crsSelector.setMessage( QString() );
156160
if ( crsSelector.exec() )

‎src/server/qgsserverapiutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,9 @@ QgsExpression QgsServerApiUtils::temporalFilterExpression( const QgsVectorLayer
438438
json QgsServerApiUtils::layerExtent( const QgsVectorLayer *layer )
439439
{
440440
auto extent { layer->extent() };
441-
if ( layer->crs().postgisSrid() != 4326 )
441+
if ( layer->crs().authid() != QLatin1String( "EPSG:4326" ) )
442442
{
443-
static const QgsCoordinateReferenceSystem targetCrs { 4326 };
443+
static const QgsCoordinateReferenceSystem targetCrs( QStringLiteral( "EPSG:4326" ) );
444444
const QgsCoordinateTransform ct( layer->crs(), targetCrs, layer->transformContext() );
445445
extent = ct.transform( extent );
446446
}

‎tests/src/core/testqgscoordinatereferencesystem.cpp

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class TestQgsCoordinateReferenceSystem: public QObject
9090
void geographicCrsAuthId();
9191
void noProj();
9292
void customProjString();
93+
void recentProjections();
9394

9495
private:
9596
void debugPrint( QgsCoordinateReferenceSystem &crs );
@@ -136,8 +137,15 @@ void TestQgsCoordinateReferenceSystem::initTestCase()
136137
QgsApplication::init( mTempFolder );
137138
QgsApplication::createDatabase();
138139
QgsApplication::initQgis();
140+
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
141+
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
142+
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
143+
144+
QSettings().clear();
145+
139146
QgsApplication::showSettings();
140147

148+
141149
QgsDebugMsg( QStringLiteral( "Custom srs database: %1" ).arg( QgsApplication::qgisUserDatabaseFilePath() ) );
142150

143151
qDebug() << "geoProj4() constant: " << geoProj4();
@@ -179,9 +187,10 @@ void TestQgsCoordinateReferenceSystem::wktCtor()
179187
}
180188
void TestQgsCoordinateReferenceSystem::idCtor()
181189
{
190+
Q_NOWARN_DEPRECATED_PUSH
182191
QgsCoordinateReferenceSystem myCrs( GEOSRID,
183192
QgsCoordinateReferenceSystem::EpsgCrsId );
184-
debugPrint( myCrs );
193+
Q_NOWARN_DEPRECATED_POP
185194
QVERIFY( myCrs.isValid() );
186195

187196
#if PROJ_VERSION_MAJOR>=6
@@ -192,15 +201,14 @@ void TestQgsCoordinateReferenceSystem::idCtor()
192201
}
193202
void TestQgsCoordinateReferenceSystem::copyCtor()
194203
{
195-
QgsCoordinateReferenceSystem myCrs( GEOSRID,
196-
QgsCoordinateReferenceSystem::EpsgCrsId );
204+
QgsCoordinateReferenceSystem myCrs( QStringLiteral( "EPSG:4326" ) );
197205
QgsCoordinateReferenceSystem myCrs2( myCrs );
198206
debugPrint( myCrs2 );
199207
QVERIFY( myCrs2.isValid() );
200208
QCOMPARE( myCrs2.authid(), QStringLiteral( "EPSG:4326" ) );
201209

202210
//test implicit sharing detachment - modify original
203-
myCrs.createFromString( QStringLiteral( "EPSG:3111" ) ) );
211+
myCrs.createFromString( QStringLiteral( "EPSG:3111" ) );
204212
QVERIFY( myCrs.isValid() );
205213
QCOMPARE( myCrs.authid(), QStringLiteral( "EPSG:3111" ) );
206214
QVERIFY( myCrs2.isValid() );
@@ -209,8 +217,7 @@ void TestQgsCoordinateReferenceSystem::copyCtor()
209217

210218
void TestQgsCoordinateReferenceSystem::assignmentCtor()
211219
{
212-
QgsCoordinateReferenceSystem myCrs( GEOSRID,
213-
QgsCoordinateReferenceSystem::EpsgCrsId );
220+
QgsCoordinateReferenceSystem myCrs( QStringLiteral( "EPSG:4326" ) );
214221
QgsCoordinateReferenceSystem myCrs2 = myCrs;
215222
debugPrint( myCrs2 );
216223
QVERIFY( myCrs2.isValid() );
@@ -227,8 +234,10 @@ void TestQgsCoordinateReferenceSystem::assignmentCtor()
227234
void TestQgsCoordinateReferenceSystem::createFromId()
228235
{
229236
QgsCoordinateReferenceSystem myCrs;
237+
Q_NOWARN_DEPRECATED_PUSH
230238
myCrs.createFromId( GEO_EPSG_CRS_ID,
231239
QgsCoordinateReferenceSystem::EpsgCrsId );
240+
Q_NOWARN_DEPRECATED_POP
232241
debugPrint( myCrs );
233242
QVERIFY( myCrs.isValid() );
234243
QCOMPARE( myCrs.srsid(), GEOCRS_ID );
@@ -423,6 +432,7 @@ void TestQgsCoordinateReferenceSystem::createFromWktUnknown()
423432
crs.createFromWkt( wkt );
424433
QVERIFY( crs.isValid() );
425434
QgsDebugMsg( crs.toWkt() );
435+
crs.saveAsUserCrs( QStringLiteral( "Test CRS" ) );
426436
QCOMPARE( crs.toWkt(), expectedWkt );
427437
QCOMPARE( crs.srsid(), static_cast< long >( USER_CRS_START_ID + 1 ) );
428438
QCOMPARE( crs.authid(), QStringLiteral( "USER:100001" ) );
@@ -895,11 +905,13 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
895905
// valid CRS from proj string
896906
QgsCoordinateReferenceSystem myCrs8;
897907
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" ) );
898909
node = document.createElement( QStringLiteral( "crs" ) );
899910
document.appendChild( node );
900911
QVERIFY( myCrs8.writeXml( node, document ) );
901912
QgsCoordinateReferenceSystem myCrs9;
902913
QVERIFY( myCrs9.readXml( node ) );
914+
myCrs9.saveAsUserCrs( QStringLiteral( "test2" ) );
903915

904916
#if PROJ_VERSION_MAJOR>=6
905917
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()
914926
// valid CRS from WKT string
915927
QgsCoordinateReferenceSystem myCrs10;
916928
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" ) );
917930
node = document.createElement( QStringLiteral( "crs" ) );
918931
document.appendChild( node );
919932
QVERIFY( myCrs10.writeXml( node, document ) );
920933
QgsCoordinateReferenceSystem myCrs11;
921934
QVERIFY( myCrs11.readXml( node ) );
935+
myCrs11.saveAsUserCrs( QStringLiteral( "test4" ) );
922936
#if PROJ_VERSION_MAJOR>=6
923937
QCOMPARE( myCrs11.authid(), QStringLiteral( "USER:100005" ) );
924938
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()
932946
// try reloading, make sure it gets the same user crs assigned
933947
QgsCoordinateReferenceSystem myCrs11b;
934948
QVERIFY( myCrs11b.readXml( node ) );
949+
myCrs11b.saveAsUserCrs( QStringLiteral( "test4" ) );
935950
#if PROJ_VERSION_MAJOR>=6
936-
QCOMPARE( myCrs11b.authid(), QStringLiteral( "USER:100005" ) );
951+
QCOMPARE( myCrs11b.authid(), QStringLiteral( "USER:100006" ) );
937952
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" ) );
938953
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]])""" ) );
939954
#else
@@ -952,6 +967,7 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
952967
nodeList.at( 0 ).parentNode().removeChild( nodeList.at( 0 ) );
953968
QgsCoordinateReferenceSystem myCrs13;
954969
QVERIFY( myCrs13.readXml( node ) );
970+
myCrs13.saveAsUserCrs( QStringLiteral( "test6" ) );
955971
#if PROJ_VERSION_MAJOR>=6
956972
QCOMPARE( myCrs13.authid(), QStringLiteral( "USER:100007" ) );
957973
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()
965981
// fudge a dom element with conflicting proj and wkt, wkt should be preferred
966982
QgsCoordinateReferenceSystem myCrs14;
967983
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" ) );
968985
node = document.createElement( QStringLiteral( "crs" ) );
969986
document.appendChild( node );
970987
QVERIFY( myCrs14.writeXml( node, document ) );
@@ -975,6 +992,7 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
975992
node.toElement().elementsByTagName( QStringLiteral( "spatialrefsys" ) ).at( 0 ).toElement().appendChild( proj4Element );
976993
QgsCoordinateReferenceSystem myCrs15;
977994
QVERIFY( myCrs15.readXml( node ) );
995+
myCrs15.saveAsUserCrs( QStringLiteral( "test6" ) );
978996
#if PROJ_VERSION_MAJOR>=6
979997
QCOMPARE( myCrs15.authid(), QStringLiteral( "USER:100009" ) );
980998
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()
13561374
#endif
13571375
}
13581376

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+
13591439
QGSTEST_MAIN( TestQgsCoordinateReferenceSystem )
13601440
#include "testqgscoordinatereferencesystem.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.