Skip to content
Permalink
Browse files

Implement logic for calculating datum transforms to use

  • Loading branch information
nyalldawson committed Nov 5, 2017
1 parent 7af5545 commit 3d1b8aeeb35d51570dd806483df6be21fc970054
Showing with 62 additions and 0 deletions.
  1. +16 −0 src/core/qgscoordinatetransformcontext.cpp
  2. +46 −0 tests/src/python/test_qgscoordinatetransformcontext.py
@@ -129,5 +129,21 @@ bool QgsCoordinateTransformContext::addSourceDestinationDatumTransform( const Qg

QPair<int, int> QgsCoordinateTransformContext::calculateDatumTransforms( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination ) const
{
QString srcKey = source.authid();
QString destKey = destination.authid();

d->mLock.lockForRead();
// highest priority is exact match for source/dest pair
QPair< int, int > res = d->mSourceDestDatumTransforms.value( qMakePair( srcKey, destKey ), qMakePair( -1, -1 ) );
if ( res.first != -1 && res.second != -1 )
{
d->mLock.unlock();
return res;
}

// fallback to checking src and dest separately
int srcTransform = d->mSourceDatumTransforms.value( srcKey, -1 );
int destTransform = d->mDestDatumTransforms.value( destKey, -1 );
d->mLock.unlock();
return qMakePair( srcTransform, destTransform );
}
@@ -134,6 +134,52 @@ def testSourceDestinationDatumTransforms(self):
context.clear()
self.assertEqual(context.sourceDestinationDatumTransforms(), {})

def testCalculate(self):
context = QgsCoordinateTransformContext()

#empty context
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(-1, -1))

#add src transform
context.addSourceDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'), 1)
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(-1, -1))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(1, -1))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:4283'),
QgsCoordinateReferenceSystem('EPSG:28356')),
(-1, -1))

#add dest transform
context.addDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:4283'), 2)
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4326')),
(-1, -1))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(-1, 2))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:4283'),
QgsCoordinateReferenceSystem('EPSG:3111')),
(-1, -1))

#add specific source/dest pair - should take precedence
context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem('EPSG:4283'),
3, 4)
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(3, 4))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:3111'),
QgsCoordinateReferenceSystem('EPSG:4283')),
(-1, 2))
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:28356'),
QgsCoordinateReferenceSystem('EPSG:3111')),
(1, -1))


if __name__ == '__main__':
unittest.main()

0 comments on commit 3d1b8ae

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