23 #include <QTemporaryFile>
25 #include <QDomElement>
28 #include <QTextStream>
42 #include <ogr_srs_api.h>
43 #include <cpl_error.h>
53 , mMapUnits(
QGis::UnknownUnit )
56 , mValidationHint(
"" )
57 , mAxisInverted( false )
59 mCRS = OSRNewSpatialReference( NULL );
65 , mMapUnits(
QGis::UnknownUnit )
68 , mValidationHint(
"" )
69 , mAxisInverted( false )
71 mCRS = OSRNewSpatialReference( NULL );
79 , mMapUnits(
QGis::UnknownUnit )
82 , mValidationHint(
"" )
83 , mAxisInverted( false )
85 mCRS = OSRNewSpatialReference( NULL );
91 OSRDestroySpatialReference(
mCRS );
118 QRegExp reCrsId(
"^(epsg|postgis|internal)\\:(\\d+)$", Qt::CaseInsensitive );
119 if ( reCrsId.indexIn( theDefinition ) == 0 )
121 QString authName = reCrsId.cap( 1 ).toLower();
123 if ( authName ==
"epsg" )
125 if ( authName ==
"postgis" )
127 long id = reCrsId.cap( 2 ).toLong();
132 QRegExp reCrsStr(
"^(?:(wkt|proj4)\\:)?(.+)$", Qt::CaseInsensitive );
133 if ( reCrsStr.indexIn( theDefinition ) == 0 )
135 if ( reCrsStr.cap( 1 ).toLower() ==
"proj4" )
144 QString myName = QString(
" * %1 (%2)" )
145 .arg(
QObject::tr(
"Generated CRS",
"A CRS automatically generated from layer info get this prefix for description" ) )
166 #if GDAL_VERSION_NUM >= 1900
167 if ( theDefinition.startsWith(
"ESRI::" ) )
173 if ( OSRSetFromUserInput( crs, theDefinition.toLocal8Bit().constData() ) == OGRERR_NONE )
175 if ( OSRExportToWkt( crs, &wkt ) == OGRERR_NONE )
180 OSRDestroySpatialReference( crs );
190 #if GDAL_VERSION_NUM >= 1900
191 const char* configOld = CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" );
192 const char* configNew =
"GEOGCS";
194 if ( strcmp( configOld,
"" ) == 0 )
196 CPLSetConfigOption(
"GDAL_FIX_ESRI_WKT", configNew );
197 if ( strcmp( configNew, CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" ) ) != 0 )
199 ).arg( configNew ).arg( CPLGetConfigOption(
"GDAL_FIX_ESRI_WKT",
"" ) ) ) ;
200 QgsDebugMsg( QString(
"set GDAL_FIX_ESRI_WKT : %1" ).arg( configNew ) );
204 QgsDebugMsg( QString(
"GDAL_FIX_ESRI_WKT was already set : %1" ).arg( configNew ) );
211 QRegExp re(
"urn:ogc:def:crs:([^:]+).+([^:]+)", Qt::CaseInsensitive );
212 if ( re.exactMatch( theCrs ) )
214 theCrs = re.cap( 1 ) +
":" + re.cap( 2 );
218 re.setPattern(
"(user|custom|qgis):(\\d+)" );
219 if ( re.exactMatch( theCrs ) &&
createFromSrsId( re.cap( 2 ).toInt() ) )
229 if ( theCrs.compare(
"CRS:27", Qt::CaseInsensitive ) == 0 ||
230 theCrs.compare(
"OGC:CRS27", Qt::CaseInsensitive ) == 0 )
237 if ( theCrs.compare(
"CRS:83", Qt::CaseInsensitive ) == 0 ||
238 theCrs.compare(
"OGC:CRS83", Qt::CaseInsensitive ) == 0 )
245 if ( theCrs.compare(
"CRS:84", Qt::CaseInsensitive ) == 0 ||
246 theCrs.compare(
"OGC:CRS84", Qt::CaseInsensitive ) == 0 )
258 mCRS = OSRNewSpatialReference( NULL );
281 OSRDestroySpatialReference(
mCRS );
315 "srs_id", QString::number(
id ) );
320 QgsDebugMsgLevel(
"load CRS from " + db +
" where " + expression +
" is " + value, 3 );
324 QFileInfo myInfo( db );
325 if ( !myInfo.exists() )
327 QgsDebugMsg(
"failed : " + db +
" does not exist!" );
333 sqlite3_stmt *myPreparedStatement;
336 myResult =
openDb( db, &myDatabase );
337 if ( myResult != SQLITE_OK )
339 QgsDebugMsg(
"failed : " + db +
" could not be opened!" );
355 QString mySql =
"select srs_id,description,projection_acronym,"
356 "ellipsoid_acronym,parameters,srid,auth_name||':'||auth_id,is_geo "
357 "from tbl_srs where " + expression +
"=" +
quotedValue( value ) +
" order by deprecated";
358 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(),
359 mySql.toUtf8().length(),
360 &myPreparedStatement, &myTail );
362 if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
364 mSrsId = QString::fromUtf8((
char * )sqlite3_column_text(
365 myPreparedStatement, 0 ) ).toLong();
366 mDescription = QString::fromUtf8((
char * )sqlite3_column_text(
367 myPreparedStatement, 1 ) );
368 mProjectionAcronym = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 2 ) );
369 mEllipsoidAcronym = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 3 ) );
370 QString
toProj4 = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 4 ) );
371 mSRID = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 5 ) ).toLong();
372 mAuthId = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 6 ) );
373 mGeoFlag = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 7 ) ).toInt() != 0;
380 else if (
mAuthId.startsWith(
"EPSG:", Qt::CaseInsensitive ) )
382 OSRDestroySpatialReference(
mCRS );
383 mCRS = OSRNewSpatialReference( NULL );
397 sqlite3_finalize( myPreparedStatement );
398 sqlite3_close( myDatabase );
406 OGRAxisOrientation orientation;
407 const char *axis0 = OSRGetAxis(
mCRS,
mGeoFlag ?
"GEOGCS" :
"PROJCS", 0, &orientation );
409 ? ( orientation == OAO_East || orientation == OAO_West || orientation == OAO_Other )
410 : ( orientation == OAO_North || orientation == OAO_South );
411 QgsDebugMsg( QString(
"srid:%1 axis0:%2 orientation:%3 inverted:%4" ).arg(
mSRID ).arg( axis0 ).arg( OSRAxisEnumToName( orientation ) ).arg(
mAxisInverted ) );
423 if ( theWkt.isEmpty() )
425 QgsDebugMsg(
"theWkt is uninitialised, operation failed" );
429 QByteArray ba = theWkt.toLatin1();
430 const char *pWkt = ba.data();
432 OGRErr myInputResult = OSRImportFromWkt(
mCRS, (
char ** ) & pWkt );
434 if ( myInputResult != OGRERR_NONE )
436 QgsDebugMsg(
"\n---------------------------------------------------------------" );
437 QgsDebugMsg(
"This CRS could *** NOT *** be set from the supplied Wkt " );
439 QgsDebugMsg( QString(
"UNUSED WKT: %1" ).arg( pWkt ) );
440 QgsDebugMsg(
"---------------------------------------------------------------\n" );
444 if ( OSRAutoIdentifyEPSG(
mCRS ) == OGRERR_NONE )
446 QString
authid = QString(
"%1:%2" )
447 .arg( OSRGetAuthorityName(
mCRS, NULL ) )
448 .arg( OSRGetAuthorityCode(
mCRS, NULL ) );
458 char *proj4src = NULL;
459 OSRExportToProj4(
mCRS, &proj4src );
471 OSRExportToProj4(
mCRS, &proj4src );
481 QString myName = QString(
" * %1 (%2)" )
482 .arg(
QObject::tr(
"Generated CRS",
"A CRS automatically generated from layer info get this prefix for description" ) )
508 QString myProj4String = theProj4String.trimmed();
513 QRegExp myProjRegExp(
"\\+proj=(\\S+)" );
514 int myStart = myProjRegExp.indexIn( myProj4String );
517 QgsDebugMsg(
"proj string supplied has no +proj argument" );
523 QRegExp myEllipseRegExp(
"\\+ellps=(\\S+)" );
524 myStart = myEllipseRegExp.indexIn( myProj4String );
527 QgsDebugMsg(
"proj string supplied has no +ellps argument" );
535 QRegExp myAxisRegExp(
"\\+a=(\\S+)" );
536 myStart = myAxisRegExp.indexIn( myProj4String );
539 QgsDebugMsg(
"proj string supplied has no +a argument" );
557 myRecord =
getRecord(
"select * from tbl_srs where parameters=" +
quotedValue( myProj4String ) +
" order by deprecated" );
558 if ( myRecord.empty() )
563 QRegExp myLat1RegExp(
"\\+lat_1=\\S+" );
564 QRegExp myLat2RegExp(
"\\+lat_2=\\S+" );
569 QString lat1Str =
"";
570 QString lat2Str =
"";
571 myStart1 = myLat1RegExp.indexIn( myProj4String, myStart1 );
572 myStart2 = myLat2RegExp.indexIn( myProj4String, myStart2 );
573 if ( myStart1 != -1 && myStart2 != -1 )
575 myLength1 = myLat1RegExp.matchedLength();
576 myLength2 = myLat2RegExp.matchedLength();
581 if ( lat1Str !=
"" && lat2Str !=
"" )
584 QString theProj4StringModified = myProj4String;
589 myStart2 = myLat2RegExp.indexIn( theProj4String, myStart2 );
591 QgsDebugMsg(
"trying proj4string match with swapped lat_1,lat_2" );
592 myRecord =
getRecord(
"select * from tbl_srs where parameters=" +
quotedValue( theProj4StringModified.trimmed() ) +
" order by deprecated" );
596 if ( myRecord.empty() )
603 QString sql =
"SELECT * FROM tbl_srs WHERE ";
610 foreach ( QString param, myProj4String.split( QRegExp(
"\\s+(?=\\+)" ), QString::SkipEmptyParts ) )
612 QString arg = QString(
"' '||parameters||' ' LIKE %1" ).arg(
quotedValue( QString(
"% %1 %" ).arg( param.trimmed() ) ) );
613 if ( param.startsWith(
"+datum=" ) )
624 if ( !datum.isEmpty() )
626 myRecord =
getRecord( sql + delim + datum +
" order by deprecated" );
629 if ( myRecord.empty() )
632 myRecord =
getRecord( sql +
" order by deprecated" );
636 if ( !myRecord.empty() )
638 mySrsId = myRecord[
"srs_id"].toLong();
639 QgsDebugMsg(
"proj4string param match search for srsid returned srsid: " + QString::number( mySrsId ) );
648 QgsDebugMsg(
"globbing search for srsid from this proj string" );
651 QgsDebugMsg(
"globbing search for srsid returned srsid: " + QString::number( mySrsId ) );
665 QgsDebugMsg(
"Projection is not found in databases." );
676 QString myDatabaseFileName;
679 QString myFieldValue;
682 sqlite3_stmt *myPreparedStatement;
688 QFileInfo myInfo( myDatabaseFileName );
689 if ( !myInfo.exists() )
691 QgsDebugMsg(
"failed : " + myDatabaseFileName +
" does not exist!" );
696 myResult =
openDb( myDatabaseFileName, &myDatabase );
697 if ( myResult != SQLITE_OK )
702 myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().length(), &myPreparedStatement, &myTail );
704 if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
707 int myColumnCount = sqlite3_column_count( myPreparedStatement );
709 for (
int myColNo = 0; myColNo < myColumnCount; myColNo++ )
711 myFieldName = QString::fromUtf8((
char * )sqlite3_column_name( myPreparedStatement, myColNo ) );
712 myFieldValue = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, myColNo ) );
713 myMap[myFieldName] = myFieldValue;
715 if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
729 sqlite3_finalize( myPreparedStatement );
730 sqlite3_close( myDatabase );
733 QFileInfo myFileInfo;
734 myFileInfo.setFile( myDatabaseFileName );
735 if ( !myFileInfo.exists( ) )
742 myResult =
openDb( myDatabaseFileName, &myDatabase );
743 if ( myResult != SQLITE_OK )
748 myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().length(), &myPreparedStatement, &myTail );
750 if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
752 int myColumnCount = sqlite3_column_count( myPreparedStatement );
754 for (
int myColNo = 0; myColNo < myColumnCount; myColNo++ )
756 myFieldName = QString::fromUtf8((
char * )sqlite3_column_name( myPreparedStatement, myColNo ) );
757 myFieldValue = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, myColNo ) );
758 myMap[myFieldName] = myFieldValue;
761 if ( sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
772 sqlite3_finalize( myPreparedStatement );
773 sqlite3_close( myDatabase );
777 RecordMap::Iterator it;
778 for ( it = myMap.begin(); it != myMap.end(); ++it )
848 char *proj4src = NULL;
849 OSRExportToProj4(
mCRS, &proj4src );
854 return toProj4.trimmed();
889 char *oldlocale = setlocale( LC_NUMERIC, NULL );
892 oldlocale = strdup( oldlocale );
894 setlocale( LC_NUMERIC,
"C" );
895 OSRDestroySpatialReference(
mCRS );
896 mCRS = OSRNewSpatialReference( NULL );
898 OSRImportFromProj4(
mCRS, theProj4String.trimmed().toLatin1().constData() )
903 #if defined(QGISDEBUG) && QGISDEBUG>=3
907 setlocale( LC_NUMERIC, oldlocale );
916 mAuthId = QString(
"EPSG:%1" ).arg( theEpsg );
941 if ( OSRIsProjected(
mCRS ) )
943 double toMeter = OSRGetLinearUnits(
mCRS, &unitName );
944 QString unit( unitName );
951 static const double feetToMeter = 0.3048;
952 static const double smallNum = 1e-3;
954 if ( qAbs( toMeter - feetToMeter ) < smallNum )
957 QgsDebugMsg(
"Projection has linear units of " + unit );
961 else if ( unit ==
"Foot" )
971 OSRGetAngularUnits(
mCRS, &unitName );
972 QString unit( unitName );
973 if ( unit ==
"degree" )
999 QgsDebugMsg(
"QgsCoordinateReferenceSystem::findMatchingProj will only "
1000 "work if prj acr ellipsoid acr and proj4string are set"
1001 " and the current projection is valid!" );
1007 sqlite3_stmt *myPreparedStatement;
1012 QString mySql = QString(
"select srs_id,parameters from tbl_srs where "
1013 "projection_acronym=%1 and ellipsoid_acronym=%2 order by deprecated" )
1020 myResult =
openDb( myDatabaseFileName, &myDatabase );
1021 if ( myResult != SQLITE_OK )
1026 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1028 if ( myResult == SQLITE_OK )
1031 while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
1033 QString mySrsId = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
1034 QString myProj4String = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 1 ) );
1035 if (
toProj4() == myProj4String.trimmed() )
1037 QgsDebugMsg(
"-------> MATCH FOUND in srs.db srsid: " + mySrsId );
1039 sqlite3_finalize( myPreparedStatement );
1040 sqlite3_close( myDatabase );
1041 return mySrsId.toLong();
1049 QgsDebugMsg(
"no match found in srs.db, trying user db now!" );
1051 sqlite3_finalize( myPreparedStatement );
1052 sqlite3_close( myDatabase );
1059 myResult =
openDb( myDatabaseFileName, &myDatabase );
1060 if ( myResult != SQLITE_OK )
1065 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1067 if ( myResult == SQLITE_OK )
1070 while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
1072 QString mySrsId = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
1073 QString myProj4String = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 1 ) );
1074 if (
toProj4() == myProj4String.trimmed() )
1076 QgsDebugMsg(
"-------> MATCH FOUND in user qgis.db srsid: " + mySrsId );
1078 sqlite3_finalize( myPreparedStatement );
1079 sqlite3_close( myDatabase );
1080 return mySrsId.toLong();
1091 sqlite3_finalize( myPreparedStatement );
1092 sqlite3_close( myDatabase );
1103 return !( *
this == theSrs );
1108 if (
mWkt.isEmpty() )
1111 if ( OSRExportToWkt(
mCRS, &wkt ) == OGRERR_NONE )
1122 QgsDebugMsg(
"Reading Spatial Ref Sys from xml ------------------------!" );
1123 QDomNode srsNode = theNode.namedItem(
"spatialrefsys" );
1125 if ( ! srsNode.isNull() )
1127 bool initialized =
false;
1129 long srsid = srsNode.namedItem(
"srsid" ).toElement().text().toLong();
1135 myNode = srsNode.namedItem(
"authid" );
1136 if ( !myNode.isNull() )
1147 myNode = srsNode.namedItem(
"epsg" );
1148 if ( !myNode.isNull() )
1160 QgsDebugMsg(
"Ignoring authid/epsg for user crs." );
1169 myNode = srsNode.namedItem(
"proj4" );
1178 QgsDebugMsg(
"Setting from elements one by one" );
1180 myNode = srsNode.namedItem(
"proj4" );
1183 myNode = srsNode.namedItem(
"srsid" );
1186 myNode = srsNode.namedItem(
"srid" );
1187 setSrid( myNode.toElement().text().toLong() );
1189 myNode = srsNode.namedItem(
"authid" );
1192 myNode = srsNode.namedItem(
"description" );
1195 myNode = srsNode.namedItem(
"projectionacronym" );
1198 myNode = srsNode.namedItem(
"ellipsoidacronym" );
1201 myNode = srsNode.namedItem(
"geographicflag" );
1202 if ( myNode.toElement().text().compare(
"true" ) )
1223 QString myName = QString(
" * %1 (%2)" )
1224 .arg(
QObject::tr(
"Generated CRS",
"A CRS automatically generated from layer info get this prefix for description" ) )
1242 QDomElement myLayerNode = theNode.toElement();
1243 QDomElement mySrsElement = theDoc.createElement(
"spatialrefsys" );
1245 QDomElement myProj4Element = theDoc.createElement(
"proj4" );
1246 myProj4Element.appendChild( theDoc.createTextNode(
toProj4() ) );
1247 mySrsElement.appendChild( myProj4Element );
1249 QDomElement mySrsIdElement = theDoc.createElement(
"srsid" );
1250 mySrsIdElement.appendChild( theDoc.createTextNode( QString::number(
srsid() ) ) );
1251 mySrsElement.appendChild( mySrsIdElement );
1253 QDomElement mySridElement = theDoc.createElement(
"srid" );
1254 mySridElement.appendChild( theDoc.createTextNode( QString::number(
postgisSrid() ) ) );
1255 mySrsElement.appendChild( mySridElement );
1257 QDomElement myEpsgElement = theDoc.createElement(
"authid" );
1258 myEpsgElement.appendChild( theDoc.createTextNode(
authid() ) );
1259 mySrsElement.appendChild( myEpsgElement );
1261 QDomElement myDescriptionElement = theDoc.createElement(
"description" );
1262 myDescriptionElement.appendChild( theDoc.createTextNode(
description() ) );
1263 mySrsElement.appendChild( myDescriptionElement );
1265 QDomElement myProjectionAcronymElement = theDoc.createElement(
"projectionacronym" );
1266 myProjectionAcronymElement.appendChild( theDoc.createTextNode(
projectionAcronym() ) );
1267 mySrsElement.appendChild( myProjectionAcronymElement );
1269 QDomElement myEllipsoidAcronymElement = theDoc.createElement(
"ellipsoidacronym" );
1270 myEllipsoidAcronymElement.appendChild( theDoc.createTextNode(
ellipsoidAcronym() ) );
1271 mySrsElement.appendChild( myEllipsoidAcronymElement );
1273 QDomElement myGeographicFlagElement = theDoc.createElement(
"geographicflag" );
1274 QString myGeoFlagText =
"false";
1277 myGeoFlagText =
"true";
1280 myGeographicFlagElement.appendChild( theDoc.createTextNode( myGeoFlagText ) );
1281 mySrsElement.appendChild( myGeographicFlagElement );
1283 myLayerNode.appendChild( mySrsElement );
1300 QString myDatabaseFileName;
1301 QString myProjString;
1302 QString mySql = QString(
"select parameters from tbl_srs where srs_id = %1 order by deprecated" ).arg( theSrsId );
1304 QgsDebugMsg(
"mySrsId = " + QString::number( theSrsId ) );
1315 QFileInfo myFileInfo;
1316 myFileInfo.setFile( myDatabaseFileName );
1317 if ( !myFileInfo.exists( ) )
1331 rc =
openDb( myDatabaseFileName, &db );
1338 sqlite3_stmt *ppStmt;
1340 rc = sqlite3_prepare( db, mySql.toUtf8(), mySql.toUtf8().length(), &ppStmt, &pzTail );
1343 if ( rc == SQLITE_OK )
1345 if ( sqlite3_step( ppStmt ) == SQLITE_ROW )
1347 myProjString = QString::fromUtf8((
char* )sqlite3_column_text( ppStmt, 0 ) );
1351 sqlite3_finalize( ppStmt );
1353 sqlite3_close( db );
1356 return myProjString;
1362 int myResult = readonly
1363 ? sqlite3_open_v2( path.toUtf8().data(), db, SQLITE_OPEN_READONLY, NULL )
1364 : sqlite3_open( path.toUtf8().data(), db );
1366 if ( myResult != SQLITE_OK )
1368 QgsDebugMsg(
"Can't open database: " + QString( sqlite3_errmsg( *db ) ) );
1375 .arg( sqlite3_errmsg( *db ) ),
QObject::tr(
"CRS" ) );
1441 mySql =
"insert into tbl_srs (srs_id,description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ("
1451 mySql =
"insert into tbl_srs (description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ("
1460 sqlite3_stmt *myPreparedStatement;
1464 if ( myResult != SQLITE_OK )
1466 QgsDebugMsg( QString(
"Can't open or create database %1: %2" )
1468 .arg( sqlite3_errmsg( myDatabase ) ) );
1471 QgsDebugMsg( QString(
"Update or insert sql \n%1" ).arg( mySql ) );
1472 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1473 sqlite3_step( myPreparedStatement );
1478 if ( myResult == SQLITE_OK )
1480 return_id = sqlite3_last_insert_rowid( myDatabase );
1486 QStringList projectionsProj4 = settings.value(
"/UI/recentProjectionsProj4" ).toStringList();
1487 QStringList projectionsAuthId = settings.value(
"/UI/recentProjectionsAuthId" ).toStringList();
1490 projectionsProj4.append(
toProj4() );
1491 projectionsAuthId.append(
authid() );
1492 settings.setValue(
"/UI/recentProjectionsProj4", projectionsProj4 );
1493 settings.setValue(
"/UI/recentProjectionsAuthId", projectionsAuthId );
1505 sqlite3_stmt *myPreparedStatement;
1507 long myRecordCount = 0;
1510 if ( myResult != SQLITE_OK )
1512 QgsDebugMsg( QString(
"Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
1516 QString mySql =
"select count(*) from tbl_srs";
1517 myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
1519 if ( myResult == SQLITE_OK )
1521 if ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
1523 QString myRecordCountString = QString::fromUtf8((
char * )sqlite3_column_text( myPreparedStatement, 0 ) );
1524 myRecordCount = myRecordCountString.toLong();
1528 sqlite3_finalize( myPreparedStatement );
1529 sqlite3_close( myDatabase );
1530 return myRecordCount;
1535 value.replace(
"'",
"''" );
1536 return value.prepend(
"'" ).append(
"'" );
1542 qDebug(
"Loading %s", filename );
1543 const char *pszFilename = CPLFindFile(
"gdal", filename );
1547 QFile csv( pszFilename );
1548 if ( !csv.open( QIODevice::ReadOnly ) )
1551 QTextStream lines( &csv );
1555 QString line = lines.readLine();
1556 if ( line.isNull() )
1559 if ( line.startsWith(
'#' ) )
1563 else if ( line.startsWith(
"include " ) )
1565 if ( !
loadWkts( wkts, line.mid( 8 ).toUtf8() ) )
1570 int pos = line.indexOf(
"," );
1575 int epsg = line.left( pos ).toInt( &ok );
1579 wkts.insert( epsg, line.mid( pos + 1 ) );
1592 foreach ( QString csv, QStringList() <<
"gcs.csv" <<
"pcs.csv" <<
"vertcs.csv" <<
"compdcs.csv" <<
"geoccs.csv" )
1594 QString filename = CPLFindFile(
"gdal", csv.toUtf8() );
1596 QFile f( filename );
1597 if ( !f.open( QIODevice::ReadOnly ) )
1600 QTextStream lines( &f );
1607 QString line = lines.readLine();
1608 if ( line.isNull() )
1611 int pos = line.indexOf(
"," );
1616 int epsg = line.left( pos ).toInt( &ok );
1621 if ( epsg == 2218 || epsg == 2221 || epsg == 2296 || epsg == 2297 || epsg == 2298 || epsg == 2299 || epsg == 2300 || epsg == 2301 || epsg == 2302 ||
1622 epsg == 2303 || epsg == 2304 || epsg == 2305 || epsg == 2306 || epsg == 2307 || epsg == 2963 || epsg == 2985 || epsg == 2986 || epsg == 3052 ||
1623 epsg == 3053 || epsg == 3139 || epsg == 3144 || epsg == 3145 || epsg == 3173 || epsg == 3295 || epsg == 3993 || epsg == 4087 || epsg == 4088 ||
1624 epsg == 5017 || epsg == 5221 || epsg == 5224 || epsg == 5225 || epsg == 5514 || epsg == 5515 || epsg == 5516 || epsg == 5819 || epsg == 5820 ||
1625 epsg == 5821 || epsg == 32600 || epsg == 32663 || epsg == 32700 )
1628 if ( OSRImportFromEPSG( crs, epsg ) != OGRERR_NONE )
1630 qDebug(
"EPSG %d: not imported", epsg );
1635 if ( OSRExportToWkt( crs, &wkt ) != OGRERR_NONE )
1637 qWarning(
"EPSG %d: not exported to WKT", epsg );
1641 wkts.insert( epsg, wkt );
1649 qDebug(
"Loaded %d/%d from %s", n, l, filename.toUtf8().constData() );
1652 OSRDestroySpatialReference( crs );
1659 int inserted = 0, updated = 0, deleted = 0, errors = 0;
1668 if ( sqlite3_exec( database,
"BEGIN TRANSACTION", 0, 0, 0 ) != SQLITE_OK )
1670 qCritical(
"Could not begin transaction: %s [%s]\n",
QgsApplication::srsDbFilePath().toLocal8Bit().constData(), sqlite3_errmsg( database ) );
1675 sqlite3_exec( database,
"UPDATE tbl_srs SET srid=141001 WHERE srid=41001 AND auth_name='OSGEO' AND auth_id='41001'", 0, 0, 0 );
1679 sqlite3_stmt *select;
1680 char *errMsg = NULL;
1684 QHash<int, QString> wkts;
1688 qDebug(
"%d WKTs loaded", wkts.count() );
1690 for ( QHash<int, QString>::const_iterator it = wkts.constBegin(); it != wkts.constEnd(); ++it )
1692 QByteArray ba( it.value().toUtf8() );
1693 char *psz = ba.data();
1694 OGRErr ogrErr = OSRImportFromWkt( crs, &psz );
1695 if ( ogrErr != OGRERR_NONE )
1700 if ( OSRExportToProj4( crs, &psz ) != OGRERR_NONE )
1704 proj4 = proj4.trimmed();
1708 if ( proj4.isEmpty() )
1713 sql = QString(
"SELECT parameters,noupdate FROM tbl_srs WHERE auth_name='EPSG' AND auth_id='%1'" ).arg( it.key() );
1714 if ( sqlite3_prepare( database, sql.toAscii(), sql.size(), &select, &tail ) != SQLITE_OK )
1716 qCritical(
"Could not prepare: %s [%s]\n", sql.toAscii().constData(), sqlite3_errmsg( database ) );
1721 if ( sqlite3_step( select ) == SQLITE_ROW )
1723 srsProj4 = (
const char * ) sqlite3_column_text( select, 0 );
1725 if ( QString::fromUtf8((
char * )sqlite3_column_text( select, 1 ) ).toInt() != 0 )
1729 sqlite3_finalize( select );
1731 if ( !srsProj4.isEmpty() )
1733 if ( proj4 != srsProj4 )
1736 sql = QString(
"UPDATE tbl_srs SET parameters=%1 WHERE auth_name='EPSG' AND auth_id=%2" ).arg(
quotedValue( proj4 ) ).arg( it.key() );
1738 if ( sqlite3_exec( database, sql.toUtf8(), 0, 0, &errMsg ) != SQLITE_OK )
1740 qCritical(
"Could not execute: %s [%s/%s]\n",
1741 sql.toLocal8Bit().constData(),
1742 sqlite3_errmsg( database ),
1743 errMsg ? errMsg :
"(unknown error)" );
1749 QgsDebugMsgLevel( QString(
"SQL: %1\n OLD:%2\n NEW:%3" ).arg( sql ).arg( srsProj4 ).arg( proj4 ), 3 );
1755 QRegExp projRegExp(
"\\+proj=(\\S+)" );
1756 if ( projRegExp.indexIn( proj4 ) < 0 )
1758 QgsDebugMsg( QString(
"EPSG %1: no +proj argument found [%2]" ).arg( it.key() ).arg( proj4 ) );
1762 QRegExp ellipseRegExp(
"\\+ellps=(\\S+)" );
1764 if ( ellipseRegExp.indexIn( proj4 ) >= 0 )
1766 ellps = ellipseRegExp.cap( 1 );
1769 QString name( OSRIsGeographic( crs ) ? OSRGetAttrValue( crs,
"GEOCS", 0 ) : OSRGetAttrValue( crs,
"PROJCS", 0 ) );
1770 if ( name.isEmpty() )
1773 sql = QString(
"INSERT INTO tbl_srs(description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated) VALUES (%1,%2,%3,%4,%5,'EPSG',%5,%6,0)" )
1779 .arg( OSRIsGeographic( crs ) );
1782 if ( sqlite3_exec( database, sql.toUtf8(), 0, 0, &errMsg ) == SQLITE_OK )
1788 qCritical(
"Could not execute: %s [%s/%s]\n",
1789 sql.toLocal8Bit().constData(),
1790 sqlite3_errmsg( database ),
1791 errMsg ? errMsg :
"(unknown error)" );
1795 sqlite3_free( errMsg );
1800 sql =
"DELETE FROM tbl_srs WHERE auth_name='EPSG' AND NOT auth_id IN (";
1802 foreach (
int i, wkts.keys() )
1804 sql += delim + QString::number( i );
1807 sql +=
") AND NOT noupdate";
1809 if ( sqlite3_exec( database, sql.toUtf8(), 0, 0, 0 ) == SQLITE_OK )
1811 deleted = sqlite3_changes( database );
1816 qCritical(
"Could not execute: %s [%s]\n",
1817 sql.toLocal8Bit().constData(),
1818 sqlite3_errmsg( database ) );
1821 #if !defined(PJ_VERSION) || PJ_VERSION!=470
1822 sql = QString(
"select auth_name,auth_id,parameters from tbl_srs WHERE auth_name<>'EPSG' AND NOT deprecated AND NOT noupdate" );
1823 if ( sqlite3_prepare( database, sql.toAscii(), sql.size(), &select, &tail ) == SQLITE_OK )
1825 while ( sqlite3_step( select ) == SQLITE_ROW )
1827 const char *auth_name = (
const char * ) sqlite3_column_text( select, 0 );
1828 const char *auth_id = (
const char * ) sqlite3_column_text( select, 1 );
1829 const char *params = (
const char * ) sqlite3_column_text( select, 2 );
1831 QString input = QString(
"+init=%1:%2" ).arg( QString( auth_name ).toLower() ).arg( auth_id );
1832 projPJ pj = pj_init_plus( input.toAscii() );
1835 input = QString(
"+init=%1:%2" ).arg( QString( auth_name ).toUpper() ).arg( auth_id );
1836 pj = pj_init_plus( input.toAscii() );
1841 char *def = pj_get_def( pj, 0 );
1847 input.prepend(
' ' ).append(
' ' );
1848 if ( proj4.startsWith( input ) )
1850 proj4 = proj4.mid( input.size() );
1851 proj4 = proj4.trimmed();
1854 if ( proj4 != params )
1856 sql = QString(
"UPDATE tbl_srs SET parameters=%1 WHERE auth_name=%2 AND auth_id=%3" )
1861 if ( sqlite3_exec( database, sql.toUtf8(), 0, 0, &errMsg ) == SQLITE_OK )
1864 QgsDebugMsgLevel( QString(
"SQL: %1\n OLD:%2\n NEW:%3" ).arg( sql ).arg( params ).arg( proj4 ), 3 );
1868 qCritical(
"Could not execute: %s [%s/%s]\n",
1869 sql.toLocal8Bit().constData(),
1870 sqlite3_errmsg( database ),
1871 errMsg ? errMsg :
"(unknown error)" );
1878 QgsDebugMsg( QString(
"could not retrieve proj string for %1 from PROJ" ).arg( input ) );
1883 QgsDebugMsgLevel( QString(
"could not retrieve crs for %1 from PROJ" ).arg( input ), 3 );
1892 qCritical(
"Could not execute: %s [%s]\n",
1893 sql.toLocal8Bit().constData(),
1894 sqlite3_errmsg( database ) );
1898 OSRDestroySpatialReference( crs );
1900 if ( sqlite3_exec( database,
"COMMIT", 0, 0, 0 ) != SQLITE_OK )
1902 qCritical(
"Could not commit transaction: %s [%s]\n",
QgsApplication::srsDbFilePath().toLocal8Bit().constData(), sqlite3_errmsg( database ) );
1906 sqlite3_close( database );
1908 qWarning(
"CRS update (inserted:%d updated:%d deleted:%d errors:%d)", inserted, updated, deleted, errors );
1913 return updated + inserted;