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
  • You need to define start_point in your code since you're not using it as a QgsExpression. Try using if feature.geometry().buffer(5, 10).intersects(chainage_selection.geometry()):
    – Joseph
    Commented Aug 20, 2019 at 9:52
  • @Joseph based on your input and gis.stackexchange.com/questions/21552/spatial-query-from-pyqgis and gis.stackexchange.com/questions/208831/… - I have updated my code. I am still not getting a result. I just want to get the 'distance' value for the chainage point that's closest to the start point of the line where the line segment and chainage selection it's using has the same RdMntnc value.
    – GeorgeC
    Commented Aug 21, 2019 at 10:54

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.