Skip to content

Commit 70a8d49

Browse files
committedApr 5, 2019
Use proj6 for custom projections dialog
1 parent c2cac5a commit 70a8d49

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed
 

‎src/app/qgscustomprojectiondialog.cpp

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@
3838
#include <sqlite3.h>
3939

4040
//proj4 includes
41-
extern "C"
42-
{
43-
#ifndef ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
44-
#define ACCEPT_USE_OF_DEPRECATED_PROJ_API_H
45-
#endif
41+
#if PROJ_VERSION_MAJOR>=6
42+
#include "qgsprojutils.h"
43+
#include <proj.h>
44+
#else
4645
#include <proj_api.h>
47-
}
48-
46+
#endif
4947

5048
QgsCustomProjectionDialog::QgsCustomProjectionDialog( QWidget *parent, Qt::WindowFlags fl )
5149
: QDialog( parent, fl )
@@ -446,38 +444,51 @@ void QgsCustomProjectionDialog::updateListFromCurrentItem()
446444
void QgsCustomProjectionDialog::pbnCalculate_clicked()
447445
{
448446
// We must check the prj def is valid!
447+
#if PROJ_VERSION_MAJOR>=6
448+
PJ_CONTEXT *pContext = QgsProjContext::get();
449+
const QString projDef = teParameters->toPlainText();
450+
QgsDebugMsg( QStringLiteral( "Proj: %1" ).arg( projDef ) );
451+
#else
449452
projCtx pContext = pj_ctx_alloc();
450453
projPJ proj = pj_init_plus_ctx( pContext, teParameters->toPlainText().toLocal8Bit().data() );
451-
452454
QgsDebugMsg( QStringLiteral( "Proj: %1" ).arg( teParameters->toPlainText() ) );
453455

454456
if ( !proj )
455457
{
456458
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
457-
tr( "This proj4 projection definition is not valid." ) );
459+
tr( "This proj projection definition is not valid." ) );
458460
projectedX->clear();
459461
projectedY->clear();
460462
pj_free( proj );
461463
pj_ctx_free( pContext );
462464
return;
463465

464466
}
467+
#endif
465468
// Get the WGS84 coordinates
466469
bool okN, okE;
467-
double northing = northWGS84->text().toDouble( &okN ) * DEG_TO_RAD;
468-
double easting = eastWGS84->text().toDouble( &okE ) * DEG_TO_RAD;
470+
double northing = northWGS84->text().toDouble( &okN );
471+
double easting = eastWGS84->text().toDouble( &okE );
472+
473+
#if PROJ_VERSION_MAJOR<6
474+
northing *= DEG_TO_RAD;
475+
easting * = DEG_TO_RAD;
476+
#endif
469477

470478
if ( !okN || !okE )
471479
{
472480
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
473-
tr( "Northing and Easthing must be in decimal form." ) );
481+
tr( "Northing and Easting must be in decimal form." ) );
474482
projectedX->clear();
475483
projectedY->clear();
484+
#if PROJ_VERSION_MAJOR<6
476485
pj_free( proj );
477486
pj_ctx_free( pContext );
487+
#endif
478488
return;
479489
}
480490

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

483494
if ( !wgs84Proj )
@@ -490,38 +501,65 @@ void QgsCustomProjectionDialog::pbnCalculate_clicked()
490501
pj_ctx_free( pContext );
491502
return;
492503
}
504+
#endif
493505

494-
double z = 0.0;
506+
#if PROJ_VERSION_MAJOR>=6
507+
QgsProjUtils::proj_pj_unique_ptr res( proj_create_crs_to_crs( pContext, "EPSG:4326", projDef.toUtf8(), nullptr ) );
508+
if ( !res )
509+
{
510+
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
511+
tr( "This proj projection definition is not valid." ) );
512+
projectedX->clear();
513+
projectedY->clear();
514+
return;
515+
}
495516

517+
proj_trans_generic( res.get(), PJ_FWD,
518+
&easting, sizeof( double ), 1,
519+
&northing, sizeof( double ), 1,
520+
nullptr, sizeof( double ), 0,
521+
nullptr, sizeof( double ), 0 );
522+
int projResult = proj_errno( res.get() );
523+
#else
524+
double z = 0.0;
496525
int projResult = pj_transform( wgs84Proj, proj, 1, 0, &easting, &northing, &z );
526+
#endif
497527
if ( projResult != 0 )
498528
{
499529
projectedX->setText( tr( "Error" ) );
500530
projectedY->setText( tr( "Error" ) );
531+
#if PROJ_VERSION_MAJOR>=6
532+
QgsDebugMsg( proj_errno_string( projResult ) );
533+
#else
501534
QgsDebugMsg( pj_strerrno( projResult ) );
535+
#endif
502536
}
503537
else
504538
{
505539
QString tmp;
506540

507-
int precision = 4;
541+
int precision = 7;
508542

543+
#if PROJ_VERSION_MAJOR<6
509544
if ( pj_is_latlong( proj ) )
510545
{
511546
northing *= RAD_TO_DEG;
512547
easting *= RAD_TO_DEG;
513548
precision = 7;
514549
}
550+
#endif
515551

516552
tmp = QLocale().toString( northing, 'f', precision );
517553
projectedX->setText( tmp );
518554
tmp = QLocale().toString( easting, 'f', precision );
519555
projectedY->setText( tmp );
520556
}
521557

558+
#if PROJ_VERSION_MAJOR<6
522559
pj_free( proj );
523560
pj_free( wgs84Proj );
524561
pj_ctx_free( pContext );
562+
#endif
525563
}
526564

527565
void QgsCustomProjectionDialog::showHelp()

0 commit comments

Comments
 (0)
Please sign in to comment.