2

I want to count intersections between two line layers using pyqgis. First layer connects two points (Memorylayer_VL / Connections), the second one represents streets (lines_layer / Streets). I need to figure out how many times Connectionlines intersects with a Street. Note that the streets can have curves. So I also need to count, if a line crosses the same street more than once!

Thats where I am struggling to find a good logic how I could implement this. Anyone has a brilliant idea how this could be done? Thats what I have so far... It only counts if a street has been crossed, but not how many times.

    for tmp_line_feat in Memorylayer_VL.getFeatures():
        for streets in lines_layer.getFeatures():
            counter = 0
            if tmp_line_feat.geometry().intersects(streets.geometry()):
                counter = counter + 1
                attr = {5:counter}
                Memorylayer_PR.changeAttributeValues({ tmp_line_feat.id() : attr })

Example:

enter image description here

I need to know for each connection line, how many times a street has been crossed.

2

Turned out I just needed to make some minor adjustments in the code and especially, just explode the streets before... Then this works just fine:

    for tmp_line_feat in Memorylayer_VL.getFeatures():
        counter = 0
        for streets in lines_layer.getFeatures():                
            if tmp_line_feat.geometry().intersects(streets.geometry()):
                counter = counter + 1
            attr = {5:counter}
            Memorylayer_PR.changeAttributeValues({ tmp_line_feat.id() : attr })

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.