I have a number of layers each with a prefix like A_ B_ and so forth.
I'd like to:
- Add a column (text) called DENSITY to each shp
- Add the prefix letter from the layer name to each row in the DENSITY column for each shp file.
Can it be done?
I'm using QGIS 2.8.1
Update:
I think there was a typo here with a missing +
print "- layer: " child.layerName() + " ID: " + child.layerId()
and I ran this:
# Iterate through the Legend to find all groups and layers
root = QgsProject.instance().layerTreeRoot()
for child in root.children():
if isinstance(child, QgsLayerTreeGroup):
print "- group: " + child.name()
# If we find a group, save the prefix name
prefix = group.name().split('_')[0]
elif isinstance(child, QgsLayerTreeLayer):
print "- layer: " + child.layerName() + " ID: " + child.layerId()
layer = child.layer()
# Add code to add a field named density to the layer.
#######
# Add code to iterate through each feature in the layer and populate the field with prefix value
#######
but got these errors:
line 4, in root = QgsProject.instance().layerTreeRoot() AttributeError: 'QgsProject' object has no attribute 'layerTreeRoot'