Skip to content

Commit

Permalink
yet another take on matching proj4string in our database
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n authored and mach0 committed Nov 13, 2011
1 parent f7c3469 commit b8388ee
Showing 1 changed file with 58 additions and 38 deletions.
96 changes: 58 additions & 38 deletions src/core/qgscoordinatereferencesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,27 +388,12 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
long mySrsId = 0;
QgsCoordinateReferenceSystem::RecordMap myRecord;

// *** Matching on descriptions feels iffy. Different projs can have same description. Homann ***
// if ( !mDescription.trimmed().isEmpty() )
//{
// myRecord = getRecord( "select * from tbl_srs where description=" + quotedValue( mDescription.trimmed() ) );
//}

/*
* - if the above does not match perform a whole text search on proj4 string (if not null)
*/
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( theProj4String.trimmed() ) );
if ( !myRecord.empty() )
{
mySrsId = myRecord["srs_id"].toLong();
QgsDebugMsg( "proj4string match search for srsid returned srsid: " + QString::number( mySrsId ) );
if ( mySrsId > 0 )
{
createFromSrsId( mySrsId );
}
}
else
if ( myRecord.empty() )
{
// Ticket #722 - aaronr
// Check if we can swap the lat_1 and lat_2 params (if they exist) to see if we match...
Expand All @@ -423,15 +408,15 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
QString lat2Str = "";
myStart1 = myLat1RegExp.indexIn( theProj4String, myStart1 );
myStart2 = myLat2RegExp.indexIn( theProj4String, myStart2 );
if (( myStart1 != -1 ) && ( myStart2 != -1 ) )
if ( myStart1 != -1 && myStart2 != -1 )
{
myLength1 = myLat1RegExp.matchedLength();
myLength2 = myLat2RegExp.matchedLength();
lat1Str = theProj4String.mid( myStart1 + LAT_PREFIX_LEN, myLength1 - LAT_PREFIX_LEN );
lat2Str = theProj4String.mid( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN );
}
// If we found the lat_1 and lat_2 we need to swap and check to see if we can find it...
if (( lat1Str != "" ) && ( lat2Str != "" ) )
if ( lat1Str != "" && lat2Str != "" )
{
// Make our new string to check...
QString theProj4StringModified = theProj4String;
Expand All @@ -443,34 +428,69 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
theProj4StringModified.replace( myStart2 + LAT_PREFIX_LEN, myLength2 - LAT_PREFIX_LEN, lat1Str );
QgsDebugMsg( "trying proj4string match with swapped lat_1,lat_2" );
myRecord = getRecord( "select * from tbl_srs where parameters=" + quotedValue( theProj4StringModified.trimmed() ) );
if ( !myRecord.empty() )
{
// Success! We have found the proj string by swapping the lat_1 and lat_2
setProj4String( theProj4StringModified );
mySrsId = myRecord["srs_id"].toLong();
QgsDebugMsg( "proj4string match search for srsid returned srsid: " + QString::number( mySrsId ) );
if ( mySrsId > 0 )
{
createFromSrsId( mySrsId );
}
}
}
else
}

if ( myRecord.empty() )
{
// match all arameters individually:
// - order of parameters doesn't matter
// - found definition may have more parameters (like +towgs84 in GDAL)
// - retry without datum, if no match is found (looks like +datum<>WGS84 was dropped in GDAL)

QString sql = "SELECT * FROM tbl_srs WHERE ";
QString delim = "";
QString datum;
foreach( QString param, theProj4String.split( " ", QString::SkipEmptyParts ) )
{
// Last ditch attempt to piece together what we know of the projection to find a match...
QgsDebugMsg( "globbing search for srsid from this proj string" );
setProj4String( theProj4String );
mySrsId = findMatchingProj();
QgsDebugMsg( "globbing search for srsid returned srsid: " + QString::number( mySrsId ) );
if ( mySrsId > 0 )
QString arg = QString( "' '||parameters||' ' LIKE %1" ).arg( quotedValue( QString( "% %1 %" ).arg( param ) ) );
if ( param.startsWith( "+datum=" ) )
{
createFromSrsId( mySrsId );
datum = arg;
}
else
{
mIsValidFlag = false;
sql += delim + arg;
delim = " AND ";
}
}

if ( !datum.isEmpty() )
{
myRecord = getRecord( sql + delim + datum );
}

if ( myRecord.empty() )
{
// datum might have disappeared in definition - retry without it
myRecord = getRecord( sql );
}
}

if ( !myRecord.empty() )
{
mySrsId = myRecord["srs_id"].toLong();
QgsDebugMsg( "proj4string param match search for srsid returned srsid: " + QString::number( mySrsId ) );
if ( mySrsId > 0 )
{
createFromSrsId( mySrsId );
}
}
else
{
// Last ditch attempt to piece together what we know of the projection to find a match...
QgsDebugMsg( "globbing search for srsid from this proj string" );
setProj4String( theProj4String );
mySrsId = findMatchingProj();
QgsDebugMsg( "globbing search for srsid returned srsid: " + QString::number( mySrsId ) );
if ( mySrsId > 0 )
{
createFromSrsId( mySrsId );
}
else
{
mIsValidFlag = false;
}
}

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

0 comments on commit b8388ee

Please sign in to comment.