Merge pull request #30816 from m-kuhn/ellipsoid_from_first_layer

Update the project ellipsoid if CRS is taken from first layer
This commit is contained in:
Matthias Kuhn 2019-07-20 10:29:54 +02:00 committed by GitHub
commit 4e0102f3b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 12 deletions

View File

@ -164,9 +164,11 @@ Returns the project's native coordinate reference system.
.. versionadded:: 3.0
%End
void setCrs( const QgsCoordinateReferenceSystem &crs );
void setCrs( const QgsCoordinateReferenceSystem &crs, bool adjustEllipsoid = false );
%Docstring
Sets the project's native coordinate reference system.
If ``adjustEllipsoid`` is set to ``True``, the ellpsoid of this project will be set to
the ellipsoid imposed by the CRS.
.. seealso:: :py:func:`crs`

View File

@ -5498,8 +5498,7 @@ bool QgisApp::fileNew( bool promptToSaveFlag, bool forceBlank )
// set project CRS
const QgsCoordinateReferenceSystem srs = QgsCoordinateReferenceSystem( settings.value( QStringLiteral( "/projections/defaultProjectCrs" ), GEO_EPSG_CRS_AUTHID, QgsSettings::App ).toString() );
// write the projections _proj string_ to project settings
prj->setCrs( srs );
prj->setEllipsoid( srs.ellipsoidAcronym() );
prj->setCrs( srs, true );
/* New Empty Project Created
(before attempting to load custom project templates/filepaths) */

View File

@ -647,15 +647,18 @@ QgsCoordinateReferenceSystem QgsProject::crs() const
return mCrs;
}
void QgsProject::setCrs( const QgsCoordinateReferenceSystem &crs )
void QgsProject::setCrs( const QgsCoordinateReferenceSystem &crs, bool adjustEllipsoid )
{
if ( crs == mCrs )
return;
if ( crs != mCrs )
{
mCrs = crs;
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectionsEnabled" ), crs.isValid() ? 1 : 0 );
setDirty( true );
emit crsChanged();
}
mCrs = crs;
writeEntry( QStringLiteral( "SpatialRefSys" ), QStringLiteral( "/ProjectionsEnabled" ), crs.isValid() ? 1 : 0 );
setDirty( true );
emit crsChanged();
if ( adjustEllipsoid )
setEllipsoid( crs.ellipsoidAcronym() );
}
QString QgsProject::ellipsoid() const
@ -668,6 +671,9 @@ QString QgsProject::ellipsoid() const
void QgsProject::setEllipsoid( const QString &ellipsoid )
{
if ( ellipsoid == readEntry( QStringLiteral( "Measure" ), QStringLiteral( "/Ellipsoid" ) ) )
return;
writeEntry( QStringLiteral( "Measure" ), QStringLiteral( "/Ellipsoid" ), ellipsoid );
emit ellipsoidChanged( ellipsoid );
}

View File

@ -211,11 +211,14 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
/**
* Sets the project's native coordinate reference system.
* If \a adjustEllipsoid is set to TRUE, the ellpsoid of this project will be set to
* the ellipsoid imposed by the CRS.
*
* \see crs()
* \see setEllipsoid()
* \since QGIS 3.0
*/
void setCrs( const QgsCoordinateReferenceSystem &crs );
void setCrs( const QgsCoordinateReferenceSystem &crs, bool adjustEllipsoid = false );
/**
* Returns a proj string representing the project's ellipsoid setting, e.g., "WGS84".

View File

@ -115,7 +115,7 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()
switch ( projectCrsBehavior )
{
case QgsGui::UseCrsOfFirstLayerAdded:
QgsProject::instance()->setCrs( mFirstCRS );
QgsProject::instance()->setCrs( mFirstCRS, true );
break;
case QgsGui::UsePresetCrs:

View File

@ -44,6 +44,7 @@ class TestQgsProject : public QObject
void testLocalFiles();
void testLocalUrlFiles();
void testReadFlags();
void testSetGetCrs();
};
void TestQgsProject::init()
@ -458,6 +459,73 @@ void TestQgsProject::testReadFlags()
QCOMPARE( qobject_cast< QgsVectorLayer * >( layers.value( QStringLiteral( "polys20170310142652234" ) ) )->renderer()->type(), QStringLiteral( "categorizedSymbol" ) );
}
void TestQgsProject::testSetGetCrs()
{
QgsProject p;
// Set 4326
// - CRS changes
// - ellipsoid stays as NONE
QSignalSpy crsChangedSpy( &p, &QgsProject::crsChanged );
QSignalSpy ellipsoidChangedSpy( &p, &QgsProject::ellipsoidChanged );
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
QCOMPARE( crsChangedSpy.count(), 1 );
QCOMPARE( ellipsoidChangedSpy.count(), 0 );
QCOMPARE( p.crs(), QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
QCOMPARE( p.ellipsoid(), QStringLiteral( "NONE" ) );
crsChangedSpy.clear();
ellipsoidChangedSpy.clear();
// Set 21781
// - CRS changes
// - ellipsoid stays as NONE
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 21781 ) );
QCOMPARE( crsChangedSpy.count(), 1 );
QCOMPARE( ellipsoidChangedSpy.count(), 0 );
QCOMPARE( p.crs(), QgsCoordinateReferenceSystem::fromEpsgId( 21781 ) );
QCOMPARE( p.ellipsoid(), QStringLiteral( "NONE" ) );
crsChangedSpy.clear();
ellipsoidChangedSpy.clear();
// Set 21781 again, including adjustEllipsoid flag
// - CRS changes
// - ellipsoid changes to Bessel
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 21781 ), true );
QCOMPARE( crsChangedSpy.count(), 0 );
QCOMPARE( ellipsoidChangedSpy.count(), 1 );
QCOMPARE( p.crs(), QgsCoordinateReferenceSystem::fromEpsgId( 21781 ) );
QCOMPARE( p.ellipsoid(), QStringLiteral( "bessel" ) );
crsChangedSpy.clear();
ellipsoidChangedSpy.clear();
// Set 2056, including adjustEllipsoid flag
// - CRS changes
// - ellipsoid stays
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 2056 ), true );
QCOMPARE( crsChangedSpy.count(), 1 );
QCOMPARE( ellipsoidChangedSpy.count(), 0 );
QCOMPARE( p.crs(), QgsCoordinateReferenceSystem::fromEpsgId( 2056 ) );
QCOMPARE( p.ellipsoid(), QStringLiteral( "bessel" ) );
crsChangedSpy.clear();
ellipsoidChangedSpy.clear();
}
QGSTEST_MAIN( TestQgsProject )
#include "testqgsproject.moc"