TransWikia.com

Counting intersections between two line layers using pyqgis

Geographic Information Systems Asked on November 25, 2020

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.

One Answer

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 })

Correct answer by MrXsquared on November 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP