Skip to content

Commit ab5d895

Browse files
committed
Add some more datum related unit tests
1 parent 69c3deb commit ab5d895

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

tests/src/python/test_qgscoordinatetransform.py

+11
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ def testProjectContext(self):
207207
self.assertEqual(transform.sourceDatumTransformId(), 1)
208208
self.assertEqual(transform.destinationDatumTransformId(), 2)
209209

210+
def testTransformInfo(self):
211+
# hopefully this transform is available on all platforms!
212+
transforms = QgsCoordinateTransform.datumTransformations(QgsCoordinateReferenceSystem(4613), QgsCoordinateReferenceSystem(4326))
213+
self.assertTrue(len(transforms) > 0)
214+
self.assertIn('+towgs84=-403,684,41', [QgsCoordinateTransform.datumTransformToProj(t.sourceTransformId) for t in transforms])
215+
self.assertIn('+towgs84=-403,684,41', [QgsCoordinateTransform.datumTransformToProj(t.destinationTransformId) for t in transforms])
216+
self.assertIn('EPSG:4613', [QgsCoordinateTransform.datumTransformInfo(t.destinationTransformId).sourceCrsAuthId for t in
217+
transforms])
218+
self.assertIn('EPSG:4326', [QgsCoordinateTransform.datumTransformInfo(t.destinationTransformId).destinationCrsAuthId for t in
219+
transforms])
220+
210221

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

tests/src/python/test_qgscoordinatetransformcontext.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@
1818
QgsCoordinateTransformContext,
1919
QgsCoordinateTransform,
2020
QgsReadWriteContext,
21-
QgsProject)
21+
QgsProject,
22+
QgsSettings)
2223
from qgis.testing import start_app, unittest
2324
from qgis.PyQt.QtXml import QDomDocument
2425
from qgis.PyQt.QtTest import QSignalSpy
26+
from qgis.PyQt.QtCore import QCoreApplication
2527

2628
app = start_app()
2729

2830

2931
class TestQgsCoordinateTransformContext(unittest.TestCase):
3032

33+
@classmethod
34+
def setUpClass(cls):
35+
"""Run before all tests"""
36+
QCoreApplication.setOrganizationName("QGIS_Test")
37+
QCoreApplication.setOrganizationDomain("TestPyQgsWFSProvider.com")
38+
QCoreApplication.setApplicationName("TestPyQgsWFSProvider")
39+
QgsSettings().clear()
40+
3141
@unittest.skip('ifdefed out in c++ until required')
3242
def testSourceDatumTransforms(self):
3343
context = QgsCoordinateTransformContext()
@@ -249,6 +259,10 @@ def testCalculateSourceDest(self):
249259
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:28356'),
250260
QgsCoordinateReferenceSystem('EPSG:3111')),
251261
QgsCoordinateTransform.TransformPair(-1, -1))
262+
# check that reverse transforms are automatically supported
263+
self.assertEqual(context.calculateDatumTransforms(QgsCoordinateReferenceSystem('EPSG:4283'),
264+
QgsCoordinateReferenceSystem('EPSG:28356')),
265+
QgsCoordinateTransform.TransformPair(4, 3))
252266

253267
@unittest.skip('ifdefed out in c++ until required')
254268
def testWriteReadXmlSingleVariant(self):
@@ -320,6 +334,34 @@ def testProject(self):
320334
self.assertEqual(len(context_changed_spy), 1)
321335
self.assertEqual(project.transformContext().sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): QgsCoordinateTransform.TransformPair(1, 2)})
322336

337+
def testReadWriteSettings(self):
338+
context = QgsCoordinateTransformContext()
339+
context.readSettings()
340+
# should be empty
341+
self.assertEqual(context.sourceDestinationDatumTransforms(), {})
342+
343+
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
344+
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2))
345+
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'),
346+
QgsCoordinateReferenceSystem(4283), 3, 4))
347+
348+
self.assertEqual(context.sourceDestinationDatumTransforms(),
349+
{('EPSG:3111', 'EPSG:4283'): QgsCoordinateTransform.TransformPair(1, 2),
350+
('EPSG:28356', 'EPSG:4283'): QgsCoordinateTransform.TransformPair(3, 4)})
351+
352+
# save to settings
353+
context.writeSettings()
354+
355+
# restore from settings
356+
context2 = QgsCoordinateTransformContext()
357+
self.assertEqual(context2.sourceDestinationDatumTransforms(), {})
358+
context2.readSettings()
359+
360+
# check result
361+
self.assertEqual(context2.sourceDestinationDatumTransforms(),
362+
{('EPSG:3111', 'EPSG:4283'): QgsCoordinateTransform.TransformPair(1, 2),
363+
('EPSG:28356', 'EPSG:4283'): QgsCoordinateTransform.TransformPair(3, 4)})
364+
323365

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

0 commit comments

Comments
 (0)