14

My problem is the following: i have a line shp (target layer) and a point shp (join layer). the second one (point layer) holds an attribute which i want to join with a line layer based on closest distance to the line. Points and lines are not intersecting

I can do this with ArcGIS and spatial join(match option: closest), but i don't know how can I achieve this in QGIS.

1
  • 3
    There is a tool to do Join by location but it doesn't have an option "closest". I don't know if there is any workaround. In general please evaluate to support the development of your missing, favorite options/tools, it is also this way that users can help this piece of software grow. Oct 16, 2011 at 16:43

4 Answers 4

8

I don't think there's a 'core' QGIS function for this. However, install the MMQGIS plugin, and use the 'hub distance' tool. Chose your points as the "Source Points" layer, and your lines as the "Destination Hubs Layer" and set "Output Shape Type" to "Line to Hub." This will give you a shapefile of lines (with length) that join the closest features. Conveniently, it also adds the ID of the closest feature to the attribute table.

Edit/Comment: This approach works well if there are fewer points than lines, or else a one-to-one line-to-point situation. Having more points than lines complicates things. 'Hub Distance' tool adds the ID of the line feature to the point attribute table as 'HubName' along with the distance between the point and the center of the line as 'HubDist'. If there are more points than lines, additional work is required to determine within each subset of points (those multiple records linked to each line hub) which of those is the closest to each line, and/or which is one with the desired set of attributes to join to the line.

2
  • this should be marked as the answer, subject to minor edit of "lines" v "points"
    – Willy
    May 10, 2012 at 1:11
  • 1
    "Hub distance" is not the right tool if "closest distance to the line" is the criterion. The mmqgis code reveals that hub distance uses an approximation of the "hub" geometry. The distance calculations (mmqgis_distance) takes two points as parameters. Here is the code that does the approximation of the hub geometry in mmqgis_library.py: "feature.geometry().boundingBox().center()". This means that the centre of the geometry's bounding box is used instead of the real geometry. So if "closest distance to line" is to be the criterion, GRASS v.distance or the NNJoin plugin are more suitable. Oct 28, 2014 at 0:01
5

The GRASS v.distance function can be used for this. In QGIS you can access the function through the GRASS plugin (the Processing version is currently not working). The NNJoin QGIS plugin may be a simpler alternative.

3

The NNJoin Plugin has never let me down and works well with large datasets.

Plugins > Install: NNJoin > NNJoin > choose input layer and join layer > Then click join.

Examine results from the new output layer with the attribute table. Warning: make sure both of your layers are using the same spatial projection otherwise your results will be blank.

3

In 2019 this may be possible with the QGIS 3 Zanzibar tool 'Join attributes by nearest'. This tool allows you to find the closest feature and join selected attributes. It is also possibly to do a one-to-many join, if you specify the max distance and the max nearest neighbors.

Your Answer

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

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