QGIS Vector – Making QGIS Attribute Table Fields Automatic

autofieldsqgisvector

I'm working in a way to make hydrologic projects using QGIS and an Excel worksheet that I have. To do this, I want to extract some information of the lines, included in a vector layer, which represents a pipe section.

The information that I need to extract are:

  • ID Number
  • Length
  • X, Y start and end coordinates

I've found a way to capture this field using the "$length" and another algorithm for the X and Y coordinates, but for that I need to open the Attributes table, put the expressions in each attribute column and click to update fields.

Is there a way to when I draw a line these fields are filled in automatically? That is, I draw / edit a line (start editing or end node ) and when I open the attribute table the length fields and coordinates X , Y are filled and updated.

Best Answer

Interesting question! I'm not aware of any other way of achieving what you want, but using PyQGIS.

Read the code below. It has some texts in it: 'lines', 'length', 'startX', 'startY', 'endX', 'endY'. You can adjust those names in the script for it to work on your data. The first one is you layer name, whereas the rest corresponds to field names. I assume your line layer has those fields (after all, you want values to be written there).

Once you have adjusted your layer name and the names of the fields you want to be automatically updated, copy and paste the script into the QGIS Python console.

If everything goes well, you should be able to see that field values are automatically updated in two scenarios: 1) When new features are added, and 2) When geometries are modified.

# Initialize required variables
myLayer = QgsMapLayerRegistry.instance().mapLayersByName( 'lines' )[0]
lengthField = myLayer.fieldNameIndex( 'length' )
startXField = myLayer.fieldNameIndex( 'startX' )
startYField = myLayer.fieldNameIndex( 'startY' )
endXField = myLayer.fieldNameIndex( 'endX' )
endYField = myLayer.fieldNameIndex( 'endY' )

# Slot, updates field values
def updateFeatureAttrs( fId, geom=None ):
    f = myLayer.getFeatures( QgsFeatureRequest( fId ) ).next()    
    if not geom:
        geom = f.geometry() 
    myLayer.changeAttributeValue( fId, lengthField, geom.length() )
    myLayer.changeAttributeValue( fId, startXField, geom.vertexAt( 0 )[0] )
    myLayer.changeAttributeValue( fId, startYField, geom.vertexAt( 0 )[1] )
    myLayer.changeAttributeValue( fId, endXField, geom.asPolyline()[-1][0] )
    myLayer.changeAttributeValue( fId, endYField, geom.asPolyline()[-1][1] )

# Update feature attributes when new features are added or geometry changes
myLayer.featureAdded.connect( updateFeatureAttrs )
myLayer.geometryChanged.connect( updateFeatureAttrs )

This is how it works:

Automatic updates of fields in QGIS

If you have any problem while running the script, add a comment below this answer.

It might be handy for you to have this functionality already available when you open your QGIS project. If that's the case, tell me, I could post instructions to do that.


EDIT:

For this functionality to be available every time you open your QGIS project (i.e., a .qgs file containing, among others, your line layer) you need to follow these steps:

  1. Go to QGIS->Project->Project Properties->Macros, check the Python macros option, and replace the whole code with this one (adjust the values indicating your layer and field names):

    from qgis.core import QgsMapLayerRegistry, QgsFeatureRequest
    def openProject():    
        # Initialize required variables
        myLayer = QgsMapLayerRegistry.instance().mapLayersByName( 'lines' )[0]
    
        # Update feature attributes when new features are added or geometry changes
        myLayer.featureAdded.connect( updateFeatureAttrs )
        myLayer.geometryChanged.connect( updateFeatureAttrs )
    
    # Slot, updates field values
    def updateFeatureAttrs( fId, geom=None ):
        myLayer = QgsMapLayerRegistry.instance().mapLayersByName( 'lines' )[0]
        lengthField = myLayer.fieldNameIndex( 'length' )
        startXField = myLayer.fieldNameIndex( 'startX' )
        startYField = myLayer.fieldNameIndex( 'startY' )
        endXField = myLayer.fieldNameIndex( 'endX' )
        endYField = myLayer.fieldNameIndex( 'endY' )
        f = myLayer.getFeatures( QgsFeatureRequest( fId ) ).next()    
        if not geom:
            geom = f.geometry() 
        myLayer.changeAttributeValue( fId, lengthField, geom.length() )
        myLayer.changeAttributeValue( fId, startXField, geom.vertexAt( 0 )[0] )
        myLayer.changeAttributeValue( fId, startYField, geom.vertexAt( 0 )[1] )
        myLayer.changeAttributeValue( fId, endXField, geom.asPolyline()[-1][0] )
        myLayer.changeAttributeValue( fId, endYField, geom.asPolyline()[-1][1] )
    
    def saveProject():
        pass
    
    def closeProject():
        pass
    
  2. Make sure you enable macros on your project, this way: Settings->Options->General->Enable macros: Always.

  3. Save your QGIS project.

Now, every time you open the .qgs file you've just saved, the attributes of your line layer will be automatically updated when you add a new feature or modify a geometry (i.e., no need to copy anything into the QGIS Python Console anymore).


2nd EDIT:

I've just published a plugin called AutoFields to help people to solve this kind of problems. I even made a video showing how to solve your ploblem, you can watch it at:

https://vimeo.com/germap/autofields-geometric-properties

AutoFields documentation: http://geotux.tuxfamily.org/index.php/en/geo-blogs/item/333-autofields-plugin-for-qgis