25 |
25 |
#include <QFileInfo>
|
26 |
26 |
#include <QRegExp>
|
27 |
27 |
#include <QTextStream>
|
|
28 |
#include <QSqlDatabase>
|
|
29 |
#include <QSqlQuery>
|
|
30 |
#include <QSqlRecord>
|
|
31 |
#include <QVariant>
|
28 |
32 |
|
29 |
33 |
#include "qgsapplication.h"
|
30 |
34 |
#include "qgslogger.h"
|
... | ... | |
32 |
36 |
#include "qgis.h" //const vals declared here
|
33 |
37 |
|
34 |
38 |
#include <cassert>
|
35 |
|
#include <sqlite3.h>
|
36 |
39 |
|
37 |
40 |
//gdal and ogr includes (needed for == operator)
|
38 |
41 |
#include <ogr_srs_api.h>
|
... | ... | |
169 |
172 |
createFromProj4( GEOPROJ4 );
|
170 |
173 |
}
|
171 |
174 |
|
|
175 |
//srid is a postgis sys ref no
|
172 |
176 |
bool QgsCoordinateReferenceSystem::createFromSrid( long id )
|
173 |
177 |
{
|
174 |
178 |
return loadFromDb( QgsApplication::srsDbFilePath(), "srid", id );
|
... | ... | |
179 |
183 |
return loadFromDb( QgsApplication::srsDbFilePath(), "epsg", id );
|
180 |
184 |
}
|
181 |
185 |
|
|
186 |
//qgis internal sys ref now
|
182 |
187 |
bool QgsCoordinateReferenceSystem::createFromSrsId( long id )
|
183 |
188 |
{
|
184 |
|
return loadFromDb( id < 100000 ? QgsApplication::srsDbFilePath() :
|
|
189 |
return loadFromDb( id < USER_CRS_START_ID ? QgsApplication::srsDbFilePath() :
|
185 |
190 |
QgsApplication::qgisUserDbFilePath(), "srs_id", id );
|
186 |
191 |
}
|
187 |
192 |
|
... | ... | |
197 |
202 |
return mIsValidFlag;
|
198 |
203 |
}
|
199 |
204 |
|
200 |
|
sqlite3 *myDatabase;
|
201 |
|
const char *myTail;
|
202 |
|
sqlite3_stmt *myPreparedStatement;
|
203 |
|
int myResult;
|
204 |
|
//check the db is available
|
205 |
|
myResult = openDb( db, &myDatabase );
|
206 |
|
if ( myResult )
|
|
205 |
QSqlDatabase myDb = QSqlDatabase::addDatabase ( "QSQLITE" );
|
|
206 |
myDb.setDatabaseName ( db );
|
|
207 |
if (!myDb.open())
|
207 |
208 |
{
|
208 |
209 |
QgsDebugMsg( "failed : " + db + " could not be opened!" );
|
209 |
|
return mIsValidFlag;
|
|
210 |
return false;
|
210 |
211 |
}
|
211 |
212 |
|
212 |
213 |
/*
|
... | ... | |
220 |
221 |
is_geo integer NOT NULL);
|
221 |
222 |
*/
|
222 |
223 |
|
|
224 |
QSqlQuery myQuery;
|
223 |
225 |
QString mySql = "select srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,epsg,is_geo from tbl_srs where " + field + "='" + QString::number( id ) + "'";
|
224 |
|
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
|
225 |
|
// XXX Need to free memory from the error msg if one is set
|
226 |
|
if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
|
|
226 |
if (!myQuery.exec( mySql ) )
|
227 |
227 |
{
|
228 |
|
mSrsId = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) ).toLong();
|
229 |
|
mDescription = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 1 ) );
|
230 |
|
mProjectionAcronym = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 2 ) );
|
231 |
|
mEllipsoidAcronym = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 3 ) );
|
232 |
|
QString toProj4 = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 4 ) );
|
233 |
|
mSRID = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 5 ) ).toLong();
|
234 |
|
mEpsg = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 6 ) ).toLong();
|
235 |
|
int geo = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 7 ) ).toInt();
|
|
228 |
QgsDebugMsg( "failed : " + mySql );
|
|
229 |
return false;
|
|
230 |
}
|
|
231 |
if (myQuery.next()) //there should be only one...
|
|
232 |
{
|
|
233 |
mSrsId = myQuery.value(myQuery.record().indexOf("srs_id")).toLongLong();
|
|
234 |
mDescription = myQuery.value(myQuery.record().indexOf("description")).toString();
|
|
235 |
mProjectionAcronym = myQuery.value(myQuery.record().indexOf("projection_acronym")).toString();
|
|
236 |
mEllipsoidAcronym = myQuery.value(myQuery.record().indexOf("ellipsoid_acronym")).toString();
|
|
237 |
QString toProj4 = myQuery.value(myQuery.record().indexOf("parameters")).toString();
|
|
238 |
mSRID = myQuery.value(myQuery.record().indexOf("srid")).toLongLong();
|
|
239 |
mEpsg = myQuery.value(myQuery.record().indexOf("epsg")).toLongLong();
|
|
240 |
int geo = myQuery.value(myQuery.record().indexOf("is_geo")).toInt();
|
236 |
241 |
mGeoFlag = ( geo == 0 ? false : true );
|
237 |
242 |
setProj4String( toProj4 );
|
238 |
243 |
setMapUnits();
|
... | ... | |
241 |
246 |
{
|
242 |
247 |
QgsDebugMsg( "failed : " + mySql );
|
243 |
248 |
}
|
244 |
|
sqlite3_finalize( myPreparedStatement );
|
245 |
|
sqlite3_close( myDatabase );
|
246 |
249 |
return mIsValidFlag;
|
247 |
250 |
}
|
248 |
251 |
|
... | ... | |
481 |
484 |
QgsCoordinateReferenceSystem::RecordMap myMap;
|
482 |
485 |
QString myFieldName;
|
483 |
486 |
QString myFieldValue;
|
484 |
|
sqlite3 *myDatabase;
|
485 |
|
const char *myTail;
|
486 |
|
sqlite3_stmt *myPreparedStatement;
|
487 |
|
int myResult;
|
488 |
487 |
|
489 |
488 |
QgsDebugMsg( "running query: " + theSql );
|
490 |
489 |
// Get the full path name to the sqlite3 spatial reference database.
|
... | ... | |
496 |
495 |
" does not exist!" );
|
497 |
496 |
return myMap;
|
498 |
497 |
}
|
499 |
|
|
500 |
|
//check the db is available
|
501 |
|
myResult = openDb( myDatabaseFileName, &myDatabase );
|
502 |
|
if ( myResult )
|
|
498 |
QSqlDatabase myDb = QSqlDatabase::addDatabase ( "QSQLITE" );
|
|
499 |
myDb.setDatabaseName ( myDatabaseFileName );
|
|
500 |
if (!myDb.open())
|
503 |
501 |
{
|
|
502 |
QgsDebugMsg( "failed : " + myDatabaseFileName + " could not be opened!" );
|
504 |
503 |
return myMap;
|
505 |
504 |
}
|
506 |
|
|
507 |
|
myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().length(), &myPreparedStatement, &myTail );
|
508 |
|
// XXX Need to free memory from the error msg if one is set
|
509 |
|
if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
|
|
505 |
QSqlQuery myQuery;
|
|
506 |
if (!myQuery.exec( theSql ) )
|
510 |
507 |
{
|
|
508 |
QgsDebugMsg( "failed : " + theSql );
|
|
509 |
return myMap;
|
|
510 |
}
|
|
511 |
if (myQuery.next()) //there should be only one...
|
|
512 |
{
|
511 |
513 |
QgsDebugMsg( "trying system srs.db" );
|
512 |
|
int myColumnCount = sqlite3_column_count( myPreparedStatement );
|
|
514 |
int myColumnCount = myQuery.record().count();
|
513 |
515 |
//loop through each column in the record adding its field name and vvalue to the map
|
514 |
516 |
for ( int myColNo = 0; myColNo < myColumnCount; myColNo++ )
|
515 |
517 |
{
|
516 |
|
myFieldName = QString::fromUtf8(( char * )sqlite3_column_name( myPreparedStatement, myColNo ) );
|
517 |
|
myFieldValue = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, myColNo ) );
|
|
518 |
myFieldName = myQuery.record().fieldName( myColNo );
|
|
519 |
myFieldValue = myQuery.record().indexOf( myFieldName );
|
518 |
520 |
myMap[myFieldName] = myFieldValue;
|
519 |
521 |
}
|
520 |
522 |
}
|
521 |
523 |
else
|
522 |
524 |
{
|
523 |
|
QgsDebugMsg( "trying system qgis.db" );
|
524 |
|
sqlite3_finalize( myPreparedStatement );
|
525 |
|
sqlite3_close( myDatabase );
|
|
525 |
QgsDebugMsg( "trying user qgis.db" );
|
526 |
526 |
|
527 |
527 |
myDatabaseFileName = QgsApplication::qgisUserDbFilePath();
|
528 |
528 |
QFileInfo myFileInfo;
|
... | ... | |
532 |
532 |
QgsDebugMsg( "users qgis.db not found" );
|
533 |
533 |
return myMap;
|
534 |
534 |
}
|
535 |
|
|
536 |
|
//check the db is available
|
537 |
|
myResult = openDb( myDatabaseFileName, &myDatabase );
|
538 |
|
if ( myResult )
|
|
535 |
myDb.setDatabaseName ( myDatabaseFileName );
|
|
536 |
if (!myDb.open())
|
539 |
537 |
{
|
|
538 |
QgsDebugMsg( "failed : " + myDatabaseFileName + " could not be opened!" );
|
540 |
539 |
return myMap;
|
541 |
540 |
}
|
542 |
|
|
543 |
|
myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().length(), &myPreparedStatement, &myTail );
|
544 |
|
// XXX Need to free memory from the error msg if one is set
|
545 |
|
if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
|
|
541 |
QSqlQuery myQuery;
|
|
542 |
if (!myQuery.exec( theSql ) )
|
546 |
543 |
{
|
547 |
|
int myColumnCount = sqlite3_column_count( myPreparedStatement );
|
|
544 |
QgsDebugMsg( "failed : " + theSql );
|
|
545 |
return myMap;
|
|
546 |
}
|
|
547 |
if (myQuery.next()) //there should be only one...
|
|
548 |
{
|
|
549 |
QgsDebugMsg( "trying system srs.db" );
|
|
550 |
int myColumnCount = myQuery.record().count();
|
548 |
551 |
//loop through each column in the record adding its field name and vvalue to the map
|
549 |
552 |
for ( int myColNo = 0; myColNo < myColumnCount; myColNo++ )
|
550 |
553 |
{
|
551 |
|
myFieldName = QString::fromUtf8(( char * )sqlite3_column_name( myPreparedStatement, myColNo ) );
|
552 |
|
myFieldValue = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, myColNo ) );
|
|
554 |
myFieldName = myQuery.record().fieldName( myColNo );
|
|
555 |
myFieldValue = myQuery.value( myQuery.record().indexOf( myFieldName ) ).toString();
|
553 |
556 |
myMap[myFieldName] = myFieldValue;
|
554 |
557 |
}
|
555 |
558 |
}
|
... | ... | |
559 |
562 |
|
560 |
563 |
}
|
561 |
564 |
}
|
562 |
|
sqlite3_finalize( myPreparedStatement );
|
563 |
|
sqlite3_close( myDatabase );
|
564 |
565 |
|
565 |
566 |
#ifdef QGISDEBUG
|
566 |
567 |
QgsDebugMsg( "retrieved: " + theSql );
|
... | ... | |
782 |
783 |
return 0;
|
783 |
784 |
}
|
784 |
785 |
|
785 |
|
sqlite3 *myDatabase;
|
786 |
|
const char *myTail;
|
787 |
|
sqlite3_stmt *myPreparedStatement;
|
788 |
|
int myResult;
|
789 |
|
|
790 |
786 |
// Set up the query to retrieve the projection information needed to populate the list
|
791 |
787 |
QString mySql = QString( "select srs_id,parameters from tbl_srs where projection_acronym='" +
|
792 |
788 |
mProjectionAcronym + "' and ellipsoid_acronym='" + mEllipsoidAcronym + "'" );
|
... | ... | |
794 |
790 |
QString myDatabaseFileName = QgsApplication::srsDbFilePath();
|
795 |
791 |
|
796 |
792 |
//check the db is available
|
797 |
|
myResult = openDb( myDatabaseFileName, &myDatabase );
|
798 |
|
if ( myResult )
|
|
793 |
bool myResult = openDb( myDatabaseFileName );
|
|
794 |
if ( !myResult )
|
799 |
795 |
{
|
800 |
796 |
return 0;
|
801 |
797 |
}
|
802 |
|
|
803 |
|
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
|
804 |
|
// XXX Need to free memory from the error msg if one is set
|
805 |
|
if ( myResult == SQLITE_OK )
|
|
798 |
QSqlQuery myQuery;
|
|
799 |
if (!myQuery.exec( mySql ) )
|
806 |
800 |
{
|
|
801 |
QgsDebugMsg( "failed : " + mySql );
|
|
802 |
return false;
|
|
803 |
}
|
807 |
804 |
|
808 |
|
while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
|
|
805 |
|
|
806 |
while ( myQuery.next() )
|
|
807 |
{
|
|
808 |
QString mySrsId = myQuery.value(myQuery.record().indexOf("srs_id")).toString();
|
|
809 |
QString myProj4String = myQuery.value(myQuery.record().indexOf("parameters")).toString();
|
|
810 |
if ( equals( myProj4String ) )
|
809 |
811 |
{
|
810 |
|
QString mySrsId = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) );
|
811 |
|
QString myProj4String = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 1 ) );
|
812 |
|
if ( equals( myProj4String ) )
|
813 |
|
{
|
814 |
|
QgsDebugMsg( "-------> MATCH FOUND in srs.db srsid: " + mySrsId );
|
815 |
|
// close the sqlite3 statement
|
816 |
|
sqlite3_finalize( myPreparedStatement );
|
817 |
|
sqlite3_close( myDatabase );
|
818 |
|
return mySrsId.toLong();
|
819 |
|
}
|
820 |
|
else
|
821 |
|
{
|
822 |
|
// QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String));
|
823 |
|
}
|
|
812 |
QgsDebugMsg( "-------> MATCH FOUND in srs.db srsid: " + mySrsId );
|
|
813 |
return mySrsId.toLong();
|
824 |
814 |
}
|
|
815 |
else
|
|
816 |
{
|
|
817 |
QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String));
|
|
818 |
}
|
825 |
819 |
}
|
826 |
820 |
QgsDebugMsg( "no match found in srs.db, trying user db now!" );
|
827 |
|
// close the sqlite3 statement
|
828 |
|
sqlite3_finalize( myPreparedStatement );
|
829 |
|
sqlite3_close( myDatabase );
|
|
821 |
|
830 |
822 |
//
|
831 |
823 |
// Try the users db now
|
832 |
824 |
//
|
833 |
825 |
|
834 |
826 |
myDatabaseFileName = QgsApplication::qgisUserDbFilePath();
|
835 |
827 |
//check the db is available
|
836 |
|
myResult = openDb( myDatabaseFileName, &myDatabase );
|
837 |
|
if ( myResult )
|
|
828 |
myResult = openDb( myDatabaseFileName );
|
|
829 |
if ( !myResult )
|
838 |
830 |
{
|
839 |
831 |
return 0;
|
840 |
832 |
}
|
841 |
|
|
842 |
|
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
|
843 |
|
// XXX Need to free memory from the error msg if one is set
|
844 |
|
if ( myResult == SQLITE_OK )
|
|
833 |
if (!myQuery.exec( mySql ) )
|
845 |
834 |
{
|
846 |
|
|
847 |
|
while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
|
|
835 |
QgsDebugMsg( "failed : " + mySql );
|
|
836 |
return false;
|
|
837 |
}
|
|
838 |
while ( myQuery.next() )
|
|
839 |
{
|
|
840 |
QString mySrsId = myQuery.value(myQuery.record().indexOf("srs_id")).toString();
|
|
841 |
QString myProj4String = myQuery.value(myQuery.record().indexOf("parameters")).toString();
|
|
842 |
if ( equals( myProj4String ) )
|
848 |
843 |
{
|
849 |
|
QString mySrsId = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) );
|
850 |
|
QString myProj4String = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 1 ) );
|
851 |
|
if ( equals( myProj4String ) )
|
852 |
|
{
|
853 |
|
QgsDebugMsg( "-------> MATCH FOUND in user qgis.db srsid: " + mySrsId );
|
854 |
|
// close the sqlite3 statement
|
855 |
|
sqlite3_finalize( myPreparedStatement );
|
856 |
|
sqlite3_close( myDatabase );
|
857 |
|
return mySrsId.toLong();
|
858 |
|
}
|
859 |
|
else
|
860 |
|
{
|
861 |
|
// QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String));
|
862 |
|
}
|
|
844 |
QgsDebugMsg( "-------> MATCH FOUND in user qgis.db srsid: " + mySrsId );
|
|
845 |
return mySrsId.toLong();
|
863 |
846 |
}
|
|
847 |
else
|
|
848 |
{
|
|
849 |
QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String));
|
|
850 |
}
|
864 |
851 |
}
|
865 |
852 |
QgsDebugMsg( "no match found in user db" );
|
866 |
853 |
|
867 |
|
// close the sqlite3 statement
|
868 |
|
sqlite3_finalize( myPreparedStatement );
|
869 |
|
sqlite3_close( myDatabase );
|
870 |
854 |
return 0;
|
871 |
855 |
}
|
872 |
856 |
|
... | ... | |
1082 |
1066 |
|
1083 |
1067 |
//
|
1084 |
1068 |
// Determine if this is a user projection or a system on
|
1085 |
|
// user projection defs all have srs_id >= 100000
|
|
1069 |
// user projection defs all have srs_id >= USER_CRS_START_ID
|
1086 |
1070 |
//
|
1087 |
1071 |
if ( theSrsId >= USER_CRS_START_ID )
|
1088 |
1072 |
{
|
... | ... | |
1101 |
1085 |
}
|
1102 |
1086 |
QgsDebugMsg( "db = " + myDatabaseFileName );
|
1103 |
1087 |
|
1104 |
|
sqlite3 *db;
|
1105 |
|
int rc;
|
1106 |
|
rc = openDb( myDatabaseFileName, &db );
|
1107 |
|
if ( rc )
|
|
1088 |
bool myResult = openDb( myDatabaseFileName );
|
|
1089 |
if ( !myResult )
|
1108 |
1090 |
{
|
1109 |
1091 |
return QString();
|
1110 |
1092 |
}
|
1111 |
|
// prepare the sql statement
|
1112 |
|
const char *pzTail;
|
1113 |
|
sqlite3_stmt *ppStmt;
|
1114 |
1093 |
|
1115 |
|
rc = sqlite3_prepare( db, mySql.toUtf8(), mySql.toUtf8().length(), &ppStmt, &pzTail );
|
1116 |
|
// XXX Need to free memory from the error msg if one is set
|
1117 |
|
|
1118 |
|
if ( rc == SQLITE_OK )
|
|
1094 |
QSqlQuery myQuery;
|
|
1095 |
if ( !myQuery.exec( mySql ) )
|
1119 |
1096 |
{
|
1120 |
|
if ( sqlite3_step( ppStmt ) == SQLITE_ROW )
|
1121 |
|
{
|
1122 |
|
myProjString = QString::fromUtf8(( char* )sqlite3_column_text( ppStmt, 0 ) );
|
1123 |
|
}
|
|
1097 |
QgsDebugMsg( "failed : " + mySql );
|
|
1098 |
return false;
|
1124 |
1099 |
}
|
1125 |
|
// close the statement
|
1126 |
|
sqlite3_finalize( ppStmt );
|
1127 |
|
// close the database
|
1128 |
|
sqlite3_close( db );
|
|
1100 |
if ( myQuery.next() ) //there should be only one...
|
|
1101 |
{
|
|
1102 |
myProjString = myQuery.value(myQuery.record().indexOf( "parameters" ) ).toString();
|
|
1103 |
}
|
1129 |
1104 |
|
1130 |
1105 |
//assert(myProjString.length() > 0);
|
1131 |
1106 |
return myProjString;
|
1132 |
1107 |
}
|
1133 |
1108 |
|
1134 |
|
int QgsCoordinateReferenceSystem::openDb( QString path, sqlite3 **db )
|
|
1109 |
bool QgsCoordinateReferenceSystem::openDb( QString db )
|
1135 |
1110 |
{
|
1136 |
|
QgsDebugMsg( "path = " + path );
|
1137 |
|
int myResult = sqlite3_open( path.toUtf8().data(), db );
|
|
1111 |
QFileInfo myInfo( db );
|
|
1112 |
if ( !myInfo.exists() )
|
|
1113 |
{
|
|
1114 |
QgsDebugMsg( "failed : " + db + " does not exist!" );
|
|
1115 |
return false;
|
|
1116 |
}
|
1138 |
1117 |
|
1139 |
|
if ( myResult )
|
|
1118 |
QSqlDatabase myDb = QSqlDatabase::addDatabase ( "QSQLITE" );
|
|
1119 |
myDb.setDatabaseName ( db );
|
|
1120 |
if (!myDb.open())
|
1140 |
1121 |
{
|
1141 |
|
QgsDebugMsg( "Can't open database: " + QString( sqlite3_errmsg( *db ) ) );
|
1142 |
|
// XXX This will likely never happen since on open, sqlite creates the
|
1143 |
|
// database if it does not exist.
|
1144 |
|
// ... unfortunately it happens on Windows
|
1145 |
|
QgsMessageOutput* output = QgsMessageOutput::createMessageOutput();
|
1146 |
|
output->setTitle( "Error" );
|
1147 |
|
output->setMessage( "Could not open CRS database " + path +
|
1148 |
|
"<br>Error(" + QString::number( myResult ) + "): " +
|
1149 |
|
QString( sqlite3_errmsg( *db ) ), QgsMessageOutput::MessageText );
|
1150 |
|
output->showMessage();
|
|
1122 |
QgsDebugMsg( "failed : " + db + " could not be opened!" );
|
|
1123 |
return false;
|
1151 |
1124 |
}
|
1152 |
|
return myResult;
|
|
1125 |
return true;
|
1153 |
1126 |
}
|
1154 |
1127 |
|
1155 |
1128 |
void QgsCoordinateReferenceSystem::setCustomSrsValidation( CUSTOM_CRS_VALIDATION f )
|
... | ... | |
1212 |
1185 |
.arg( toProj4() );
|
1213 |
1186 |
|
1214 |
1187 |
//if this is the first record we need to ensure that its srs_id is 10000. For
|
1215 |
|
//any rec after that sqlite3 will take care of the autonumering
|
|
1188 |
//any rec after that sqlite3 will take care of the autonumbering
|
1216 |
1189 |
//this was done to support sqlite 3.0 as it does not yet support
|
1217 |
1190 |
//the autoinc related system tables.
|
1218 |
1191 |
if ( getRecordCount() == 0 )
|
... | ... | |
1230 |
1203 |
+ "','" + ellipsoidAcronym() + "','" + sqlSafeString( toProj4() )
|
1231 |
1204 |
+ "',0)"; // <-- is_geo shamelessly hard coded for now
|
1232 |
1205 |
}
|
1233 |
|
sqlite3 *myDatabase;
|
1234 |
|
const char *myTail;
|
1235 |
|
sqlite3_stmt *myPreparedStatement;
|
1236 |
|
int myResult;
|
1237 |
1206 |
//check the db is available
|
1238 |
|
myResult = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase );
|
1239 |
|
if ( myResult != SQLITE_OK )
|
|
1207 |
bool myResult = openDb( QgsApplication::qgisUserDbFilePath().toUtf8().data() );
|
|
1208 |
if ( !myResult )
|
1240 |
1209 |
{
|
1241 |
|
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() ) );
|
1242 |
|
// XXX This will likely never happen since on open, sqlite creates the
|
1243 |
|
// database if it does not exist.
|
1244 |
|
assert( myResult == SQLITE_OK );
|
|
1210 |
QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n (file name) " ).arg( QgsApplication::qgisUserDbFilePath() ) );
|
|
1211 |
assert( myResult == true );
|
1245 |
1212 |
}
|
1246 |
1213 |
QgsDebugMsg( QString( "Update or insert sql \n%1" ).arg( mySql ) );
|
1247 |
|
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
|
1248 |
|
sqlite3_step( myPreparedStatement );
|
1249 |
|
// XXX Need to free memory from the error msg if one is set
|
1250 |
|
return myResult == SQLITE_OK;
|
|
1214 |
QSqlQuery myQuery;
|
|
1215 |
if ( !myQuery.exec( mySql ) )
|
|
1216 |
{
|
|
1217 |
QgsDebugMsg( "failed : " + mySql );
|
|
1218 |
return false;
|
|
1219 |
}
|
|
1220 |
return true;
|
1251 |
1221 |
|
1252 |
1222 |
}
|
1253 |
1223 |
|
1254 |
1224 |
long QgsCoordinateReferenceSystem::getRecordCount()
|
1255 |
1225 |
{
|
1256 |
|
sqlite3 *myDatabase;
|
1257 |
|
const char *myTail;
|
1258 |
|
sqlite3_stmt *myPreparedStatement;
|
1259 |
|
int myResult;
|
1260 |
1226 |
long myRecordCount = 0;
|
1261 |
1227 |
//check the db is available
|
1262 |
|
myResult = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase );
|
1263 |
|
if ( myResult != SQLITE_OK )
|
|
1228 |
QString myDatabaseFileName = QgsApplication::srsDbFilePath();
|
|
1229 |
bool myResult = openDb( myDatabaseFileName );
|
|
1230 |
if ( !myResult )
|
1264 |
1231 |
{
|
1265 |
|
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
|
|
1232 |
QgsDebugMsg( QString( "Can't open database: %1" ).arg( myDatabaseFileName ) );
|
1266 |
1233 |
// XXX This will likely never happen since on open, sqlite creates the
|
1267 |
1234 |
// database if it does not exist.
|
1268 |
|
assert( myResult == SQLITE_OK );
|
|
1235 |
assert( myResult );
|
1269 |
1236 |
}
|
1270 |
1237 |
// Set up the query to retrieve the projection information needed to populate the ELLIPSOID list
|
1271 |
|
QString mySql = "select count(*) from tbl_srs";
|
1272 |
|
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
|
1273 |
|
// XXX Need to free memory from the error msg if one is set
|
1274 |
|
if ( myResult == SQLITE_OK )
|
|
1238 |
QSqlQuery myQuery;
|
|
1239 |
QString mySql = "select count(*) as reccount from tbl_srs";
|
|
1240 |
if ( !myQuery.exec( mySql ) )
|
1275 |
1241 |
{
|
1276 |
|
if ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
|
1277 |
|
{
|
1278 |
|
QString myRecordCountString = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) );
|
1279 |
|
myRecordCount = myRecordCountString.toLong();
|
1280 |
|
}
|
|
1242 |
QgsDebugMsg( "failed : " + mySql );
|
|
1243 |
return false;
|
1281 |
1244 |
}
|
1282 |
|
// close the sqlite3 statement
|
1283 |
|
sqlite3_finalize( myPreparedStatement );
|
1284 |
|
sqlite3_close( myDatabase );
|
|
1245 |
if ( myQuery.next() ) //there should be only one...
|
|
1246 |
{
|
|
1247 |
QString myRecordCountString = myQuery.value(myQuery.record().indexOf("reccount")).toString();
|
|
1248 |
myRecordCount = myRecordCountString.toLong();
|
|
1249 |
}
|
1285 |
1250 |
return myRecordCount;
|
1286 |
1251 |
|
1287 |
1252 |
}
|