0

I am trying to create a loop to rename the fields of my attribute table. The fields should start at 1 and increment by one until the end of the table (e.g. 1,2,3,4,5,...,n)

my code on the python shell works fine, but not in QGIS, why?

Can anyone help me?

def loop (x):
    num = 1
    while num < x:
       print num
       num += 1

loop(188)
  • What happens in QGIS? – HeikkiVesanto Feb 3 '16 at 11:29
  • Nothing happens. Here my code """ Define new functions using @qgsfunction. feature and parent must always be the last args. Use args=-1 to pass a list of values as arguments """ from qgis.core import * from qgis.gui import * @qgsfunction(args='auto', group='Custom') def loop (x): num = 1 while num < x: print num num += 1 – geoDIY Feb 3 '16 at 11:52
  • What else are you expecting to happen besides the number being printed? Your code seems to do exactly what you told it to do. – Kersten Feb 3 '16 at 11:54
  • Indeed seems to run fine. imgur.com/9n69sbQ – HeikkiVesanto Feb 3 '16 at 12:11
  • 1
    Your question is a bit confusing, are you wanting to rename field/column names or simply change the attribute value of a given field attribute(s)? – artwork21 Feb 3 '16 at 13:44
1

The custom function can be used to run a specific function on each feature in a layer. But this will be feature specific. For more uses for this please see the following posts specifically for this: http://www.lutraconsulting.co.uk/blog/2015/06/05/qgis-function-editor/ http://nathanw.net/2012/11/10/user-defined-expression-functions-for-qgis/ http://www.qgistutorials.com/en/docs/custom_python_functions.html

If you want to iterate a growing number for each feature superficially using python, you can use a custom script that can be run from the processing toolbox. An example of this would be as follows. This will create an "Increment" attribute that will increase by 1, starting with 1.

#Definition of inputs and outputs
#==================================
##[Iterate script]=group
##input=vector
from qgis.core import *
from PyQt4.QtCore import *
#from processing.core.VectorWriter import VectorWriter
import processing
#print "Start"
layer = processing.getObject(input)
#layer = qgis.utils.iface.activeLayer()
provider = layer.dataProvider()
fields = provider.fields()
caps = layer.dataProvider().capabilities()
#Delete increment attribute if it exists
class_field_index = layer.fieldNameIndex('increment')
if caps & QgsVectorDataProvider.DeleteAttributes:
  res = layer.dataProvider().deleteAttributes( [ class_field_index ] )
layer.updateFields()
nElement = 0
if caps & QgsVectorDataProvider.AddAttributes:
  res = layer.dataProvider().addAttributes( [ QgsField("increment", \
    QVariant.Int) ] )
layer.updateFields()
class_field_index = layer.fieldNameIndex('increment')
feats = processing.features(layer)
nFeat = len(feats)
writers = []
increment_no = 0
for inFeat in feats:
    #Loop through all the features
    progress.setPercentage(int(100 * nElement / nFeat))
    increment_no += 1
    attrs = { class_field_index : increment_no }
    fid = inFeat.id()
    #Write the increment number to the feature currently being looped on
    layer.dataProvider().changeAttributeValues({ fid : attrs })
#print "Finish"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.