Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use proj6 for custom projections dialog
  • Loading branch information
nyalldawson committed Apr 5, 2019
1 parent c2cac5a commit 70a8d49
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions src/app/qgscustomprojectiondialog.cpp
Expand Up @@ -38,14 +38,12 @@
#include <sqlite3.h>

//proj4 includes
extern "C"
{
#ifndef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
#define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
#endif
#if PROJ_VERSION_MAJOR>=6
#include "qgsprojutils.h"
#include <proj.h>
#else
#include <proj_api.h>
}

#endif

QgsCustomProjectionDialog::QgsCustomProjectionDialog( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
Expand Down Expand Up @@ -446,38 +444,51 @@ void QgsCustomProjectionDialog::updateListFromCurrentItem()
void QgsCustomProjectionDialog::pbnCalculate_clicked()
{
// We must check the prj def is valid!
#if PROJ_VERSION_MAJOR>=6
PJ_CONTEXT *pContext = QgsProjContext::get();
const QString projDef = teParameters->toPlainText();
QgsDebugMsg( QStringLiteral( "Proj: %1" ).arg( projDef ) );
#else
projCtx pContext = pj_ctx_alloc();
projPJ proj = pj_init_plus_ctx( pContext, teParameters->toPlainText().toLocal8Bit().data() );

QgsDebugMsg( QStringLiteral( "Proj: %1" ).arg( teParameters->toPlainText() ) );

if ( !proj )
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "This proj4 projection definition is not valid." ) );
tr( "This proj projection definition is not valid." ) );
projectedX->clear();
projectedY->clear();
pj_free( proj );
pj_ctx_free( pContext );
return;

}
#endif
// Get the WGS84 coordinates
bool okN, okE;
double northing = northWGS84->text().toDouble( &okN ) * DEG_TO_RAD;
double easting = eastWGS84->text().toDouble( &okE ) * DEG_TO_RAD;
double northing = northWGS84->text().toDouble( &okN );
double easting = eastWGS84->text().toDouble( &okE );

#if PROJ_VERSION_MAJOR<6
northing *= DEG_TO_RAD;
easting * = DEG_TO_RAD;
#endif

if ( !okN || !okE )
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "Northing and Easthing must be in decimal form." ) );
tr( "Northing and Easting must be in decimal form." ) );
projectedX->clear();
projectedY->clear();
#if PROJ_VERSION_MAJOR<6
pj_free( proj );
pj_ctx_free( pContext );
#endif
return;
}

#if PROJ_VERSION_MAJOR < 6
projPJ wgs84Proj = pj_init_plus_ctx( pContext, GEOPROJ4.toLocal8Bit().data() ); //defined in qgis.h

if ( !wgs84Proj )
Expand All @@ -490,38 +501,65 @@ void QgsCustomProjectionDialog::pbnCalculate_clicked()
pj_ctx_free( pContext );
return;
}
#endif

double z = 0.0;
#if PROJ_VERSION_MAJOR>=6
QgsProjUtils::proj_pj_unique_ptr res( proj_create_crs_to_crs( pContext, "EPSG:4326", projDef.toUtf8(), nullptr ) );
if ( !res )
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "This proj projection definition is not valid." ) );
projectedX->clear();
projectedY->clear();
return;
}

proj_trans_generic( res.get(), PJ_FWD,
&easting, sizeof( double ), 1,
&northing, sizeof( double ), 1,
nullptr, sizeof( double ), 0,
nullptr, sizeof( double ), 0 );
int projResult = proj_errno( res.get() );
#else
double z = 0.0;
int projResult = pj_transform( wgs84Proj, proj, 1, 0, &easting, &northing, &z );
#endif
if ( projResult != 0 )
{
projectedX->setText( tr( "Error" ) );
projectedY->setText( tr( "Error" ) );
#if PROJ_VERSION_MAJOR>=6
QgsDebugMsg( proj_errno_string( projResult ) );
#else
QgsDebugMsg( pj_strerrno( projResult ) );
#endif
}
else
{
QString tmp;

int precision = 4;
int precision = 7;

#if PROJ_VERSION_MAJOR<6
if ( pj_is_latlong( proj ) )
{
northing *= RAD_TO_DEG;
easting *= RAD_TO_DEG;
precision = 7;
}
#endif

tmp = QLocale().toString( northing, 'f', precision );
projectedX->setText( tmp );
tmp = QLocale().toString( easting, 'f', precision );
projectedY->setText( tmp );
}

#if PROJ_VERSION_MAJOR<6
pj_free( proj );
pj_free( wgs84Proj );
pj_ctx_free( pContext );
#endif
}

void QgsCustomProjectionDialog::showHelp()
Expand Down

0 comments on commit 70a8d49

Please sign in to comment.