I try to join attributes between two different line layers.

The first line layer (data source) is given. The second line layer is consisting of filtered lines from first layer, created by python code.

I want to have the same attributes in filtered lines as in basic layer. I was trying to use NNJoin plugin, but unfortunately it is giving me wrong data. The example below.

My question is: How to join the attributes properly and why this plugin is giving me the uncorrect results?

I am using QGIS 2.14.3 as well as Python.

Left side shows proper attribute. Right side shows bad matching of attributes with NNJoin


  • Is there a common id field/values between the two layers?
    – artwork21
    Sep 8, 2016 at 12:03
  • Unfortunately there is not. I create second layer basing only on geometry of the first one. Sep 8, 2016 at 12:09
  • So the two layers are slightly offset spatially? Are the features identical in number of features and length?
    – artwork21
    Sep 8, 2016 at 12:10
  • I think that they some of them are slightly different. But the difference is in range 10 x 10 ^(-09). I do not really know why this difference is occuring only in some of features. From the first glance they are totally the same, even if I get extremly close (you cannot see the difference) Sep 8, 2016 at 12:19
  • What was the code you used to create your second layer? If you just copied the geometry, you could consider copying the attributes also. That way, you would have common fields between the two layers which you can join.
    – Joseph
    Sep 8, 2016 at 12:25

2 Answers 2


One option would be to:

  1. Use Vector>Geometry Tools>Export/Add geometry columns on both layers
  2. Export both in-memory layer to a GIS layer (e.g. shapefile)
  3. Nest two pyqgis layer.getFeatures() for loops for both layers and look for similar length value using conditional if logic. If the value is similar write the field value that you want from source layer to destination layer

Using @Joseph hint and @artwork21 answer, I created another short python code to solve my problem.

I save the geometry of first layer together with interesting attributes in a dictionary: geometry is a key and attribute is a value. Next I look for the same geometry in second layer and save attribute loaded from dictionary.

But what is still considering me is: why the NNJoin plugin is choosing bad lines to copy the attributes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.