def testSaveCreate(self): """ test saving and recreating from XML """ r = QgsPointClusterRenderer() self._setProperties(r) doc = QDomDocument("testdoc") elem = r.save(doc, QgsReadWriteContext()) c = QgsPointClusterRenderer.create(elem, QgsReadWriteContext()) self._checkProperties(c)
def setUp(self): myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') self.layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') QgsProject.instance().addMapLayer(self.layer) self.renderer = QgsPointClusterRenderer() sym1 = QgsMarkerSymbol.createSimple({'color': '#ff00ff', 'size': '3', 'outline_style': 'no'}) renderer = QgsSingleSymbolRenderer(sym1) self.renderer.setEmbeddedRenderer(renderer) self.renderer.setClusterSymbol(QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'})) self.layer.setRenderer(self.renderer) rendered_layers = [self.layer] self.mapsettings = QgsMapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-123, 18, -70, 52)) self.mapsettings.setLayers(rendered_layers)
class TestQgsPointClusterRenderer(unittest.TestCase): def setUp(self): myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') self.layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') QgsProject.instance().addMapLayer(self.layer) self.renderer = QgsPointClusterRenderer() sym1 = QgsMarkerSymbol.createSimple({'color': '#ff00ff', 'size': '3', 'outline_style': 'no'}) renderer = QgsSingleSymbolRenderer(sym1) self.renderer.setEmbeddedRenderer(renderer) self.renderer.setClusterSymbol(QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'})) self.layer.setRenderer(self.renderer) rendered_layers = [self.layer] self.mapsettings = QgsMapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-123, 18, -70, 52)) self.mapsettings.setLayers(rendered_layers) def tearDown(self): QgsProject.instance().removeAllMapLayers() def _setProperties(self, r): """ set properties for a renderer for testing with _checkProperties""" r.setTolerance(5) r.setToleranceUnit(QgsUnitTypes.RenderMapUnits) r.setToleranceMapUnitScale(QgsMapUnitScale(5, 15)) m = QgsMarkerSymbol() m.setColor(QColor(0, 255, 0)) r.setClusterSymbol(m) sym1 = QgsMarkerSymbol.createSimple({'color': '#fdbf6f'}) renderer = QgsSingleSymbolRenderer(sym1) r.setEmbeddedRenderer(renderer) def _checkProperties(self, r): """ test properties of renderer against expected""" self.assertEqual(r.tolerance(), 5) self.assertEqual(r.toleranceUnit(), QgsUnitTypes.RenderMapUnits) self.assertEqual(r.toleranceMapUnitScale(), QgsMapUnitScale(5, 15)) self.assertEqual(r.clusterSymbol().color(), QColor(0, 255, 0)) self.assertEqual(r.embeddedRenderer().symbol().color().name(), '#fdbf6f') def testGettersSetters(self): """ test getters and setters """ r = QgsPointClusterRenderer() self._setProperties(r) self._checkProperties(r) def testClone(self): """ test cloning renderer """ r = QgsPointClusterRenderer() self._setProperties(r) c = r.clone() self._checkProperties(c) def testSaveCreate(self): """ test saving and recreating from XML """ r = QgsPointClusterRenderer() self._setProperties(r) doc = QDomDocument("testdoc") elem = r.save(doc, QgsReadWriteContext()) c = QgsPointClusterRenderer.create(elem, QgsReadWriteContext()) self._checkProperties(c) def testConvert(self): """ test renderer conversion """ # same type, should clone r = QgsPointClusterRenderer() self._setProperties(r) c = QgsPointClusterRenderer.convertFromRenderer(r) self._checkProperties(c) # test conversion from displacement renderer r = QgsPointDisplacementRenderer() r.setTolerance(5) r.setToleranceUnit(QgsUnitTypes.RenderMapUnits) r.setToleranceMapUnitScale(QgsMapUnitScale(5, 15)) m = QgsMarkerSymbol() m.setColor(QColor(0, 255, 0)) r.setCenterSymbol(m) sym1 = QgsMarkerSymbol.createSimple({'color': '#fdbf6f'}) renderer = QgsSingleSymbolRenderer(sym1) r.setEmbeddedRenderer(renderer) # want to keep as many settings as possible when converting between cluster and displacement renderer d = QgsPointClusterRenderer.convertFromRenderer(r) self.assertEqual(d.tolerance(), 5) self.assertEqual(d.toleranceUnit(), QgsUnitTypes.RenderMapUnits) self.assertEqual(d.toleranceMapUnitScale(), QgsMapUnitScale(5, 15)) self.assertEqual(d.clusterSymbol().color(), QColor(0, 255, 0)) self.assertEqual(d.embeddedRenderer().symbol().color().name(), '#fdbf6f') def testRenderNoCluster(self): self.layer.renderer().setTolerance(1) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(self.mapsettings) renderchecker.setControlPathPrefix('cluster_renderer') renderchecker.setControlName('expected_cluster_no_cluster') self.assertTrue(renderchecker.runTest('cluster_no_cluster')) def testRenderWithin(self): self.layer.renderer().setTolerance(10) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(self.mapsettings) renderchecker.setControlPathPrefix('cluster_renderer') renderchecker.setControlName('expected_cluster_cluster') self.assertTrue(renderchecker.runTest('expected_cluster_cluster')) def testRenderVariables(self): """ test rendering with expression variables in marker """ self.layer.renderer().setTolerance(10) old_marker = self.layer.renderer().clusterSymbol().clone() new_marker = QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'}) new_marker.symbolLayer(0).setDataDefinedProperty(QgsSymbolLayer.PropertyFillColor, QgsProperty.fromExpression('@cluster_color')) new_marker.symbolLayer(0).setDataDefinedProperty(QgsSymbolLayer.PropertySize, QgsProperty.fromExpression('@cluster_size*2')) self.layer.renderer().setClusterSymbol(new_marker) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(self.mapsettings) renderchecker.setControlPathPrefix('cluster_renderer') renderchecker.setControlName('expected_cluster_variables') result = renderchecker.runTest('expected_cluster_variables') self.layer.renderer().setClusterSymbol(old_marker) self.assertTrue(result)
def testConvert(self): """ test renderer conversion """ # same type, should clone r = QgsPointClusterRenderer() self._setProperties(r) c = QgsPointClusterRenderer.convertFromRenderer(r) self._checkProperties(c) # test conversion from displacement renderer r = QgsPointDisplacementRenderer() r.setTolerance(5) r.setToleranceUnit(QgsUnitTypes.RenderMapUnits) r.setToleranceMapUnitScale(QgsMapUnitScale(5, 15)) m = QgsMarkerSymbol() m.setColor(QColor(0, 255, 0)) r.setCenterSymbol(m) sym1 = QgsMarkerSymbol.createSimple({'color': '#fdbf6f'}) renderer = QgsSingleSymbolRenderer(sym1) r.setEmbeddedRenderer(renderer) # want to keep as many settings as possible when converting between cluster and displacement renderer d = QgsPointClusterRenderer.convertFromRenderer(r) self.assertEqual(d.tolerance(), 5) self.assertEqual(d.toleranceUnit(), QgsUnitTypes.RenderMapUnits) self.assertEqual(d.toleranceMapUnitScale(), QgsMapUnitScale(5, 15)) self.assertEqual(d.clusterSymbol().color(), QColor(0, 255, 0)) self.assertEqual(d.embeddedRenderer().symbol().color().name(), '#fdbf6f')
def testClone(self): """ test cloning renderer """ r = QgsPointClusterRenderer() self._setProperties(r) c = r.clone() self._checkProperties(c)
def testGettersSetters(self): """ test getters and setters """ r = QgsPointClusterRenderer() self._setProperties(r) self._checkProperties(r)
class TestQgsPointClusterRenderer(unittest.TestCase): def setUp(self): myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') self.layer = QgsVectorLayer(myShpFile, 'Points', 'ogr') QgsProject.instance().addMapLayer(self.layer) self.renderer = QgsPointClusterRenderer() sym1 = QgsMarkerSymbol.createSimple({'color': '#ff00ff', 'size': '3', 'outline_style': 'no'}) renderer = QgsSingleSymbolRenderer(sym1) self.renderer.setEmbeddedRenderer(renderer) self.renderer.setClusterSymbol(QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'})) self.layer.setRenderer(self.renderer) rendered_layers = [self.layer] self.mapsettings = QgsMapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-123, 18, -70, 52)) self.mapsettings.setLayers(rendered_layers) def tearDown(self): QgsProject.instance().removeAllMapLayers() def _setProperties(self, r): """ set properties for a renderer for testing with _checkProperties""" r.setTolerance(5) r.setToleranceUnit(QgsUnitTypes.RenderMapUnits) r.setToleranceMapUnitScale(QgsMapUnitScale(5, 15)) m = QgsMarkerSymbol() m.setColor(QColor(0, 255, 0)) r.setClusterSymbol(m) sym1 = QgsMarkerSymbol.createSimple({'color': '#fdbf6f'}) renderer = QgsSingleSymbolRenderer(sym1) r.setEmbeddedRenderer(renderer) def _checkProperties(self, r): """ test properties of renderer against expected""" self.assertEqual(r.tolerance(), 5) self.assertEqual(r.toleranceUnit(), QgsUnitTypes.RenderMapUnits) self.assertEqual(r.toleranceMapUnitScale(), QgsMapUnitScale(5, 15)) self.assertEqual(r.clusterSymbol().color(), QColor(0, 255, 0)) self.assertEqual(r.embeddedRenderer().symbol().color().name(), '#fdbf6f') def testGettersSetters(self): """ test getters and setters """ r = QgsPointClusterRenderer() self._setProperties(r) self._checkProperties(r) def testClone(self): """ test cloning renderer """ r = QgsPointClusterRenderer() self._setProperties(r) c = r.clone() self._checkProperties(c) def testSaveCreate(self): """ test saving and recreating from XML """ r = QgsPointClusterRenderer() self._setProperties(r) doc = QDomDocument("testdoc") elem = r.save(doc) c = QgsPointClusterRenderer.create(elem) self._checkProperties(c) def testConvert(self): """ test renderer conversion """ # same type, should clone r = QgsPointClusterRenderer() self._setProperties(r) c = QgsPointClusterRenderer.convertFromRenderer(r) self._checkProperties(c) # test conversion from displacement renderer r = QgsPointDisplacementRenderer() r.setTolerance(5) r.setToleranceUnit(QgsUnitTypes.RenderMapUnits) r.setToleranceMapUnitScale(QgsMapUnitScale(5, 15)) m = QgsMarkerSymbol() m.setColor(QColor(0, 255, 0)) r.setCenterSymbol(m) sym1 = QgsMarkerSymbol.createSimple({'color': '#fdbf6f'}) renderer = QgsSingleSymbolRenderer(sym1) r.setEmbeddedRenderer(renderer) # want to keep as many settings as possible when converting between cluster and displacement renderer d = QgsPointClusterRenderer.convertFromRenderer(r) self.assertEqual(d.tolerance(), 5) self.assertEqual(d.toleranceUnit(), QgsUnitTypes.RenderMapUnits) self.assertEqual(d.toleranceMapUnitScale(), QgsMapUnitScale(5, 15)) self.assertEqual(d.clusterSymbol().color(), QColor(0, 255, 0)) self.assertEqual(d.embeddedRenderer().symbol().color().name(), '#fdbf6f') def testRenderNoCluster(self): self.layer.renderer().setTolerance(1) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(self.mapsettings) renderchecker.setControlPathPrefix('cluster_renderer') renderchecker.setControlName('expected_cluster_no_cluster') self.assertTrue(renderchecker.runTest('cluster_no_cluster')) def testRenderWithin(self): self.layer.renderer().setTolerance(10) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(self.mapsettings) renderchecker.setControlPathPrefix('cluster_renderer') renderchecker.setControlName('expected_cluster_cluster') self.assertTrue(renderchecker.runTest('expected_cluster_cluster')) def testRenderVariables(self): """ test rendering with expression variables in marker """ self.layer.renderer().setTolerance(10) old_marker = self.layer.renderer().clusterSymbol().clone() new_marker = QgsMarkerSymbol.createSimple({'color': '#ffff00', 'size': '3', 'outline_style': 'no'}) new_marker.symbolLayer(0).setDataDefinedProperty(QgsSymbolLayer.PropertyFillColor, QgsProperty.fromExpression('@cluster_color')) new_marker.symbolLayer(0).setDataDefinedProperty(QgsSymbolLayer.PropertySize, QgsProperty.fromExpression('@cluster_size*2')) self.layer.renderer().setClusterSymbol(new_marker) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(self.mapsettings) renderchecker.setControlPathPrefix('cluster_renderer') renderchecker.setControlName('expected_cluster_variables') result = renderchecker.runTest('expected_cluster_variables') self.layer.renderer().setClusterSymbol(old_marker) self.assertTrue(result)