Skip to content

Commit

Permalink
More logic to QgsCoordinateReferenceSystemRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 27, 2021
1 parent fae30b5 commit 8e26132
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 67 deletions.
Expand Up @@ -124,6 +124,7 @@ user-defined CRS) to change.

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
67 changes: 1 addition & 66 deletions src/app/qgscustomprojectiondialog.cpp
Expand Up @@ -168,69 +168,12 @@ void QgsCustomProjectionDialog::populateList()
}
}

void QgsCustomProjectionDialog::insertProjection( const QString &projectionAcronym )
{
sqlite3_database_unique_ptr database;
sqlite3_database_unique_ptr srsDatabase;
QString sql;
//check the db is available
int result = database.open( QgsApplication::qgisUserDatabaseFilePath() );
if ( result != SQLITE_OK )
{
QgsDebugMsg( QStringLiteral( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " ).arg( database.errorMessage(),
QgsApplication::qgisUserDatabaseFilePath() ) );
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
Q_ASSERT( result == SQLITE_OK );
}
int srsResult = srsDatabase.open( QgsApplication::srsDatabaseFilePath() );
if ( result != SQLITE_OK )
{
QgsDebugMsg( QStringLiteral( "Can't open database %1 [%2]" ).arg( QgsApplication::srsDatabaseFilePath(),
srsDatabase.errorMessage() ) );
}
else
{
// Set up the query to retrieve the projection information needed to populate the PROJECTION list
QString srsSql = "select acronym,name,notes,parameters from tbl_projection where acronym=" + QgsSqliteUtils::quotedString( projectionAcronym );

sqlite3_statement_unique_ptr srsPreparedStatement = srsDatabase.prepare( srsSql, srsResult );
if ( srsResult == SQLITE_OK )
{
if ( srsPreparedStatement.step() == SQLITE_ROW )
{
QgsDebugMsgLevel( QStringLiteral( "Trying to insert projection" ), 4 );
// We have the result from system srs.db. Now insert into user db.
sql = "insert into tbl_projection(acronym,name,notes,parameters) values ("
+ QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 0 ) )
+ ',' + QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 1 ) )
+ ',' + QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 2 ) )
+ ',' + QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 3 ) )
+ ')';
sqlite3_statement_unique_ptr preparedStatement = database.prepare( sql, result );
if ( result != SQLITE_OK || preparedStatement.step() != SQLITE_DONE )
{
QgsDebugMsg( QStringLiteral( "Update or insert failed in custom projection dialog: %1 [%2]" ).arg( sql, database.errorMessage() ) );
}
}
}
else
{
QgsDebugMsg( QStringLiteral( "prepare failed: %1 [%2]" ).arg( srsSql, srsDatabase.errorMessage() ) );
}
}
}

bool QgsCustomProjectionDialog::saveCrs( QgsCoordinateReferenceSystem crs, const QString &name, const QString &existingId, bool newEntry, QgsCoordinateReferenceSystem::Format format )
{
QString id = existingId;
QString sql;
long returnId = -1;
QString projectionAcronym = crs.projectionAcronym();
QString ellipsoidAcronym = crs.ellipsoidAcronym();
if ( newEntry )
{
returnId = QgsApplication::coordinateReferenceSystemRegistry()->addUserCrs( crs, name, format );
const long returnId = QgsApplication::coordinateReferenceSystemRegistry()->addUserCrs( crs, name, format );
if ( returnId == -1 )
return false;
else
Expand All @@ -248,14 +191,6 @@ bool QgsCustomProjectionDialog::saveCrs( QgsCoordinateReferenceSystem crs, const
mExistingCRSproj[id] = format == QgsCoordinateReferenceSystem::FormatProj ? crs.toProj() : QString();
mExistingCRSnames[id] = name;

QgsCoordinateReferenceSystem::invalidateCache();
QgsCoordinateTransform::invalidateCache();

// If we have a projection acronym not in the user db previously, add it.
// This is a must, or else we can't select it from the vw_srs table.
// Actually, add it always and let the SQL PRIMARY KEY remove duplicates.
insertProjection( projectionAcronym );

return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/app/qgscustomprojectiondialog.h
Expand Up @@ -53,7 +53,6 @@ class APP_EXPORT QgsCustomProjectionDialog : public QDialog, private Ui::QgsCust
//helper functions
void populateList();
bool saveCrs( QgsCoordinateReferenceSystem crs, const QString &name, const QString &id, bool newEntry, QgsCoordinateReferenceSystem::Format format );
void insertProjection( const QString &projectionAcronym );
void showHelp();
QString multiLineWktToSingleLine( const QString &wkt );

Expand Down
70 changes: 70 additions & 0 deletions src/core/qgscoordinatereferencesystemregistry.cpp
Expand Up @@ -103,6 +103,14 @@ long QgsCoordinateReferenceSystemRegistry::addUserCrs( const QgsCoordinateRefere
crs.d->mDescription = name;
}

if ( returnId != -1 )
{
// If we have a projection acronym not in the user db previously, add it.
// This is a must, or else we can't select it from the vw_srs table.
// Actually, add it always and let the SQL PRIMARY KEY remove duplicates.
insertProjection( crs.projectionAcronym() );
}

QgsCoordinateReferenceSystem::invalidateCache();
QgsCoordinateTransform::invalidateCache();

Expand Down Expand Up @@ -165,6 +173,14 @@ bool QgsCoordinateReferenceSystemRegistry::updateUserCrs( long id, const QgsCoor
}
}

if ( res )
{
// If we have a projection acronym not in the user db previously, add it.
// This is a must, or else we can't select it from the vw_srs table.
// Actually, add it always and let the SQL PRIMARY KEY remove duplicates.
insertProjection( crs.projectionAcronym() );
}

QgsCoordinateReferenceSystem::invalidateCache();
QgsCoordinateTransform::invalidateCache();

Expand Down Expand Up @@ -226,3 +242,57 @@ bool QgsCoordinateReferenceSystemRegistry::removeUserCrs( long id )

return res;
}

bool QgsCoordinateReferenceSystemRegistry::insertProjection( const QString &projectionAcronym )
{
sqlite3_database_unique_ptr database;
sqlite3_database_unique_ptr srsDatabase;
QString sql;
//check the db is available
int result = database.open( QgsApplication::qgisUserDatabaseFilePath() );
if ( result != SQLITE_OK )
{
QgsDebugMsg( QStringLiteral( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " ).arg( database.errorMessage(),
QgsApplication::qgisUserDatabaseFilePath() ) );
return false;
}
int srsResult = srsDatabase.open( QgsApplication::srsDatabaseFilePath() );
if ( result != SQLITE_OK )
{
QgsDebugMsg( QStringLiteral( "Can't open database %1 [%2]" ).arg( QgsApplication::srsDatabaseFilePath(),
srsDatabase.errorMessage() ) );
return false;
}

// Set up the query to retrieve the projection information needed to populate the PROJECTION list
QString srsSql = "select acronym,name,notes,parameters from tbl_projection where acronym=" + QgsSqliteUtils::quotedString( projectionAcronym );

sqlite3_statement_unique_ptr srsPreparedStatement = srsDatabase.prepare( srsSql, srsResult );
if ( srsResult == SQLITE_OK )
{
if ( srsPreparedStatement.step() == SQLITE_ROW )
{
QgsDebugMsgLevel( QStringLiteral( "Trying to insert projection" ), 4 );
// We have the result from system srs.db. Now insert into user db.
sql = "insert into tbl_projection(acronym,name,notes,parameters) values ("
+ QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 0 ) )
+ ',' + QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 1 ) )
+ ',' + QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 2 ) )
+ ',' + QgsSqliteUtils::quotedString( srsPreparedStatement.columnAsText( 3 ) )
+ ')';
sqlite3_statement_unique_ptr preparedStatement = database.prepare( sql, result );
if ( result != SQLITE_OK || preparedStatement.step() != SQLITE_DONE )
{
QgsDebugMsg( QStringLiteral( "Could not insert projection into database: %1 [%2]" ).arg( sql, database.errorMessage() ) );
return false;
}
}
}
else
{
QgsDebugMsg( QStringLiteral( "prepare failed: %1 [%2]" ).arg( srsSql, srsDatabase.errorMessage() ) );
return false;
}

return true;
}
5 changes: 5 additions & 0 deletions src/core/qgscoordinatereferencesystemregistry.h
Expand Up @@ -125,6 +125,11 @@ class CORE_EXPORT QgsCoordinateReferenceSystemRegistry : public QObject
*/
void crsDefinitionsChanged();

private:

bool insertProjection( const QString &projectionAcronym );

};


#endif // QGSCOORDINATEREFERENCESYSTEMREGISTRY_H

0 comments on commit 8e26132

Please sign in to comment.