Skip to content

Commit

Permalink
Create ellipsoid definitions using proj db
Browse files Browse the repository at this point in the history
Sponsored by ICSM
  • Loading branch information
nyalldawson committed Mar 27, 2019
1 parent ef8d893 commit edd84e9
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/core/qgsellipsoidutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include "qgslogger.h"
#include "qgsmessagelog.h"
#include <sqlite3.h>
#include <QCollator>

#if PROJ_VERSION_MAJOR>=6
#include <proj.h>
#endif

QReadWriteLock QgsEllipsoidUtils::sEllipsoidCacheLock;
QHash< QString, QgsEllipsoidUtils::EllipsoidParameters > QgsEllipsoidUtils::sEllipsoidCache;
Expand Down Expand Up @@ -186,12 +191,47 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
sDefinitionCacheLock.unlock();

sDefinitionCacheLock.lockForWrite();
QList<QgsEllipsoidUtils::EllipsoidDefinition> defs;

#if PROJ_VERSION_MAJOR>=6
// use proj to get ellipsoids
const PJ_ELLPS *ellipsoid = proj_list_ellps();
while ( ellipsoid->name )
{
EllipsoidDefinition def;
def.acronym = ellipsoid->id ;
def.description = ellipsoid->name;
const QString majorString( ellipsoid->major );
def.parameters.semiMajor = majorString.midRef( 2 ).toDouble();
const QString minorString( ellipsoid->ell );
if ( minorString.startsWith( 'b' ) )
{
// b= style
def.parameters.semiMinor = minorString.midRef( 2 ).toDouble();
def.parameters.inverseFlattening = def.parameters.semiMajor / ( def.parameters.semiMajor - def.parameters.semiMinor );
}
else
{
// rf= style
def.parameters.inverseFlattening = minorString.midRef( 2 ).toDouble();
def.parameters.semiMinor = def.parameters.semiMajor * ( 1 - def.parameters.inverseFlattening );
}

QgsCoordinateReferenceSystem crs = QgsCoordinateReferenceSystem::fromProj4( QStringLiteral( "+proj=longlat +ellps=%1 +no_defs" ).arg( def.acronym ) );
if ( crs.isValid() )
def.parameters.crs = crs;

defs << def;

ellipsoid++;
}


#else
sqlite3_database_unique_ptr database;
sqlite3_statement_unique_ptr statement;
int result;

QList<QgsEllipsoidUtils::EllipsoidDefinition> defs;

//check the db is available
result = database.open_v2( QgsApplication::srsDatabaseFilePath(), SQLITE_OPEN_READONLY, nullptr );
if ( result )
Expand Down Expand Up @@ -221,6 +261,8 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
}
}

#endif

sDefinitionCache = defs;
sDefinitionCacheLock.unlock();

Expand Down

0 comments on commit edd84e9

Please sign in to comment.