29

I have two layers with points, call them A and B, each with different attributes.

For each point in A I want to join all the attributes from the nearest point in B.

Ideally this would also include a field measuring the distance between both points.

I know how to so this in ArcGIS: right click layer A>joins and relates>joins, then join data based on spatial location and choose to join attributes from nearest point.

I could not find similar functionality in QGIS. Any pointers?

26

Using "Distance Matrix" you can calculate the nearest point. Make sure that both layers are in the same CRS! Then, you can load the results and use them to join table A and B.

In "Distance matrix" chose: "Linear distance matrix" and "use only the nearest k target points: 1"

enter image description here

Load the CSV using "Add Vector Layer" and join the layers in Layer A's layer properties - join tab.

enter image description here

And the result:

enter image description here

  • If you want to have the distances between points within one layer using distance matrix you should change k=2, because of this known issue: hub.qgis.org/issues/4761 – Celia Feb 21 '12 at 17:31
  • 1
    @underdark It seems this solution does not work anymore. I am able to create the matrix.csv and join it to my first layer. In your example you added a second join where you attach the second layer to the first, based on the TargetID from the matrix. But the joined fields are not show to me in the dialog, so I can't select the TargetID. Of course i can first save_as the layer and then do the second join ... – Bernd V. May 22 '13 at 16:37
12

The NNJoin Plugin should do the job. For each feature of the input layer it adds all the attributes of the nearest feature in the join layer and also adds a distance attribute with the distance to this feature.

  • Works perfectly. Faster than the accepted answer. Note that the accepted answer if from 2011. – juminet Jun 9 '17 at 11:54
  • At least, today is much better than the accepted answer. Much much faster. – cag Nov 17 '17 at 18:27
4

If you can live with just joining a single attribute from your "B" layer, take a look at the "Hub Distance" tool from the mmqgis plugin.

If you need the rest of the "B" attributes, you should then be able to join the rest of your "B" attributes back to this layer.

You can also optionally generate lines from the A to B if desired. This is helpful for visualizing the distances and directions between associated features.

0

Another approach using refFunctions plugin.

(1) Sample Dataset

enter image description here

(2) Open an attribute table of the layer which has more features (points_b layer in this example) so that no pairs are missed out. Create new fields.

  • ID field of the other layer: geomnearest('points_a', 'id_a')
  • Distance field of the detected pairs: geomnearest('points_a', '$distance')

enter image description here

-5

Using QGIS 1.7 Vector >Data Management> Join Attributes by Location

enter image description here

  • 2
    I thought of this, too, but when I tested it out, it only worked for exact matches, such as point-polygon. It did not find the nearest point to another point. Tested in v1.7 – RyanDalton Jun 22 '11 at 21:48
  • 1
    I got zero matches. This does not work. – Fred Jun 22 '11 at 21:55

protected by whuber Mar 31 '14 at 14:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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