Skip to content

Commit 63cd0d9

Browse files
committed
Read available datum transformations from gdal file
1 parent 96bd7e7 commit 63cd0d9

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

resources/srs.db

1 KB
Binary file not shown.

src/core/qgscoordinatereferencesystem.cpp

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,10 +1677,13 @@ bool QgsCoordinateReferenceSystem::loadIDs( QHash<int, QString> &wkts )
16771677

16781678
int QgsCoordinateReferenceSystem::syncDb()
16791679
{
1680+
QString dbFilePath = QgsApplication::srsDbFilePath();
1681+
syncDatumTransform( dbFilePath );
1682+
16801683
int inserted = 0, updated = 0, deleted = 0, errors = 0;
16811684

16821685
sqlite3 *database;
1683-
if ( sqlite3_open( QgsApplication::srsDbFilePath().toUtf8().constData(), &database ) != SQLITE_OK )
1686+
if ( sqlite3_open( dbFilePath.toUtf8().constData(), &database ) != SQLITE_OK )
16841687
{
16851688
qCritical( "Could not open database: %s [%s]\n", QgsApplication::srsDbFilePath().toLocal8Bit().constData(), sqlite3_errmsg( database ) );
16861689
return -1;
@@ -1933,3 +1936,97 @@ int QgsCoordinateReferenceSystem::syncDb()
19331936
else
19341937
return updated + inserted;
19351938
}
1939+
1940+
bool QgsCoordinateReferenceSystem::syncDatumTransform( const QString& dbPath )
1941+
{
1942+
QString filename = CPLFindFile( "gdal", "datum_shift.csv" );
1943+
1944+
QFile f( filename );
1945+
if ( !f.open( QIODevice::ReadOnly ) )
1946+
{
1947+
return false;
1948+
}
1949+
1950+
sqlite3* db;
1951+
int openResult = sqlite3_open( dbPath.toUtf8().constData(), &db );
1952+
if ( openResult != SQLITE_OK )
1953+
{
1954+
return false;
1955+
}
1956+
1957+
1958+
QTextStream textStream( &f );
1959+
textStream.readLine();
1960+
1961+
QString line, coord_op, source_crs, target_crs, coord_op_method,
1962+
p1, p2, p3, p4, p5, p6, p7;
1963+
1964+
while ( !textStream.atEnd() )
1965+
{
1966+
line = textStream.readLine();
1967+
QStringList csList = line.split( "," );
1968+
int csSize = csList.size();
1969+
if ( csSize < 22 )
1970+
{
1971+
continue;
1972+
}
1973+
1974+
coord_op = csList[1];
1975+
source_crs = csList[2];
1976+
target_crs = csList[3];
1977+
coord_op_method = csList[csSize - 9];
1978+
p1 = csList[csSize - 8];
1979+
p1 = p1.isEmpty() ? "NULL" : p1;
1980+
p2 = csList[csSize - 7];
1981+
p2 = p2.isEmpty() ? "NULL" : p2;
1982+
p3 = csList[csSize - 6];
1983+
p3 = p3.isEmpty() ? "NULL" : p3;
1984+
p4 = csList[csSize - 5];
1985+
p4 = p4.isEmpty() ? "NULL" : p4;
1986+
p5 = csList[csSize - 4];
1987+
p5 = p5.isEmpty() ? "NULL" : p5;
1988+
p6 = csList[csSize - 3];
1989+
p6 = p6.isEmpty() ? "NULL" : p6;
1990+
p7 = csList[csSize - 2];
1991+
p7 = p7.isEmpty() ? "NULL" : p7;
1992+
1993+
//entry already in db?
1994+
sqlite3_stmt* stmt;
1995+
QString cOpCode;
1996+
QString sql = QString( "SELECT coord_op_code FROM tbl_datum_transform WHERE coord_op_code=%1" ).arg( coord_op );
1997+
int prepareRes = sqlite3_prepare( db, sql.toAscii(), sql.size(), &stmt, NULL );
1998+
if ( prepareRes != SQLITE_OK )
1999+
{
2000+
continue;
2001+
}
2002+
2003+
if ( sqlite3_step( stmt ) == SQLITE_ROW )
2004+
{
2005+
cOpCode = ( const char * ) sqlite3_column_text( stmt, 0 );
2006+
}
2007+
sqlite3_finalize( stmt );
2008+
2009+
if ( !cOpCode.isEmpty() )
2010+
{
2011+
//already in database, do update
2012+
QgsDebugMsg( "Trying datum transform update" );
2013+
sql = QString( "UPDATE tbl_datum_transform SET source_crs = %2, target_crs = %3, coord_op_method = %4, p1 = %5, p2 = %6, p3 = %7, p4 = %8, p5 = %9, p6 = %10, p7 = %11 WHERE coord_op = %1" )
2014+
.arg( coord_op ).arg( source_crs ).arg( target_crs ).arg( coord_op_method ).arg( p1 ).arg( p2 ).arg( p3 ).arg( p4 ).arg( p5 ).arg( p6 ).arg( p7 );
2015+
}
2016+
{
2017+
//not yet in database, do insert
2018+
QgsDebugMsg( "Trying datum transform insert" );
2019+
sql = QString( "INSERT INTO tbl_datum_transform ( coord_op_code, source_crs_code, target_crs_code, coord_op_method_code, p1, p2, p3, p4, p5, p6, p7 ) VALUES ( %1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11 )" )
2020+
.arg( coord_op ).arg( source_crs ).arg( target_crs ).arg( coord_op_method ).arg( p1 ).arg( p2 ).arg( p3 ).arg( p4 ).arg( p5 ).arg( p6 ).arg( p7 );
2021+
2022+
}
2023+
2024+
if ( sqlite3_exec( db, sql.toUtf8(), 0, 0, 0 ) != SQLITE_OK )
2025+
{
2026+
QgsDebugMsg( "Error" );
2027+
}
2028+
}
2029+
2030+
sqlite3_close( db );
2031+
return true; //soon...
2032+
}

src/core/qgscoordinatereferencesystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
465465

466466
static bool loadIDs( QHash<int, QString> &wkts );
467467
static bool loadWkts( QHash<int, QString> &wkts, const char *filename );
468+
static bool syncDatumTransform( const QString& dbPath );
468469

469470
//!Whether this is a coordinate system has inverted axis
470471
mutable int mAxisInverted;

0 commit comments

Comments
 (0)