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)
LAC_Population_sum
field ? I don't see it.attrs = mem_layer.getFeature(0).attributes() value = attrs[mem_layer.fields().indexFromName('LAC_Population_sum')] print(value)
for feat in mem_layer.getFeatures(): print(feat["LAC_Population_sum"])
?