Skip to content

Commit

Permalink
Add method to retrieve grid details from a proj string
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 3, 2019
1 parent 56c265f commit fe4ea33
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/core/qgsprojutils.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgis.h"
#include <QString>
#include <QSet>
#include <QRegularExpression>

#if PROJ_VERSION_MAJOR>=6
#include <proj.h>
Expand Down Expand Up @@ -191,6 +192,40 @@ bool QgsProjUtils::coordinateOperationIsAvailable( const QString &projDef )
return static_cast< bool >( proj_coordoperation_is_instantiable( context, coordinateOperation.get() ) );
}

QList<QgsDatumTransform::GridDetails> QgsProjUtils::gridsUsed( const QString &proj )
{
static QRegularExpression sRegex( QStringLiteral( "\\+(?:nad)?grids=(.*?)\\s" ) );

QList< QgsDatumTransform::GridDetails > grids;
QRegularExpressionMatchIterator matches = sRegex.globalMatch( proj );
while ( matches.hasNext() )
{
const QRegularExpressionMatch match = matches.next();
const QString gridName = match.captured( 1 );
QgsDatumTransform::GridDetails grid;
grid.shortName = gridName;
#if PROJ_VERSION_MAJOR >= 6
#if PROJ_VERSION_MINOR >= 2
const char *fullName = nullptr;
const char *packageName = nullptr;
const char *url = nullptr;
int directDownload = 0;
int openLicense = 0;
int available = 0;
proj_grid_get_info_from_database( QgsProjContext::get(), gridName.toUtf8().constData(), &fullName, &packageName, &url, &directDownload, &openLicense, &available );
grid.fullName = QString( fullName );
grid.packageName = QString( packageName );
grid.url = QString( url );
grid.directDownload = directDownload;
grid.openLicense = openLicense;
grid.isAvailable = available;
#endif
#endif
grids.append( grid );
}
return grids;
}

#if 0
QStringList QgsProjUtils::nonAvailableGrids( const QString &projDef )
{
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsprojutils.h
Expand Up @@ -21,6 +21,7 @@

#include "qgis_core.h"
#include "qgsconfig.h"
#include "qgsdatumtransform.h"
#include <memory>
#include <QStringList>

Expand Down Expand Up @@ -107,6 +108,12 @@ class CORE_EXPORT QgsProjUtils
*/
static bool coordinateOperationIsAvailable( const QString &projDef );

/**
* Returns a list of grids used by the given \a proj string.
*/
static QList< QgsDatumTransform::GridDetails > gridsUsed( const QString &proj );


#if 0 // not possible in current Proj 6 API

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/src/core/testqgsprojutils.cpp
Expand Up @@ -36,6 +36,7 @@ class TestQgsProjUtils: public QObject
void usesAngularUnits();
void axisOrderIsSwapped();
void searchPath();
void gridsUsed();

};

Expand Down Expand Up @@ -107,5 +108,19 @@ void TestQgsProjUtils::searchPath()
#endif
}

void TestQgsProjUtils::gridsUsed()
{
#if PROJ_VERSION_MAJOR>=6
// ensure local user-writable path is present in Proj search paths
const QList< QgsDatumTransform::GridDetails > grids = QgsProjUtils::gridsUsed( QStringLiteral( "+proj=pipeline +step +proj=axisswap +order=2,1 +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +inv +proj=hgridshift +grids=GDA94_GDA2020_conformal_and_distortion.gsb +step +proj=unitconvert +xy_in=rad +xy_out=deg +step +proj=axisswap +order=2,1" ) );
QCOMPARE( grids.count(), 1 );
QCOMPARE( grids.at( 0 ).shortName, QStringLiteral( "GDA94_GDA2020_conformal_and_distortion.gsb" ) );
#if PROJ_VERSION_MINOR>=2
QCOMPARE( grids.at( 0 ).packageName, QStringLiteral( "proj-datumgrid-oceania" ) );
QVERIFY( grids.at( 0 ).directDownload );
#endif
#endif
}

QGSTEST_MAIN( TestQgsProjUtils )
#include "testqgsprojutils.moc"

0 comments on commit fe4ea33

Please sign in to comment.