@@ -190,7 +190,7 @@ bool QgsCoordinateReferenceSystem::loadFromDb( QString db, QString expression, Q
190
190
int myResult;
191
191
// check the db is available
192
192
myResult = openDb ( db, &myDatabase );
193
- if ( myResult )
193
+ if ( myResult != SQLITE_OK )
194
194
{
195
195
QgsDebugMsg ( " failed : " + db + " could not be opened!" );
196
196
return mIsValidFlag ;
@@ -266,7 +266,7 @@ bool QgsCoordinateReferenceSystem::createFromWkt( QString theWkt )
266
266
char *proj4src = NULL ;
267
267
OSRExportToProj4 ( mCRS , &proj4src );
268
268
269
- // now that we have the proj4string, delegate to createFromProj4String so
269
+ // now that we have the proj4string, delegate to createFromProj4 so
270
270
// that we can try to fill in the remaining class members...
271
271
// create from Proj will set the isValidFlag
272
272
createFromProj4 ( QString ( proj4src ) );
@@ -330,13 +330,13 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
330
330
}
331
331
332
332
/*
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
+ */
340
340
long mySrsId = 0 ;
341
341
QgsCoordinateReferenceSystem::RecordMap myRecord;
342
342
@@ -347,8 +347,8 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
347
347
// }
348
348
349
349
/*
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
+ */
352
352
// QgsDebugMsg( "wholetext match on name failed, trying proj4string match" );
353
353
myRecord = getRecord ( " select * from tbl_srs where parameters='" + theProj4String.trimmed () + " '" );
354
354
if ( !myRecord.empty () )
@@ -430,30 +430,44 @@ bool QgsCoordinateReferenceSystem::createFromProj4( const QString theProj4String
430
430
{
431
431
QgsDebugMsg ( " Projection is not found in databases." );
432
432
setProj4String ( theProj4String );
433
+
433
434
// Is the SRS is valid now, we know it's a decent +proj string that can be entered into the srs.db
434
435
if ( mIsValidFlag )
435
436
{
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 () )
441
440
{
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 ;
457
471
}
458
472
}
459
473
}
@@ -487,7 +501,7 @@ QgsCoordinateReferenceSystem::RecordMap QgsCoordinateReferenceSystem::getRecord(
487
501
488
502
// check the db is available
489
503
myResult = openDb ( myDatabaseFileName, &myDatabase );
490
- if ( myResult )
504
+ if ( myResult != SQLITE_OK )
491
505
{
492
506
return myMap;
493
507
}
@@ -523,7 +537,7 @@ QgsCoordinateReferenceSystem::RecordMap QgsCoordinateReferenceSystem::getRecord(
523
537
524
538
// check the db is available
525
539
myResult = openDb ( myDatabaseFileName, &myDatabase );
526
- if ( myResult )
540
+ if ( myResult != SQLITE_OK )
527
541
{
528
542
return myMap;
529
543
}
@@ -795,7 +809,7 @@ long QgsCoordinateReferenceSystem::findMatchingProj()
795
809
796
810
// check the db is available
797
811
myResult = openDb ( myDatabaseFileName, &myDatabase );
798
- if ( myResult )
812
+ if ( myResult != SQLITE_OK )
799
813
{
800
814
return 0 ;
801
815
}
@@ -834,7 +848,7 @@ long QgsCoordinateReferenceSystem::findMatchingProj()
834
848
myDatabaseFileName = QgsApplication::qgisUserDbFilePath ();
835
849
// check the db is available
836
850
myResult = openDb ( myDatabaseFileName, &myDatabase );
837
- if ( myResult )
851
+ if ( myResult != SQLITE_OK )
838
852
{
839
853
return 0 ;
840
854
}
@@ -1138,7 +1152,7 @@ int QgsCoordinateReferenceSystem::openDb( QString path, sqlite3 **db )
1138
1152
QgsDebugMsgLevel ( " path = " + path, 3 );
1139
1153
int myResult = sqlite3_open ( path.toUtf8 ().data (), db );
1140
1154
1141
- if ( myResult )
1155
+ if ( myResult != SQLITE_OK )
1142
1156
{
1143
1157
QgsDebugMsg ( " Can't open database: " + QString ( sqlite3_errmsg ( *db ) ) );
1144
1158
// XXX This will likely never happen since on open, sqlite creates the
@@ -1201,7 +1215,6 @@ QString QgsCoordinateReferenceSystem::validationHint()
1201
1215
1202
1216
bool QgsCoordinateReferenceSystem::saveAsUserCRS ()
1203
1217
{
1204
-
1205
1218
if ( ! mIsValidFlag )
1206
1219
{
1207
1220
QgsDebugMsg ( " Can't save an invalid CRS!" );
@@ -1219,17 +1232,21 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
1219
1232
// the autoinc related system tables.
1220
1233
if ( getRecordCount () == 0 )
1221
1234
{
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 () )
1226
1241
+ " ',0)" ; // <-- is_geo shamelessly hard coded for now
1227
1242
}
1228
1243
else
1229
1244
{
1230
1245
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 () )
1233
1250
+ " ',0)" ; // <-- is_geo shamelessly hard coded for now
1234
1251
}
1235
1252
sqlite3 *myDatabase;
@@ -1240,7 +1257,9 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
1240
1257
myResult = sqlite3_open ( QgsApplication::qgisUserDbFilePath ().toUtf8 ().data (), &myDatabase );
1241
1258
if ( myResult != SQLITE_OK )
1242
1259
{
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 () ) );
1244
1263
// XXX This will likely never happen since on open, sqlite creates the
1245
1264
// database if it does not exist.
1246
1265
assert ( myResult == SQLITE_OK );
@@ -1250,7 +1269,6 @@ bool QgsCoordinateReferenceSystem::saveAsUserCRS()
1250
1269
sqlite3_step ( myPreparedStatement );
1251
1270
// XXX Need to free memory from the error msg if one is set
1252
1271
return myResult == SQLITE_OK;
1253
-
1254
1272
}
1255
1273
1256
1274
long QgsCoordinateReferenceSystem::getRecordCount ()
@@ -1285,7 +1303,6 @@ long QgsCoordinateReferenceSystem::getRecordCount()
1285
1303
sqlite3_finalize ( myPreparedStatement );
1286
1304
sqlite3_close ( myDatabase );
1287
1305
return myRecordCount;
1288
-
1289
1306
}
1290
1307
1291
1308
const QString QgsCoordinateReferenceSystem::sqlSafeString ( const QString theSQL )
0 commit comments