Skip to content

Commit 2e2e124

Browse files
committedDec 15, 2017
Allow removing existing transforms from context
1 parent 8a0bd08 commit 2e2e124

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
 

‎python/core/qgscoordinatetransformcontext.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class QgsCoordinateTransformContext
5252
Adds a new ``transform`` to use when projecting coordinates from the specified source
5353
``crs``.
5454

55+
If ``transform`` is -1, then any existing source transform for the ``crs`` will be removed.
56+
5557
Returns true if the new transform was added successfully.
5658

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

86+
If ``transform`` is -1, then any existing destination transform for the ``crs`` will be removed.
87+
8488
Returns true if the new transform was added successfully.
8589

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

119+
If either ``sourceTransform`` or ``destinationTransform`` is -1, then any existing source to destination
120+
transform for the crs pair will be removed.
121+
115122
Returns true if the new transform pair was added successfully.
116123

117124
.. note::

‎src/core/qgscoordinatetransformcontext.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ bool QgsCoordinateTransformContext::addSourceDatumTransform( const QgsCoordinate
3434
if ( !crs.isValid() )
3535
return false;
3636

37+
if ( transform == -1 )
38+
{
39+
mSourceDatumTransforms.remove( crs.authid() );
40+
return true;
41+
}
42+
3743
mSourceDatumTransforms.insert( crs.authid(), transform );
3844
return true;
3945
}
@@ -48,6 +54,12 @@ bool QgsCoordinateTransformContext::addDestinationDatumTransform( const QgsCoord
4854
if ( !crs.isValid() )
4955
return false;
5056

57+
if ( transform == -1 )
58+
{
59+
mDestDatumTransforms.remove( crs.authid() );
60+
return true;
61+
}
62+
5163
mDestDatumTransforms.insert( crs.authid(), transform );
5264
return true;
5365
}
@@ -62,6 +74,12 @@ bool QgsCoordinateTransformContext::addSourceDestinationDatumTransform( const Qg
6274
if ( !sourceCrs.isValid() || !destinationCrs.isValid() )
6375
return false;
6476

77+
if ( sourceTransform == -1 || destinationTransform == -1 )
78+
{
79+
mSourceDestDatumTransforms.remove( qMakePair( sourceCrs.authid(), destinationCrs.authid() ) );
80+
return true;
81+
}
82+
6583
mSourceDestDatumTransforms.insert( qMakePair( sourceCrs.authid(), destinationCrs.authid() ), qMakePair( sourceTransform, destinationTransform ) );
6684
return true;
6785
}

‎src/core/qgscoordinatetransformcontext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class CORE_EXPORT QgsCoordinateTransformContext
6161
* Adds a new \a transform to use when projecting coordinates from the specified source
6262
* \a crs.
6363
*
64+
* If \a transform is -1, then any existing source transform for the \a crs will be removed.
65+
*
6466
* Returns true if the new transform was added successfully.
6567
*
6668
* \warning Transforms set using this method may be overridden by specific source/destination
@@ -88,6 +90,8 @@ class CORE_EXPORT QgsCoordinateTransformContext
8890
* Adds a new \a transform to use when projecting coordinates to the specified destination
8991
* \a crs.
9092
*
93+
* If \a transform is -1, then any existing destination transform for the \a crs will be removed.
94+
*
9195
* Returns true if the new transform was added successfully.
9296
*
9397
* \warning Transforms set using this method may be overridden by specific source/destination
@@ -114,6 +118,9 @@ class CORE_EXPORT QgsCoordinateTransformContext
114118
* Adds a new \a sourceTransform and \a destinationTransform to use when projecting coordinates
115119
* from the the specified \a sourceCrs to the specified \a destinationCrs.
116120
*
121+
* If either \a sourceTransform or \a destinationTransform is -1, then any existing source to destination
122+
* transform for the crs pair will be removed.
123+
*
117124
* Returns true if the new transform pair was added successfully.
118125
*
119126
* \note Transforms set using this method will override any specific source or destination

‎tests/src/python/test_qgscoordinatetransformcontext.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def testSourceDatumTransforms(self):
3939
self.assertFalse(context.addSourceDatumTransform(QgsCoordinateReferenceSystem(), 4))
4040
self.assertEqual(context.sourceDatumTransforms(), {'EPSG:3111': 1, 'EPSG:28356': 3})
4141

42+
# removing non-existing
43+
self.assertTrue(context.addSourceDatumTransform(QgsCoordinateReferenceSystem(28357), -1))
44+
self.assertEqual(context.sourceDatumTransforms(), {'EPSG:3111': 1, 'EPSG:28356': 3})
45+
46+
# remove existing
47+
self.assertTrue(context.addSourceDatumTransform(QgsCoordinateReferenceSystem(28356), -1))
48+
self.assertEqual(context.sourceDatumTransforms(), {'EPSG:3111': 1})
49+
4250
context.clear()
4351
self.assertEqual(context.sourceDatumTransforms(), {})
4452

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

69+
# removing non-existing
70+
self.assertTrue(context.addDestinationDatumTransform(QgsCoordinateReferenceSystem(28357), -1))
71+
self.assertEqual(context.destinationDatumTransforms(), {'EPSG:3111': 1, 'EPSG:28356': 3})
72+
73+
# remove existing
74+
self.assertTrue(context.addDestinationDatumTransform(QgsCoordinateReferenceSystem(28356), -1))
75+
self.assertEqual(context.destinationDatumTransforms(), {'EPSG:3111': 1})
76+
6177
context.clear()
6278
self.assertEqual(context.destinationDatumTransforms(), {})
6379

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

113+
# removing non-existing
114+
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(28357),
115+
QgsCoordinateReferenceSystem(28356), -1, -1))
116+
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2),
117+
('EPSG:28356', 'EPSG:4283'): (3, 4),
118+
('EPSG:28356', 'EPSG:28357'): (9, 11)})
119+
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(3111),
120+
QgsCoordinateReferenceSystem(28356), -1, -1))
121+
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2),
122+
('EPSG:28356', 'EPSG:4283'): (3, 4),
123+
('EPSG:28356', 'EPSG:28357'): (9, 11)})
124+
125+
# remove existing
126+
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(3111),
127+
QgsCoordinateReferenceSystem(4283), -1, 3))
128+
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:28356', 'EPSG:4283'): (3, 4),
129+
('EPSG:28356', 'EPSG:28357'): (9, 11)})
130+
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(28356),
131+
QgsCoordinateReferenceSystem(28357), 1, -1))
132+
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:28356', 'EPSG:4283'): (3, 4)})
133+
97134
context.clear()
98135
self.assertEqual(context.sourceDestinationDatumTransforms(), {})
99136

0 commit comments

Comments
 (0)
Please sign in to comment.