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