Geographic Information Systems Asked on May 12, 2021
I currently have a script that runs data within my map and then exports it as a layout in print composer. On the map, I have a legend key that is reference to the data on the map. Within my legend, I have a yellow buffer that is titled ‘MM1 1.0 Buffer’. Within the attribute table of this vector file, I have a column called ‘LAC_Population_sum’, with a value of affected population from an earthquake, within that buffer area. However, I can not seem to find a way to get this value, so it is displayed within my legend on the layout.
The script I use to create a memory buffer is:
# Creating the distance buffers
layer = iface.activeLayer()
feats = [ feat for feat in layer.getFeatures() ]
epsg = layer.crs().postgisSrid()
uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer&field=x:real& field=y:real&field=point_id:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
'MMI 1.0 Buffer',
'memory')
prov = mem_layer.dataProvider()
#Adding distance to the buffer
for i, feat in enumerate(feats):
point = feat.geometry().asPoint()
new_feat = QgsFeature()
new_feat.setAttributes([i, point[0], point[1], feat.id()])
tmp_feat = feat.geometry().buffer(DistInt[1.0], -1) #printing a MMI 1.O Intensity Buffer for a distancethat I have calculated previously and is stored within a dictionary (DistInt)
new_feat.setGeometry(tmp_feat)
prov.addFeatures([new_feat])
QgsProject.instance().addMapLayer(mem_layer)
My Attribute table for the MM1 1.0 Buffer, with the value of LAC_Population_sum:
What my legend currently shows:
My code that creates the LAC_Population_Sum field:
#Using zonal statistics to work out affected population sum column
root = QgsProject.instance().layerTreeRoot()
for layer in root.children():
if layer.name().startswith('LAC'):
prefix = layer.name()
params = {'INPUT_RASTER': layer.name(), 'RASTER_BAND': 1, 'INPUT_VECTOR': 'MMI 1.0 Buffer', 'COLUMN_PREFIX': prefix+'_', 'STATS': 1}
processing.run("qgis:zonalstatistics", params)
You can create a categorized style for your layer so you have the name of the layer and the value of the polygon like this :
You can do it in PyQgis with this code you just need to change the color and opacity in the first 2 lines :
rangeColor = QColor('#ffee00')
opacity = 1
targetField = 'LAC_Population_sum'
attrs = mem_layer.getFeature(0).attributes()
value = attrs[mem_layer.fields().indexFromName(targetField)]
rangeList = []
symbol1 = QgsSymbol.defaultSymbol(mem_layer.geometryType())
symbol1.setColor(rangeColor)
symbol1.setOpacity(opacity)
range1 = QgsRendererCategory(value, symbol1, str(value))
rangeList.append(range1)
groupRenderer = QgsCategorizedSymbolRenderer('', rangeList)
groupRenderer.setClassAttribute(targetField)
mem_layer.setRenderer(groupRenderer)
You can also change the name of the layer to obtain something like this :
You can do this with this code :
targetField = 'LAC_Population_sum'
attrs = mem_layer.getFeature(0).attributes()
value = attrs[mem_layer.fields().indexFromName(targetField)]
layer_name = mem_layer.sourceName() + " : " + str(value)
mem_layer.setName(layer_name)
Correct answer by JULESG on May 12, 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