I am trying to create a new attribute field "FromNode"
in a layer that contains string with an integer.
EXAMPLE: name1
, name2
, name3
, name4
, ...
till the end of the table.
I got the solution by doing manually in QGIS Field Calculator.
concat('name', "id")
The manual procedure is tedious due to multiple layers.
Is there a way to automatically do this using PyQGIS or even SQL query in Virtual layer
approach?
Attempt in PyQGIS
Problem: It only creates empty 'String' field. I want to update the field with string "Name"
+ increment of "integer 1"
in each row. For instance, name1, name2, name3, ...
layer = iface.activeLayer()
myField = QgsField('FromNode', QVariant.String,'character', 200)
layer.dataProvider().addAttributes([myField])
layer.updateFields()
-
You can search for your specific tasks: gis.stackexchange.com/search?q=iterate+qgis+layers and docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/… After you attempt to use both in a single script, you could let us know if you're facing any trouble.Germán Carrillo– Germán Carrillo2021年10月06日 02:15:03 +00:00Commented Oct 6, 2021 at 2:15
-
I already search but most of solutions are based on field calculator such as gis.stackexchange.com/questions/387624/…user12729159– user127291592021年10月06日 03:00:51 +00:00Commented Oct 6, 2021 at 3:00
-
@GermánCarrillo. Edited the question and updated the script.user12729159– user127291592021年10月06日 03:10:24 +00:00Commented Oct 6, 2021 at 3:10
1 Answer 1
You can use the code below:
lyr = iface.activeLayer()
lyr.dataProvider().addAttributes([QgsField('FromNode', QVariant.String, '', 20)])
lyr.updateFields()
fld_idx = lyr.fields().lookupField('FromNode')
for count, f in enumerate(lyr.getFeatures()):
lyr.dataProvider().changeAttributeValues({f.id(): {fld_idx: f'name{count+1}'}})
Edit in response to further request in comment (start count at 6000):
lyr = iface.activeLayer()
lyr.dataProvider().addAttributes([QgsField('FromNode', QVariant.String, '', 20)])
lyr.updateFields()
fld_idx = lyr.fields().lookupField('FromNode')
count = 6000
for f in lyr.getFeatures():
lyr.dataProvider().changeAttributeValues({f.id(): {fld_idx: f'name{count}'}})
count += 1
Edit 2 in response to further subsequent request. After running the code above to add and populate the 'FromNode' field, run the code below:
lyr = iface.activeLayer()
fld_list = [QgsField('baseVoltag', QVariant.String, '', 20),
QgsField('headTermin', QVariant.String, '', 20),
QgsField('class', QVariant.String, '', 20)]
lyr.dataProvider().addAttributes(fld_list)
lyr.updateFields()
fld_values = ['415V', NULL, 'EnergyConsumer']
att_map = {}
for i, fld in enumerate(fld_list):
att_map[lyr.fields().lookupField(fld.name())] = fld_values[i]
for feat in lyr.getFeatures():
lyr.dataProvider().changeAttributeValues({feat.id(): att_map})
Resulting attribute table shown below:
-
Thank you. The above solution create new field 'FromNode' in all layers. How to restrict it to the active layer or by name of the layer?user12729159– user127291592021年10月06日 06:11:59 +00:00Commented Oct 6, 2021 at 6:11
-
Great. Is it possible to start the count in the field with 6000 and then incremented with 1. For example, 6001,6002,.....user12729159– user127291592021年10月06日 06:19:33 +00:00Commented Oct 6, 2021 at 6:19
-
1@user12729159, I have updated my answer. I don't really understand though. The following remark in your question: "The manual procedure is tedious due to multiple layers" gave the impression that you wanted to perform the operation on multiple layers.Ben W– Ben W2021年10月06日 06:25:51 +00:00Commented Oct 6, 2021 at 6:25
-
1
-
1@user12729159, see latest update!Ben W– Ben W2021年10月06日 12:04:10 +00:00Commented Oct 6, 2021 at 12:04
Explore related questions
See similar questions with these tags.