I have a table with three columns that I'm interested in: X, Y and rotate. These specify the position and angle of a symbol. I want to find duplicates across these columns, so anything with the same X,Y co-ords which also has the same angle set. I can find duplicates in geometry but not by finding the angle as well. I thought along the line of a For loop with nested If statements, but am unsure how to set that up - I'm a bit of a beginner with PyQGIS. This the code I'm using to find the duplicates which was found elsewhere on this forum:
layer = subscriber_feed_layer
allfeatures={}
index = QgsSpatialIndex()
for ft in layer.getFeatures():
allfeatures[ft.id()] = ft
index.insertFeature(ft)
selection = []
for feat in layer.getFeatures():
inGeom = feat.geometry()
idsList = index.intersects(inGeom.boundingBox())
if len(idsList) > 1:
for id in idsList:
selection.append(allfeatures[id])
layer.setSelectedFeatures([k.id() for k in selection])
-
find all the geometry duplicates, then look for symbol angle duplicates in that list?Ian Turton– Ian Turton2017年03月10日 12:13:02 +00:00Commented Mar 10, 2017 at 12:13
-
Please, add a snippet of your code in order to receiving potential answers. As it's currently written, it's hard to give you any help.mgri– mgri2017年03月10日 12:18:30 +00:00Commented Mar 10, 2017 at 12:18
-
This is the code I'm using to find the geometry duplicates:K. Osborne– K. Osborne2017年03月10日 12:41:37 +00:00Commented Mar 10, 2017 at 12:41
-
@K.Osborne are you dealing with point features? Is "rotate" a field of your layer?mgri– mgri2017年03月10日 14:39:37 +00:00Commented Mar 10, 2017 at 14:39
1 Answer 1
After finding the geometry duplicates, I've used a dictionary to find the same angle among them. Assuming that 'rotate' is the name of the field in your layer containing the angle of symbol, I've made a few changes to the code. Following should work for you -
layer = subscriber_feed_layer
allfeatures={}
index = QgsSpatialIndex()
for ft in layer.getFeatures():
allfeatures[ft.id()] = ft
index.insertFeature(ft)
selection = []
for feat in layer.getFeatures():
inGeom = feat.geometry()
idsList = index.intersects(inGeom.boundingBox())
if len(idsList) > 1:
d = {}
for id in idsList:
rot_ang = allfeatures[id]['rotate']
if rot_ang in d:
selection.append(allfeatures[id])
selection.append(allfeatures[ d[rot_ang] ])
else:
d[rot_ang] = id
layer.setSelectedFeatures([k.id() for k in selection])