Skip to content

Commit

Permalink
Allow removing existing transforms from context
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 15, 2017
1 parent 8a0bd08 commit 2e2e124
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/qgscoordinatetransformcontext.sip
Expand Up @@ -52,6 +52,8 @@ class QgsCoordinateTransformContext
Adds a new ``transform`` to use when projecting coordinates from the specified source
``crs``.

If ``transform`` is -1, then any existing source transform for the ``crs`` will be removed.

Returns true if the new transform was added successfully.

\warning Transforms set using this method may be overridden by specific source/destination
Expand Down Expand Up @@ -81,6 +83,8 @@ class QgsCoordinateTransformContext
Adds a new ``transform`` to use when projecting coordinates to the specified destination
``crs``.

If ``transform`` is -1, then any existing destination transform for the ``crs`` will be removed.

Returns true if the new transform was added successfully.

\warning Transforms set using this method may be overridden by specific source/destination
Expand Down Expand Up @@ -112,6 +116,9 @@ class QgsCoordinateTransformContext
Adds a new ``sourceTransform`` and ``destinationTransform`` to use when projecting coordinates
from the the specified ``sourceCrs`` to the specified ``destinationCrs``.

If either ``sourceTransform`` or ``destinationTransform`` is -1, then any existing source to destination
transform for the crs pair will be removed.

Returns true if the new transform pair was added successfully.

.. note::
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgscoordinatetransformcontext.cpp
Expand Up @@ -34,6 +34,12 @@ bool QgsCoordinateTransformContext::addSourceDatumTransform( const QgsCoordinate
if ( !crs.isValid() )
return false;

if ( transform == -1 )
{
mSourceDatumTransforms.remove( crs.authid() );
return true;
}

mSourceDatumTransforms.insert( crs.authid(), transform );
return true;
}
Expand All @@ -48,6 +54,12 @@ bool QgsCoordinateTransformContext::addDestinationDatumTransform( const QgsCoord
if ( !crs.isValid() )
return false;

if ( transform == -1 )
{
mDestDatumTransforms.remove( crs.authid() );
return true;
}

mDestDatumTransforms.insert( crs.authid(), transform );
return true;
}
Expand All @@ -62,6 +74,12 @@ bool QgsCoordinateTransformContext::addSourceDestinationDatumTransform( const Qg
if ( !sourceCrs.isValid() || !destinationCrs.isValid() )
return false;

if ( sourceTransform == -1 || destinationTransform == -1 )
{
mSourceDestDatumTransforms.remove( qMakePair( sourceCrs.authid(), destinationCrs.authid() ) );
return true;
}

mSourceDestDatumTransforms.insert( qMakePair( sourceCrs.authid(), destinationCrs.authid() ), qMakePair( sourceTransform, destinationTransform ) );
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgscoordinatetransformcontext.h
Expand Up @@ -61,6 +61,8 @@ class CORE_EXPORT QgsCoordinateTransformContext
* Adds a new \a transform to use when projecting coordinates from the specified source
* \a crs.
*
* If \a transform is -1, then any existing source transform for the \a crs will be removed.
*
* Returns true if the new transform was added successfully.
*
* \warning Transforms set using this method may be overridden by specific source/destination
Expand Down Expand Up @@ -88,6 +90,8 @@ class CORE_EXPORT QgsCoordinateTransformContext
* Adds a new \a transform to use when projecting coordinates to the specified destination
* \a crs.
*
* If \a transform is -1, then any existing destination transform for the \a crs will be removed.
*
* Returns true if the new transform was added successfully.
*
* \warning Transforms set using this method may be overridden by specific source/destination
Expand All @@ -114,6 +118,9 @@ class CORE_EXPORT QgsCoordinateTransformContext
* Adds a new \a sourceTransform and \a destinationTransform to use when projecting coordinates
* from the the specified \a sourceCrs to the specified \a destinationCrs.
*
* If either \a sourceTransform or \a destinationTransform is -1, then any existing source to destination
* transform for the crs pair will be removed.
*
* Returns true if the new transform pair was added successfully.
*
* \note Transforms set using this method will override any specific source or destination
Expand Down
37 changes: 37 additions & 0 deletions tests/src/python/test_qgscoordinatetransformcontext.py
Expand Up @@ -39,6 +39,14 @@ def testSourceDatumTransforms(self):
self.assertFalse(context.addSourceDatumTransform(QgsCoordinateReferenceSystem(), 4))
self.assertEqual(context.sourceDatumTransforms(), {'EPSG:3111': 1, 'EPSG:28356': 3})

# removing non-existing
self.assertTrue(context.addSourceDatumTransform(QgsCoordinateReferenceSystem(28357), -1))
self.assertEqual(context.sourceDatumTransforms(), {'EPSG:3111': 1, 'EPSG:28356': 3})

# remove existing
self.assertTrue(context.addSourceDatumTransform(QgsCoordinateReferenceSystem(28356), -1))
self.assertEqual(context.sourceDatumTransforms(), {'EPSG:3111': 1})

context.clear()
self.assertEqual(context.sourceDatumTransforms(), {})

Expand All @@ -58,6 +66,14 @@ def testDestDatumTransforms(self):
self.assertFalse(context.addDestinationDatumTransform(QgsCoordinateReferenceSystem(), 4))
self.assertEqual(context.destinationDatumTransforms(), {'EPSG:3111': 1, 'EPSG:28356': 3})

# removing non-existing
self.assertTrue(context.addDestinationDatumTransform(QgsCoordinateReferenceSystem(28357), -1))
self.assertEqual(context.destinationDatumTransforms(), {'EPSG:3111': 1, 'EPSG:28356': 3})

# remove existing
self.assertTrue(context.addDestinationDatumTransform(QgsCoordinateReferenceSystem(28356), -1))
self.assertEqual(context.destinationDatumTransforms(), {'EPSG:3111': 1})

context.clear()
self.assertEqual(context.destinationDatumTransforms(), {})

Expand Down Expand Up @@ -94,6 +110,27 @@ def testSourceDestinationDatumTransforms(self):
('EPSG:28356', 'EPSG:4283'): (3, 4),
('EPSG:28356', 'EPSG:28357'): (9, 11)})

# removing non-existing
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(28357),
QgsCoordinateReferenceSystem(28356), -1, -1))
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2),
('EPSG:28356', 'EPSG:4283'): (3, 4),
('EPSG:28356', 'EPSG:28357'): (9, 11)})
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(3111),
QgsCoordinateReferenceSystem(28356), -1, -1))
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2),
('EPSG:28356', 'EPSG:4283'): (3, 4),
('EPSG:28356', 'EPSG:28357'): (9, 11)})

# remove existing
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(3111),
QgsCoordinateReferenceSystem(4283), -1, 3))
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:28356', 'EPSG:4283'): (3, 4),
('EPSG:28356', 'EPSG:28357'): (9, 11)})
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(28356),
QgsCoordinateReferenceSystem(28357), 1, -1))
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:28356', 'EPSG:4283'): (3, 4)})

context.clear()
self.assertEqual(context.sourceDestinationDatumTransforms(), {})

Expand Down

0 comments on commit 2e2e124

Please sign in to comment.