Skip to content
Permalink
Browse files

Unit tests for transform context project<->layer sync

  • Loading branch information
elpaso authored and nyalldawson committed Apr 17, 2019
1 parent 3d687e5 commit be374104ea05d9b128b28f24e8d9c43c0f558db6
@@ -19,6 +19,7 @@
import qgis # NOQA

from qgis.core import (QgsProject,
QgsCoordinateTransformContext,
QgsProjectDirtyBlocker,
QgsApplication,
QgsUnitTypes,
@@ -1190,6 +1191,16 @@ def testColorScheme(self):
del p
self.assertEqual(len(spy), 0)

def testTransformContextSignalIsEmitted(self):
"""Test that when a project transform context changes a transformContextChanged signal is emitted"""

p = QgsProject()
spy = QSignalSpy(p.transformContextChanged)
ctx = QgsCoordinateTransformContext()
ctx.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857), 1234, 1235)
p.setTransformContext(ctx)
self.assertEqual(len(spy), 1)


if __name__ == '__main__':
unittest.main()
@@ -47,7 +47,10 @@
QgsSingleBandPseudoColorRenderer,
QgsLimitedRandomColorRamp,
QgsGradientColorRamp,
QgsHueSaturationFilter)
QgsHueSaturationFilter,
QgsCoordinateTransformContext,
QgsCoordinateReferenceSystem
)
from utilities import unitTestDataPath
from qgis.testing import start_app, unittest
from qgis.testing.mocked import get_iface
@@ -1030,6 +1033,62 @@ def layerToSld(self, layer, properties={}):
return dom, root, errorMessage


class TestQgsRasterLayerTransformContext(unittest.TestCase):

def setUp(self):
"""Prepare tc"""
super(TestQgsRasterLayerTransformContext, self).setUp()
self.ctx = QgsCoordinateTransformContext()
self.ctx.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857), 1234, 1235)
self.rpath = os.path.join(unitTestDataPath(), 'landsat.tif')

def testTransformContextIsSetInCtor(self):
"""Test transform context can be set from ctor"""

rl = QgsRasterLayer(self.rpath, 'raster')
self.assertFalse(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

options = QgsRasterLayer.LayerOptions(transformContext=self.ctx)
rl = QgsRasterLayer(self.rpath, 'raster', 'gdal', options)
self.assertTrue(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

def testTransformContextInheritsFromProject(self):
"""Test that when a layer is added to a project it inherits its context"""

rl = QgsRasterLayer(self.rpath, 'raster')
self.assertFalse(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p = QgsProject()
self.assertFalse(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
p.setTransformContext(self.ctx)
self.assertTrue(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p.addMapLayers([rl])
self.assertTrue(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

def testTransformContextIsSyncedFromProject(self):
"""Test that when a layer is synced when project context changes"""

rl = QgsRasterLayer(self.rpath, 'raster')
self.assertFalse(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p = QgsProject()
self.assertFalse(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
p.setTransformContext(self.ctx)
self.assertTrue(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p.addMapLayers([rl])
self.assertTrue(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

# Now change the project context
tc2 = QgsCoordinateTransformContext()
p.setTransformContext(tc2)
self.assertFalse(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
self.assertFalse(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
p.setTransformContext(self.ctx)
self.assertTrue(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
self.assertTrue(rl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))


if __name__ == '__main__':
unittest.main()
rule
@@ -22,6 +22,7 @@

from qgis.core import (QgsWkbTypes,
QgsAction,
QgsCoordinateTransformContext,
QgsDataProvider,
QgsDefaultValue,
QgsEditorWidgetSetup,
@@ -3167,6 +3168,70 @@ def testMaximumValue(self):
pass


class TestQgsVectorLayerTransformContext(unittest.TestCase):

def setUp(self):
"""Prepare tc"""
super(TestQgsVectorLayerTransformContext, self).setUp()
self.ctx = QgsCoordinateTransformContext()
self.ctx.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857), 1234, 1235)

def testTransformContextIsSetInCtor(self):
"""Test transform context can be set from ctor"""

vl = QgsVectorLayer(
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
'test', 'memory')
self.assertFalse(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

options = QgsVectorLayer.LayerOptions(self.ctx)
vl = QgsVectorLayer(
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
'test', 'memory', options)
self.assertTrue(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

def testTransformContextInheritsFromProject(self):
"""Test that when a layer is added to a project it inherits its context"""

vl = QgsVectorLayer(
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
'test', 'memory')
self.assertFalse(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p = QgsProject()
self.assertFalse(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
p.setTransformContext(self.ctx)
self.assertTrue(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p.addMapLayers([vl])
self.assertTrue(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

def testTransformContextIsSyncedFromProject(self):
"""Test that when a layer is synced when project context changes"""

vl = QgsVectorLayer(
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
'test', 'memory')
self.assertFalse(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p = QgsProject()
self.assertFalse(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
p.setTransformContext(self.ctx)
self.assertTrue(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

p.addMapLayers([vl])
self.assertTrue(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))

# Now change the project context
tc2 = QgsCoordinateTransformContext()
p.setTransformContext(tc2)
self.assertFalse(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
self.assertFalse(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
p.setTransformContext(self.ctx)
self.assertTrue(p.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))
self.assertTrue(vl.transformContext().hasTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857)))


# TODO:
# - fetch rect: feat with changed geometry: 1. in rect, 2. out of rect
# - more join tests

0 comments on commit be37410

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