Skip to content

Commit

Permalink
Correctly handle crs restoration after an invalid crs was written to xml
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 24, 2019
1 parent e49b07c commit 8350ad6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -1655,11 +1655,12 @@ bool QgsCoordinateReferenceSystem::readXml( const QDomNode &node )
{
bool initialized = false;

long srsid = srsNode.namedItem( QStringLiteral( "srsid" ) ).toElement().text().toLong();
bool ok = false;
long srsid = srsNode.namedItem( QStringLiteral( "srsid" ) ).toElement().text().toLong( &ok );

QDomNode myNode;

if ( srsid < USER_CRS_START_ID )
if ( ok && srsid > 0 && srsid < USER_CRS_START_ID )
{
myNode = srsNode.namedItem( QStringLiteral( "authid" ) );
if ( !myNode.isNull() )
Expand Down Expand Up @@ -1688,13 +1689,13 @@ bool QgsCoordinateReferenceSystem::readXml( const QDomNode &node )
if ( !initialized )
{
myNode = srsNode.namedItem( QStringLiteral( "proj4" ) );
const QString proj4 = myNode.toElement().text();

if ( !createFromProj4( myNode.toElement().text() ) )
if ( !createFromProj4( proj4 ) )
{
// Setting from elements one by one

myNode = srsNode.namedItem( QStringLiteral( "proj4" ) );
setProj4String( myNode.toElement().text() );
if ( !proj4.trimmed().isEmpty() )
setProj4String( myNode.toElement().text() );

myNode = srsNode.namedItem( QStringLiteral( "srsid" ) );
setInternalId( myNode.toElement().text().toLong() );
Expand Down Expand Up @@ -1731,7 +1732,7 @@ bool QgsCoordinateReferenceSystem::readXml( const QDomNode &node )
// this behavior was changed in order to separate creation and saving.
// Not sure if it necessary to save it here, should be checked by someone
// familiar with the code (should also give a more descriptive name to the generated CRS)
if ( d->mSrsId == 0 )
if ( isValid() && d->mSrsId == 0 )
{
QString myName = QStringLiteral( " * %1 (%2)" )
.arg( QObject::tr( "Generated CRS", "A CRS automatically generated from layer info get this prefix for description" ),
Expand Down
13 changes: 13 additions & 0 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Expand Up @@ -634,6 +634,19 @@ void TestQgsCoordinateReferenceSystem::readWriteXml()
QDomDocument document( "test" );
QDomElement node = document.createElement( QStringLiteral( "crs" ) );
document.appendChild( node );

// start with invalid node
QgsCoordinateReferenceSystem badCrs;
QVERIFY( !badCrs.readXml( node ) );
QVERIFY( !badCrs.isValid() );
QDomElement badSrsElement = document.createElement( QStringLiteral( "spatialrefsys" ) );
QDomElement badNode = document.createElement( QStringLiteral( "crs" ) );
document.appendChild( badNode );
badNode.appendChild( badSrsElement );
// should return true, because it's ok to write/read invalid crs to xml
QVERIFY( badCrs.readXml( badNode ) );
QVERIFY( !badCrs.isValid() );

QVERIFY( myCrs.writeXml( node, document ) );
QgsCoordinateReferenceSystem myCrs2;
QVERIFY( myCrs2.readXml( node ) );
Expand Down

0 comments on commit 8350ad6

Please sign in to comment.