Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

I'd really appreciate an answer for this.


share|improve this question
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. – Giovanni Manghi Oct 16 '11 at 16:43

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.

share|improve this answer
this should be marked as the answer, subject to minor edit of "lines" v "points" – Willy May 10 '12 at 1:11
"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 "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. – Håvard Tveite Oct 28 '14 at 0:01

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). A light-weight alternative (for small datasets) is the NNJoin plugin.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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