|
42 | 42 | #include <ogr_srs_api.h>
|
43 | 43 | #include <cpl_error.h>
|
44 | 44 | #include <cpl_conv.h>
|
| 45 | +#include <cpl_csv.h> |
45 | 46 |
|
46 | 47 | CUSTOM_CRS_VALIDATION QgsCoordinateReferenceSystem::mCustomSrsValidation = NULL;
|
47 | 48 |
|
@@ -195,8 +196,8 @@ void QgsCoordinateReferenceSystem::setupESRIWktFix( )
|
195 | 196 | {
|
196 | 197 | CPLSetConfigOption( "GDAL_FIX_ESRI_WKT", configNew );
|
197 | 198 | if ( strcmp( configNew, CPLGetConfigOption( "GDAL_FIX_ESRI_WKT", "" ) ) != 0 )
|
198 |
| - QgsLogger::warning( QString( "GDAL_FIX_ESRI_WKT could not be set to %1 : %2" |
199 |
| - ).arg( configNew ).arg( CPLGetConfigOption( "GDAL_FIX_ESRI_WKT", "" ) ) ) ; |
| 199 | + QgsLogger::warning( QString( "GDAL_FIX_ESRI_WKT could not be set to %1 : %2" ) |
| 200 | + .arg( configNew ).arg( CPLGetConfigOption( "GDAL_FIX_ESRI_WKT", "" ) ) ) ; |
200 | 201 | QgsDebugMsg( QString( "set GDAL_FIX_ESRI_WKT : %1" ).arg( configNew ) );
|
201 | 202 | }
|
202 | 203 | else
|
@@ -1945,107 +1946,178 @@ int QgsCoordinateReferenceSystem::syncDb()
|
1945 | 1946 |
|
1946 | 1947 | bool QgsCoordinateReferenceSystem::syncDatumTransform( const QString& dbPath )
|
1947 | 1948 | {
|
1948 |
| - QString filename = CPLFindFile( "gdal", "datum_shift.csv" ); |
1949 |
| - |
1950 |
| - QFile f( filename ); |
1951 |
| - if ( !f.open( QIODevice::ReadOnly ) ) |
| 1949 | + const char *filename = CSVFilename( "datum_shift.csv" ); |
| 1950 | + FILE *fp = VSIFOpen( filename, "rb" ); |
| 1951 | + if ( !fp ) |
1952 | 1952 | {
|
1953 | 1953 | return false;
|
1954 | 1954 | }
|
1955 | 1955 |
|
1956 |
| - sqlite3* db; |
| 1956 | + char **fieldnames = CSVReadParseLine( fp ); |
| 1957 | + |
| 1958 | + // "SEQ_KEY","COORD_OP_CODE","SOURCE_CRS_CODE","TARGET_CRS_CODE","REMARKS","COORD_OP_SCOPE","AREA_OF_USE_CODE","AREA_SOUTH_BOUND_LAT","AREA_NORTH_BOUND_LAT","AREA_WEST_BOUND_LON","AREA_EAST_BOUND_LON","SHOW_OPERATION","DEPRECATED","COORD_OP_METHOD_CODE","DX","DY","DZ","RX","RY","RZ","DS","PREFERRED" |
| 1959 | + |
| 1960 | + struct |
| 1961 | + { |
| 1962 | + const char *src; |
| 1963 | + const char *dst; |
| 1964 | + int idx; |
| 1965 | + } map[] = |
| 1966 | + { |
| 1967 | + // { "SEQ_KEY", "", -1 }, |
| 1968 | + { "SOURCE_CRS_CODE", "source_crs_code", -1 }, |
| 1969 | + { "TARGET_CRS_CODE", "target_crs_code", -1 }, |
| 1970 | + { "REMARKS", "remarks", -1 }, |
| 1971 | + { "COORD_OP_SCOPE", "scope", -1 }, |
| 1972 | + { "AREA_OF_USE_CODE", "area_of_use_code", -1 }, |
| 1973 | + // { "AREA_SOUTH_BOUND_LAT", "", -1 }, |
| 1974 | + // { "AREA_NORTH_BOUND_LAT", "", -1 }, |
| 1975 | + // { "AREA_WEST_BOUND_LON", "", -1 }, |
| 1976 | + // { "AREA_EAST_BOUND_LON", "", -1 }, |
| 1977 | + // { "SHOW_OPERATION", "", -1 }, |
| 1978 | + { "DEPRECATED", "deprecated", -1 }, |
| 1979 | + { "COORD_OP_METHOD_CODE", "coord_op_method_code", -1 }, |
| 1980 | + { "DX", "p1", -1 }, |
| 1981 | + { "DY", "p2", -1 }, |
| 1982 | + { "DZ", "p3", -1 }, |
| 1983 | + { "RX", "p4", -1 }, |
| 1984 | + { "RY", "p5", -1 }, |
| 1985 | + { "RZ", "p6", -1 }, |
| 1986 | + { "DS", "p7", -1 }, |
| 1987 | + { "PREFERRED", "preferred", -1 }, |
| 1988 | + { "COORD_OP_CODE", "coord_op_code", -1 }, |
| 1989 | + }; |
| 1990 | + |
| 1991 | + QString update = "UPDATE tbl_datum_transform SET "; |
| 1992 | + QString insert, values; |
| 1993 | + |
| 1994 | + int n = CSLCount( fieldnames ); |
| 1995 | + |
| 1996 | + int idxid, idxrx, idxry, idxrz; |
| 1997 | + for ( unsigned int i = 0; i < sizeof( map ) / sizeof( *map ); i++ ) |
| 1998 | + { |
| 1999 | + bool last = i == sizeof( map ) / sizeof( *map ) - 1; |
| 2000 | + |
| 2001 | + map[i].idx = CSLFindString( fieldnames, map[i].src ); |
| 2002 | + if ( map[i].idx < 0 ) |
| 2003 | + { |
| 2004 | + qWarning( "field %s not found", map[i].src ); |
| 2005 | + CSLDestroy( fieldnames ); |
| 2006 | + fclose( fp ); |
| 2007 | + return false; |
| 2008 | + } |
| 2009 | + |
| 2010 | + if ( strcmp( map[i].src, "COORD_OP_CODE" ) == 0 ) |
| 2011 | + idxid = i; |
| 2012 | + if ( strcmp( map[i].src, "RX" ) == 0 ) |
| 2013 | + idxrx = i; |
| 2014 | + if ( strcmp( map[i].src, "RY" ) == 0 ) |
| 2015 | + idxry = i; |
| 2016 | + if ( strcmp( map[i].src, "RZ" ) == 0 ) |
| 2017 | + idxrz = i; |
| 2018 | + |
| 2019 | + if ( i > 0 ) |
| 2020 | + { |
| 2021 | + insert += ","; |
| 2022 | + values += ","; |
| 2023 | + |
| 2024 | + if ( last ) |
| 2025 | + { |
| 2026 | + update += " WHERE "; |
| 2027 | + } |
| 2028 | + else |
| 2029 | + { |
| 2030 | + update += ","; |
| 2031 | + } |
| 2032 | + } |
| 2033 | + |
| 2034 | + update += QString( "%1=%%2" ).arg( map[i].dst ).arg( i + 1 ); |
| 2035 | + |
| 2036 | + insert += map[i].dst; |
| 2037 | + values += QString( "%%1" ).arg( i + 1 ); |
| 2038 | + |
| 2039 | + qWarning( "%d: src=%s dst=%s idx=%d", i, map[i].src, map[i].dst, map[i].idx ); |
| 2040 | + } |
| 2041 | + |
| 2042 | + insert = "INSERT INTO tbl_datum_transform(" + insert + ") VALUES (" + values + ")"; |
| 2043 | + |
| 2044 | + QgsDebugMsg( QString( "insert:%1" ).arg( insert ) ); |
| 2045 | + QgsDebugMsg( QString( "update:%1" ).arg( update ) ); |
| 2046 | + |
| 2047 | + CSLDestroy( fieldnames ); |
| 2048 | + |
| 2049 | + sqlite3 *db; |
1957 | 2050 | int openResult = sqlite3_open( dbPath.toUtf8().constData(), &db );
|
1958 | 2051 | if ( openResult != SQLITE_OK )
|
1959 | 2052 | {
|
| 2053 | + fclose( fp ); |
1960 | 2054 | return false;
|
1961 | 2055 | }
|
1962 | 2056 |
|
1963 | 2057 | if ( sqlite3_exec( db, "BEGIN TRANSACTION", 0, 0, 0 ) != SQLITE_OK )
|
1964 | 2058 | {
|
1965 | 2059 | qCritical( "Could not begin transaction: %s [%s]\n", QgsApplication::srsDbFilePath().toLocal8Bit().constData(), sqlite3_errmsg( db ) );
|
| 2060 | + sqlite3_close( db ); |
| 2061 | + fclose( fp ); |
1966 | 2062 | return false;
|
1967 | 2063 | }
|
1968 | 2064 |
|
| 2065 | + QStringList v; |
| 2066 | + v.reserve( sizeof( map ) / sizeof( *map ) ); |
1969 | 2067 |
|
1970 |
| - QTextStream textStream( &f ); |
1971 |
| - textStream.readLine(); |
| 2068 | + while ( !feof( fp ) ) |
| 2069 | + { |
| 2070 | + char **values = CSVReadParseLine( fp ); |
1972 | 2071 |
|
1973 |
| - QString line, coord_op, source_crs, target_crs, coord_op_method, |
1974 |
| - p1, p2, p3, p4, p5, p6, p7; |
| 2072 | + v.clear(); |
1975 | 2073 |
|
1976 |
| - while ( !textStream.atEnd() ) |
1977 |
| - { |
1978 |
| - line = textStream.readLine(); |
1979 |
| - QStringList csList = line.split( "," ); |
1980 |
| - int csSize = csList.size(); |
1981 |
| - if ( csSize < 22 ) |
| 2074 | + if ( CSLCount( values ) < n ) |
1982 | 2075 | {
|
| 2076 | + qWarning( "Only %d columns", CSLCount( values ) ); |
1983 | 2077 | continue;
|
1984 | 2078 | }
|
1985 | 2079 |
|
1986 |
| - coord_op = csList[1]; |
1987 |
| - source_crs = csList[2]; |
1988 |
| - target_crs = csList[3]; |
1989 |
| - coord_op_method = csList[csSize - 9]; |
1990 |
| - p1 = csList[csSize - 8]; |
1991 |
| - p1 = p1.isEmpty() ? "NULL" : p1; |
1992 |
| - p2 = csList[csSize - 7]; |
1993 |
| - p2 = p2.isEmpty() ? "NULL" : p2; |
1994 |
| - p3 = csList[csSize - 6]; |
1995 |
| - p3 = p3.isEmpty() ? "NULL" : p3; |
1996 |
| - p4 = csList[csSize - 5]; |
1997 |
| - p4 = p4.isEmpty() ? "NULL" : p4; |
1998 |
| - p5 = csList[csSize - 4]; |
1999 |
| - p5 = p5.isEmpty() ? "NULL" : p5; |
2000 |
| - p6 = csList[csSize - 3]; |
2001 |
| - p6 = p6.isEmpty() ? "NULL" : p6; |
2002 |
| - p7 = csList[csSize - 2]; |
2003 |
| - p7 = p7.isEmpty() ? "NULL" : p7; |
| 2080 | + for ( unsigned int i = 0; i < sizeof( map ) / sizeof( *map ); i++ ) |
| 2081 | + { |
| 2082 | + int idx = map[i].idx; |
| 2083 | + Q_ASSERT( idx != -1 ); |
| 2084 | + Q_ASSERT( idx < n ); |
| 2085 | + v.insert( i, *values[ idx ] ? quotedValue( values[idx] ) : "NULL" ); |
| 2086 | + } |
2004 | 2087 |
|
2005 | 2088 | //switch sign of rotation parameters. See http://trac.osgeo.org/proj/wiki/GenParms#towgs84-DatumtransformationtoWGS84
|
2006 |
| - if ( coord_op_method == "9607" ) |
| 2089 | + if ( v[ idxid ] == "9607" ) |
2007 | 2090 | {
|
2008 |
| - p4 = qgsDoubleToString( -( p4.toDouble() ) ); |
2009 |
| - p5 = qgsDoubleToString( -( p5.toDouble() ) ); |
2010 |
| - p6 = qgsDoubleToString( -( p6.toDouble() ) ); |
2011 |
| - coord_op_method = "9606"; |
| 2091 | + v[ idxid ] = "9606"; |
| 2092 | + v[ idxrx ] = qgsDoubleToString( -v[ idxrx ].toDouble() ); |
| 2093 | + v[ idxry ] = qgsDoubleToString( -v[ idxry ].toDouble() ); |
| 2094 | + v[ idxrz ] = qgsDoubleToString( -v[ idxrz ].toDouble() ); |
2012 | 2095 | }
|
2013 | 2096 |
|
2014 | 2097 | //entry already in db?
|
2015 |
| - sqlite3_stmt* stmt; |
| 2098 | + sqlite3_stmt *stmt; |
2016 | 2099 | QString cOpCode;
|
2017 |
| - QString sql = QString( "SELECT coord_op_code FROM tbl_datum_transform WHERE coord_op_code=%1" ).arg( coord_op ); |
| 2100 | + QString sql = QString( "SELECT coord_op_code FROM tbl_datum_transform WHERE coord_op_code=%1" ).arg( v[ idxid ] ); |
2018 | 2101 | int prepareRes = sqlite3_prepare( db, sql.toAscii(), sql.size(), &stmt, NULL );
|
2019 | 2102 | if ( prepareRes != SQLITE_OK )
|
2020 |
| - { |
2021 | 2103 | continue;
|
2022 |
| - } |
2023 | 2104 |
|
2024 | 2105 | if ( sqlite3_step( stmt ) == SQLITE_ROW )
|
2025 | 2106 | {
|
2026 | 2107 | cOpCode = ( const char * ) sqlite3_column_text( stmt, 0 );
|
2027 | 2108 | }
|
2028 | 2109 | sqlite3_finalize( stmt );
|
2029 | 2110 |
|
2030 |
| - if ( !cOpCode.isEmpty() ) |
| 2111 | + sql = cOpCode.isEmpty() ? insert : update; |
| 2112 | + for ( int i = 0; i < v.size(); i++ ) |
2031 | 2113 | {
|
2032 |
| - //already in database, do update |
2033 |
| - QgsDebugMsgLevel( "Trying datum transform update", 4 ); |
2034 |
| - sql = QString( "UPDATE tbl_datum_transform SET source_crs_code = %2, target_crs_code = %3, coord_op_method_code = %4, p1 = %5, p2 = %6, p3 = %7, p4 = %8, p5 = %9, p6 = %10, p7 = %11 WHERE coord_op_code = %1" ) |
2035 |
| - .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 ); |
2036 |
| - } |
2037 |
| - else |
2038 |
| - { |
2039 |
| - //not yet in database, do insert |
2040 |
| - QgsDebugMsgLevel( "Trying datum transform insert", 4 ); |
2041 |
| - sql = QString( "INSERT INTO tbl_datum_transform ( epsg_nr, coord_op_code, source_crs_code, target_crs_code, coord_op_method_code, p1, p2, p3, p4, p5, p6, p7 ) VALUES ( %1, %1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11 )" ) |
2042 |
| - .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 ); |
2043 |
| - |
| 2114 | + sql = sql.arg( v[i] ); |
2044 | 2115 | }
|
2045 | 2116 |
|
2046 | 2117 | if ( sqlite3_exec( db, sql.toUtf8(), 0, 0, 0 ) != SQLITE_OK )
|
2047 | 2118 | {
|
2048 |
| - QgsDebugMsg( QString( "Error [%1]" ).arg( sqlite3_errmsg( db ) ) ); |
| 2119 | + qCritical( "SQL: %s", sql.toUtf8().constData() ); |
| 2120 | + qCritical( "Error: %s", sqlite3_errmsg( db ) ); |
2049 | 2121 | }
|
2050 | 2122 | }
|
2051 | 2123 |
|
|
0 commit comments