1

In my plugin I iterate through 8 different line layers and add only certain lines to a list. This list I add to a new vector layer because these lines are the only ones I care about. However, I need to know which lines in the new layer were the same as the old ones. when building the original list I make a dictionary and keep track of which lines are associated with certain other features.

Here is my code that attempts to go through and find the lines in the new layer that are the same as the original list but I get no matches

 self.final_lines.dataProvider().addFeatures(final_lines) # final lines is list of lines from different sources
 QgsProject.instance().addMapLayer(self.final_lines)
 self.final_lines_features = [feat for feat in self.final_lines.getFeatures()]
 for line in final_lines:
 for lin in self.final_lines_features:
 if line.geometry() == lin.geometry():
 print(f'geometries are equal for {line.id()} in list and {lin.id()} in final layer')
 if line == lin:
 print(f'features are equal for {line.id()} in list and {lin.id()} in final layer')
 else:
 print(f'features are NOT equal for {line.id()} in list and {lin.id()} in final layer')

This code prints nothing out. So I'm guessing I need to do something special when adding the lines to the final layer instead of just adding them as I do here, but I'm not sure what to do.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 3, 2022 at 16:44

1 Answer 1

2

Both of your equality tests will return False because you are simply comparing two different geometry objects and two different feature objects.

To test for identical geometries, you can use the equals() method and, if you copied the attributes from one feature to the other, you could check: line.attributes() == lin.attributes().

So your code could look like:

for line in final_lines:
 for lin in self.final_lines_features:
 if line.geometry().equals(lin.geometry()):
 print(f'geometries are equal for {line.id()} in list and {lin.id()} in final layer')
 if line.attributes() == lin.attributes():
 print(f'attributes are equal for {line.id()} in list and {lin.id()} in final layer')
 else:
 print(f'attributes are NOT equal for {line.id()} in list and {lin.id()} in final layer')

Or, slightly more succinctly:

for line in final_lines:
 for lin in self.final_lines_features:
 if line.geometry().equals(lin.geometry()) and line.attributes() == lin.attributes():
 print(f'geometries and attributes are equal for {line.id()} in list and {lin.id()} in final layer')
answered Jun 4, 2022 at 5:34
2
  • 1
    Ah that makes perfect sense. Thanks so much for clearing that up. Commented Jun 6, 2022 at 16:13
  • 1
    @tbob, no problem, glad it helped :-) Commented Jun 9, 2022 at 4:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.