1

Using PyQGIS I'm trying to find the chainage point at the start/end of a line based on a chainage points dataset.

I can't figure out how to write the intersect query that buffers the start_point and captures the intersecting chainage points and for now just prints the value.

roadlist=[]
layer = iface.activeLayer()
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():
    if feature['RoadMntnc'] not in roadlist:
        roadlist.append(feature['RoadMntnc'])

for road in roadlist:
    print (road)
    layer.selectByExpression('"RoadMntnc"= \'{0}\''.format(road), QgsVectorLayer.SetSelection)
    selection=layer.selectedFeatures()
    for feature in selection:
            chainage_layer.selectByExpression('"RoadMntnc"= \'{0}\''.format(road), QgsVectorLayer.SetSelection)
            chainage_selection=layer.selectedFeatures()
            if start_point(buffer(feature.geometry,5,10)).intersects(chainage_selection.geometry()): #based on https://gis.stackexchange.com/questions/256569/select-features-from-another-layer-based-on-a-selection-in-pyqgis/256646
                print (feature2['distance'])
        #aggregate(layer:='DSC_Roads_10m_Chainage.tab', aggregate:='max', expression:="distance" * (attribute(@parent, 'RoadMntnc') = attribute($currentfeature, 'RoadMntnc')),filter:= within(start_point(geometry(@parent)),buffer($geometry,1,10))) # process in model builder -field calculator.

The query needs to run only on the points with the same road name as the line being evaluated.

I get start_point/buffer not defined error.

===== Here's the updated code

roadlist=[]
layer = iface.activeLayer()
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():
    if feature['RoadMntnc'] not in roadlist:
        roadlist.append(feature['RoadMntnc'])

for road in roadlist:
    print (road)
    layer.selectByExpression('"RoadMntnc"= \'{0}\''.format(road), QgsVectorLayer.SetSelection)
    selection=layer.selectedFeatures()
    for feature in selection:
        geom = feature.geometry().asPolyline()
        pnt = QgsGeometry.fromPointXY(QgsPointXY(geom[0]))
        f.setGeometry(pnt)
        fbuff=f.geometry().buffer(5,4)
        chainage_layer.selectByExpression('"RoadMntnc"= \'{0}\''.format(road), QgsVectorLayer.SetSelection)
        chainage_selection=chainage_layer.selectedFeatures()
        if fbuff.intersects(chainage_selection.geometry()):
            print (chainage_selection['distance'])

I get ...

TypeError: QgsGeometry.intersects(): arguments did not match any overloaded call: overload 1: argument 1 has unexpected type 'QgsFeature' overload 2: argument 1 has unexpected type 'QgsFeature'

Ideally it's best to use an envelope that's the same as the buffer and only select the points within it that are part of the selection_chainage selection to ensure it's the same road. Maybe using something like areaOfInterest = QgsRectangle(fbuff) request = QgsFeatureRequest().setFilterRect(areaOfInterest).setFlags(QgsFeatureRequest.ExactIntersect)

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.