Add method to determine whether transform context has a valid transform

for a specific src/dest CRS pair
This commit is contained in:
Nyall Dawson 2017-11-28 10:49:29 +10:00
parent f55da4affb
commit d2353e7c01
5 changed files with 29 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -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.

View File

@ -94,8 +94,15 @@ class TestQgsCoordinateTransformContext(unittest.TestCase):
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))