Skip to content

Commit

Permalink
Merge pull request #5892 from nyalldawson/fix_17254
Browse files Browse the repository at this point in the history
 Fix creation of QgsCoordinateReferenceSystem from "user:xxxx" strings
  • Loading branch information
nyalldawson committed Dec 18, 2017
2 parents 0a6eb12 + 2e6386e commit d998c3e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -28,6 +28,7 @@
#include <QRegExp>
#include <QTextStream>
#include <QFile>
#include <QRegularExpression>

#include "qgsapplication.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -220,26 +221,28 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
sCrsStringLock.unlock();

bool result = false;
QRegExp reCrsId( "^(epsg|postgis|internal)\\:(\\d+)$", Qt::CaseInsensitive );
if ( reCrsId.indexIn( definition ) == 0 )
QRegularExpression reCrsId( "^(epsg|postgis|internal|user)\\:(\\d+)$", QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match = reCrsId.match( definition );
if ( match.capturedStart() == 0 )
{
QString authName = reCrsId.cap( 1 ).toLower();
QString authName = match.captured( 1 ).toLower();
CrsType type = InternalCrsId;
if ( authName == QLatin1String( "epsg" ) )
type = EpsgCrsId;
if ( authName == QLatin1String( "postgis" ) )
type = PostgisCrsId;
long id = reCrsId.cap( 2 ).toLong();
long id = match.captured( 2 ).toLong();
result = createFromId( id, type );
}
else
{
QRegExp reCrsStr( "^(?:(wkt|proj4)\\:)?(.+)$", Qt::CaseInsensitive );
if ( reCrsStr.indexIn( definition ) == 0 )
QRegularExpression reCrsStr( "^(?:(wkt|proj4)\\:)?(.+)$", QRegularExpression::CaseInsensitiveOption );
match = reCrsStr.match( definition );
if ( match.capturedStart() == 0 )
{
if ( reCrsStr.cap( 1 ).toLower() == QLatin1String( "proj4" ) )
if ( match.captured( 1 ).toLower() == QLatin1String( "proj4" ) )
{
result = createFromProj4( reCrsStr.cap( 2 ) );
result = createFromProj4( match.captured( 2 ) );
//TODO: createFromProj4 used to save to the user database any new CRS
// 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
Expand All @@ -254,7 +257,7 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
}
else
{
result = createFromWkt( reCrsStr.cap( 2 ) );
result = createFromWkt( match.captured( 2 ) );
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/testqgscoordinatereferencesystem.cpp
Expand Up @@ -842,6 +842,14 @@ void TestQgsCoordinateReferenceSystem::saveAsUserCrs()
QCOMPARE( userCrs2.srsid(), userCrs.srsid() );
QCOMPARE( userCrs2.authid(), QStringLiteral( "USER:100000" ) );
QCOMPARE( userCrs2.description(), QStringLiteral( "babies first projection" ) );

// createFromString with user crs
QgsCoordinateReferenceSystem userCrs3;
userCrs3.createFromString( QStringLiteral( "USER:100000" ) );
QVERIFY( userCrs3.isValid() );
QCOMPARE( userCrs3.authid(), QString( "USER:100000" ) );
QCOMPARE( userCrs3.toProj4(), madeUpProjection );
QCOMPARE( userCrs3.description(), QStringLiteral( "babies first projection" ) );
}

void TestQgsCoordinateReferenceSystem::projectWithCustomCrs()
Expand Down

0 comments on commit d998c3e

Please sign in to comment.