Skip to content

Commit b8388ee

Browse files
jef-nmach0
authored andcommittedNov 13, 2011
yet another take on matching proj4string in our database
1 parent f7c3469 commit b8388ee

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -388,27 +388,12 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
388388
long mySrsId = 0;
389389
QgsCoordinateReferenceSystem::RecordMap myRecord;
390390

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-
397391
/*
398392
* - if the above does not match perform a whole text search on proj4 string (if not null)
399393
*/
400394
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
401395
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() )
412397
{
413398
// Ticket #722 - aaronr
414399
// 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
423408
QString lat2Str = "";
424409
myStart1 = myLat1RegExp.indexIn( theProj4String, myStart1 );
425410
myStart2 = myLat2RegExp.indexIn( theProj4String, myStart2 );
426-
if (( myStart1 != -1 ) && ( myStart2 != -1 ) )
411+
if ( myStart1 != -1 && myStart2 != -1 )
427412
{
428413
myLength1 = myLat1RegExp.matchedLength();
429414
myLength2 = myLat2RegExp.matchedLength();
430415
lat1Str = theProj4String.mid( myStart1 + LAT_PREFIX_LEN, myLength1 - LAT_PREFIX_LEN );
431416
lat2Str = theProj4String.mid( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN );
432417
}
433418
// 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 != "" )
435420
{
436421
// Make our new string to check...
437422
QString theProj4StringModified = theProj4String;
@@ -443,34 +428,69 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
443428
theProj4StringModified.replace( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN, lat1Str );
444429
QgsDebugMsg( "trying proj4string match with swapped lat_1,lat_2" );
445430
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-
}
457431
}
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 ) )
459445
{
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=" ) )
466448
{
467-
createFromSrsId( mySrsId );
449+
datum = arg;
468450
}
469451
else
470452
{
471-
mIsValidFlag = false;
453+
sql += delim + arg;
454+
delim = " AND ";
472455
}
473456
}
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+
}
474494
}
475495

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

0 commit comments

Comments
 (0)
Please sign in to comment.