Skip to content

Commit aa21d55

Browse files
committedApr 15, 2019
Port more QgsCoordinateReferenceSystem internals to proj v6
1 parent 018f3bc commit aa21d55

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed
 

‎src/core/qgscoordinatereferencesystem.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,14 @@ bool QgsCoordinateReferenceSystem::loadFromDatabase( const QString &db, const QS
525525
}
526526
else if ( d->mAuthId.startsWith( QLatin1String( "EPSG:" ), Qt::CaseInsensitive ) )
527527
{
528+
#if PROJ_VERSION_MAJOR>=6
529+
d->mPj.reset( proj_create_from_database( QgsProjContext::get(), "EPSG", d->mAuthId.mid( 5 ).toLatin1(), PJ_CATEGORY_CRS, false, nullptr ) );
530+
d->mIsValid = static_cast< bool >( d->mPj );
531+
#else
528532
OSRDestroySpatialReference( d->mCRS );
529533
d->mCRS = OSRNewSpatialReference( nullptr );
530534
d->mIsValid = OSRSetFromUserInput( d->mCRS, d->mAuthId.toLower().toLatin1() ) == OGRERR_NONE;
535+
#endif
531536
setMapUnits();
532537
}
533538

@@ -547,6 +552,9 @@ bool QgsCoordinateReferenceSystem::hasAxisInverted() const
547552
{
548553
if ( d->mAxisInvertedDirty )
549554
{
555+
#if PROJ_VERSION_MAJOR>=6
556+
d->mAxisInverted = QgsProjUtils::axisOrderIsSwapped( d->mPj.get() );
557+
#else
550558
OGRAxisOrientation orientation;
551559
OSRGetAxis( d->mCRS, OSRIsGeographic( d->mCRS ) ? "GEOGCS" : "PROJCS", 0, &orientation );
552560

@@ -564,6 +572,7 @@ bool QgsCoordinateReferenceSystem::hasAxisInverted() const
564572
}
565573

566574
d->mAxisInverted = orientation == OAO_North;
575+
#endif
567576
d->mAxisInvertedDirty = false;
568577
}
569578

@@ -588,25 +597,48 @@ bool QgsCoordinateReferenceSystem::createFromWkt( const QString &wkt )
588597
d->mIsValid = false;
589598
d->mWkt.clear();
590599
d->mProj4.clear();
591-
592600
if ( wkt.isEmpty() )
593601
{
594602
QgsDebugMsgLevel( QStringLiteral( "theWkt is uninitialized, operation failed" ), 4 );
595603
return d->mIsValid;
596604
}
605+
606+
bool res = false;
607+
#if PROJ_VERSION_MAJOR>=6
608+
PROJ_STRING_LIST warnings = nullptr;
609+
PROJ_STRING_LIST grammerErrors = nullptr;
610+
d->mPj.reset( proj_create_from_wkt( QgsProjContext::get(), wkt.toLatin1().constData(), nullptr, &warnings, &grammerErrors ) );
611+
res = static_cast< bool >( d->mPj );
612+
if ( !res )
613+
{
614+
QgsDebugMsg( QStringLiteral( "\n---------------------------------------------------------------" ) );
615+
QgsDebugMsg( QStringLiteral( "This CRS could *** NOT *** be set from the supplied Wkt " ) );
616+
QgsDebugMsg( "INPUT: " + wkt );
617+
for ( auto iter = warnings; iter && *iter; ++iter )
618+
QgsDebugMsg( *iter );
619+
for ( auto iter = grammerErrors; iter && *iter; ++iter )
620+
QgsDebugMsg( *iter );
621+
QgsDebugMsg( QStringLiteral( "---------------------------------------------------------------\n" ) );
622+
}
623+
proj_string_list_destroy( warnings );
624+
proj_string_list_destroy( grammerErrors );
625+
#else
597626
QByteArray ba = wkt.toLatin1();
598627
const char *pWkt = ba.data();
599628

600629
OGRErr myInputResult = OSRImportFromWkt( d->mCRS, const_cast< char ** >( & pWkt ) );
601-
602-
if ( myInputResult != OGRERR_NONE )
630+
res = myInputResult != OGRERR_NONE;
631+
if ( !res )
603632
{
604633
QgsDebugMsg( QStringLiteral( "\n---------------------------------------------------------------" ) );
605634
QgsDebugMsg( QStringLiteral( "This CRS could *** NOT *** be set from the supplied Wkt " ) );
606635
QgsDebugMsg( "INPUT: " + wkt );
607636
QgsDebugMsg( QStringLiteral( "UNUSED WKT: %1" ).arg( pWkt ) );
608637
QgsDebugMsg( QStringLiteral( "---------------------------------------------------------------\n" ) );
609-
638+
}
639+
#endif
640+
if ( !res )
641+
{
610642
sCRSWktLock.lockForWrite();
611643
sWktCache.insert( wkt, *this );
612644
sCRSWktLock.unlock();
@@ -1129,17 +1161,23 @@ void QgsCoordinateReferenceSystem::setProj4String( const QString &proj4String )
11291161

11301162
OSRDestroySpatialReference( d->mCRS );
11311163
d->mCRS = OSRNewSpatialReference( nullptr );
1164+
11321165
const QString trimmed = proj4String.trimmed();
11331166
d->mIsValid = OSRImportFromProj4( d->mCRS, trimmed.toLatin1().constData() ) == OGRERR_NONE;
1167+
11341168
#if PROJ_VERSION_MAJOR>=6
11351169
PJ_CONTEXT *ctx = QgsProjContext::get();
1136-
QgsProjUtils::proj_pj_unique_ptr proj( proj_create( ctx, trimmed.toLatin1().constData() ) );
1137-
if ( !proj )
1170+
d->mPj.reset( proj_create( ctx, trimmed.toLatin1().constData() ) );
1171+
if ( !d->mPj )
11381172
{
11391173
const int errNo = proj_context_errno( ctx );
11401174
QgsDebugMsg( QStringLiteral( "proj string rejected: %1" ).arg( proj_errno_string( errNo ) ) );
11411175
d->mIsValid = false;
11421176
}
1177+
else
1178+
{
1179+
d->mIsValid = true;
1180+
}
11431181
#else
11441182
// OSRImportFromProj4() may accept strings that are not valid proj.4 strings,
11451183
// e.g if they lack a +ellps parameter, it will automatically add +ellps=WGS84, but as
@@ -1870,6 +1908,7 @@ bool QgsCoordinateReferenceSystem::loadIds( QHash<int, QString> &wkts )
18701908

18711909
int QgsCoordinateReferenceSystem::syncDatabase()
18721910
{
1911+
#if 1
18731912
setlocale( LC_ALL, "C" );
18741913
QString dbFilePath = QgsApplication::srsDatabaseFilePath();
18751914
syncDatumTransform( dbFilePath );
@@ -2370,6 +2409,7 @@ bool QgsCoordinateReferenceSystem::syncDatumTransform( const QString &dbPath )
23702409
return false;
23712410
}
23722411

2412+
#endif
23732413
return true;
23742414
}
23752415

‎src/core/qgscoordinatereferencesystem_p.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
#include "qgscoordinatereferencesystem.h"
3333
#include <ogr_srs_api.h>
3434

35+
#if PROJ_VERSION_MAJOR>=6
36+
#include <proj.h>
37+
#include "qgsprojutils.h"
38+
#endif
39+
3540
#ifdef DEBUG
3641
typedef struct OGRSpatialReferenceHS *OGRSpatialReferenceH;
3742
#else
@@ -73,6 +78,10 @@ class QgsCoordinateReferenceSystemPrivate : public QSharedData
7378
{
7479
mCRS = OSRNewSpatialReference( nullptr );
7580
}
81+
#if PROJ_VERSION_MAJOR>=6
82+
if ( mIsValid && mPj.get() )
83+
mPj.reset( proj_clone( QgsProjContext::get(), mPj.get() ) );
84+
#endif
7685
}
7786

7887
~QgsCoordinateReferenceSystemPrivate()
@@ -107,6 +116,9 @@ class QgsCoordinateReferenceSystemPrivate : public QSharedData
107116
//! Whether this CRS is properly defined and valid
108117
bool mIsValid = false;
109118

119+
#if PROJ_VERSION_MAJOR>=6
120+
QgsProjUtils::proj_pj_unique_ptr mPj;
121+
#endif
110122
OGRSpatialReferenceH mCRS;
111123

112124
QString mValidationHint;

0 commit comments

Comments
 (0)
Please sign in to comment.