Skip to content

Commit 0848999

Browse files
author
jef
committedApr 5, 2010
don't implictly add the same CRS over and over
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13255 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

1 file changed

+63
-46
lines changed

1 file changed

+63
-46
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ bool QgsCoordinateReferenceSystem::loadFromDb( QString db, QString expression, Q
190190
int myResult;
191191
//check the db is available
192192
myResult = openDb( db, &myDatabase );
193-
if ( myResult )
193+
if ( myResult != SQLITE_OK )
194194
{
195195
QgsDebugMsg( "failed : " + db + " could not be opened!" );
196196
return mIsValidFlag;
@@ -266,7 +266,7 @@ bool QgsCoordinateReferenceSystem::createFromWkt( QString theWkt )
266266
char *proj4src = NULL;
267267
OSRExportToProj4( mCRS, &proj4src );
268268

269-
//now that we have the proj4string, delegate to createFromProj4String so
269+
//now that we have the proj4string, delegate to createFromProj4 so
270270
// that we can try to fill in the remaining class members...
271271
//create from Proj will set the isValidFlag
272272
createFromProj4( QString( proj4src ) );
@@ -330,13 +330,13 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
330330
}
331331

332332
/*
333-
* We try to match the proj string to and srsid using the following logic:
334-
*
335-
* - perform a whole text search on srs name (if not null). The srs name will
336-
* have been set if this method has been delegated to from createFromWkt.
337-
* Normally we wouldnt expect this to work, but its worth trying first
338-
* as its quicker than methods below..
339-
*/
333+
* We try to match the proj string to and srsid using the following logic:
334+
*
335+
* - perform a whole text search on srs name (if not null). The srs name will
336+
* have been set if this method has been delegated to from createFromWkt.
337+
* Normally we wouldnt expect this to work, but its worth trying first
338+
* as its quicker than methods below..
339+
*/
340340
long mySrsId = 0;
341341
QgsCoordinateReferenceSystem::RecordMap myRecord;
342342

@@ -347,8 +347,8 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
347347
//}
348348

349349
/*
350-
* - if the above does not match perform a whole text search on proj4 string (if not null)
351-
*/
350+
* - if the above does not match perform a whole text search on proj4 string (if not null)
351+
*/
352352
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
353353
myRecord = getRecord( "select * from tbl_srs where parameters='" + theProj4String.trimmed() + "'" );
354354
if ( !myRecord.empty() )
@@ -430,30 +430,44 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
430430
{
431431
QgsDebugMsg( "Projection is not found in databases." );
432432
setProj4String( theProj4String );
433+
433434
// Is the SRS is valid now, we know it's a decent +proj string that can be entered into the srs.db
434435
if ( mIsValidFlag )
435436
{
436-
// Try to save. If not possible, set to invalid. Problems on read only systems?
437-
QgsDebugMsg( "Projection appears to be valid. Save to database!" );
438-
mIsValidFlag = saveAsUserCRS();
439-
// The srsid is not set, we should do that now.
440-
if ( mIsValidFlag )
437+
// but the proj.4 parsed string might already be in our database
438+
myRecord = getRecord( "select * from tbl_srs where parameters='" + toProj4() + "'" );
439+
if ( myRecord.empty() )
441440
{
442-
myRecord = getRecord( "select * from tbl_srs where parameters='" + theProj4String.trimmed() + "'" );
443-
if ( !myRecord.empty() )
444-
{
445-
mySrsId = myRecord["srs_id"].toLong();
446-
QgsDebugMsg( "proj4string match search for srsid returned srsid: " + QString::number( mySrsId ) );
447-
if ( mySrsId > 0 )
448-
{
449-
createFromSrsId( mySrsId );
450-
}
451-
else
452-
{
453-
QgsDebugMsg( "Couldn't find newly added proj string?" );
454-
mIsValidFlag = false;
455-
}
456-
}
441+
// It's not, so try to add it
442+
QgsDebugMsg( "Projection appears to be valid. Save to database!" );
443+
mIsValidFlag = saveAsUserCRS();
444+
445+
if ( mIsValidFlag )
446+
{
447+
// but validate that it's there afterwards
448+
myRecord = getRecord( "select * from tbl_srs where parameters='" + toProj4() + "'" );
449+
}
450+
}
451+
452+
if ( !myRecord.empty() )
453+
{
454+
// take the srid from the record
455+
mySrsId = myRecord["srs_id"].toLong();
456+
QgsDebugMsg( "proj4string match search for srsid returned srsid: " + QString::number( mySrsId ) );
457+
if ( mySrsId > 0 )
458+
{
459+
createFromSrsId( mySrsId );
460+
}
461+
else
462+
{
463+
QgsDebugMsg( QString( "invalid srid %1 found" ).arg( mySrsId ) );
464+
mIsValidFlag = false;
465+
}
466+
}
467+
else
468+
{
469+
QgsDebugMsg( "Couldn't find newly added proj string?" );
470+
mIsValidFlag = false;
457471
}
458472
}
459473
}
@@ -487,7 +501,7 @@ QgsCoordinateReferenceSystem::RecordMap QgsCoordinateReferenceSystem::getRecord(
487501

488502
//check the db is available
489503
myResult = openDb( myDatabaseFileName, &myDatabase );
490-
if ( myResult )
504+
if ( myResult != SQLITE_OK )
491505
{
492506
return myMap;
493507
}
@@ -523,7 +537,7 @@ QgsCoordinateReferenceSystem::RecordMap QgsCoordinateReferenceSystem::getRecord(
523537

524538
//check the db is available
525539
myResult = openDb( myDatabaseFileName, &myDatabase );
526-
if ( myResult )
540+
if ( myResult != SQLITE_OK )
527541
{
528542
return myMap;
529543
}
@@ -795,7 +809,7 @@ long QgsCoordinateReferenceSystem::findMatchingProj()
795809

796810
//check the db is available
797811
myResult = openDb( myDatabaseFileName, &myDatabase );
798-
if ( myResult )
812+
if ( myResult != SQLITE_OK )
799813
{
800814
return 0;
801815
}
@@ -834,7 +848,7 @@ long QgsCoordinateReferenceSystem::findMatchingProj()
834848
myDatabaseFileName = QgsApplication::qgisUserDbFilePath();
835849
//check the db is available
836850
myResult = openDb( myDatabaseFileName, &myDatabase );
837-
if ( myResult )
851+
if ( myResult != SQLITE_OK )
838852
{
839853
return 0;
840854
}
@@ -1138,7 +1152,7 @@ int QgsCoordinateReferenceSystem::openDb( QString path, sqlite3 **db )
11381152
QgsDebugMsgLevel( "path = " + path, 3 );
11391153
int myResult = sqlite3_open( path.toUtf8().data(), db );
11401154

1141-
if ( myResult )
1155+
if ( myResult != SQLITE_OK )
11421156
{
11431157
QgsDebugMsg( "Can't open database: " + QString( sqlite3_errmsg( *db ) ) );
11441158
// XXX This will likely never happen since on open, sqlite creates the
@@ -1201,7 +1215,6 @@ QString QgsCoordinateReferenceSystem::validationHint()
12011215

12021216
bool QgsCoordinateReferenceSystem::saveAsUserCRS()
12031217
{
1204-
12051218
if ( ! mIsValidFlag )
12061219
{
12071220
QgsDebugMsg( "Can't save an invalid CRS!" );
@@ -1219,17 +1232,21 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
12191232
//the autoinc related system tables.
12201233
if ( getRecordCount() == 0 )
12211234
{
1222-
mySql = QString( "insert into tbl_srs (srs_id,description,projection_acronym,ellipsoid_acronym,parameters,is_geo) " )
1223-
+ " values (" + QString::number( USER_CRS_START_ID ) + ",'"
1224-
+ sqlSafeString( myName ) + "','" + projectionAcronym()
1225-
+ "','" + ellipsoidAcronym() + "','" + sqlSafeString( toProj4() )
1235+
mySql = "insert into tbl_srs (srs_id,description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ("
1236+
+ QString::number( USER_CRS_START_ID ) + ",'"
1237+
+ sqlSafeString( myName ) + "','"
1238+
+ projectionAcronym() + "','"
1239+
+ ellipsoidAcronym() + "','"
1240+
+ sqlSafeString( toProj4() )
12261241
+ "',0)"; // <-- is_geo shamelessly hard coded for now
12271242
}
12281243
else
12291244
{
12301245
mySql = "insert into tbl_srs (description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ('"
1231-
+ sqlSafeString( myName ) + "','" + projectionAcronym()
1232-
+ "','" + ellipsoidAcronym() + "','" + sqlSafeString( toProj4() )
1246+
+ sqlSafeString( myName ) + "','"
1247+
+ projectionAcronym() + "','"
1248+
+ ellipsoidAcronym() + "','"
1249+
+ sqlSafeString( toProj4() )
12331250
+ "',0)"; // <-- is_geo shamelessly hard coded for now
12341251
}
12351252
sqlite3 *myDatabase;
@@ -1240,7 +1257,9 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
12401257
myResult = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase );
12411258
if ( myResult != SQLITE_OK )
12421259
{
1243-
QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " ).arg( sqlite3_errmsg( myDatabase ) ).arg( QgsApplication::qgisUserDbFilePath() ) );
1260+
QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " )
1261+
.arg( sqlite3_errmsg( myDatabase ) )
1262+
.arg( QgsApplication::qgisUserDbFilePath() ) );
12441263
// XXX This will likely never happen since on open, sqlite creates the
12451264
// database if it does not exist.
12461265
assert( myResult == SQLITE_OK );
@@ -1250,7 +1269,6 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
12501269
sqlite3_step( myPreparedStatement );
12511270
// XXX Need to free memory from the error msg if one is set
12521271
return myResult == SQLITE_OK;
1253-
12541272
}
12551273

12561274
long QgsCoordinateReferenceSystem::getRecordCount()
@@ -1285,7 +1303,6 @@ long QgsCoordinateReferenceSystem::getRecordCount()
12851303
sqlite3_finalize( myPreparedStatement );
12861304
sqlite3_close( myDatabase );
12871305
return myRecordCount;
1288-
12891306
}
12901307

12911308
const QString QgsCoordinateReferenceSystem::sqlSafeString( const QString theSQL )

0 commit comments

Comments
 (0)
Please sign in to comment.