Skip to content

Commit 59a4db6

Browse files
etienneskyjef-n
authored andcommittedMay 28, 2012
make sure QgsCoordinateReferenceSystem::setProj4String() and createFromProj4() trim the input proj4 string
1 parent 6cff784 commit 59a4db6

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,13 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
396396
// +proj=lcc +lat_1=46.8 +lat_0=46.8 +lon_0=2.337229166666664 +k_0=0.99987742
397397
// +x_0=600000 +y_0=2200000 +a=6378249.2 +b=6356515.000000472 +units=m +no_defs
398398
//
399-
QgsDebugMsg( "proj4: " + theProj4String );
399+
QString myProj4String = theProj4String.trimmed();
400+
QgsDebugMsg( "proj4: " + myProj4String );
400401
mIsValidFlag = false;
401402
mWkt.clear();
402403

403404
QRegExp myProjRegExp( "\\+proj=(\\S+)" );
404-
int myStart = myProjRegExp.indexIn( theProj4String );
405+
int myStart = myProjRegExp.indexIn( myProj4String );
405406
if ( myStart == -1 )
406407
{
407408
QgsDebugMsg( "proj string supplied has no +proj argument" );
@@ -411,7 +412,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
411412
mProjectionAcronym = myProjRegExp.cap( 1 );
412413

413414
QRegExp myEllipseRegExp( "\\+ellps=(\\S+)" );
414-
myStart = myEllipseRegExp.indexIn( theProj4String );
415+
myStart = myEllipseRegExp.indexIn( myProj4String );
415416
if ( myStart == -1 )
416417
{
417418
QgsDebugMsg( "proj string supplied has no +ellps argument" );
@@ -423,7 +424,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
423424
}
424425

425426
QRegExp myAxisRegExp( "\\+a=(\\S+)" );
426-
myStart = myAxisRegExp.indexIn( theProj4String );
427+
myStart = myAxisRegExp.indexIn( myProj4String );
427428
if ( myStart == -1 )
428429
{
429430
QgsDebugMsg( "proj string supplied has no +a argument" );
@@ -444,7 +445,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
444445
* - if the above does not match perform a whole text search on proj4 string (if not null)
445446
*/
446447
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
447-
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( theProj4String.trimmed() ) + " order by deprecated" );
448+
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( myProj4String ) + " order by deprecated" );
448449
if ( myRecord.empty() )
449450
{
450451
// Ticket #722 - aaronr
@@ -458,20 +459,20 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
458459
int myLength2 = 0;
459460
QString lat1Str = "";
460461
QString lat2Str = "";
461-
myStart1 = myLat1RegExp.indexIn( theProj4String, myStart1 );
462-
myStart2 = myLat2RegExp.indexIn( theProj4String, myStart2 );
462+
myStart1 = myLat1RegExp.indexIn( myProj4String, myStart1 );
463+
myStart2 = myLat2RegExp.indexIn( myProj4String, myStart2 );
463464
if ( myStart1 != -1 && myStart2 != -1 )
464465
{
465466
myLength1 = myLat1RegExp.matchedLength();
466467
myLength2 = myLat2RegExp.matchedLength();
467-
lat1Str = theProj4String.mid( myStart1 + LAT_PREFIX_LEN, myLength1 - LAT_PREFIX_LEN );
468-
lat2Str = theProj4String.mid( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN );
468+
lat1Str = myProj4String.mid( myStart1 + LAT_PREFIX_LEN, myLength1 - LAT_PREFIX_LEN );
469+
lat2Str = myProj4String.mid( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN );
469470
}
470471
// If we found the lat_1 and lat_2 we need to swap and check to see if we can find it...
471472
if ( lat1Str != "" && lat2Str != "" )
472473
{
473474
// Make our new string to check...
474-
QString theProj4StringModified = theProj4String;
475+
QString theProj4StringModified = myProj4String;
475476
// First just swap in the lat_2 value for lat_1 value
476477
theProj4StringModified.replace( myStart1 + LAT_PREFIX_LEN, myLength1 - LAT_PREFIX_LEN, lat2Str );
477478
// Now we have to find the lat_2 location again since it has potentially moved...
@@ -497,7 +498,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
497498
// split on spaces followed by a plus sign (+) to deal
498499
// also with parameters containing spaces (e.g. +nadgrids)
499500
// make sure result is trimmed (#5598)
500-
foreach( QString param, theProj4String.split( QRegExp( "\\s+(?=\\+)" ), QString::SkipEmptyParts ) )
501+
foreach( QString param, myProj4String.split( QRegExp( "\\s+(?=\\+)" ), QString::SkipEmptyParts ) )
501502
{
502503
QString arg = QString( "' '||parameters||' ' LIKE %1" ).arg( quotedValue( QString( "% %1 %" ).arg( param.trimmed() ) ) );
503504
if ( param.startsWith( "+datum=" ) )
@@ -536,7 +537,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
536537
{
537538
// Last ditch attempt to piece together what we know of the projection to find a match...
538539
QgsDebugMsg( "globbing search for srsid from this proj string" );
539-
setProj4String( theProj4String );
540+
setProj4String( myProj4String );
540541
mySrsId = findMatchingProj();
541542
QgsDebugMsg( "globbing search for srsid returned srsid: " + QString::number( mySrsId ) );
542543
if ( mySrsId > 0 )
@@ -553,7 +554,7 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
553554
if ( !mIsValidFlag )
554555
{
555556
QgsDebugMsg( "Projection is not found in databases." );
556-
setProj4String( theProj4String );
557+
setProj4String( myProj4String );
557558

558559
// Is the SRS is valid now, we know it's a decent +proj string that can be entered into the srs.db
559560
if ( mIsValidFlag )
@@ -815,7 +816,7 @@ void QgsCoordinateReferenceSystem::setProj4String( QString theProj4String )
815816
OSRDestroySpatialReference( mCRS );
816817
mCRS = OSRNewSpatialReference( NULL );
817818
mIsValidFlag =
818-
OSRImportFromProj4( mCRS, theProj4String.toLatin1().constData() )
819+
OSRImportFromProj4( mCRS, theProj4String.trimmed().toLatin1().constData() )
819820
== OGRERR_NONE;
820821
mWkt.clear();
821822
setMapUnits();
@@ -1516,6 +1517,7 @@ int QgsCoordinateReferenceSystem::syncDb()
15161517
if ( proj4.startsWith( input ) )
15171518
{
15181519
proj4 = proj4.mid( input.size() );
1520+
proj4 = proj4.trimmed();
15191521
}
15201522
}
15211523
else

0 commit comments

Comments
 (0)
Please sign in to comment.