Starting from this question, I need a script to be insterted in the processing graphical modeler that can copy some attributes from the first digitised row in all the newly digitised lines of a polyline vector layer. As said I don't need to copy all the attributes, just 8 attributes (column names are: ALT_0, ALT_1, ALT_2, ALT_3, ALT_4, ALT_5, ALT_Ideale and ALT_propos)

The script has to produce an output in order to be inserted in the next algorithms of my model.

1 Answer 1


You can use the following script (save it in Processing Toolbox > Scripts > Tools > Create new script) in the graphical modeler to copy the first row of attributes and apply it to each feature in the input layer.

(Unfortunately I was unable to add a filter for your required fields so hopefully others can provide a solution which completely answers your question).

##Copy shapefile and first row attributes=name
##output=output vector

import qgis
from qgis.core import QgsFeatureRequest, QgsVectorFileWriter, QgsFeature, QgsGeometry, QgsVectorLayer, QgsMapLayerRegistry

layer = processing.getObject(input)
provider = layer.dataProvider()
fields = provider.fields()
writer = QgsVectorFileWriter(output, 'UTF-8', fields, provider.geometryType(), layer.crs(), "ESRI Shapefile")

feat = QgsFeature()
for field in layer.pendingFields():
    name = field.name()
    request = QgsFeatureRequest().setFilterFid(0)
    feat = layer.getFeatures(request).next()
    result = feat[name]

for feature in layer.getFeatures():
    feat.setGeometry(feature.geometry() )

del writer

Script in modeler

Tested this with a simple point layer with the buffer tool and it worked so hopefully it will for you :)

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.