I am having an attribute table information for line features. Each Line feature has LINK ID's which are supposed to be unique but in some places are repeating. Also I have a field "NEW" whose Id's are completely unique.
Now I want to obtain "New" Id values of those features which are having same Link ID value, and based on the length of those features (if among them the feature having highest length will have its LINK ID unchanged) and the others will be changed to 0 (Link Id).
So my plan was to create a dictionary with Link ID as key, - dict['LinkId'] = [1 , [2]]
Here 1 represents a list of New Id's and 2 represents list of lengths of the line features. Here 1 and 2 item should be synchronous for e.g- { 714882134 : [0: 1823048.3948], [23 : 485879.84]}
so from that of 2 which ever is having the highest value I will obtain its corresponding new id.
Here is the code I have written but it is not exactly returning the thing I want.
vlayer.startEditing()
ids = []
for feature in vlayer.getFeatures():
id = feature['LINK_ID']
if id not in ids and id != 0:
ids.append(id)
else:
pass
vlayer.commitChanges()
dict = {}
vlayer.startEditing()
for feature in vlayer.getFeatures():
geom = feature.geometry()
length = round(geom.length(),8)
link_id = feature['LINK_ID']
cat = int(feature['NEW'])
for data in ids:
if data == link_id:
dict[link_id] = [(length),(cat)]
vlayer.commitChanges()
print("dict")
print(dict)
I am using QGIS version 3.16.3
link_id
will overwrite each other indict
. So maybe you can use adict = defaultdict(list)
and append the values (e.g.,dict[link_id].append((length), (cat))
when you find repeatinglink_id
. It can be a suggestion for the start.