Skip to content

Commit 3c67104

Browse files
committed
yet another take on matching proj4string in our database
1 parent 76f9c4d commit 3c67104

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

src/core/qgscoordinatereferencesystem.cpp

+58-38
Original file line numberDiff line numberDiff line change
@@ -392,27 +392,12 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
392392
long mySrsId = 0;
393393
QgsCoordinateReferenceSystem::RecordMap myRecord;
394394

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-
401395
/*
402396
* - if the above does not match perform a whole text search on proj4 string (if not null)
403397
*/
404398
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
405399
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() )
416401
{
417402
// Ticket #722 - aaronr
418403
// 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
427412
QString lat2Str = "";
428413
myStart1 = myLat1RegExp.indexIn( theProj4String, myStart1 );
429414
myStart2 = myLat2RegExp.indexIn( theProj4String, myStart2 );
430-
if (( myStart1 != -1 ) && ( myStart2 != -1 ) )
415+
if ( myStart1 != -1 && myStart2 != -1 )
431416
{
432417
myLength1 = myLat1RegExp.matchedLength();
433418
myLength2 = myLat2RegExp.matchedLength();
434419
lat1Str = theProj4String.mid( myStart1 + LAT_PREFIX_LEN, myLength1 - LAT_PREFIX_LEN );
435420
lat2Str = theProj4String.mid( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN );
436421
}
437422
// 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 != "" )
439424
{
440425
// Make our new string to check...
441426
QString theProj4StringModified = theProj4String;
@@ -447,34 +432,69 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
447432
theProj4StringModified.replace( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN, lat1Str );
448433
QgsDebugMsg( "trying proj4string match with swapped lat_1,lat_2" );
449434
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-
}
461435
}
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 ) )
463449
{
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=" ) )
470452
{
471-
createFromSrsId( mySrsId );
453+
datum = arg;
472454
}
473455
else
474456
{
475-
mIsValidFlag = false;
457+
sql += delim + arg;
458+
delim = " AND ";
476459
}
477460
}
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+
}
478498
}
479499

480500
// if we failed to look up the projection in database, don't worry. we can still use it :)

0 commit comments

Comments
 (0)