38
38
#include < sqlite3.h>
39
39
40
40
// 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
46
45
#include < proj_api.h>
47
- }
48
-
46
+ #endif
49
47
50
48
QgsCustomProjectionDialog::QgsCustomProjectionDialog ( QWidget *parent, Qt::WindowFlags fl )
51
49
: QDialog( parent, fl )
@@ -446,38 +444,51 @@ void QgsCustomProjectionDialog::updateListFromCurrentItem()
446
444
void QgsCustomProjectionDialog::pbnCalculate_clicked ()
447
445
{
448
446
// 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
449
452
projCtx pContext = pj_ctx_alloc ();
450
453
projPJ proj = pj_init_plus_ctx ( pContext, teParameters->toPlainText ().toLocal8Bit ().data () );
451
-
452
454
QgsDebugMsg ( QStringLiteral ( " Proj: %1" ).arg ( teParameters->toPlainText () ) );
453
455
454
456
if ( !proj )
455
457
{
456
458
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." ) );
458
460
projectedX->clear ();
459
461
projectedY->clear ();
460
462
pj_free ( proj );
461
463
pj_ctx_free ( pContext );
462
464
return ;
463
465
464
466
}
467
+ #endif
465
468
// Get the WGS84 coordinates
466
469
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
469
477
470
478
if ( !okN || !okE )
471
479
{
472
480
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." ) );
474
482
projectedX->clear ();
475
483
projectedY->clear ();
484
+ #if PROJ_VERSION_MAJOR<6
476
485
pj_free ( proj );
477
486
pj_ctx_free ( pContext );
487
+ #endif
478
488
return ;
479
489
}
480
490
491
+ #if PROJ_VERSION_MAJOR < 6
481
492
projPJ wgs84Proj = pj_init_plus_ctx ( pContext, GEOPROJ4.toLocal8Bit ().data () ); // defined in qgis.h
482
493
483
494
if ( !wgs84Proj )
@@ -490,38 +501,65 @@ void QgsCustomProjectionDialog::pbnCalculate_clicked()
490
501
pj_ctx_free ( pContext );
491
502
return ;
492
503
}
504
+ #endif
493
505
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
+ }
495
516
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 ;
496
525
int projResult = pj_transform ( wgs84Proj, proj, 1 , 0 , &easting, &northing, &z );
526
+ #endif
497
527
if ( projResult != 0 )
498
528
{
499
529
projectedX->setText ( tr ( " Error" ) );
500
530
projectedY->setText ( tr ( " Error" ) );
531
+ #if PROJ_VERSION_MAJOR>=6
532
+ QgsDebugMsg ( proj_errno_string ( projResult ) );
533
+ #else
501
534
QgsDebugMsg ( pj_strerrno ( projResult ) );
535
+ #endif
502
536
}
503
537
else
504
538
{
505
539
QString tmp;
506
540
507
- int precision = 4 ;
541
+ int precision = 7 ;
508
542
543
+ #if PROJ_VERSION_MAJOR<6
509
544
if ( pj_is_latlong ( proj ) )
510
545
{
511
546
northing *= RAD_TO_DEG;
512
547
easting *= RAD_TO_DEG;
513
548
precision = 7 ;
514
549
}
550
+ #endif
515
551
516
552
tmp = QLocale ().toString ( northing, ' f' , precision );
517
553
projectedX->setText ( tmp );
518
554
tmp = QLocale ().toString ( easting, ' f' , precision );
519
555
projectedY->setText ( tmp );
520
556
}
521
557
558
+ #if PROJ_VERSION_MAJOR<6
522
559
pj_free ( proj );
523
560
pj_free ( wgs84Proj );
524
561
pj_ctx_free ( pContext );
562
+ #endif
525
563
}
526
564
527
565
void QgsCustomProjectionDialog::showHelp ()
0 commit comments