Merge pull request #5892 from nyalldawson/fix_17254

Fix creation of QgsCoordinateReferenceSystem from "user:xxxx" strings
This commit is contained in:
Nyall Dawson 2017-12-18 16:37:15 +11:00 committed by GitHub
commit d998c3ea28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -28,6 +28,7 @@
#include <QRegExp> #include <QRegExp>
#include <QTextStream> #include <QTextStream>
#include <QFile> #include <QFile>
#include <QRegularExpression>
#include "qgsapplication.h" #include "qgsapplication.h"
#include "qgslogger.h" #include "qgslogger.h"
@ -220,26 +221,28 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
sCrsStringLock.unlock(); sCrsStringLock.unlock();
bool result = false; bool result = false;
QRegExp reCrsId( "^(epsg|postgis|internal)\\:(\\d+)$", Qt::CaseInsensitive ); QRegularExpression reCrsId( "^(epsg|postgis|internal|user)\\:(\\d+)$", QRegularExpression::CaseInsensitiveOption );
if ( reCrsId.indexIn( definition ) == 0 ) QRegularExpressionMatch match = reCrsId.match( definition );
if ( match.capturedStart() == 0 )
{ {
QString authName = reCrsId.cap( 1 ).toLower(); QString authName = match.captured( 1 ).toLower();
CrsType type = InternalCrsId; CrsType type = InternalCrsId;
if ( authName == QLatin1String( "epsg" ) ) if ( authName == QLatin1String( "epsg" ) )
type = EpsgCrsId; type = EpsgCrsId;
if ( authName == QLatin1String( "postgis" ) ) if ( authName == QLatin1String( "postgis" ) )
type = PostgisCrsId; type = PostgisCrsId;
long id = reCrsId.cap( 2 ).toLong(); long id = match.captured( 2 ).toLong();
result = createFromId( id, type ); result = createFromId( id, type );
} }
else else
{ {
QRegExp reCrsStr( "^(?:(wkt|proj4)\\:)?(.+)$", Qt::CaseInsensitive ); QRegularExpression reCrsStr( "^(?:(wkt|proj4)\\:)?(.+)$", QRegularExpression::CaseInsensitiveOption );
if ( reCrsStr.indexIn( definition ) == 0 ) 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 //TODO: createFromProj4 used to save to the user database any new CRS
// this behavior was changed in order to separate creation and saving. // 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 // Not sure if it necessary to save it here, should be checked by someone
@ -254,7 +257,7 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
} }
else else
{ {
result = createFromWkt( reCrsStr.cap( 2 ) ); result = createFromWkt( match.captured( 2 ) );
} }
} }
} }

View File

@ -842,6 +842,14 @@ void TestQgsCoordinateReferenceSystem::saveAsUserCrs()
QCOMPARE( userCrs2.srsid(), userCrs.srsid() ); QCOMPARE( userCrs2.srsid(), userCrs.srsid() );
QCOMPARE( userCrs2.authid(), QStringLiteral( "USER:100000" ) ); QCOMPARE( userCrs2.authid(), QStringLiteral( "USER:100000" ) );
QCOMPARE( userCrs2.description(), QStringLiteral( "babies first projection" ) ); 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() void TestQgsCoordinateReferenceSystem::projectWithCustomCrs()