def testRenderLineLayerDataDefined(self): """ test that rendering a line symbol with data defined enabled layer works""" lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') lines_layer = QgsVectorLayer(lines_shp, 'Lines', 'ogr') QgsProject.instance().addMapLayer(lines_layer) layer = QgsSimpleLineSymbolLayer() layer.setDataDefinedProperty(QgsSymbolLayer.PropertyLayerEnabled, QgsProperty.fromExpression("Name='Highway'")) layer.setColor(QColor(100, 150, 150)) layer.setWidth(5) symbol = QgsLineSymbol() symbol.changeSymbolLayer(0, layer) lines_layer.setRenderer(QgsSingleSymbolRenderer(symbol)) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-133, 22, -70, 52)) ms.setLayers([lines_layer]) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(ms) renderchecker.setControlPathPrefix('symbol_layer') renderchecker.setControlName('expected_linelayer_ddenabled') self.assertTrue(renderchecker.runTest('linelayer_ddenabled')) QgsProject.instance().removeMapLayer(lines_layer)
def testPartNum(self): # test geometry_part_num variable s = QgsLineSymbol() s.deleteSymbolLayer(0) sym_layer = QgsGeometryGeneratorSymbolLayer.create({'geometryModifier': 'segments_to_lines($geometry)'}) sym_layer.setSymbolType(QgsSymbol.Line) s.appendSymbolLayer(sym_layer) marker_line = QgsMarkerLineSymbolLayer(False) marker_line.setPlacement(QgsMarkerLineSymbolLayer.FirstVertex) f = QgsFontUtils.getStandardTestFont('Bold', 24) marker = QgsFontMarkerSymbolLayer(f.family(), 'x', 24, QColor(255, 255, 0)) marker.setDataDefinedProperty(QgsSymbolLayer.PropertyCharacter, QgsProperty.fromExpression('@geometry_part_num')) marker_symbol = QgsMarkerSymbol() marker_symbol.changeSymbolLayer(0, marker) marker_line.setSubSymbol(marker_symbol) marker_line.setAverageAngleLength(0) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, marker_line) sym_layer.setSubSymbol(line_symbol) # rendering test g = QgsGeometry.fromWkt('LineString(0 0, 10 0, 10 10, 0 10)') rendered_image = self.renderGeometry(s, g, buffer=4) assert self.imageCheck('part_num_variable', 'part_num_variable', rendered_image) marker.setDataDefinedProperty(QgsSymbolLayer.PropertyCharacter, QgsProperty.fromExpression('@geometry_part_count')) # rendering test g = QgsGeometry.fromWkt('LineString(0 0, 10 0, 10 10, 0 10)') rendered_image = self.renderGeometry(s, g, buffer=4) assert self.imageCheck('part_count_variable', 'part_count_variable', rendered_image)
def testHashAngle(self): s = QgsLineSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval) hash_line.setInterval(6) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(7) hash_line.setHashAngle(45) hash_line.setAverageAngleLength(0) s.appendSymbolLayer(hash_line.clone()) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_hash_angle', 'line_hash_angle', rendered_image) s.symbolLayer(0).setRotateSymbols(False) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_hash_no_rotate', 'line_hash_no_rotate', rendered_image)
def testWidth(self): ms = QgsMapSettings() extent = QgsRectangle(100, 200, 100, 200) ms.setExtent(extent) ms.setOutputSize(QSize(400, 400)) context = QgsRenderContext.fromMapSettings(ms) context.setScaleFactor(96 / 25.4) # 96 DPI ms.setExtent(QgsRectangle(100, 150, 100, 150)) ms.setOutputDpi(ms.outputDpi() * 2) context2 = QgsRenderContext.fromMapSettings(ms) context2.setScaleFactor(300 / 25.4) s = QgsFillSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.FirstVertex) simple_line = QgsSimpleLineSymbolLayer() line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) self.assertEqual(hash_line.width(), 10) self.assertAlmostEqual(hash_line.width(context), 37.795275590551185, 3) self.assertAlmostEqual(hash_line.width(context2), 118.11023622047244, 3) hash_line.setHashLengthUnit(QgsUnitTypes.RenderPixels) self.assertAlmostEqual(hash_line.width(context), 10.0, 3) self.assertAlmostEqual(hash_line.width(context2), 10.0, 3)
def testRenderLineLayerDisabled(self): """ test that rendering a line symbol with disabled layer works""" layer = QgsSimpleLineSymbolLayer() layer.setEnabled(False) symbol = QgsLineSymbol() symbol.changeSymbolLayer(0, layer) image = QImage(200, 200, QImage.Format_RGB32) painter = QPainter() ms = QgsMapSettings() geom = QgsGeometry.fromWkt('LineString (0 0,3 4,4 3)') f = QgsFeature() f.setGeometry(geom) extent = geom.constGet().boundingBox() # buffer extent by 10% extent = extent.buffered((extent.height() + extent.width()) / 20.0) ms.setExtent(extent) ms.setOutputSize(image.size()) context = QgsRenderContext.fromMapSettings(ms) context.setPainter(painter) context.setScaleFactor(96 / 25.4) # 96 DPI painter.begin(image) image.fill(QColor(255, 255, 255)) symbol.startRender(context) symbol.renderFeature(f, context) symbol.stopRender(context) painter.end() self.assertTrue(self.imageCheck('symbol_layer', 'symbollayer_disabled', image))
def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) self.mapSettings = QgsMapSettings() # create composition self.mComposition = QgsComposition(self.mapSettings, QgsProject.instance()) self.mComposition.setPaperSize(297, 210) # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.mComposerPolyline = QgsComposerPolyline( polygon, self.mComposition) self.mComposition.addComposerPolyline(self.mComposerPolyline) # style props = {} props["color"] = "0,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) self.mComposerPolyline.setPolylineStyleSymbol(style)
def testReadWriteXml(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(0.0, 0.0)) p.append(QPointF(100.0, 0.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolyline(p, l) props = {} props["color"] = "255,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) shape.setSymbol(style) #save original item to xml doc = QDomDocument("testdoc") elem = doc.createElement("test") self.assertTrue(shape.writeXml(elem, doc, QgsReadWriteContext())) shape2 = QgsLayoutItemPolyline(l) self.assertTrue(shape2.readXml(elem.firstChildElement(), doc, QgsReadWriteContext())) self.assertEqual(shape2.nodes(), shape.nodes()) self.assertEqual(shape2.symbol().symbolLayer(0).color().name(), '#ff0000')
def testBounds(self): pr = QgsProject() l = QgsLayout(pr) p = QPolygonF() p.append(QPointF(50.0, 30.0)) p.append(QPointF(100.0, 10.0)) p.append(QPointF(200.0, 100.0)) shape = QgsLayoutItemPolyline(p, l) props = {} props["color"] = "255,0,0,255" props["width"] = "6.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) shape.setSymbol(style) # scene bounding rect should include symbol outline bounds = shape.sceneBoundingRect() self.assertEqual(bounds.left(), 47.0) self.assertEqual(bounds.right(), 203.0) self.assertEqual(bounds.top(), 7.0) self.assertEqual(bounds.bottom(), 103.0) # rectWithFrame should include symbol outline too bounds = shape.rectWithFrame() self.assertEqual(bounds.left(), -3.0) self.assertEqual(bounds.right(), 153.0) self.assertEqual(bounds.top(), -3.0) self.assertEqual(bounds.bottom(), 93.0)
def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) # create composition self.layout = QgsLayout(QgsProject.instance()) self.layout.initializeDefaults() # create polygon = QPolygonF() polygon.append(QPointF(0.0, 0.0)) polygon.append(QPointF(100.0, 0.0)) polygon.append(QPointF(200.0, 100.0)) polygon.append(QPointF(100.0, 200.0)) self.polyline = QgsLayoutItemPolyline( polygon, self.layout) self.layout.addLayoutItem(self.polyline) # style props = {} props["color"] = "0,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) self.polyline.setSymbol(style)
def setUp(self): self.iface = get_iface() polys_shp = os.path.join(TEST_DATA_DIR, "polys.shp") points_shp = os.path.join(TEST_DATA_DIR, "points.shp") lines_shp = os.path.join(TEST_DATA_DIR, "lines.shp") self.polys_layer = QgsVectorLayer(polys_shp, "Polygons", "ogr") self.points_layer = QgsVectorLayer(points_shp, "Points", "ogr") self.lines_layer = QgsVectorLayer(lines_shp, "Lines", "ogr") QgsMapLayerRegistry.instance().addMapLayer(self.polys_layer) QgsMapLayerRegistry.instance().addMapLayer(self.lines_layer) QgsMapLayerRegistry.instance().addMapLayer(self.points_layer) # Create style sym1 = QgsFillSymbol.createSimple({"color": "#fdbf6f"}) sym2 = QgsLineSymbol.createSimple({"color": "#fdbf6f"}) sym3 = QgsMarkerSymbol.createSimple({"color": "#fdbf6f"}) self.polys_layer.setRenderer(QgsSingleSymbolRenderer(sym1)) self.lines_layer.setRenderer(QgsSingleSymbolRenderer(sym2)) self.points_layer.setRenderer(QgsSingleSymbolRenderer(sym3)) self.mapsettings = self.iface.mapCanvas().mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-133, 22, -70, 52))
def setUp(self): self.iface = get_iface() polys_shp = os.path.join(TEST_DATA_DIR, 'polys.shp') points_shp = os.path.join(TEST_DATA_DIR, 'points.shp') lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') self.polys_layer = QgsVectorLayer(polys_shp, 'Polygons', 'ogr') self.points_layer = QgsVectorLayer(points_shp, 'Points', 'ogr') self.lines_layer = QgsVectorLayer(lines_shp, 'Lines', 'ogr') QgsProject.instance().addMapLayer(self.polys_layer) QgsProject.instance().addMapLayer(self.lines_layer) QgsProject.instance().addMapLayer(self.points_layer) # Create style sym1 = QgsFillSymbol.createSimple({'color': '#fdbf6f', 'outline_color': 'black'}) sym2 = QgsLineSymbol.createSimple({'color': '#fdbf6f'}) sym3 = QgsMarkerSymbol.createSimple({'color': '#fdbf6f', 'outline_color': 'black'}) self.polys_layer.setRenderer(QgsSingleSymbolRenderer(sym1)) self.lines_layer.setRenderer(QgsSingleSymbolRenderer(sym2)) self.points_layer.setRenderer(QgsSingleSymbolRenderer(sym3)) self.mapsettings = self.iface.mapCanvas().mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-133, 22, -70, 52))
def testRingFilter(self): # test filtering rings during rendering s = QgsFillSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval) hash_line.setInterval(6) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) hash_line.setAverageAngleLength(0) s.appendSymbolLayer(hash_line.clone()) self.assertEqual(s.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.AllRings) s.symbolLayer(0).setRingFilter(QgsLineSymbolLayer.ExteriorRingOnly) self.assertEqual(s.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.ExteriorRingOnly) s2 = s.clone() self.assertEqual(s2.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.ExteriorRingOnly) doc = QDomDocument() context = QgsReadWriteContext() element = QgsSymbolLayerUtils.saveSymbol('test', s, doc, context) s2 = QgsSymbolLayerUtils.loadSymbol(element, context) self.assertEqual(s2.symbolLayer(0).ringFilter(), QgsLineSymbolLayer.ExteriorRingOnly) # rendering test s3 = QgsFillSymbol() s3.deleteSymbolLayer(0) s3.appendSymbolLayer( hash_line.clone()) s3.symbolLayer(0).setRingFilter(QgsLineSymbolLayer.ExteriorRingOnly) g = QgsGeometry.fromWkt('Polygon((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1),(8 8, 9 8, 9 9, 8 9, 8 8))') rendered_image = self.renderGeometry(s3, g) assert self.imageCheck('hashline_exterioronly', 'hashline_exterioronly', rendered_image) s3.symbolLayer(0).setRingFilter(QgsLineSymbolLayer.InteriorRingsOnly) g = QgsGeometry.fromWkt('Polygon((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1),(8 8, 9 8, 9 9, 8 9, 8 8))') rendered_image = self.renderGeometry(s3, g) assert self.imageCheck('hashline_interioronly', 'hashline_interioronly', rendered_image)
def testPointNumInterval(self): s = QgsLineSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval) hash_line.setInterval(6) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) hash_line.setAverageAngleLength(0) s.appendSymbolLayer(hash_line.clone()) s.symbolLayer(0).setDataDefinedProperty(QgsSymbolLayer.PropertyLineDistance, QgsProperty.fromExpression( "@geometry_point_num * 2")) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_dd_size', 'line_dd_size', rendered_image)
def setUp(self): self.iface = get_iface() lines_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') self.lines_layer = QgsVectorLayer(lines_shp, 'Lines', 'ogr') QgsProject.instance().addMapLayer(self.lines_layer) # Create style sym2 = QgsLineSymbol.createSimple({'color': '#fdbf6f'}) self.lines_layer.setRenderer(QgsSingleSymbolRenderer(sym2)) self.mapsettings = self.iface.mapCanvas().mapSettings() self.mapsettings.setOutputSize(QSize(400, 400)) self.mapsettings.setOutputDpi(96) self.mapsettings.setExtent(QgsRectangle(-113, 28, -91, 40)) self.mapsettings.setBackgroundColor(QColor("white"))
def testSimpleLineWithCustomDashPattern(self): """ test that rendering a simple line symbol with custom dash pattern""" layer = QgsSimpleLineSymbolLayer(QColor(0, 0, 0)) layer.setWidth(0.5) layer.setCustomDashVector([2, 5]) layer.setUseCustomDashPattern(True) symbol = QgsLineSymbol() symbol.changeSymbolLayer(0, layer) image = QImage(200, 200, QImage.Format_RGB32) painter = QPainter() ms = QgsMapSettings() geom = QgsGeometry.fromWkt('LineString (0 0, 10 0, 10 10, 0 10, 0 0)') f = QgsFeature() f.setGeometry(geom) extent = geom.constGet().boundingBox() # buffer extent by 10% extent = extent.buffered((extent.height() + extent.width()) / 20.0) ms.setExtent(extent) ms.setOutputSize(image.size()) context = QgsRenderContext.fromMapSettings(ms) context.setPainter(painter) context.setScaleFactor(96 / 25.4) # 96 DPI painter.begin(image) image.fill(QColor(255, 255, 255)) symbol.startRender(context) symbol.renderFeature(f, context) symbol.stopRender(context) painter.end() self.assertTrue(self.imageCheck('symbol_layer_simpleline_customdashpattern', 'simpleline_customdashpattern', image))
def testMarkerAverageAngleCenter(self): s = QgsLineSymbol() s.deleteSymbolLayer(0) marker_line = QgsMarkerLineSymbolLayer(True) marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.CentralPoint) marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 4) marker.setColor(QColor(255, 0, 0)) marker.setStrokeStyle(Qt.NoPen) marker_symbol = QgsMarkerSymbol() marker_symbol.changeSymbolLayer(0, marker) marker_line.setSubSymbol(marker_symbol) marker_line.setAverageAngleLength(60) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, marker_line) s.appendSymbolLayer(marker_line.clone()) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('markerline_center_average_angle', 'markerline_center_average_angle', rendered_image)
def testNoPoint(self): s = QgsLineSymbol() s.deleteSymbolLayer(0) marker_line = QgsMarkerLineSymbolLayer(True) marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval) marker_line.setOffsetAlongLine(1000) marker_line.setIntervalUnit(QgsUnitTypes.RenderMapUnits) marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Circle, 4) marker.setColor(QColor(255, 0, 0, 100)) marker.setStrokeStyle(Qt.NoPen) marker_symbol = QgsMarkerSymbol() marker_symbol.changeSymbolLayer(0, marker) marker_line.setSubSymbol(marker_symbol) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, marker_line) s.appendSymbolLayer(marker_line.clone()) g = QgsGeometry.fromWkt('LineString(0 0, 0 10, 10 10)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('markerline_none', 'markerline_none', rendered_image)
def testVerticalLine(self): pr = QgsProject() l = QgsLayout(pr) l.initializeDefaults() p = QPolygonF() p.append(QPointF(100.0, 50.0)) p.append(QPointF(100.0, 100.0)) shape = QgsLayoutItemPolyline(p, l) l.addLayoutItem(shape) props = {} props["color"] = "0,0,0,255" props["width"] = "10.0" props["capstyle"] = "square" style = QgsLineSymbol.createSimple(props) shape.setSymbol(style) checker = QgsLayoutChecker( 'composerpolyline_vertline', l) checker.setControlPathPrefix("composer_polyline") myTestResult, myMessage = checker.testLayout() assert myTestResult, myMessage
def testReprojectionErrorsWhileRendering(self): # WKT of a polygon which causes reprojection errors while rendering # (apologies for the ridiculously complex wkt, but I can't find a way to reproduce with simplifiction) wkt = 'MultiPolygon (((16.93392988400009358 42.77094147300012139, 16.88493899800005238 42.72939687700012712, ' \ '16.80298912900011032 42.76349518400014915, 16.85816491000014139 42.78400299700011544, ' \ '16.93392988400009358 42.77094147300012139)),((17.38200931100010393 42.79783763200002511, ' \ '17.65894616000011297 42.74298737200008702, 17.74887129000009622 42.69456614800010641, ' \ '17.32374108200008322 42.79083893400003547, 17.38200931100010393 42.79783763200002511)),' \ '((16.768565300000148 42.97223541900014254, 17.03207441500009622 42.98261139500014849, ' \ '17.13184655000009116 42.96954987200014386, 17.20020592500009116 42.92177969000012183, ' \ '16.85141035200010151 42.90070221600008438, 16.65544681100004709 42.92625560099999404, ' \ '16.70679772200014668 42.96954987200014386, 16.63168379000003938 42.98261139500014849, ' \ '16.768565300000148 42.97223541900014254)),((17.05567467500011958 43.02895742400001211, ' \ '17.24024498800011429 43.02277252800014651, 17.74146569100011561 42.83926015800001608, ' \ '17.70736738400009358 42.88703034100014122, 17.65334906206413734 42.8909283361407887, ' \ '17.70158573400010482 42.91950022500007833, 17.81175988700005064 42.909862570000044, ' \ '17.85847538200005147 42.81697418200012351, 18.22413781700009849 42.62807098500009317, ' \ '18.43735477700010961 42.55921213800017711, 18.4371480710000526 42.4934022020000981, ' \ '18.49642988400009358 42.41632721600008438, 18.23894290500010129 42.55906810100005089, ' \ '18.21753991000014139 42.6201032570001388, 18.07601972700010151 42.65131256700003348, ' \ '18.0432235040000819 42.70205312700007028, 17.90162194100014403 42.75189850500014188, ' \ '17.8928328790000819 42.79083893400003547, 17.72095787900005348 42.8262393250000315, ' \ '17.7618921230000808 42.77871328300012976, 17.74870853000004445 42.77204010600017625, ' \ '17.21387780000011958 42.98261139500014849, 17.04615319100011561 42.9950625670000619, ' \ '17.00163821700004974 43.05149974200010377, 17.05567467500011958 43.02895742400001211)),' \ '((16.19467207100007045 43.07440827000000638, 16.254893425000148 43.06854889500006323, ' \ '16.08716881600014403 43.01146067900008063, 16.04883873800011429 43.06517161700004692, ' \ '16.19467207100007045 43.07440827000000638)),((16.56275475400011032 43.22898997600010773, ' \ '16.65951582100009887 43.21596914300012315, 16.72771243600001867 43.16461823100003414, ' \ '17.19336998800014271 43.12726471600016964, 16.67017662900013875 43.12547435099999404, ' \ '16.37159264400014536 43.19550202000006323, 16.49642988400006516 43.21808502800014651, ' \ '16.58326256600014403 43.18866608300005794, 16.52051842500006273 43.22898997600010773, ' \ '16.56275475400011032 43.22898997600010773)),((16.80681399800010922 43.34247467700005529, ' \ '16.89234459700011826 43.31220123900006058, 16.84620201900008851 43.27338288000005662, ' \ '16.62826582100012729 43.26373932500008834, 16.50074303500014139 43.28424713700003679, ' \ '16.42188561300008587 43.31757233300011478, 16.40577233200011165 43.33270905200011214, ' \ '16.45346113400009358 43.35317617400009738, 16.42628014400008851 43.39411041900011412, ' \ '16.44703209700008983 43.39484284100014122, 16.80681399800010922 43.34247467700005529)),' \ '((16.29818769600012729 43.40363190300011809, 16.30274498800008587 43.38727448100009099, ' \ '16.39144941500012465 43.34638092700005529, 16.348643425000148 43.33869049700003018, ' \ '16.20045006600014403 43.40704987200003018, 16.29818769600012729 43.40363190300011809)),' \ '((16.33415774800010922 43.50153229400014254, 16.3752547540000819 43.49017975500008504, ' \ '16.21143639400008851 43.49005768400009231, 16.26441491000014139 43.51288483300011478, ' \ '16.33415774800010922 43.50153229400014254)),((15.67888431100004709 43.64801666900014254, ' \ '15.74040774800010922 43.62750885600009099, 15.67204837300002396 43.63743724200010377, ' \ '15.60377037900013875 43.67470937700007028, 15.67888431100004709 43.64801666900014254)),' \ '((15.36736087300005238 43.79010651200015047, 15.39568118600007551 43.7724063170000619, ' \ '15.22779381600014403 43.87445709800014981, 15.24073326900014536 43.88076406500009341, ' \ '15.36736087300005238 43.79010651200015047)),((15.44271894600009887 43.89907461100013109, ' \ '15.35865319100014403 43.91937897300014981, 15.26124108200011165 44.01105377800003282, ' \ '15.38404381600008719 43.9701602230000077, 15.44271894600009887 43.89907461100013109)),' \ '((15.22575931100010393 44.06622955900014915, 15.25440514400008851 44.01788971600014122, ' \ '15.12183678500014139 44.09223053600005926, 15.06251061300008587 44.16193268400012073, ' \ '15.22575931100010393 44.06622955900014915)),((14.83545983200014007 44.15102773600013109, ' \ '14.85726972700010151 44.15204498900000374, 14.86915123800014271 44.14052969000006499, ' \ '14.83521569100008719 44.14166901200009363, 14.81983483200014007 44.15302155199999845, ' \ '14.82243899800005238 44.16868724200004692, 14.83545983200014007 44.15102773600013109)),' \ '((14.98511803500011297 44.09096914300012315, 15.21680748800008587 43.91278717700008372, ' \ '15.13331139400011693 43.92121002800003282, 15.19450931100004709 43.87262604400017096, ' \ '15.10661868600007551 43.92544179900015422, 14.84961998800014271 44.17560455900014915, ' \ '14.98511803500011297 44.09096914300012315)),((14.765961134000122 44.26504140800015819, ' \ '14.74854576900014536 44.26166413000014188, 14.73959394600012729 44.28017812700015554, ' \ '14.79167728000007287 44.27252838700003679, 14.765961134000122 44.26504140800015819)),' \ '((14.66138756600011561 44.30866120000014519, 14.6407983730000808 44.31183502800003282, ' \ '14.59506269600007045 44.34711334800006455, 14.643565300000148 44.32575104400011412, ' \ '14.66138756600011561 44.30866120000014519)),((14.81120853000004445 44.35004303600000242, ' \ '14.75619550900009358 44.36399974200004692, 14.76343834700008983 44.41535065300017493, ' \ '14.80323326900008851 44.40550364800004957, 14.81120853000004445 44.35004303600000242)),' \ '((14.27116946700002131 44.61253489800004957, 14.23259524800005238 44.62604401200012205, ' \ '14.2657983730000808 44.67951080900003547, 14.28044681100007551 44.67755768400009231, ' \ '14.27116946700002131 44.61253489800004957)),((14.84522545700008322 44.60053131700011875, ' \ '14.93824303500014139 44.59414297100001079, 15.07553144600007045 44.48407623900006058, ' \ '14.91114342500011958 44.54547760600014783, 15.04802493600004709 44.43943919500001982, ' \ '15.09669030000009116 44.41518789300000947, 15.04151451900014536 44.47662995000008834, ' \ '15.25440514400008851 44.34003327000000638, 15.165049675000148 44.36737702000006323, ' \ '15.22022545700008322 44.3127302100001117, 15.13086998800008587 44.33258698100003414, ' \ '15.17237389400014536 44.29913971600016964, 15.12875410200007309 44.31199778900018771, ' \ '15.08920332100009887 44.37421295800000109, 15.11719811300014271 44.38719310099999404, ' \ '15.04900149800010922 44.39468008000015686, 14.89747155000009116 44.49091217699999845, ' \ '14.91863040500010129 44.50454336100013109, 14.87696373800011429 44.55975983300005794, ' \ '14.73365319100008719 44.70319245000014519, 14.84522545700008322 44.60053131700011875)),' \ '((14.41000410200010151 44.60097890800001608, 14.52662194100011561 44.50372955900012073, ' \ '14.53435306100010393 44.48407623900006058, 14.42261803500008455 44.57387929900009738, ' \ '14.36304772200014668 44.57343170800000109, 14.38257897200014668 44.60325755399999537, ' \ '14.33578535200007309 44.71678294500010509, 14.39747155000009116 44.6856143250000315, ' \ '14.41000410200010151 44.60097890800001608)),((14.75326582100007045 44.84585195500012844, ' \ '14.74048912900011032 44.82050202000000638, 14.82243899800005238 44.77142975500005662, ' \ '14.84961998800014271 44.70319245000014519, 14.65788821700004974 44.79877350500014188, ' \ '14.7268172540000819 44.79877350500014188, 14.6858016290000819 44.8471540390000456, ' \ '14.75326582100007045 44.84585195500012844)),((14.47103925900006516 44.95392487200003018, ' \ '14.45191491000008455 44.79877350500014188, 14.47217858200011165 44.7079531920000619, ' \ '14.53435306100010393 44.63426341400010244, 14.51335696700007816 44.618841864000089, ' \ '14.42790774800005238 44.65656159100014122, 14.29420006600008719 44.9086367860001161, ' \ '14.30152428500011297 44.94342682500014519, 14.38738040500004445 44.90900299700003018, ' \ '14.39031009200004974 44.96039459800012139, 14.41138756600008719 44.95636627800014651, ' \ '14.27849368600004709 45.1133487000000315, 14.29957116000014139 45.16233958499999801, ' \ '14.35621178500014139 45.16925690300008966, 14.387705925000148 45.03904857000013351, ' \ '14.47103925900006516 44.95392487200003018)),((14.56332441500012465 45.24974192900008063, ' \ '14.62378991000011297 45.17548248900006058, 14.59742272200011826 45.16644928600005926, ' \ '14.66529381600011561 45.16181061400011743, 14.66529381600011561 45.08734772300006455, ' \ '14.74048912900011032 45.07306549700014386, 14.81495201900008851 44.97748444200009033, ' \ '14.70639082100009887 44.9467227230000077, 14.62891686300014271 44.97817617400004053, ' \ '14.62086022200008983 45.04559967700011214, 14.61695397200008983 45.02464427300007799, ' \ '14.51050866000014139 45.03217194200011875, 14.43873131600014403 45.07050202000006323, ' \ '14.4670516290000819 45.12409088700015047, 14.53012129000009622 45.13483307500014519, ' \ '14.53435306100010393 45.23753489800002114, 14.56332441500012465 45.24974192900008063)),' \ '((16.36947066200013978 46.54057118800012915, 16.63767134600004738 46.47447703100009164, ' \ '16.75508020000012266 46.38187286400001597, 16.83765913900006694 46.38187286400001597, ' \ '16.88923221800007468 46.29216257800014489, 17.05294315600005461 46.15346303300005104, ' \ '17.20859257000006437 46.11656606000003933, 17.27587528500004055 46.01202463800002818, ' \ '17.31680301900004793 45.99765859000002877, 17.29013798000011093 45.98463612900009423, ' \ '17.40620324700006449 45.94365671800015605, 17.59110152100009827 45.93621531200012953, ' \ '17.65652388500006964 45.84541982000014571, 17.80917606600013414 45.81441396100005647, ' \ '17.85806197100004056 45.77172922800004073, 18.21121870900006456 45.78537180600012846, ' \ '18.40438521300006869 45.74180857400001798, 18.57347049900010916 45.81668772400014689, ' \ '18.6556360270001278 45.90758656800015558, 18.7755253500000947 45.88283355700004051, ' \ '18.90130578600007993 45.93120269800006383, 18.87288374800004931 45.89523590100002082, ' \ '18.90699019400011593 45.86795074500018643, 18.85531376100007606 45.85735707600009903, ' \ '18.84497847500006174 45.8157058720000947, 18.96848514800012708 45.66873809800016204, ' \ '18.90357954900008508 45.57308502200005762, 18.94171675700005153 45.53892689999999277, ' \ '19.01809452300011571 45.56740061400002162, 19.10625451700005328 45.51164174500017623, ' \ '19.00961958800010621 45.49867095900005154, 19.00300500400010151 45.45536611000007099, ' \ '19.03742150900006891 45.42229319300010104, 18.97592655400006834 45.39495636000008005, ' \ '19.09199182100007874 45.34999786400005917, 19.12475467900009107 45.29811472600006539, ' \ '19.36308638500014467 45.24824696900010679, 19.40783817500010855 45.20313344400013023, ' \ '19.39068160000005037 45.16933705700016333, 19.22593713300008744 45.16194732700016345, ' \ '19.12186079900010327 45.195795390000157, 19.13767378700009658 45.14603098600004216, ' \ '19.04486291500009543 45.13724599300006446, 19.08227665200013234 45.08494944300004192, ' \ '19.0872375890000967 44.97710072800013847, 19.13167932100006396 44.95317454000003465, ' \ '19.06667036900009293 44.90568389900012392, 18.99142948400006503 44.9149339800001286, ' \ '19.01582076000008215 44.86563466400004074, 18.88962691200009658 44.86119049100013001, ' \ '18.78338016700013213 44.91374542300012251, 18.79175174900009893 45.00154368100008639, ' \ '18.73831831900008638 45.0159097290000858, 18.68405806500004473 45.08479441400000098, ' \ '18.64871138500012648 45.06267689999999959, 18.61667199700013953 45.09766184500010411, ' \ '18.54959598800010667 45.09476796500011631, 18.51703983500007666 45.05585561200003042, ' \ '18.23788374800011525 45.15745147700012296, 18.15365116400005263 45.0975584930001645, ' \ '18.00347945100011771 45.1493382780000303, 17.83573775200005684 45.0644338990000648, ' \ '17.68473921700012852 45.1639627080000281, 17.48185754400009273 45.11440500900012296, ' \ '17.49622359200009214 45.1416901650001563, 17.44775109900012922 45.13430043600014585, ' \ '17.44330692500011537 45.16205068000009248, 17.38243208800008688 45.1396231090000839, ' \ '17.26895064300006766 45.18954254200015441, 17.24548954300007608 45.15538442000017483, ' \ '17.18709517400012032 45.14856313100001728, 17.0363033440001459 45.23047027600007652, ' \ '17.00829471800011561 45.21615590500009318, 17.00829471800011561 45.24416453100009505, ' \ '16.94731652900014751 45.23568959600000028, 16.9243721930001243 45.28452382500016427, ' \ '16.81171757000004163 45.18122263700009, 16.52894413300009546 45.22225372400005483, ' \ '16.38921106000003647 45.11683380099999852, 16.31624393700010955 45.00123362300008978, ' \ '16.12152714000009723 45.09616322900008356, 16.02044803900011516 45.213933818000001, ' \ '15.79234826700013627 45.18980092400012438, 15.76361617000014803 44.97555043600003444, ' \ '15.7308533120001357 44.92723297200008403, 15.77343469200010873 44.84501576800015243, ' \ '15.71607385200013596 44.80320953400008932, 15.72847619600008784 44.76910308800002269, ' \ '15.80568078600006743 44.69665273000013883, 15.88877648900006534 44.72424794500012979, ' \ '15.96897831200004703 44.63924021400013942, 16.02830285600006732 44.62471913700009907, ' \ '16.04473596200011798 44.58937245700018082, 16.00608199000004106 44.54100331600012908, ' \ '16.11646285000011858 44.52146962500013672, 16.15966434700004584 44.41610138000002905, ' \ '16.13827030500004867 44.37760243800015303, 16.20286584400008678 44.35977406800010669, ' \ '16.18756962000011868 44.28241444999999032, 16.21578495300011014 44.20815541600011045, ' \ '16.32688928200008149 44.08237498000012522, 16.50103885900011846 43.99271637000008184, ' \ '16.67859908100004418 43.8406843060001421, 16.71260217300007866 43.77151540100005889, ' \ '17.03051558500007445 43.54847991900005866, 17.27050093600007585 43.46321380700000248, ' \ '17.28993127500007176 43.3034302780000786, 17.44206669100009321 43.15243174300015028, ' \ '17.6284119050001209 43.04657257100008394, 17.66272505700004558 42.96569895500012137, ' \ '17.63450972400008254 42.950402731000068, 17.51563561300008587 42.95888906500012183, ' \ '17.47087649800005238 43.01341380400010905, 17.50196373800014271 43.03099192900005221, ' \ '17.43360436300014271 43.01740143400009231, 17.46021569100011561 43.03099192900005221, ' \ '17.42611738400009358 43.06517161700004692, 17.4045516290000819 43.05149974200010377, ' \ '17.31625410200012993 43.12726471600016964, 17.11394290500004445 43.21320221600008438, ' \ '16.88062584700011826 43.40595123900006058, 16.62582441500009622 43.44904205900009231, ' \ '16.52466881600011561 43.51080963700009363, 16.39144941500012465 43.51080963700009363, ' \ '16.47339928500008455 43.5381533870001789, 16.43384850400013875 43.54975006700000506, ' \ '16.11768639400008851 43.52448151200003679, 16.17237389400014536 43.4896914730000077, ' \ '16.11312910200004467 43.47890859600009605, 15.95948326900011693 43.50397370000008834, ' \ '15.987315300000148 43.54490794500010509, 15.92530358200011165 43.55857982000004824, ' \ '15.91895592500009116 43.62872955900012073, 15.96631920700011165 43.64118073100003414, ' \ '15.90479576900014536 43.64801666900014254, 15.95297285200010151 43.65086497599999404, ' \ '15.95045006600008719 43.68854401200015047, 15.70630944100008719 43.76341380400005221, ' \ '15.6174422540000819 43.82550690300017493, 15.66309655000009116 43.81297435099999404, ' \ '15.67888431100004709 43.81928131700011875, 15.45508873800014271 43.92804596600014122, ' \ '15.14454186300011429 44.19546133000015686, 15.15219160200012993 44.23529694200014717, ' \ '15.11036217500011958 44.26434967700011214, 15.14063561300011429 44.28245677300013483, ' \ '15.17660566500009622 44.24994538000005662, 15.20777428500008455 44.27277252800014651, ' \ '15.19809004000012465 44.30166250200007028, 15.295258009000122 44.25067780199999845, ' \ '15.30274498800008587 44.29913971600016964, 15.26124108200011165 44.33258698100003414, ' \ '15.42448978000001603 44.26797109600006763, 15.52865644600009887 44.27179596600008438, ' \ '15.30795332100009887 44.35439687700007028, 15.00733483200014007 44.56972890800012976, ' \ '14.883799675000148 44.7236188820001388, 14.883799675000148 44.86147695500012844, 14.92164147200008983 ' \ '44.95880768400009231, 14.85279381600011561 45.09365469000000815, 14.65788821700004974 ' \ '45.19660065300017493, 14.57081139400008851 45.29364655200011214, 14.31153405000009116 ' \ '45.34398021000005485, 14.23259524800005238 45.14935944200000506, 14.17937259200007816 ' \ '45.13450755400005221, 14.19312584700008983 45.10561758000012844, 14.14389082100007045 ' \ '45.05939362200003018, 14.151377800000148 44.97748444200009033, 14.06885826900014536 ' \ '44.94953034100014122, 14.08383222700007309 44.9863955750000315, 14.04029381600014403 ' \ '45.03896719000015025, 14.0756942070000548 44.98371002800003282, 14.02051842500011958 ' \ '44.90110911700004692, 13.97266686300011429 44.90110911700004692, 13.99301191500009622 ' \ '44.88129303600014453, 13.97266686300011429 44.82664622599999404, 14.00001061300008587 ' \ '44.81305573100003414, 13.89014733200011165 44.83348216400010244, 13.91797936300014271 ' \ '44.77826569200009033, 13.90316816500009622 44.77240631700014717, 13.89698326900011693 ' \ '44.81305573100003414, 13.78711998800014271 44.87506745000008834, 13.84229576900008851 ' \ '44.88812897300006455, 13.79460696700010658 44.89496491100008768, 13.77409915500007287 ' \ '44.96381256700014717, 13.6232202480000808 45.07306549700014386, 13.61255944100014403 ' \ '45.11786530199999845, 13.72624759200004974 45.13450755400005221, 13.5959578790000819 ' \ '45.14541250200001343, 13.57545006600011561 45.26487864800007799, 13.60271243600001867 ' \ '45.28534577000012007, 13.57545006600011561 45.30646393400006389, 13.60954837300005238 ' \ '45.32013580900017757, 13.54127037900013875 45.34613678600005926, 13.50709069100014403 ' \ '45.51190827000000638, 13.62901778100007277 45.45898346000016943, 13.75929406800014476 ' \ '45.46316925100011019, 13.88900191200011136 45.42363678000005223, 13.98263960800005634 ' \ '45.47531321200001742, 13.97189091000012695 45.5142255660000643, 14.09291711400010172 ' \ '45.47391794800002174, 14.21869755100007637 45.49717234400004884, 14.37279667100006009 ' \ '45.47784535800009564, 14.4689148350000778 45.52559438100014688, 14.49857710800012001 ' \ '45.59618438800005435, 14.58094934100009255 45.66780792200010808, 14.66848921700008646 ' \ '45.53396596300005683, 14.79716353300005949 45.46518463200006011, 14.88160282300009385 ' \ '45.46978383400001178, 14.9226339110000481 45.51494903600017494, 15.13926151500010064 ' \ '45.43004465799999991, 15.32519331800011742 45.45283396399999276, 15.36136682100004691 ' \ '45.48203114900003641, 15.29666792800006192 45.52295888300012905, 15.2685559480001416 ' \ '45.60166208900012919, 15.37376916500011248 45.64021270800010655, 15.25501672300006817 ' \ '45.72346344000011698, 15.42906294700014769 45.77529490200011253, 15.45128381300008868 ' \ '45.81513743100013869, 15.67607629400006886 45.84169911700014666, 15.65943648300003588 ' \ '45.88882802400014782, 15.69798710100010908 46.0362092080000167, 15.58988000500005455 ' \ '46.11351715100001059, 15.62284956800010605 46.19170359400006021, 16.01920780400010358 ' \ '46.29882883700007312, 16.05961877400008575 46.33231516600015709, 16.0579651280001201 ' \ '46.37753204400003426, 16.2756262620000598 46.37316538500006402, 16.23490523300009158 ' \ '46.4933389280001137, 16.36947066200013978 46.54057118800012915))) ' geom = QgsGeometry.fromWkt(wkt) f = QgsFeature() f.setGeometry(geom) image = QImage(200, 200, QImage.Format_RGB32) painter = QPainter() ms = QgsMapSettings() crs = QgsCoordinateReferenceSystem.fromProj4('+proj=ortho +lat_0=36.5 +lon_0=-118.8 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs') self.assertTrue(crs.isValid()) ms.setDestinationCrs(crs) ms.setExtent(QgsRectangle(1374999.8, 3912610.7, 4724462.5, 6505499.6)) ms.setOutputSize(image.size()) context = QgsRenderContext.fromMapSettings(ms) context.setPainter(painter) context.setScaleFactor(96 / 25.4) # 96 DPI ct = QgsCoordinateTransform(QgsCoordinateReferenceSystem('epsg:4326'), crs, QgsProject.instance()) self.assertTrue(ct.isValid()) context.setCoordinateTransform(ct) context.setExtent(ct.transformBoundingBox(ms.extent(), QgsCoordinateTransform.ReverseTransform)) fill_symbol = QgsFillSymbol.createSimple({'color': '#ffffff', 'outline_color': '#ffffff', 'outline_width': '10'}) painter.begin(image) try: image.fill(QColor(0, 0, 0)) fill_symbol.startRender(context) fill_symbol.renderFeature(f, context) fill_symbol.stopRender(context) finally: painter.end() assert self.imageCheck('Reprojection errors polygon', 'reprojection_errors_polygon', image) #also test linestring linestring = QgsGeometry(geom.constGet().boundary()) f.setGeometry(linestring) line_symbol = QgsLineSymbol.createSimple({'color': '#ffffff', 'outline_width': '10'}) image = QImage(200, 200, QImage.Format_RGB32) painter.begin(image) try: image.fill(QColor(0, 0, 0)) line_symbol.startRender(context) line_symbol.renderFeature(f, context) line_symbol.stopRender(context) finally: painter.end() assert self.imageCheck('Reprojection errors linestring', 'reprojection_errors_linestring', image)
def testMarkerAngleDD(self): """Test issue https://github.com/qgis/QGIS/issues/38716""" s = QgsLineSymbol() s.deleteSymbolLayer(0) marker_line = QgsMarkerLineSymbolLayer(True) marker_line.setRotateSymbols(True) marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.CentralPoint) marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Arrow, 10) marker.setAngle(90) marker.setColor(QColor(255, 0, 0)) marker.setStrokeStyle(Qt.NoPen) marker_symbol = QgsMarkerSymbol() marker_symbol.changeSymbolLayer(0, marker) marker_line.setSubSymbol(marker_symbol) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, marker_line) s.appendSymbolLayer(marker_line.clone()) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 20 20)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('markerline_center_angle_dd', 'markerline_center_angle_dd', rendered_image) # Now with DD s = QgsLineSymbol() s.deleteSymbolLayer(0) marker_line = QgsMarkerLineSymbolLayer(True) marker_line.setRotateSymbols(True) marker_line.setPlacement(QgsTemplatedLineSymbolLayerBase.CentralPoint) marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Arrow, 10) # Note: set this to a different value than the reference test (90) marker.setAngle(30) marker.setColor(QColor(255, 0, 0)) marker.setStrokeStyle(Qt.NoPen) marker_symbol = QgsMarkerSymbol() marker_symbol.changeSymbolLayer(0, marker) # This is the same value of the reference test marker_symbol.setDataDefinedAngle(QgsProperty.fromExpression('90')) marker_line.setSubSymbol(marker_symbol) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, marker_line) s.appendSymbolLayer(marker_line.clone()) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 20 20)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('markerline_center_angle_dd', 'markerline_center_angle_dd', rendered_image)
def test_render_via_job(self): """ Test rendering an annotation layer via a map render job """ layer = QgsAnnotationLayer( 'test', QgsAnnotationLayer.LayerOptions( QgsProject.instance().transformContext())) self.assertTrue(layer.isValid()) item = QgsAnnotationPolygonItem( QgsPolygon( QgsLineString([ QgsPoint(11.5, 13), QgsPoint(12, 13), QgsPoint(12, 13.5), QgsPoint(11.5, 13) ]))) item.setSymbol( QgsFillSymbol.createSimple({ 'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2' })) item.setZIndex(1) i1_id = layer.addItem(item) item = QgsAnnotationLineItem( QgsLineString( [QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])) item.setSymbol( QgsLineSymbol.createSimple({ 'color': '#ffff00', 'line_width': '3' })) item.setZIndex(2) i2_id = layer.addItem(item) item = QgsAnnotationMarkerItem(QgsPoint(12, 13)) item.setSymbol( QgsMarkerSymbol.createSimple({ 'color': '100,200,200', 'size': '6', 'outline_color': 'black' })) item.setZIndex(3) i3_id = layer.addItem(item) layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) settings = QgsMapSettings() settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326')) settings.setExtent(QgsRectangle(10, 10, 18, 18)) settings.setOutputSize(QSize(200, 200)) settings.setLayers([layer]) job = QgsMapRendererParallelJob(settings) job.start() job.waitForFinished() # check rendered item results item_results = job.takeRenderedItemResults() item_details = item_results.renderedItems() self.assertEqual(len(item_details), 3) self.assertEqual([i.layerId() for i in item_details], [layer.id()] * 3) self.assertCountEqual([i.itemId() for i in item_details], [i1_id, i2_id, i3_id]) self.assertCountEqual([ i.itemId() for i in item_results.renderedAnnotationItemsInBounds( QgsRectangle(0, 0, 1, 1)) ], []) self.assertCountEqual([ i.itemId() for i in item_results.renderedAnnotationItemsInBounds( QgsRectangle(10, 10, 11, 18)) ], [i2_id]) self.assertCountEqual([ i.itemId() for i in item_results.renderedAnnotationItemsInBounds( QgsRectangle(10, 10, 12, 18)) ], [i1_id, i2_id, i3_id]) # bounds should be in map crs self.assertEqual( [i.boundingBox() for i in item_details if i.itemId() == i1_id][0], QgsRectangle(11.5, 13, 12, 13.5)) self.assertEqual( [i.boundingBox() for i in item_details if i.itemId() == i2_id][0], QgsRectangle(11, 13, 12, 15)) self.assertEqual([ i.boundingBox().toString(1) for i in item_details if i.itemId() == i3_id ][0], '11.5,12.5 : 12.5,13.5')
def testDataDefinedOpacity(self): line_shp = os.path.join(TEST_DATA_DIR, 'lines.shp') line_layer = QgsVectorLayer(line_shp, 'Lines', 'ogr') self.assertTrue(line_layer.isValid()) s = QgsLineSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setDataDefinedProperty(QgsSymbolLayer.PropertyStrokeColor, QgsProperty.fromExpression( "if(Name='Arterial', 'red', 'green')")) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) line_symbol.setOpacity(0.5) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) hash_line.setAverageAngleLength(0) s.appendSymbolLayer(hash_line.clone()) s.setDataDefinedProperty(QgsSymbol.PropertyOpacity, QgsProperty.fromExpression("if(\"Value\" = 1, 25, 50)")) line_layer.setRenderer(QgsSingleSymbolRenderer(s)) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-118.5, 19.0, -81.4, 50.4)) ms.setLayers([line_layer]) # Test rendering renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(ms) renderchecker.setControlPathPrefix('symbol_hashline') renderchecker.setControlName('expected_hashline_ddopacity') res = renderchecker.runTest('expected_hashline_ddopacity') self.report += renderchecker.report() self.assertTrue(res)
def add_design_to_map(qris_project, item, node): """adds designs to the map""" # Establish paths to layers design_id = item.data(item_code['feature_id']) subset_string = ("design_id = " + str(design_id)) design_name = item.text() geopackage_path = qris_project.project_designs.geopackage_path( qris_project.project_path) designs_layer = QgsVectorLayer(geopackage_path + "|layername=designs", "Designs", "ogr") structure_types_layer = QgsVectorLayer( geopackage_path + "|layername=structure_types", "Structure Types", "ogr") phases_layer = QgsVectorLayer(geopackage_path + "|layername=phases", "Implementation Phases", "ogr") zoi_layer = QgsVectorLayer(geopackage_path + "|layername=zoi", "ZOI", "ogr") zoi_types_layer = QgsVectorLayer(geopackage_path + "|layername=zoi_types", "ZOI", "ogr") complexes_layer = QgsVectorLayer(geopackage_path + "|layername=complexes", "Complexes", "ogr") structure_points_layer = QgsVectorLayer( geopackage_path + "|layername=structure_points", "Structures", "ogr") structure_lines_layer = QgsVectorLayer( geopackage_path + "|layername=structure_lines", "Structures", "ogr") # Get the structure geometry type # Could also do this with SQL design_iterator = designs_layer.getFeatures( QgsFeatureRequest().setFilterFid(design_id)) design_feature = next(design_iterator) structure_geometry = design_feature['structure_geometry'] def add_design_table(display_name, table_name, qml_name, read_only, group_node): """A handy way to add design layers and tables to the map""" if not any([c.name() == display_name for c in group_node.children()]): layer = QgsProject.instance().addMapLayer( QgsVectorLayer(geopackage_path + "|layername=" + table_name, display_name, "ogr"), False) if qml_name: layer_qml = os.path.join(symbology_path, 'symbology', qml_name) layer.loadNamedStyle(layer_qml) if read_only: layer.setReadOnly() group_node.addLayer(layer) # Summary tables (views) if any([c.name() == "Low-Tech Tables" for c in node.children()]): # if is there set it to the design node table_node = next(n for n in node.children() if n.name() == "Low-Tech Tables") else: # if not add the node as a group table_node = node.addGroup("Low-Tech Tables") table_node.setExpanded(False) # Summary tables (views) if any([c.name() == "Summary Tables" for c in table_node.children()]): # if is there set it to the design node summary_node = next(n for n in table_node.children() if n.name() == "Summary Tables") else: # if not add the node as a group summary_node = table_node.addGroup("Summary Tables") summary_node.setExpanded(False) add_design_table('Structure Totals - Points', 'qry_total_structures_points', None, True, summary_node) add_design_table('Structure Totals - Lines', 'qry_total_structures_lines', None, True, summary_node) add_design_table('Structure Summary - Points', 'qry_structure_summary_points', None, True, summary_node) add_design_table('Structure Summary - Lines', 'qry_structure_summary_lines', None, True, summary_node) add_design_table('Complexes Summary - Points', 'qry_complexes_by_type_points', None, True, summary_node) add_design_table('Complexes Summary - Lines', 'qry_complexes_by_type_lines', None, True, summary_node) add_design_table('ZOI Summary', 'qry_zoi_summary', None, True, summary_node) # Lookup Tables if any([c.name() == "Lookup Tables" for c in table_node.children()]): # if is there set it to the design node lookup_node = next(n for n in table_node.children() if n.name() == "Lookup Tables") else: # if not add the node as a group lookup_node = table_node.addGroup("Lookup Tables") lookup_node.setExpanded(False) add_design_table('Design Status', 'lkp_design_status', 'lkp_design_status.qml', True, lookup_node) add_design_table('Phase Action', 'lkp_phase_action', 'lkp_phase_action.qml', True, lookup_node) add_design_table('ZOI Influence', 'lkp_zoi_influence', 'lkp_zoi_influence.qml', True, lookup_node) add_design_table('ZOI Stage', 'lkp_zoi_stage', 'lkp_zoi_stage.qml', True, lookup_node) add_design_table('Structure Mimics', 'lkp_structure_mimics', 'lkp_structure_mimics.qml', True, lookup_node) # Add Design Tables if any([c.name() == "Design Tables" for c in table_node.children()]): # if is there set it to the design node design_tables_node = next(n for n in table_node.children() if n.name() == "Design Tables") else: # if not add the node as a group design_tables_node = table_node.addGroup("Design Tables") design_tables_node.setExpanded(False) # Check if the designs table has been added and if not add it. add_design_table('Designs', 'designs', 'designs.qml', False, design_tables_node) add_design_table('Structure Types', 'structure_types', 'structure_types.qml', False, design_tables_node) add_design_table('ZOI Types', 'zoi_types', 'zoi_types.qml', False, design_tables_node) add_design_table('Phases', 'phases', 'phases.qml', False, design_tables_node) # Check if the design node is already added design_group_name = str(design_id) + "-" + item.text() if any([c.name() == design_group_name for c in node.children()]): # if is there set it to the design node design_node = next(n for n in node.children() if n.name() == design_group_name) else: # if not add the node as a group design_node = node.addGroup(design_group_name) # Add structures structure_layer_name = str(design_id) + "-Structures" if structure_geometry == 'Point': # Start setting custom symbology # TODO Refactor into a functio unique_values = [] for feature in structure_types_layer.getFeatures(): values = (feature["fid"], feature["name"]) unique_values.append(values) categories = [] for value in unique_values: layer_style = {} layer_style["color"] = '%d, %d, %d' % (randrange( 0, 256), randrange(0, 256), randrange(0, 256)) layer_style['size'] = '3' layer_style['outline_color'] = 'black' symbol_layer = QgsMarkerSymbol.createSimple(layer_style) category = QgsRendererCategory(str(value[0]), symbol_layer, value[1]) categories.append(category) renderer = QgsCategorizedSymbolRenderer('structure_type_id', categories) if not any( [c.name() == structure_layer_name for c in design_node.children()]): # Adding the type suffix as I could see adding qml that symbolizes on other attributes structure_points_qml = os.path.join(symbology_path, 'symbology', 'structure_points.qml') structure_points_layer.loadNamedStyle(structure_points_qml) QgsExpressionContextUtils.setLayerVariable(structure_points_layer, 'parent_id', design_id) structure_points_layer.setSubsetString(subset_string) QgsProject.instance().addMapLayer(structure_points_layer, False) structure_points_layer.setName(structure_layer_name) design_node.addLayer(structure_points_layer) layer_node = design_node.findLayer(structure_points_layer.id()) layer_node.setExpanded(False) else: structure_points_layer = QgsProject.instance().mapLayersByName( structure_layer_name)[0] if renderer is not None: structure_points_layer.setRenderer(renderer) structure_points_layer.triggerRepaint() else: # Add line structures # Start setting custom symbology # TODO Refactor into a function unique_values = [] for feature in structure_types_layer.getFeatures(): values = (feature["fid"], feature["name"]) unique_values.append(values) categories = [] for value in unique_values: layer_style = {} layer_style["color"] = '%d, %d, %d' % (randrange( 0, 256), randrange(0, 256), randrange(0, 256)) layer_style['width'] = '1' layer_style['capstyle'] = 'round' symbol_layer = QgsLineSymbol.createSimple(layer_style) category = QgsRendererCategory(str(value[0]), symbol_layer, value[1]) categories.append(category) renderer = QgsCategorizedSymbolRenderer('structure_type_id', categories) # end custom symbology if not any( [c.name() == structure_layer_name for c in design_node.children()]): structures_lines_qml = os.path.join(symbology_path, 'symbology', 'structure_lines.qml') structure_lines_layer.loadNamedStyle(structures_lines_qml) QgsExpressionContextUtils.setLayerVariable(structure_lines_layer, 'parent_id', design_id) structure_lines_layer.setSubsetString(subset_string) QgsProject.instance().addMapLayer(structure_lines_layer, False) structure_lines_layer.setName(structure_layer_name) design_node.addLayer(structure_lines_layer) layer_node = design_node.findLayer(structure_lines_layer.id()) layer_node.setExpanded(False) else: structure_lines_layer = QgsProject.instance().mapLayersByName( structure_layer_name)[0] if renderer is not None: structure_lines_layer.setRenderer(renderer) structure_lines_layer.triggerRepaint() # Add zoi zoi_layer_name = str(design_id) + "-ZOI" # TODO Refactor into a function # TODO Refactor into sql query unique_values = [] for feature in zoi_types_layer.getFeatures(): values = (feature["fid"], feature["name"]) unique_values.append(values) categories = [] for value in unique_values: layer_style = {} alpha = 60 layer_style["color"] = "{}, {}, {}, {}".format(randrange(0, 256), randrange(0, 256), randrange(0, 256), alpha) layer_style["outline_width"] = '0.50' layer_style["outline_style"] = 'dash' symbol_layer = QgsFillSymbol.createSimple(layer_style) category = QgsRendererCategory(str(value[0]), symbol_layer, value[1]) categories.append(category) renderer = QgsCategorizedSymbolRenderer('influence_type_id', categories) # End custom symbology # check for the zoi layer, and if it is not there symbolize and add it if not any([c.name() == zoi_layer_name for c in design_node.children()]): zoi_qml = os.path.join(symbology_path, 'symbology', 'zoi.qml') zoi_layer.loadNamedStyle(zoi_qml) QgsExpressionContextUtils.setLayerVariable(zoi_layer, 'parent_id', design_id) zoi_layer.setSubsetString(subset_string) QgsProject.instance().addMapLayer(zoi_layer, False) zoi_layer.setName(zoi_layer_name) design_node.addLayer(zoi_layer) layer_node = design_node.findLayer(zoi_layer.id()) layer_node.setExpanded(False) else: zoi_layer = QgsProject.instance().mapLayersByName(zoi_layer_name)[0] if renderer is not None: zoi_layer.setRenderer(renderer) zoi_layer.triggerRepaint() # Add complexes complex_layer_name = str(design_id) + "-Complexes" if not any( [c.name() == complex_layer_name for c in design_node.children()]): complex_qml = os.path.join(symbology_path, 'symbology', 'complexes.qml') complexes_layer.loadNamedStyle(complex_qml) QgsExpressionContextUtils.setLayerVariable(complexes_layer, 'parent_id', design_id) complexes_layer.setSubsetString(subset_string) QgsProject.instance().addMapLayer(complexes_layer, False) complexes_layer.setName(complex_layer_name) design_node.addLayer(complexes_layer)
def createLineSymbol(): symbol = QgsLineSymbol.createSimple({ "color": "100,150,50" }) return symbol
def utm_symb_generator(self, grid_spacing, trUTMLL, trLLUTM, grid_symb, properties, geo_number_x, geo_number_y, UTM_num_x, UTM_num_y, t, u, geo_bound_bb, bound_UTM_bb, utmcheck): xmin_source = float(geo_bound_bb.split()[1]) ymin_source = float(geo_bound_bb.split()[2]) xmax_source = float(geo_bound_bb.split()[3]) ymax_source = float(geo_bound_bb.split()[4]) xmin_UTM = float(bound_UTM_bb.split()[1]) ymin_UTM = float(bound_UTM_bb.split()[2]) xmax_UTM = float(bound_UTM_bb.split()[3]) ymax_UTM = float(bound_UTM_bb.split()[4]) test_line = [None] * 2 properties = {'color': 'black'} line_temp = QgsLineSymbol.createSimple(properties) line_temp.setWidth(0.05) symb = QgsGeometryGeneratorSymbolLayer.create(properties) symb.setSymbolType(1) symb.setSubSymbol(line_temp) #Test First And Last Grid Lines #Vertical if (t == 1 and u == 0) or (t == UTM_num_x and u == 0): #Symbol vertices auxPointlist = self.gridLinesymbolMaker( ((floor(xmin_UTM / grid_spacing) + t) * grid_spacing), ymin_UTM, ((floor(xmin_UTM / grid_spacing) + t) * grid_spacing), ymax_UTM, xmax_source, xmin_source, ymax_source, ymin_source, trUTMLL, trLLUTM, utmcheck, True) #0: left bound; 1: right bound test_line[0] = QgsGeometry.fromWkt('LINESTRING (' + str(xmin_source) + ' ' + str(ymin_source) + ',' + str(xmin_source) + ' ' + str(ymax_source) + ')') test_line[1] = QgsGeometry.fromWkt('LINESTRING (' + str(xmax_source) + ' ' + str(ymin_source) + ',' + str(xmax_source) + ' ' + str(ymax_source) + ')') test_grid = QgsGeometry.fromPolyline( [auxPointlist[0], auxPointlist[1]]) if test_line[0].intersects(test_grid): mid_point = test_line[0].intersection(test_grid).vertexAt(0) self.utmLLtransform(utmcheck, mid_point, trLLUTM) if auxPointlist[0].x() > auxPointlist[1].x(): symb.setGeometryExpression('make_line(make_point(' + str(auxPointlist[2].x()) + ',' + str(auxPointlist[2].y()) + '), make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '))') else: symb.setGeometryExpression('make_line(make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '), make_point(' + str(auxPointlist[3].x()) + ',' + str(auxPointlist[3].y()) + '))') elif test_line[1].intersects(test_grid): mid_point = test_line[1].intersection(test_grid).vertexAt(0) self.utmLLtransform(utmcheck, mid_point, trLLUTM) if auxPointlist[0].x() < auxPointlist[1].x(): symb.setGeometryExpression('make_line(make_point(' + str(auxPointlist[2].x()) + ',' + str(auxPointlist[2].y()) + '), make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '))') else: symb.setGeometryExpression('make_line(make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '), make_point(' + str(auxPointlist[3].x()) + ',' + str(auxPointlist[3].y()) + '))') else: symb.setGeometryExpression('make_line(make_point(' + str(auxPointlist[2].x()) + ',' + str(auxPointlist[2].y()) + '), make_point(' + str(auxPointlist[3].x()) + ',' + str(auxPointlist[3].y()) + '))') #Horizontal elif (u == 1 and t == 0) or (u == UTM_num_y and t == 0): #Symbol vertices auxPointlist = self.gridLinesymbolMaker( xmin_UTM, ((floor(ymin_UTM / grid_spacing) + u) * grid_spacing), xmax_UTM, ((floor(ymin_UTM / grid_spacing) + u) * grid_spacing), xmax_source, xmin_source, ymax_source, ymin_source, trUTMLL, trLLUTM, utmcheck, False) #0: bottom bound; 1: upper bound test_line[0] = QgsGeometry.fromWkt('LINESTRING (' + str(xmin_source) + ' ' + str(ymin_source) + ',' + str(xmax_source) + ' ' + str(ymin_source) + ')') test_line[1] = QgsGeometry.fromWkt('LINESTRING (' + str(xmin_source) + ' ' + str(ymax_source) + ',' + str(xmax_source) + ' ' + str(ymax_source) + ')') test_grid = QgsGeometry.fromPolyline( [auxPointlist[0], auxPointlist[1]]) if test_line[0].intersects(test_grid): mid_point = test_line[0].intersection(test_grid).vertexAt(0) self.utmLLtransform(utmcheck, mid_point, trLLUTM) if auxPointlist[0].y() > auxPointlist[1].y(): symb.setGeometryExpression('make_line(make_point(' + str(auxPointlist[2].x()) + ',' + str(auxPointlist[2].y()) + '), make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '))') else: symb.setGeometryExpression('make_line(make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '), make_point(' + str(auxPointlist[3].x()) + ',' + str(auxPointlist[3].y()) + '))') elif test_line[1].intersects(test_grid): mid_point = test_line[1].intersection(test_grid).vertexAt(0) self.utmLLtransform(utmcheck, mid_point, trLLUTM) if auxPointlist[0].y() < auxPointlist[1].y(): symb.setGeometryExpression('make_line(make_point(' + str(auxPointlist[2].x()) + ',' + str(auxPointlist[2].y()) + '), make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '))') else: symb.setGeometryExpression('make_line(make_point(' + str(mid_point.x()) + ',' + str(mid_point.y()) + '), make_point(' + str(auxPointlist[3].x()) + ',' + str(auxPointlist[3].y()) + '))') else: symb.setGeometryExpression("make_line(make_point(" + str(auxPointlist[2].x()) + "," + str(auxPointlist[2].y()) + "), make_point(" + str(auxPointlist[3].x()) + "," + str(auxPointlist[3].y()) + "))") #Inner Grid Lines #Vertical elif (not (t == 1)) and (not (t == UTM_num_x)) and u == 0: auxPointlist = self.gridLinesymbolMaker( ((floor(xmin_UTM / grid_spacing) + t) * grid_spacing), ymin_UTM, ((floor(xmin_UTM / grid_spacing) + t) * grid_spacing), ymax_UTM, xmax_source, xmin_source, ymax_source, ymin_source, trUTMLL, trLLUTM, utmcheck, True) symb.setGeometryExpression('make_line(make_point(' + str(auxPointlist[2].x()) + ',' + str(auxPointlist[2].y()) + '), make_point(' + str(auxPointlist[3].x()) + ',' + str(auxPointlist[3].y()) + '))') #Horizontal elif (not (u == 1)) and (not (u == UTM_num_y)) and t == 0: auxPointlist = self.gridLinesymbolMaker( xmin_UTM, ((floor(ymin_UTM / grid_spacing) + u) * grid_spacing), xmax_UTM, ((floor(ymin_UTM / grid_spacing) + u) * grid_spacing), xmax_source, xmin_source, ymax_source, ymin_source, trUTMLL, trLLUTM, utmcheck, False) symb.setGeometryExpression("make_line(make_point(" + str(auxPointlist[2].x()) + "," + str(auxPointlist[2].y()) + "), make_point(" + str(auxPointlist[3].x()) + "," + str(auxPointlist[3].y()) + "))") grid_symb.appendSymbolLayer(symb) return grid_symb
def testRenderLayerWithReferenceScale(self): layer = QgsAnnotationLayer( 'test', QgsAnnotationLayer.LayerOptions( QgsProject.instance().transformContext())) layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) self.assertTrue(layer.isValid()) item = QgsAnnotationPolygonItem( QgsPolygon( QgsLineString([ QgsPoint(12, 13), QgsPoint(14, 13), QgsPoint(14, 15), QgsPoint(12, 13) ]))) item.setSymbol( QgsFillSymbol.createSimple({ 'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2' })) item.setZIndex(3) i1_id = layer.addItem(item) item = QgsAnnotationLineItem( QgsLineString( [QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])) item.setSymbol( QgsLineSymbol.createSimple({ 'color': '#ffff00', 'line_width': '3' })) item.setZIndex(2) i2_id = layer.addItem(item) item = QgsAnnotationMarkerItem(QgsPoint(12, 13)) item.setSymbol( QgsMarkerSymbol.createSimple({ 'color': '100,200,200', 'size': '6', 'outline_color': 'black' })) item.setZIndex(1) i3_id = layer.addItem(item) settings = QgsMapSettings() settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326')) settings.setExtent(QgsRectangle(10, 10, 18, 18)) settings.setOutputSize(QSize(300, 300)) settings.setFlag(QgsMapSettings.Antialiasing, False) rc = QgsRenderContext.fromMapSettings(settings) layer.item(i1_id).setUseSymbologyReferenceScale(True) layer.item(i1_id).setSymbologyReferenceScale(rc.rendererScale() * 2) # note item 3 has use symbology reference scale set to false, so should be ignored layer.item(i2_id).setUseSymbologyReferenceScale(False) layer.item(i2_id).setSymbologyReferenceScale(rc.rendererScale() * 2) layer.item(i3_id).setUseSymbologyReferenceScale(True) layer.item(i3_id).setSymbologyReferenceScale(rc.rendererScale() * 2) image = QImage(200, 200, QImage.Format_ARGB32) image.setDotsPerMeterX(int(96 / 25.4 * 1000)) image.setDotsPerMeterY(int(96 / 25.4 * 1000)) image.fill(QColor(255, 255, 255)) painter = QPainter(image) rc.setPainter(painter) try: renderer = layer.createMapRenderer(rc) renderer.render() finally: painter.end() self.assertTrue( self.imageCheck('layer_render_reference_scale', 'layer_render_reference_scale', image)) # also check details of rendered items item_details = renderer.takeRenderedItemDetails() self.assertEqual([i.layerId() for i in item_details], [layer.id()] * 3) self.assertCountEqual([i.itemId() for i in item_details], [i1_id, i2_id, i3_id]) self.assertEqual( [i.boundingBox() for i in item_details if i.itemId() == i1_id][0], QgsRectangle(12, 13, 14, 15)) self.assertEqual( [i.boundingBox() for i in item_details if i.itemId() == i2_id][0], QgsRectangle(11, 13, 12, 15)) self.assertEqual([ i.boundingBox().toString(1) for i in item_details if i.itemId() == i3_id ][0], '11.4,12.4 : 12.6,13.6')
def testPointNumInterval(self): s = QgsLineSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval) hash_line.setInterval(6) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) hash_line.setAverageAngleLength(0) s.appendSymbolLayer(hash_line.clone()) s.symbolLayer(0).setDataDefinedProperty( QgsSymbolLayer.PropertyLineDistance, QgsProperty.fromExpression("@geometry_point_num * 2")) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_dd_size', 'line_dd_size', rendered_image)
def setUp(self): # Create some simple symbols self.fill_symbol = QgsFillSymbol.createSimple({'color': '#ffffff', 'outline_color': 'black'}) self.line_symbol = QgsLineSymbol.createSimple({'color': '#ffffff', 'line_width': '3'}) self.marker_symbol = QgsMarkerSymbol.createSimple({'color': '#ffffff', 'size': '3', 'outline_color': 'black'}) self.report = "<h1>Python QgsSymbol Tests</h1>\n"
def testWidth(self): # test width and setWidth ms = QgsMapSettings() extent = QgsRectangle(100, 200, 100, 200) ms.setExtent(extent) ms.setOutputSize(QSize(400, 400)) context = QgsRenderContext.fromMapSettings(ms) context.setScaleFactor(96 / 25.4) # 96 DPI ms.setExtent(QgsRectangle(100, 150, 100, 150)) ms.setOutputDpi(ms.outputDpi() * 2) context2 = QgsRenderContext.fromMapSettings(ms) context2.setScaleFactor(300 / 25.4) # create a line symbol with a single layer line_symbol = QgsLineSymbol() line_symbol.deleteSymbolLayer(0) line_symbol.appendSymbolLayer( QgsSimpleLineSymbolLayer(color=QColor(255, 0, 0), width=10)) self.assertEqual(line_symbol.width(), 10) self.assertAlmostEqual(line_symbol.width(context), 37.795275590551185, 3) self.assertAlmostEqual(line_symbol.width(context2), 118.11023622047244, 3) line_symbol.setWidth(20) self.assertEqual(line_symbol.width(), 20) self.assertEqual(line_symbol.symbolLayer(0).width(), 20) self.assertAlmostEqual(line_symbol.width(context), 75.59055118, 3) self.assertAlmostEqual(line_symbol.width(context2), 236.2204724409449, 3) # add additional layers line_symbol.appendSymbolLayer( QgsSimpleLineSymbolLayer(color=QColor(255, 0, 0), width=10)) line_symbol.appendSymbolLayer( QgsSimpleLineSymbolLayer(color=QColor(255, 0, 0), width=30)) self.assertEqual(line_symbol.width(), 30) self.assertAlmostEqual(line_symbol.width(context), 113.38582677165356, 3) self.assertAlmostEqual(line_symbol.width(context2), 354.33070866141736, 3) line_symbol.setWidth(3) self.assertEqual(line_symbol.width(), 3) # layer widths should maintain relative size self.assertEqual(line_symbol.symbolLayer(0).width(), 2) self.assertEqual(line_symbol.symbolLayer(1).width(), 1) self.assertEqual(line_symbol.symbolLayer(2).width(), 3) # symbol layer in different size line_symbol.symbolLayer(1).setWidth(15) self.assertAlmostEqual(line_symbol.width(context), 56.69291338582678, 3) self.assertAlmostEqual(line_symbol.width(context2), 177.16535433070868, 3) line_symbol.symbolLayer(1).setWidthUnit(QgsUnitTypes.RenderPixels) self.assertAlmostEqual(line_symbol.width(context), 15, 3) self.assertAlmostEqual(line_symbol.width(context2), 35.43307086614173, 3) line_symbol.symbolLayer(1).setWidth(45) self.assertAlmostEqual(line_symbol.width(context), 45, 3) self.assertAlmostEqual(line_symbol.width(context2), 45, 3)
def testLegendRenderWithMapTheme(self): """Test rendering legends linked to map themes""" QgsProject.instance().removeAllMapLayers() point_path = os.path.join(TEST_DATA_DIR, 'points.shp') point_layer = QgsVectorLayer(point_path, 'points', 'ogr') line_path = os.path.join(TEST_DATA_DIR, 'lines.shp') line_layer = QgsVectorLayer(line_path, 'lines', 'ogr') QgsProject.instance().clear() QgsProject.instance().addMapLayers([point_layer, line_layer]) marker_symbol = QgsMarkerSymbol.createSimple({'color': '#ff0000', 'outline_style': 'no', 'size': '5'}) point_layer.setRenderer(QgsSingleSymbolRenderer(marker_symbol)) point_layer.styleManager().addStyleFromLayer("red") line_symbol = QgsLineSymbol.createSimple({'color': '#ff0000', 'line_width': '2'}) line_layer.setRenderer(QgsSingleSymbolRenderer(line_symbol)) line_layer.styleManager().addStyleFromLayer("red") red_record = QgsMapThemeCollection.MapThemeRecord() point_red_record = QgsMapThemeCollection.MapThemeLayerRecord(point_layer) point_red_record.usingCurrentStyle = True point_red_record.currentStyle = 'red' red_record.addLayerRecord(point_red_record) line_red_record = QgsMapThemeCollection.MapThemeLayerRecord(line_layer) line_red_record.usingCurrentStyle = True line_red_record.currentStyle = 'red' red_record.addLayerRecord(line_red_record) QgsProject.instance().mapThemeCollection().insert('red', red_record) marker_symbol1 = QgsMarkerSymbol.createSimple({'color': '#0000ff', 'outline_style': 'no', 'size': '5'}) marker_symbol2 = QgsMarkerSymbol.createSimple( {'color': '#0000ff', 'name': 'diamond', 'outline_style': 'no', 'size': '5'}) marker_symbol3 = QgsMarkerSymbol.createSimple( {'color': '#0000ff', 'name': 'rectangle', 'outline_style': 'no', 'size': '5'}) point_layer.setRenderer(QgsCategorizedSymbolRenderer('Class', [QgsRendererCategory('B52', marker_symbol1, ''), QgsRendererCategory('Biplane', marker_symbol2, ''), QgsRendererCategory('Jet', marker_symbol3, ''), ])) point_layer.styleManager().addStyleFromLayer("blue") line_symbol = QgsLineSymbol.createSimple({'color': '#0000ff', 'line_width': '2'}) line_layer.setRenderer(QgsSingleSymbolRenderer(line_symbol)) line_layer.styleManager().addStyleFromLayer("blue") blue_record = QgsMapThemeCollection.MapThemeRecord() point_blue_record = QgsMapThemeCollection.MapThemeLayerRecord(point_layer) point_blue_record.usingCurrentStyle = True point_blue_record.currentStyle = 'blue' blue_record.addLayerRecord(point_blue_record) line_blue_record = QgsMapThemeCollection.MapThemeLayerRecord(line_layer) line_blue_record.usingCurrentStyle = True line_blue_record.currentStyle = 'blue' blue_record.addLayerRecord(line_blue_record) QgsProject.instance().mapThemeCollection().insert('blue', blue_record) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map1 = QgsLayoutItemMap(layout) map1.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map1.setFrameEnabled(True) map1.setLayers([point_layer, line_layer]) layout.addLayoutItem(map1) map1.setExtent(point_layer.extent()) map1.setFollowVisibilityPreset(True) map1.setFollowVisibilityPresetName('red') map2 = QgsLayoutItemMap(layout) map2.attemptSetSceneRect(QRectF(20, 120, 80, 80)) map2.setFrameEnabled(True) map2.setLayers([point_layer, line_layer]) layout.addLayoutItem(map2) map2.setExtent(point_layer.extent()) map2.setFollowVisibilityPreset(True) map2.setFollowVisibilityPresetName('blue') legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") legend.attemptSetSceneRect(QRectF(120, 20, 80, 80)) legend.setFrameEnabled(True) legend.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend.setBackgroundColor(QColor(200, 200, 200)) legend.setTitle('') layout.addLayoutItem(legend) legend.setLinkedMap(map1) legend2 = QgsLayoutItemLegend(layout) legend2.setTitle("Legend") legend2.attemptSetSceneRect(QRectF(120, 120, 80, 80)) legend2.setFrameEnabled(True) legend2.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend2.setBackgroundColor(QColor(200, 200, 200)) legend2.setTitle('') layout.addLayoutItem(legend2) legend2.setLinkedMap(map2) checker = QgsLayoutChecker( 'composer_legend_theme', layout) checker.setControlPathPrefix("composer_legend") result, message = checker.testLayout() TestQgsLayoutItemLegend.report += checker.report() self.assertTrue(result, message) QgsProject.instance().clear()
def utm_Symb_Generator(self, layer_bound, grid_spacing, trUTMLL, trLLUTM, grid_symb, properties, UTM_num_x, UTM_num_y, t, u, extentsGeo, extentsUTM, linwidth_utm, color): test_line = [None] * 2 properties = {'color': color.name()} line_temp = QgsLineSymbol.createSimple(properties) line_temp.setWidth(linwidth_utm) symb = QgsGeometryGeneratorSymbolLayer.create(properties) symb.setSymbolType(1) symb.setSubSymbol(line_temp) #Test First And Last Grid Lines #Vertical if (t == 1 and u == 0) or (t == UTM_num_x and u == 0): #Symbol vertices auxPointlist = self.gridLinesymbolMaker( ((floor(extentsUTM[0] / grid_spacing) + t) * grid_spacing), extentsUTM[1], ((floor(extentsUTM[0] / grid_spacing) + t) * grid_spacing), extentsUTM[3], extentsGeo[2], extentsGeo[0], extentsGeo[3], extentsGeo[1], trUTMLL, trLLUTM, True) #0: left bound; 1: right bound test_line[0] = QgsGeometry.fromWkt('LINESTRING (' + str(extentsGeo[0]) + ' ' + str(extentsGeo[1]) + ',' + str(extentsGeo[0]) + ' ' + str(extentsGeo[3]) + ')') test_line[1] = QgsGeometry.fromWkt('LINESTRING (' + str(extentsGeo[2]) + ' ' + str(extentsGeo[1]) + ',' + str(extentsGeo[2]) + ' ' + str(extentsGeo[3]) + ')') test_grid = QgsGeometry.fromPolyline( [auxPointlist[0], auxPointlist[1]]) if test_line[0].intersects(test_grid): mid_point = test_line[0].intersection(test_grid).vertexAt(0) mid_point.transform(trLLUTM) if auxPointlist[0].x() > auxPointlist[1].x(): symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})), 'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), mid_point.x(), mid_point.y(), layer_bound)) else: symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})), 'EPSG:{}', @map_crs)" .format(mid_point.x(), mid_point.y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) elif test_line[1].intersects(test_grid): mid_point = test_line[1].intersection(test_grid).vertexAt(0) mid_point.transform(trLLUTM) if auxPointlist[0].x() < auxPointlist[1].x(): symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), mid_point.x(), mid_point.y(), layer_bound)) else: symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(mid_point.x(), mid_point.y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) else: symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) #Horizontal elif (u == 1 and t == 0) or (u == UTM_num_y and t == 0): #Symbol vertices auxPointlist = self.gridLinesymbolMaker( extentsUTM[0], ((floor(extentsUTM[1] / grid_spacing) + u) * grid_spacing), extentsUTM[2], ((floor(extentsUTM[1] / grid_spacing) + u) * grid_spacing), extentsGeo[2], extentsGeo[0], extentsGeo[3], extentsGeo[1], trUTMLL, trLLUTM, False) #0: bottom bound; 1: upper bound test_line[0] = QgsGeometry.fromWkt('LINESTRING (' + str(extentsGeo[0]) + ' ' + str(extentsGeo[1]) + ',' + str(extentsGeo[2]) + ' ' + str(extentsGeo[1]) + ')') test_line[1] = QgsGeometry.fromWkt('LINESTRING (' + str(extentsGeo[0]) + ' ' + str(extentsGeo[3]) + ',' + str(extentsGeo[2]) + ' ' + str(extentsGeo[3]) + ')') test_grid = QgsGeometry.fromPolyline( [auxPointlist[0], auxPointlist[1]]) if test_line[0].intersects(test_grid): mid_point = test_line[0].intersection(test_grid).vertexAt(0) mid_point.transform(trLLUTM) if auxPointlist[0].y() > auxPointlist[1].y(): symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), mid_point.x(), mid_point.y(), layer_bound)) else: symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(mid_point.x(), mid_point.y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) elif test_line[1].intersects(test_grid): mid_point = test_line[1].intersection(test_grid).vertexAt(0) mid_point.transform(trLLUTM) if auxPointlist[0].y() < auxPointlist[1].y(): symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), mid_point.x(), mid_point.y(), layer_bound)) else: symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(mid_point.x(), mid_point.y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) else: symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) #Inner Grid Lines #Vertical elif (not (t == 1)) and (not (t == UTM_num_x)) and u == 0: auxPointlist = self.gridLinesymbolMaker( ((floor(extentsUTM[0] / grid_spacing) + t) * grid_spacing), extentsUTM[1], ((floor(extentsUTM[0] / grid_spacing) + t) * grid_spacing), extentsUTM[3], extentsGeo[2], extentsGeo[0], extentsGeo[3], extentsGeo[1], trUTMLL, trLLUTM, True) symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) #Horizontal elif (not (u == 1)) and (not (u == UTM_num_y)) and t == 0: auxPointlist = self.gridLinesymbolMaker( extentsUTM[0], ((floor(extentsUTM[1] / grid_spacing) + u) * grid_spacing), extentsUTM[2], ((floor(extentsUTM[1] / grid_spacing) + u) * grid_spacing), extentsGeo[2], extentsGeo[0], extentsGeo[3], extentsGeo[1], trUTMLL, trLLUTM, False) symb.setGeometryExpression( "transform(make_line(make_point({}, {}), make_point({}, {})),'EPSG:{}', @map_crs)" .format(auxPointlist[2].x(), auxPointlist[2].y(), auxPointlist[3].x(), auxPointlist[3].y(), layer_bound)) grid_symb.appendSymbolLayer(symb) return grid_symb
def testLegendRenderLinkedMapScale(self): """Test rendering legends linked to maps follow scale correctly""" QgsProject.instance().removeAllMapLayers() line_path = os.path.join(TEST_DATA_DIR, 'lines.shp') line_layer = QgsVectorLayer(line_path, 'lines', 'ogr') QgsProject.instance().clear() QgsProject.instance().addMapLayers([line_layer]) line_symbol = QgsLineSymbol.createSimple({ 'color': '#ff0000', 'width_unit': 'mapunits', 'width': '0.0001' }) line_layer.setRenderer(QgsSingleSymbolRenderer(line_symbol)) layout = QgsLayout(QgsProject.instance()) layout.initializeDefaults() map1 = QgsLayoutItemMap(layout) map1.attemptSetSceneRect(QRectF(20, 20, 80, 80)) map1.setFrameEnabled(True) map1.setLayers([line_layer]) layout.addLayoutItem(map1) map1.setExtent(line_layer.extent()) map1.setScale(2000) map2 = QgsLayoutItemMap(layout) map2.attemptSetSceneRect(QRectF(20, 120, 80, 80)) map2.setFrameEnabled(True) map2.setLayers([line_layer]) layout.addLayoutItem(map2) map2.setExtent(line_layer.extent()) map2.setScale(5000) legend = QgsLayoutItemLegend(layout) legend.setTitle("Legend") legend.attemptSetSceneRect(QRectF(120, 20, 80, 80)) legend.setFrameEnabled(True) legend.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend.setBackgroundColor(QColor(200, 200, 200)) legend.setTitle('') layout.addLayoutItem(legend) legend.setLinkedMap(map1) legend2 = QgsLayoutItemLegend(layout) legend2.setTitle("Legend") legend2.attemptSetSceneRect(QRectF(120, 120, 80, 80)) legend2.setFrameEnabled(True) legend2.setFrameStrokeWidth(QgsLayoutMeasurement(2)) legend2.setBackgroundColor(QColor(200, 200, 200)) legend2.setTitle('') layout.addLayoutItem(legend2) legend2.setLinkedMap(map2) checker = QgsLayoutChecker('composer_legend_scale_map', layout) checker.setControlPathPrefix("composer_legend") result, message = checker.testLayout() TestQgsLayoutItemLegend.report += checker.report() self.assertTrue(result, message) QgsProject.instance().clear()
def testRenderWithTransform(self): layer = QgsAnnotationLayer( 'test', QgsAnnotationLayer.LayerOptions( QgsProject.instance().transformContext())) self.assertTrue(layer.isValid()) item = QgsAnnotationPolygonItem( QgsPolygon( QgsLineString([ QgsPoint(11.5, 13), QgsPoint(12, 13), QgsPoint(12, 13.5), QgsPoint(11.5, 13) ]))) item.setSymbol( QgsFillSymbol.createSimple({ 'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2' })) item.setZIndex(1) layer.addItem(item) item = QgsAnnotationLineStringItem( QgsLineString( [QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])) item.setSymbol( QgsLineSymbol.createSimple({ 'color': '#ffff00', 'line_width': '3' })) item.setZIndex(2) layer.addItem(item) item = QgsAnnotationMarkerItem(QgsPointXY(12, 13)) item.setSymbol( QgsMarkerSymbol.createSimple({ 'color': '100,200,200', 'size': '6', 'outline_color': 'black' })) item.setZIndex(3) layer.addItem(item) layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) settings = QgsMapSettings() settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3857')) settings.setExtent(QgsRectangle(1250958, 1386945, 1420709, 1532518)) settings.setOutputSize(QSize(300, 300)) settings.setFlag(QgsMapSettings.Antialiasing, False) rc = QgsRenderContext.fromMapSettings(settings) rc.setCoordinateTransform( QgsCoordinateTransform(layer.crs(), settings.destinationCrs(), QgsProject.instance())) image = QImage(200, 200, QImage.Format_ARGB32) image.setDotsPerMeterX(96 / 25.4 * 1000) image.setDotsPerMeterY(96 / 25.4 * 1000) image.fill(QColor(255, 255, 255)) painter = QPainter(image) rc.setPainter(painter) try: renderer = layer.createMapRenderer(rc) renderer.render() finally: painter.end() self.assertTrue( self.imageCheck('layer_render_transform', 'layer_render_transform', image))
def setUp(self): #Create some simple symbols self.fill_symbol = QgsFillSymbol.createSimple({'color': '#ffffff'}) self.line_symbol = QgsLineSymbol.createSimple({'color': '#ffffff', 'line_width': '3'}) self.marker_symbol = QgsMarkerSymbol.createSimple({'color': '#ffffff', 'size': '3'}) self.report = "<h1>Python QgsSymbol Tests</h1>\n"
def editNetworkLayer(self, progressBar, layerName, scenariosExpression, networkExpression, variable, level, projectPath, group, networkLinkShapePath, method, layerId, expressionNetworkText, color): """ @summary: Get operators dictionary @param layerName: Layer name @type layerName: String @param scenariosExpression: Scenarios expression @type scenariosExpression: Stack object @param networkExpression: Network expression @type networkExpression: Stack object @param variable: Variable to evaluate @type variable: String @param level: Level to evaluate (Total, Routes, Operators) @type level: Level object @param projectPath: Project path @type projectPath: String @param group: Project group @type group: Layer group @param networkLinkShapePath: Network link shape path @type networkLinkShapePath: String @return: Result of the layer creation """ if scenariosExpression is None: QMessageBox.warning(None, "Network expression", "There is not scenarios information.") print("There is not scenarios information.") return False registry = QgsProject.instance() layersCount = len(registry.mapLayers()) result, resultData, minValue, maxValue = self.network_data_access.create_network_memory( layerName, scenariosExpression, networkExpression, variable, level, projectPath) progressBar.setValue(15) if result: # Source shape, name of the new shape, providerLib layer = QgsVectorLayer(networkLinkShapePath, layerName + "_network", 'ogr') epsg = layer.crs().postgisSrid() intMethod = 0 if method == "Color" else 1 rowCounter = len(resultData) if not layer.isValid(): return False feats = [feat for feat in layer.getFeatures()] # Create a vector layer with data on Memory memoryLayer = registry.mapLayer(layerId) memory_data = memoryLayer.dataProvider() joinedFieldName = "Result" shpField = "Id" attr = layer.dataProvider().fields().toList() attr += [QgsField(joinedFieldName, QVariant.Double)] progressBar.setValue(25) memory_data.addAttributes(attr) memory_data.addFeatures(feats) num = 30 progressBar.setValue(num) progressInterval = 70 / len(resultData) memoryLayer.startEditing() for rowItem in np.nditer(resultData): value = 0 num += progressInterval progressBar.setValue(num) it = memoryLayer.getFeatures("LINKID = '{0}'".format( str(rowItem['Id']).replace("b", "").replace("'", ""))) for id_feature in it: memoryLayer.changeAttributeValue( id_feature.id(), memory_data.fieldNameIndex(joinedFieldName), QVariant(round(float(rowItem['Result']), 2))) memoryLayer.commitChanges() myStyle = QgsStyle().defaultStyle() defaultColorRampNames = myStyle.colorRampNames() ramp = myStyle.colorRamp(defaultColorRampNames[0]) ranges = [] nCats = ramp.count() rng = maxValue - minValue nCats = 8 scale = QgsMapUnitScale(minValue, maxValue) if method == "Color": color1 = list( map(lambda x: int(x), color['color1'].split(",")[0:3])) color2 = list( map(lambda x: int(x), color['color2'].split(",")[0:3])) interpolatedColors = HP.linear_gradient(color1, color2, nCats) for i in range(0, nCats): v0 = minValue + rng / float(nCats) * i v1 = minValue + rng / float(nCats) * (i + 1) if method == "Color": line = QgsSimpleLineSymbolLayer( QColor(interpolatedColors['r'][i], interpolatedColors['g'][i], interpolatedColors['b'][i])) line.setOffsetUnit(2) line.setOffset(2) line.setWidth(0.8) symbol = QgsLineSymbol() symbol.changeSymbolLayer(0, line) myRange = QgsRendererRange(v0, v1, symbol, "") elif method == "Size": qcolor = QColor() qcolor.setRgb(color) line = QgsSimpleLineSymbolLayer(qcolor) line.setOffsetUnit(2) line.setOffset(0.7) # Symbol # symbolLine = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayerBase.ArrowHead) # Mark line # markLine = QgsMarkerLineSymbolLayer() # markLine.setPlacement(4) symbolo = QgsLineSymbol() symbolo.changeSymbolLayer(0, line) # symbolo.appendSymbolLayer(line) myRange = QgsRendererRange(v0, v1, symbolo, "") ranges.append(myRange) # The first parameter refers to the name of the field that contains the calculated value (expression) modeRender = QgsGraduatedSymbolRenderer.Mode(2) renderer = QgsGraduatedSymbolRenderer(joinedFieldName, ranges) renderer.setMode(modeRender) renderer.setGraduatedMethod(intMethod) if method == "Size": renderer.setSymbolSizes(0.200000, 2.60000) renderer.setSourceColorRamp(ramp) memoryLayer.setRenderer(renderer) typeLayer = "network" fieldName = "LINKID" networkExpressionText = str(scenariosExpression) # Create XML File ".qtranus" with the parameters of the executions if FileMXML.if_exist_xml_layers(projectPath): if FileMXML.if_exist_layer(projectPath, memoryLayer.id()): FileMXML.update_xml_file(memoryLayer.name(), memoryLayer.id(), scenariosExpression, variable, networkExpression, projectPath, expressionNetworkText, method, level, color) else: FileMXML.add_layer_xml_file(memoryLayer.name(), memoryLayer.id(), scenariosExpression, variable, networkExpression, projectPath, expressionNetworkText, shpField, typeLayer, method, level, color) else: FileMXML.create_xml_file(memoryLayer.name(), memoryLayer.id(), scenariosExpression, variable, networkExpression, projectPath, expressionNetworkText, shpField, typeLayer, method, level, color) #group.insertLayer((layersCount+1), memoryLayer) progressBar.setValue(100) return True
def setStyle(self, layer, name): if name == "": return stylePath = os.path.join(os.path.dirname(os.path.dirname(__file__)), "layerStyles") # user style qmlPath = os.path.join(stylePath, name + "_user.qml") if os.path.exists(qmlPath): layer.loadNamedStyle(qmlPath) return # default style qmlPath = os.path.join(stylePath, name + ".qml.bak") if os.path.exists(qmlPath): layer.loadNamedStyle(qmlPath) svgPath = os.path.join(stylePath, name + ".svg") if os.path.exists(svgPath): if layer.geometryType() == 0: # Point svg_style = dict() svg_style['name'] = svgPath svg_style['size'] = str(7) if name == "demands": svg_style['fill'] = '#9a1313' symbol_layer = QgsSvgMarkerSymbolLayer.create(svg_style) symbol = QgsSymbol.defaultSymbol(layer.geometryType()) symbol.changeSymbolLayer(0, symbol_layer) renderer = QgsSingleSymbolRenderer(symbol) else: # Line symbol = QgsLineSymbol().createSimple({}) symbol.deleteSymbolLayer(0) # Line lineSymbol = QgsSimpleLineSymbolLayer() lineSymbol.setWidthUnit(2) # Pixels lineSymbol.setWidth(1.5) if name == "pipes": lineSymbol.setColor(QColor("#0f1291")) symbol.appendSymbolLayer(lineSymbol) # Symbol marker = QgsMarkerSymbol.createSimple({}) marker.deleteSymbolLayer(0) svg_props = dict() svg_props['name'] = svgPath size = 5 if name == "pipes": size = 0 svg_props['size'] = str(size) svg_props['offset'] = '-0.5,-0.5' svg_props['offset_unit'] = 'Pixel' markerSymbol = QgsSvgMarkerSymbolLayer.create(svg_props) marker.appendSymbolLayer(markerSymbol) # Final Symbol finalMarker = QgsMarkerLineSymbolLayer() finalMarker.setSubSymbol(marker) finalMarker.setPlacement(QgsMarkerLineSymbolLayer.CentralPoint) symbol.appendSymbolLayer(finalMarker) if name == "pipes": prop = QgsProperty() prop.setExpressionString( "if(IniStatus is NULL, 0,if(IniStatus !='CV', 0,5))") symbol.symbolLayer(1).setDataDefinedProperty( 9, prop) # 9 = PropertyWidth renderer = QgsSingleSymbolRenderer(symbol) layer.setRenderer(renderer)
def parcourir_reseau_depuis_cet_objet(*args): id_objet = args[0] method = args[1] # Use alg get_downstream_route and get_upstream_route params = { 'OUTPUT_LAYER_NAME': '', 'SOURCE_ID': id_objet, 'METHOD': method } down = {} try: down = processing.run('raepa:get_downstream_route', params) except QgsProcessingException: # If the object is at the end of the network, the SQL does not provide Geometry # so the layer is invalid but we have to continue to test upstream QgsMessageLog.logMessage('Erreur dans les logs de Processing/PostGIS.', 'RAEPA', Qgis.Critical) iface.messageBar().pushMessage( 'Erreur dans les logs de Processing/PostGIS.', level=Qgis.Critical, duration=2) down['OUTPUT_STATUS'] = 0 if down['OUTPUT_STATUS'] == 1: layer = down['OUTPUT_LAYER'] layer.setName(down['OUTPUT_LAYER_RESULT_NAME']) symbol = QgsLineSymbol.createSimple( { 'line_color': '255,50,50,255', 'line_style': 'solid', 'line_width': '1.8' } ) layer.renderer().setSymbol(symbol) QgsProject.instance().addMapLayer(layer) params = { 'OUTPUT_LAYER_NAME': '', 'SOURCE_ID': id_objet, 'METHOD': method } up = {} try: up = processing.run('raepa:get_upstream_route', params) except QgsProcessingException: # If the objetc is at the end of the network, the SQL does not provide Geometry # so the layer is invalid but we have to continue to provide downstream QgsMessageLog.logMessage('Erreur dans les logs de Processing/PostGIS.', 'RAEPA', Qgis.Critical) iface.messageBar().pushMessage( 'Erreur dans les logs de Processing/PostGIS.', level=Qgis.Critical, duration=2) up['OUTPUT_STATUS'] = 0 if up['OUTPUT_STATUS'] == 1: layer = up['OUTPUT_LAYER'] layer.setName(up['OUTPUT_LAYER_RESULT_NAME']) symbol = QgsLineSymbol.createSimple( { 'line_color': '50,255,50,255', 'line_style': 'solid', 'line_width': '1.8' } ) layer.renderer().setSymbol(symbol) QgsProject.instance().addMapLayer(layer)
def testLineOffset(self): s = QgsLineSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Interval) hash_line.setInterval(6) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(10) s.appendSymbolLayer(hash_line.clone()) s.symbolLayer(0).setOffset(3) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_offset_positive', 'line_offset_positive', rendered_image) s.symbolLayer(0).setOffset(-3) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_offset_negative', 'line_offset_negative', rendered_image)
def test_render_via_job_with_transform(self): """ Test rendering an annotation layer via a map render job """ layer = QgsAnnotationLayer( 'test', QgsAnnotationLayer.LayerOptions( QgsProject.instance().transformContext())) self.assertTrue(layer.isValid()) item = QgsAnnotationPolygonItem( QgsPolygon( QgsLineString([ QgsPoint(11.5, 13), QgsPoint(12, 13), QgsPoint(12, 13.5), QgsPoint(11.5, 13) ]))) item.setSymbol( QgsFillSymbol.createSimple({ 'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2' })) item.setZIndex(1) i1_id = layer.addItem(item) item = QgsAnnotationLineItem( QgsLineString( [QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])) item.setSymbol( QgsLineSymbol.createSimple({ 'color': '#ffff00', 'line_width': '3' })) item.setZIndex(2) i2_id = layer.addItem(item) item = QgsAnnotationMarkerItem(QgsPoint(12, 13)) item.setSymbol( QgsMarkerSymbol.createSimple({ 'color': '100,200,200', 'size': '6', 'outline_color': 'black' })) item.setZIndex(3) i3_id = layer.addItem(item) layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) settings = QgsMapSettings() settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3857')) settings.setExtent(QgsRectangle(1250958, 1386945, 1420709, 1532518)) settings.setOutputSize(QSize(200, 200)) settings.setLayers([layer]) job = QgsMapRendererSequentialJob(settings) job.start() job.waitForFinished() # check rendered item results item_results = job.takeRenderedItemResults() item_details = item_results.renderedItems() self.assertEqual(len(item_details), 3) self.assertEqual([i.layerId() for i in item_details], [layer.id()] * 3) self.assertCountEqual([i.itemId() for i in item_details], [i1_id, i2_id, i3_id]) # bounds should be in map crs self.assertEqual( [ QgsGeometry.fromRect(i.boundingBox()).asWkt(0) for i in item_details if i.itemId() == i1_id ][0], 'Polygon ((1280174 1459732, 1335834 1459732, 1335834 1516914, 1280174 1516914, 1280174 1459732))' ) self.assertEqual( [ QgsGeometry.fromRect(i.boundingBox()).asWkt(0) for i in item_details if i.itemId() == i2_id ][0], 'Polygon ((1224514 1459732, 1335834 1459732, 1335834 1689200, 1224514 1689200, 1224514 1459732))' ) expected = 'Polygon ((1325786 1449684, 1345882 1449684, 1345882 1469780, 1325786 1469780, 1325786 1449684))' result = [ QgsGeometry.fromRect(i.boundingBox()).asWkt(0) for i in item_details if i.itemId() == i3_id ][0] self.assertTrue( compareWkt(result, expected, tol=1000), "mismatch Expected:\n{}\nGot:\n{}\n".format(expected, result))
def testHashPlacement(self): s = QgsLineSymbol() s.deleteSymbolLayer(0) hash_line = QgsHashedLineSymbolLayer(True) hash_line.setPlacement(QgsTemplatedLineSymbolLayerBase.Vertex) hash_line.setInterval(6) simple_line = QgsSimpleLineSymbolLayer() simple_line.setColor(QColor(0, 255, 0)) simple_line.setWidth(1) line_symbol = QgsLineSymbol() line_symbol.changeSymbolLayer(0, simple_line) hash_line.setSubSymbol(line_symbol) hash_line.setHashLength(7) s.appendSymbolLayer(hash_line.clone()) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_hash_vertex', 'line_hash_vertex', rendered_image) s.symbolLayer(0).setPlacement( QgsTemplatedLineSymbolLayerBase.FirstVertex) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_hash_first', 'line_hash_first', rendered_image) s.symbolLayer(0).setPlacement( QgsTemplatedLineSymbolLayerBase.LastVertex) g = QgsGeometry.fromWkt('LineString(0 0, 10 10, 10 0)') rendered_image = self.renderGeometry(s, g) assert self.imageCheck('line_hash_last', 'line_hash_last', rendered_image)
def testBasic(self): props = QgsVectorLayerElevationProperties(None) self.assertEqual(props.zScale(), 1) self.assertEqual(props.zOffset(), 0) self.assertFalse(props.extrusionEnabled()) self.assertEqual(props.extrusionHeight(), 0) self.assertFalse(props.hasElevation()) self.assertEqual(props.clamping(), Qgis.AltitudeClamping.Terrain) self.assertEqual(props.binding(), Qgis.AltitudeBinding.Centroid) props.setZOffset(0.5) props.setZScale(2) props.setClamping(Qgis.AltitudeClamping.Relative) props.setBinding(Qgis.AltitudeBinding.Vertex) props.setExtrusionHeight(10) props.setExtrusionEnabled(True) self.assertEqual(props.zScale(), 2) self.assertEqual(props.zOffset(), 0.5) self.assertEqual(props.extrusionHeight(), 10) self.assertTrue(props.hasElevation()) self.assertTrue(props.extrusionEnabled()) self.assertEqual(props.clamping(), Qgis.AltitudeClamping.Relative) self.assertEqual(props.binding(), Qgis.AltitudeBinding.Vertex) sym = QgsLineSymbol.createSimple({ 'outline_color': '#ff4433', 'outline_width': 0.5 }) props.setProfileLineSymbol(sym) self.assertEqual(props.profileLineSymbol().color().name(), '#ff4433') sym = QgsFillSymbol.createSimple({ 'color': '#ff4455', 'outline_width': 0.5 }) props.setProfileFillSymbol(sym) self.assertEqual(props.profileFillSymbol().color().name(), '#ff4455') sym = QgsMarkerSymbol.createSimple({ 'color': '#ff1122', 'outline_width': 0.5 }) props.setProfileMarkerSymbol(sym) self.assertEqual(props.profileMarkerSymbol().color().name(), '#ff1122') doc = QDomDocument("testdoc") elem = doc.createElement('test') props.writeXml(elem, doc, QgsReadWriteContext()) props2 = QgsVectorLayerElevationProperties(None) props2.readXml(elem, QgsReadWriteContext()) self.assertEqual(props2.zScale(), 2) self.assertEqual(props2.zOffset(), 0.5) self.assertEqual(props2.clamping(), Qgis.AltitudeClamping.Relative) self.assertEqual(props2.binding(), Qgis.AltitudeBinding.Vertex) self.assertEqual(props2.extrusionHeight(), 10) self.assertTrue(props2.extrusionEnabled()) self.assertEqual(props2.profileLineSymbol().color().name(), '#ff4433') self.assertEqual(props2.profileFillSymbol().color().name(), '#ff4455') self.assertEqual(props2.profileMarkerSymbol().color().name(), '#ff1122')
def testRenderWithTransform(self): layer = QgsAnnotationLayer( 'test', QgsAnnotationLayer.LayerOptions( QgsProject.instance().transformContext())) self.assertTrue(layer.isValid()) item = QgsAnnotationPolygonItem( QgsPolygon( QgsLineString([ QgsPoint(11.5, 13), QgsPoint(12, 13), QgsPoint(12, 13.5), QgsPoint(11.5, 13) ]))) item.setSymbol( QgsFillSymbol.createSimple({ 'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2' })) item.setZIndex(1) i1_id = layer.addItem(item) item = QgsAnnotationLineItem( QgsLineString( [QgsPoint(11, 13), QgsPoint(12, 13), QgsPoint(12, 15)])) item.setSymbol( QgsLineSymbol.createSimple({ 'color': '#ffff00', 'line_width': '3' })) item.setZIndex(2) i2_id = layer.addItem(item) item = QgsAnnotationMarkerItem(QgsPoint(12, 13)) item.setSymbol( QgsMarkerSymbol.createSimple({ 'color': '100,200,200', 'size': '6', 'outline_color': 'black' })) item.setZIndex(3) i3_id = layer.addItem(item) layer.setCrs(QgsCoordinateReferenceSystem('EPSG:4326')) settings = QgsMapSettings() settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3857')) settings.setExtent(QgsRectangle(1250958, 1386945, 1420709, 1532518)) settings.setOutputSize(QSize(300, 300)) settings.setFlag(QgsMapSettings.Antialiasing, False) rc = QgsRenderContext.fromMapSettings(settings) rc.setCoordinateTransform( QgsCoordinateTransform(layer.crs(), settings.destinationCrs(), QgsProject.instance())) rc.setExtent(rc.coordinateTransform().transformBoundingBox( settings.extent(), QgsCoordinateTransform.ReverseTransform)) image = QImage(200, 200, QImage.Format_ARGB32) image.setDotsPerMeterX(int(96 / 25.4 * 1000)) image.setDotsPerMeterY(int(96 / 25.4 * 1000)) image.fill(QColor(255, 255, 255)) painter = QPainter(image) rc.setPainter(painter) try: renderer = layer.createMapRenderer(rc) renderer.render() finally: painter.end() self.assertTrue( self.imageCheck('layer_render_transform', 'layer_render_transform', image)) # also check details of rendered items item_details = renderer.takeRenderedItemDetails() self.assertEqual([i.layerId() for i in item_details], [layer.id()] * 3) self.assertCountEqual([i.itemId() for i in item_details], [i1_id, i2_id, i3_id]) self.assertEqual( [i.boundingBox() for i in item_details if i.itemId() == i1_id][0], QgsRectangle(11.5, 13, 12, 13.5)) self.assertEqual( [i.boundingBox() for i in item_details if i.itemId() == i2_id][0], QgsRectangle(11, 13, 12, 15)) self.assertEqual([ i.boundingBox().toString(2) for i in item_details if i.itemId() == i3_id ][0], '11.94,12.94 : 12.06,13.06')