Skip to main content
3 of 5
updated code to resolve some issues
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143

Improve performance of Aggregate process from FieldCalc by using PyQGIS 3

I have a aggregate process that takes about 9h to run collecting data from a point dataset to put into a line.

The process is

aggregate(layer:='Roads_10m_Chainage.tab', aggregate:='max', expression:="distance" * (attribute(@parent, 'RoadMntnc') = attribute($currentfeature, 'RoadMntnc')),filter:= within(start_point(geometry(@parent)),buffer($geometry,5,10)))

This ensures the value from the point data has the same RoadMntnc value as the line it's looking at the start point of. The buffer ensures it collects the closest value -this works as the chainage is 10m so there will always be a point within 5m.

I was hoping to speed up the process by using a PyQGIS selection of the road line and chainage point datasets having the same RoadMntnc value rather than it crunching through the whole dataset.

So far I have

from operator import itemgetter

def Update_Field(fieldName, ntext): # Function to simplify updating fields
    print (ntext)
    fieldIndex = layer.fields().indexFromName(fieldName)
    dpr.changeAttributeValues({feature.id(): {fieldIndex: ntext}})
    layer.commitChanges()

roadlist=[]
layer = iface.activeLayer()
dpr = layer.dataProvider()

chainage_layer=QgsVectorLayer(r'Z:\My Drive\Mangoesmapping\Spatial Projects\2019\DSC\126_Road_Updates\Working\Output\DSC_Roads_10m_Chainage.tab', "Chainage", "ogr")
#for feature in layer.getFeatures(): # Create list of unique roads to work through
    #if feature['RoadMntnc'] not in roadlist:
    #    roadlist.append(feature['RoadMntnc'])
roadlist=['Kingston_Rd'] #Manual road list for testing
for road in roadlist: # for each road get key attributes and sort
    print (road)
    layer.selectByExpression('"RoadMntnc"= \'{0}\''.format(road), QgsVectorLayer.SetSelection)
    selection=layer.selectedFeatures()
    l_selection=layer.selectedFeatures()
    i=1
    road_parts=[]
    chainage_layer.selectByExpression('"RoadMntnc"= \'{0}\''.format(road), QgsVectorLayer.SetSelection)
    chainage_selection=chainage_layer.selectedFeatures()
    for f in selection:
        for l in chainage_selection:
            if f.geometry().intersects(l.geometry()): #how do I check if a 1m buffer of the start point has a chainge point within it?
                print (l.id(),l['RoadMntnc'], l['distance'])
#if the above works then update record
                #ntext=str(item['Distance'])
                #print (ntext)
                #fieldName='Start_Chainage'
                #Update_Field(fieldName,ntext)
    

All values are printed in the last line - how to I change the if statement to just show the distance value from the chainage value within 1m of the start of the line?

How do I write the expression to utilize the two selections or modify the aggregate function to use the two selections or use an envelope to collect the required value?

GeorgeC
  • 8.3k
  • 7
  • 56
  • 143