@@ -388,27 +388,12 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
388
388
long mySrsId = 0 ;
389
389
QgsCoordinateReferenceSystem::RecordMap myRecord;
390
390
391
- // *** Matching on descriptions feels iffy. Different projs can have same description. Homann ***
392
- // if ( !mDescription.trimmed().isEmpty() )
393
- // {
394
- // myRecord = getRecord( "select * from tbl_srs where description=" + quotedValue( mDescription.trimmed() ) );
395
- // }
396
-
397
391
/*
398
392
* - if the above does not match perform a whole text search on proj4 string (if not null)
399
393
*/
400
394
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
401
395
myRecord = getRecord ( " select * from tbl_srs where parameters=" + quotedValue ( theProj4String.trimmed () ) );
402
- if ( !myRecord.empty () )
403
- {
404
- mySrsId = myRecord[" srs_id" ].toLong ();
405
- QgsDebugMsg ( " proj4string match search for srsid returned srsid: " + QString::number ( mySrsId ) );
406
- if ( mySrsId > 0 )
407
- {
408
- createFromSrsId ( mySrsId );
409
- }
410
- }
411
- else
396
+ if ( myRecord.empty () )
412
397
{
413
398
// Ticket #722 - aaronr
414
399
// Check if we can swap the lat_1 and lat_2 params (if they exist) to see if we match...
@@ -423,15 +408,15 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
423
408
QString lat2Str = " " ;
424
409
myStart1 = myLat1RegExp.indexIn ( theProj4String, myStart1 );
425
410
myStart2 = myLat2RegExp.indexIn ( theProj4String, myStart2 );
426
- if (( myStart1 != -1 ) && ( myStart2 != -1 ) )
411
+ if ( myStart1 != -1 && myStart2 != -1 )
427
412
{
428
413
myLength1 = myLat1RegExp.matchedLength ();
429
414
myLength2 = myLat2RegExp.matchedLength ();
430
415
lat1Str = theProj4String.mid ( myStart1 + LAT_PREFIX_LEN, myLength1 - LAT_PREFIX_LEN );
431
416
lat2Str = theProj4String.mid ( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN );
432
417
}
433
418
// If we found the lat_1 and lat_2 we need to swap and check to see if we can find it...
434
- if (( lat1Str != " " ) && ( lat2Str != " " ) )
419
+ if ( lat1Str != " " && lat2Str != " " )
435
420
{
436
421
// Make our new string to check...
437
422
QString theProj4StringModified = theProj4String;
@@ -443,34 +428,69 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
443
428
theProj4StringModified.replace ( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN, lat1Str );
444
429
QgsDebugMsg ( " trying proj4string match with swapped lat_1,lat_2" );
445
430
myRecord = getRecord ( " select * from tbl_srs where parameters=" + quotedValue ( theProj4StringModified.trimmed () ) );
446
- if ( !myRecord.empty () )
447
- {
448
- // Success! We have found the proj string by swapping the lat_1 and lat_2
449
- setProj4String ( theProj4StringModified );
450
- mySrsId = myRecord[" srs_id" ].toLong ();
451
- QgsDebugMsg ( " proj4string match search for srsid returned srsid: " + QString::number ( mySrsId ) );
452
- if ( mySrsId > 0 )
453
- {
454
- createFromSrsId ( mySrsId );
455
- }
456
- }
457
431
}
458
- else
432
+ }
433
+
434
+ if ( myRecord.empty () )
435
+ {
436
+ // match all arameters individually:
437
+ // - order of parameters doesn't matter
438
+ // - found definition may have more parameters (like +towgs84 in GDAL)
439
+ // - retry without datum, if no match is found (looks like +datum<>WGS84 was dropped in GDAL)
440
+
441
+ QString sql = " SELECT * FROM tbl_srs WHERE " ;
442
+ QString delim = " " ;
443
+ QString datum;
444
+ foreach ( QString param, theProj4String.split ( " " , QString::SkipEmptyParts ) )
459
445
{
460
- // Last ditch attempt to piece together what we know of the projection to find a match...
461
- QgsDebugMsg ( " globbing search for srsid from this proj string" );
462
- setProj4String ( theProj4String );
463
- mySrsId = findMatchingProj ();
464
- QgsDebugMsg ( " globbing search for srsid returned srsid: " + QString::number ( mySrsId ) );
465
- if ( mySrsId > 0 )
446
+ QString arg = QString ( " ' '||parameters||' ' LIKE %1" ).arg ( quotedValue ( QString ( " % %1 %" ).arg ( param ) ) );
447
+ if ( param.startsWith ( " +datum=" ) )
466
448
{
467
- createFromSrsId ( mySrsId ) ;
449
+ datum = arg ;
468
450
}
469
451
else
470
452
{
471
- mIsValidFlag = false ;
453
+ sql += delim + arg;
454
+ delim = " AND " ;
472
455
}
473
456
}
457
+
458
+ if ( !datum.isEmpty () )
459
+ {
460
+ myRecord = getRecord ( sql + delim + datum );
461
+ }
462
+
463
+ if ( myRecord.empty () )
464
+ {
465
+ // datum might have disappeared in definition - retry without it
466
+ myRecord = getRecord ( sql );
467
+ }
468
+ }
469
+
470
+ if ( !myRecord.empty () )
471
+ {
472
+ mySrsId = myRecord[" srs_id" ].toLong ();
473
+ QgsDebugMsg ( " proj4string param match search for srsid returned srsid: " + QString::number ( mySrsId ) );
474
+ if ( mySrsId > 0 )
475
+ {
476
+ createFromSrsId ( mySrsId );
477
+ }
478
+ }
479
+ else
480
+ {
481
+ // Last ditch attempt to piece together what we know of the projection to find a match...
482
+ QgsDebugMsg ( " globbing search for srsid from this proj string" );
483
+ setProj4String ( theProj4String );
484
+ mySrsId = findMatchingProj ();
485
+ QgsDebugMsg ( " globbing search for srsid returned srsid: " + QString::number ( mySrsId ) );
486
+ if ( mySrsId > 0 )
487
+ {
488
+ createFromSrsId ( mySrsId );
489
+ }
490
+ else
491
+ {
492
+ mIsValidFlag = false ;
493
+ }
474
494
}
475
495
476
496
// if we failed to look up the projection in database, don't worry. we can still use it :)
0 commit comments