Geographic Information Systems Asked by mathias mirra on July 17, 2021
I performed a selectByExpression on one of my layers and then created a new layer.
layer 1 : co,
layer 2 : co_temp
co.selectByExpression("SECTION=1")
co_temp = processing.run("native:saveselectedfeatures", {'INPUT': co, 'OUTPUT': 'memory:'})['OUTPUT']
co.removeSelection()
renderer = co.renderer().clone()
co_temp.setRenderer(renderer)
co_temp.renderer().filter()
co_temp.triggerRepaint()
co.removeSelection()
I would like to be able to create a co_temp caption, but only for selected co_temp expressions. There is a lot of useless information in co that I would like not to retrieve for the co_temp caption.
mapt = QgsLayoutItemMap(layout)
mapt.attemptSetSceneRect(QRectF(20, 20, 80, 80))
mapt.setFrameEnabled(True)
mapt.setLayers([co_temp])
mapt.setCrs(co_temp.crs())
layout.addLayoutItem(mapt)
mapt.setExtent(co_temp.extent().buffered(250))
legend = QgsLayoutItemLegend(layout)
legend.setTitle("Legend")
legend.attemptSetSceneRect(QRectF(120, 20, 80, 80))
legend.setFrameEnabled(True)
legend.setBackgroundColor(QColor(200, 200, 200))
legend.setTitle('')
legend.setLegendFilterByMapEnabled(True)
legend.setResizeToContents(False)
layout.addLayoutItem(legend)
legend.setLinkedMap(mapt)
I tried this but it doesn’t work.
co :
co legend:
co_temp :
co_temp legend:
I finally managed to filter my legend I'm sharing my code with you. co : layer 1 co_temp : layer 2
co.selectByExpression("SECTION=1")
co_temp = processing.run("native:saveselectedfeatures", {'INPUT': co,
'OUTPUT': 'memory:'})['OUTPUT']
co.removeSelection()
renderer = co.renderer().clone()
renderer2 = co.renderer().clone()
renderer3 = co.renderer().clone()
renderer2.deleteAllCategories()
renderer3.deleteAllCategories()
co_temp.setRenderer(renderer)
ctx = QgsRenderContext()
table = []
table2 = []
for feature in co_temp.getFeatures():
for i in renderer.categories():
renderer3.addCategory(i)
#####################################
renderer3.startRender(ctx, QgsFields())
ctx.expressionContext().setFeature(feature)
#####################################
if renderer3.willRenderFeature(feature, ctx):
if i.label() not in table and i.label() != "":
table.append(i.label())
table2.append(i)
#####################################
renderer3.stopRender(ctx)
#####################################
renderer3.deleteAllCategories()
for i in table2:
i.setLabel(i.label().lower())
renderer2.addCategory(i)
co_temp.setRenderer(renderer2)
co_temp.triggerRepaint()
Answered by mathias mirra on July 17, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP