Skip to content

Commit

Permalink
Use thread safe proj API in all other code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 16, 2017
1 parent 4396e53 commit e51737e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/app/qgscustomprojectiondialog.cpp
Expand Up @@ -453,8 +453,8 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
// //
// We must check the prj def is valid! // We must check the prj def is valid!
// //

projCtx pContext = pj_ctx_alloc();
projPJ myProj = pj_init_plus( teParameters->toPlainText().toLocal8Bit().data() ); projPJ myProj = pj_init_plus_ctx( pContext, teParameters->toPlainText().toLocal8Bit().data() );


QgsDebugMsg( QString( "My proj: %1" ).arg( teParameters->toPlainText() ) ); QgsDebugMsg( QString( "My proj: %1" ).arg( teParameters->toPlainText() ) );


Expand All @@ -465,6 +465,7 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
projectedX->setText( QLatin1String( "" ) ); projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) ); projectedY->setText( QLatin1String( "" ) );
pj_free( myProj ); pj_free( myProj );
pj_ctx_free( pContext );
return; return;


} }
Expand All @@ -480,10 +481,11 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
projectedX->setText( QLatin1String( "" ) ); projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) ); projectedY->setText( QLatin1String( "" ) );
pj_free( myProj ); pj_free( myProj );
pj_ctx_free( pContext );
return; return;
} }


projPJ wgs84Proj = pj_init_plus( GEOPROJ4.toLocal8Bit().data() ); //defined in qgis.h projPJ wgs84Proj = pj_init_plus_ctx( pContext, GEOPROJ4.toLocal8Bit().data() ); //defined in qgis.h


if ( !wgs84Proj ) if ( !wgs84Proj )
{ {
Expand All @@ -492,6 +494,7 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
projectedX->setText( QLatin1String( "" ) ); projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) ); projectedY->setText( QLatin1String( "" ) );
pj_free( wgs84Proj ); pj_free( wgs84Proj );
pj_ctx_free( pContext );
return; return;
} }


Expand All @@ -517,6 +520,7 @@ void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
// //
pj_free( myProj ); pj_free( myProj );
pj_free( wgs84Proj ); pj_free( wgs84Proj );
pj_ctx_free( pContext );


} }


Expand Down
13 changes: 9 additions & 4 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -1099,16 +1099,18 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString &proj4String )
// e.g if they lack a +ellps parameter, it will automatically add +ellps=WGS84, but as // e.g if they lack a +ellps parameter, it will automatically add +ellps=WGS84, but as
// we use the original mProj4 with QgsCoordinateTransform, it will fail to initialize // we use the original mProj4 with QgsCoordinateTransform, it will fail to initialize
// so better detect it now. // so better detect it now.
projPJ proj = pj_init_plus( proj4String.trimmed().toLatin1().constData() ); projCtx pContext = pj_ctx_alloc();
projPJ proj = pj_init_plus_ctx( pContext, proj4String.trimmed().toLatin1().constData() );
if ( !proj ) if ( !proj )
{ {
QgsDebugMsgLevel( "proj.4 string rejected by pj_init_plus()", 4 ); QgsDebugMsgLevel( "proj.4 string rejected by pj_init_plus_ctx()", 4 );
d->mIsValid = false; d->mIsValid = false;
} }
else else
{ {
pj_free( proj ); pj_free( proj );
} }
pj_ctx_free( pContext );
d->mWkt.clear(); d->mWkt.clear();
setMapUnits(); setMapUnits();
} }
Expand Down Expand Up @@ -2020,6 +2022,8 @@ int QgsCoordinateReferenceSystem::syncDatabase()
sqlite3_errmsg( database ) ); sqlite3_errmsg( database ) );
} }


projCtx pContext = pj_ctx_alloc();

#if !defined(PJ_VERSION) || PJ_VERSION!=470 #if !defined(PJ_VERSION) || PJ_VERSION!=470
sql = QStringLiteral( "select auth_name,auth_id,parameters from tbl_srs WHERE auth_name<>'EPSG' AND NOT deprecated AND NOT noupdate" ); sql = QStringLiteral( "select auth_name,auth_id,parameters from tbl_srs WHERE auth_name<>'EPSG' AND NOT deprecated AND NOT noupdate" );
if ( sqlite3_prepare( database, sql.toLatin1(), sql.size(), &select, &tail ) == SQLITE_OK ) if ( sqlite3_prepare( database, sql.toLatin1(), sql.size(), &select, &tail ) == SQLITE_OK )
Expand All @@ -2031,11 +2035,11 @@ int QgsCoordinateReferenceSystem::syncDatabase()
const char *params = reinterpret_cast< const char * >( sqlite3_column_text( select, 2 ) ); const char *params = reinterpret_cast< const char * >( sqlite3_column_text( select, 2 ) );


QString input = QStringLiteral( "+init=%1:%2" ).arg( QString( auth_name ).toLower(), auth_id ); QString input = QStringLiteral( "+init=%1:%2" ).arg( QString( auth_name ).toLower(), auth_id );
projPJ pj = pj_init_plus( input.toLatin1() ); projPJ pj = pj_init_plus_ctx( pContext, input.toLatin1() );
if ( !pj ) if ( !pj )
{ {
input = QStringLiteral( "+init=%1:%2" ).arg( QString( auth_name ).toUpper(), auth_id ); input = QStringLiteral( "+init=%1:%2" ).arg( QString( auth_name ).toUpper(), auth_id );
pj = pj_init_plus( input.toLatin1() ); pj = pj_init_plus_ctx( pContext, input.toLatin1() );
} }


if ( pj ) if ( pj )
Expand Down Expand Up @@ -2099,6 +2103,7 @@ int QgsCoordinateReferenceSystem::syncDatabase()
sqlite3_finalize( select ); sqlite3_finalize( select );
#endif #endif


pj_ctx_free( pContext );


if ( sqlite3_exec( database, "COMMIT", nullptr, nullptr, nullptr ) != SQLITE_OK ) if ( sqlite3_exec( database, "COMMIT", nullptr, nullptr, nullptr ) != SQLITE_OK )
{ {
Expand Down

0 comments on commit e51737e

Please sign in to comment.