Skip to content

Commit

Permalink
Add method to determine whether transform context has a valid transform
Browse files Browse the repository at this point in the history
for a specific src/dest CRS pair
  • Loading branch information
nyalldawson committed Dec 15, 2017
1 parent f55da4a commit d2353e7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
8 changes: 8 additions & 0 deletions python/core/qgscoordinatetransformcontext.sip
Expand Up @@ -110,6 +110,14 @@ class QgsCoordinateTransformContext
.. seealso:: addSourceDestinationDatumTransform()
%End

bool hasTransform( const QgsCoordinateReferenceSystem &source,
const QgsCoordinateReferenceSystem &destination ) const;
%Docstring
Returns true if the context has a valid datum transform to use
when transforming from the specified ``source`` CRS to ``destination`` CRS.
:rtype: bool
%End

QPair< int, int > calculateDatumTransforms( const QgsCoordinateReferenceSystem &source,
const QgsCoordinateReferenceSystem &destination ) const;
%Docstring
Expand Down
4 changes: 1 addition & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -12535,10 +12535,8 @@ bool QgisApp::askForDatumTransform( QgsCoordinateReferenceSystem sourceCrs, QgsC
bool ok = false;

QgsCoordinateTransformContext context = QgsProject::instance()->transformContext();
QPair<int, int> dt = context.calculateDatumTransforms( sourceCrs, destinationCrs );
if ( dt != qMakePair( -1, -1 ) )
if ( context.hasTransform( sourceCrs, destinationCrs ) )
{
// already defined by user
ok = true;
}
else
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgscoordinatetransformcontext.cpp
Expand Up @@ -124,6 +124,12 @@ void QgsCoordinateTransformContext::removeSourceDestinationDatumTransform( const
d->mSourceDestDatumTransforms.remove( qMakePair( sourceCrs.authid(), destinationCrs.authid() ) );
}

bool QgsCoordinateTransformContext::hasTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination ) const
{
QPair<int, int> t = calculateDatumTransforms( source, destination );
return t.first != -1 || t.second != -1;
}

QPair<int, int> QgsCoordinateTransformContext::calculateDatumTransforms( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination ) const
{
QString srcKey = source.authid();
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgscoordinatetransformcontext.h
Expand Up @@ -209,6 +209,13 @@ class CORE_EXPORT QgsCoordinateTransformContext
void removeSourceDestinationDatumTransform( const QgsCoordinateReferenceSystem &sourceCrs,
const QgsCoordinateReferenceSystem &destinationCrs );

/**
* Returns true if the context has a valid datum transform to use
* when transforming from the specified \a source CRS to \a destination CRS.
*/
bool hasTransform( const QgsCoordinateReferenceSystem &source,
const QgsCoordinateReferenceSystem &destination ) const;

/**
* Returns the pair of source and destination datum transforms to use
* for a transform from the specified \a source CRS to \a destination CRS.
Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_qgscoordinatetransformcontext.py
Expand Up @@ -94,8 +94,15 @@ def testDestDatumTransforms(self):
def testSourceDestinationDatumTransforms(self):
context = QgsCoordinateTransformContext()
self.assertEqual(context.sourceDestinationDatumTransforms(), {})
self.assertFalse(context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3111'), QgsCoordinateReferenceSystem('EPSG:4283')))
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2))
self.assertTrue(
context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3111'), QgsCoordinateReferenceSystem('EPSG:4283')))
self.assertFalse(
context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3111'), QgsCoordinateReferenceSystem('EPSG:4326')))
self.assertFalse(
context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3113'), QgsCoordinateReferenceSystem('EPSG:4283')))
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2)})
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem(4283), 3, 4))
Expand Down

0 comments on commit d2353e7

Please sign in to comment.