2

I've been getting into PyQGIS, and as a start I want to select the first instance of pair values of a point layer (start and end of a street), identified by the same integer, I got this, but it doesn't work:

("int_id" is the value that is the same for the start and end points)

cap = iface.activeLayer()
feat = cap.getFeatures()

frm = []

for f in feat:
    if f["int_id"] not in frm:
        frm.append(f.id())

cap.select(frm)

I need to create lines from these points (start and end), I haven't found a way to create them in PyQGIS, but I found point connector plugin that could at least create the streets that are a straight line, but first I need to separate the start points from the end points.

1

You can use collections.Counter to find which ID:s occur twice:

from collections import Counter
cap = iface.activeLayer()
feat = cap.getFeatures()
fieldname = 'int_id'

cnt = Counter([f[fieldname] for f in feat]) #Create dictionary of int_id and their counts
twice = [k for k,v in cnt.items() if v==2] #cnt.items() if python3/qgis3, cnt.iteritems() py2/qgis2

expr = '"{0}"={1}'.format(fieldname, twice[0])
cap.selectByExpression(expr)

enter image description here

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.