Skip to content

Commit

Permalink
Fix return type for QgsCoordinateReferenceSystem::saveAsUserCrs
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 13, 2017
1 parent 82644fb commit e0b2c27
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
9 changes: 5 additions & 4 deletions python/core/qgscoordinatereferencesystem.sip
Expand Up @@ -636,11 +636,12 @@ Returns whether this CRS is correctly initialized and usable
%End


bool saveAsUserCrs( const QString &name );
long saveAsUserCrs( const QString &name );
%Docstring
Save the proj4-string as a custom CRS
:return: bool true if success else false
:rtype: bool
Save the proj4-string as a custom CRS.

Returns the new CRS srsid(), or -1 if the CRS could not be saved.
:rtype: long
%End

QString geographicCrsAuthId() const;
Expand Down
14 changes: 7 additions & 7 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -1637,12 +1637,12 @@ QString QgsCoordinateReferenceSystem::validationHint()
/// Copied from QgsCustomProjectionDialog ///
/// Please refactor into SQL handler !!! ///

bool QgsCoordinateReferenceSystem::saveAsUserCrs( const QString &name )
long QgsCoordinateReferenceSystem::saveAsUserCrs( const QString &name )
{
if ( !d->mIsValid )
{
QgsDebugMsgLevel( "Can't save an invalid CRS!", 4 );
return false;
return -1;
}

QString mySql;
Expand Down Expand Up @@ -1691,13 +1691,13 @@ bool QgsCoordinateReferenceSystem::saveAsUserCrs( const QString &name )
}
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );

qint64 return_id;
qint64 returnId;
if ( myResult == SQLITE_OK && sqlite3_step( myPreparedStatement ) == SQLITE_DONE )
{
QgsMessageLog::logMessage( QObject::tr( "Saved user CRS [%1]" ).arg( toProj4() ), QObject::tr( "CRS" ) );

return_id = sqlite3_last_insert_rowid( myDatabase );
setInternalId( return_id );
returnId = sqlite3_last_insert_rowid( myDatabase );
setInternalId( returnId );

//We add the just created user CRS to the list of recently used CRS
QgsSettings settings;
Expand All @@ -1713,8 +1713,8 @@ bool QgsCoordinateReferenceSystem::saveAsUserCrs( const QString &name )

}
else
return_id = -1;
return return_id;
returnId = -1;
return returnId;
}

long QgsCoordinateReferenceSystem::getRecordCount()
Expand Down
7 changes: 4 additions & 3 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -613,10 +613,11 @@ class CORE_EXPORT QgsCoordinateReferenceSystem


/**
* Save the proj4-string as a custom CRS
* \returns bool true if success else false
* Save the proj4-string as a custom CRS.
*
* Returns the new CRS srsid(), or -1 if the CRS could not be saved.
*/
bool saveAsUserCrs( const QString &name );
long saveAsUserCrs( const QString &name );

//! Returns auth id of related geographic CRS
QString geographicCrsAuthId() const;
Expand Down
25 changes: 24 additions & 1 deletion tests/src/core/testqgscoordinatereferencesystem.cpp
Expand Up @@ -76,6 +76,7 @@ class TestQgsCoordinateReferenceSystem: public QObject
void validSrsIds();
void asVariant();
void bounds();
void saveAsUserCrs();

private:
void debugPrint( QgsCoordinateReferenceSystem &crs );
Expand All @@ -86,20 +87,30 @@ class TestQgsCoordinateReferenceSystem: public QObject
QStringList myProj4Strings;
QStringList myTOWGS84Strings;
QStringList myAuthIdStrings;
QString mTempFolder;
QString testESRIWkt( int i, QgsCoordinateReferenceSystem &crs );
};


void TestQgsCoordinateReferenceSystem::initTestCase()
{
// we start from a clean profile - we don't want to mess with user custom srses
// create temporary folder
QString subPath = QUuid::createUuid().toString().remove( '-' ).remove( '{' ).remove( '}' );
mTempFolder = QDir::tempPath() + '/' + subPath;
if ( !QDir( mTempFolder ).exists() )
QDir().mkpath( mTempFolder );

//
// Runs once before any tests are run
//
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::init( mTempFolder );
QgsApplication::initQgis();
QgsApplication::showSettings();

QgsDebugMsg( QString( "Custom srs database: %1" ).arg( QgsApplication::qgisUserDatabaseFilePath() ) );

qDebug() << "GEOPROJ4 constant: " << GEOPROJ4;
qDebug() << "GDAL version (build): " << GDAL_RELEASE_NAME;
qDebug() << "GDAL version (runtime): " << GDALVersionInfo( "RELEASE_NAME" );
Expand Down Expand Up @@ -799,5 +810,17 @@ void TestQgsCoordinateReferenceSystem::bounds()
QGSCOMPARENEAR( bounds.yMaximum(), 90.00000, 0.0001 );
}

void TestQgsCoordinateReferenceSystem::saveAsUserCrs()
{
QString madeUpProjection = QStringLiteral( "+proj=aea +lat_1=20 +lat_2=-23 +lat_0=4 +lon_0=29 +x_0=10 +y_0=3 +datum=WGS84 +units=m +no_defs" );
QgsCoordinateReferenceSystem userCrs = QgsCoordinateReferenceSystem::fromProj4( madeUpProjection );
QVERIFY( userCrs.isValid() );
QCOMPARE( userCrs.toProj4(), madeUpProjection );
QCOMPARE( userCrs.srsid(), 0L ); // not saved to database yet

long newId = userCrs.saveAsUserCrs( QStringLiteral( "babies first projection" ) );
QCOMPARE( newId, static_cast< long >( USER_CRS_START_ID ) );
}

QGSTEST_MAIN( TestQgsCoordinateReferenceSystem )
#include "testqgscoordinatereferencesystem.moc"

0 comments on commit e0b2c27

Please sign in to comment.