3

I have a number of line layers on which I need to perform the same task: add 4 columns with the start and end points of each line.

The expressions that I use to do this are:

$xat(0)
$xat(-1)
$yat(0)
$yat(-1)

However, I must create a new column and update it for every layer, but I am sure that it can be automated. Any ideas on how to perform the creation of 4 new columns on every layer in the layers panel in QGIS?

  • 1
    This can be done using pyqgis. To get answers to your question you will need to provide a code attempt for doing this task. Please edit your question and include your code attempt and let us know where you get stuck and you should receive feedback. – artwork21 Jun 24 '16 at 13:49
  • You could also achieve this by creating a model from the Processing Toolbox, this is useful if you don't know Python. Here's a couple of links which you might find helpful: Automating Complex Workflows using Processing Modeler, QGIS Quick Tip - Building a Processing Model. – Joseph Jun 24 '16 at 14:25
5

As mentioned in the comment, you could create a model from the Processing Toolbox but if you want to create a script, you could use the following which creates the start_lat field and updates it for each layer (comments are included which hopefully will help):

# Import required module to create field
from PyQt4.QtCore import QVariant

# For each layer in panel
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    # Add a real field (which uses decimal)
    layer.dataProvider().addAttributes( [ QgsField("start_lat", QVariant.Double) ] )
    # Update attribute table with new field
    layer.updateFields()
    # Begin editing of layer and finish loop with saving edits
    with edit(layer):
        # Let `idx` = field with name "start_lat"
        idx = layer.fieldNameIndex( "start_lat" )
        # Define expression
        e = QgsExpression( """ $x_at(0) """ )
        e.prepare( layer.pendingFields() )
        # For each feature in layer
        for f in layer.getFeatures():
            # Apply expression
            f[idx] = e.evaluate(f)
            # Update attribute table
            layer.updateFeature(f)

You could then repeat the above by adapting it to create the other 3 fields or extend the loops and create all 4 fields at once.

  • the code runs without errors but creates an attribute field full of NULL values. However, manually entering same expression x_at(0) in the field calculator produces an attribute field filled with the right values.... – the_darkside Jun 27 '16 at 9:56
  • @the_darkside - Are you running it from the Python Console or from a script? – Joseph Jun 27 '16 at 10:01
  • I am running it from the Python Console in QGIS – the_darkside Jun 27 '16 at 10:12
  • Does this happen with other line layers also? I just tested this again (directly copying/pasting the code) and it works. I used QGIS 2.14.3-Essen. – Joseph Jun 27 '16 at 10:21
  • I am running the same version of QGIS, and the same thing happened with a different line layer... I am just getting blank columns – the_darkside Jun 27 '16 at 10:25

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.