Skip to main content
added 84 characters in body
Source Link

I have a number of layers each with a prefix like A_ B_ and so forth.

I'd like to:

  1. Add a column (text) called DENSITY to each shp
  2. 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'

Image if split files:

I have a number of layers each with a prefix like A_ B_ and so forth.

I'd like to:

  1. Add a column (text) called DENSITY to each shp
  2. 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'

I have a number of layers each with a prefix like A_ B_ and so forth.

I'd like to:

  1. Add a column (text) called DENSITY to each shp
  2. 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'

Image if split files:

Formatted the question.
Link
Germán Carrillo
  • 36.6k
  • 5
  • 123
  • 180

Batch add column to layers and add layer prefix in PyQGIS

Edited grammar and formatting
Source Link
Joseph
  • 76k
  • 7
  • 172
  • 283

I have a number of layers each with a prefix like A_ B_ and so forth.

I'd like to:

  1. Add a column (text) called DENSITY to each shp
  2. 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 qgisQGIS 2.8.1

 

updateUpdate:

I think there was a typo here with a missing ++

print "- layer: " child.layerName() + " ID: " + child.layerId()

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()

# 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()

    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 godgot these errors:

line 4, in root = QgsProject.instance().layerTreeRoot() AttributeError: 'QgsProject' object has no attribute 'layerTreeRoot'

line 4, in root = QgsProject.instance().layerTreeRoot() AttributeError: 'QgsProject' object has no attribute 'layerTreeRoot'

I have a number of layers each with a prefix like A_ B_ and so forth.

I'd like to:

  1. Add a column (text) called DENSITY to each shp
  2. 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 god these errors:

line 4, in root = QgsProject.instance().layerTreeRoot() AttributeError: 'QgsProject' object has no attribute 'layerTreeRoot'

I have a number of layers each with a prefix like A_ B_ and so forth.

I'd like to:

  1. Add a column (text) called DENSITY to each shp
  2. 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'

Updated scriot and error
Source Link
Loading
I think the group sections is not usefull
Source Link
Loading
Removed signature as per help instructions
Source Link
PolyGeo
  • 65.2k
  • 29
  • 109
  • 340
Loading
Source Link
Loading