Skip to content
Permalink
Browse files

Add method to determine whether transform context has a valid transform

for a specific src/dest CRS pair
  • Loading branch information
nyalldawson committed Nov 28, 2017
1 parent f55da4a commit d2353e7c010c67cf7c46cd2ec897f5f7b2323fa2
@@ -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
@@ -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
@@ -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();
@@ -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.
@@ -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))

0 comments on commit d2353e7

Please sign in to comment.
You can’t perform that action at this time.