I am curious to know on how to implement NNJoin into the python code. I have created a point shapefile using shapely. I have another shapefile which is a centerline and is used as a reference line. Now I want to find the shortest distance from each point to the centerline. The way which I have done so far is converted the centerline shapefile into points and used NNJoin to find the shortest distance and the nearest coordinates of the point on the centerline. I want to know as to how I can implement the NNJoin using python.

Is there a way to import NNJoin as a library and use it?

If not are there any alternatives to it which I can use?

  • NNJoin is a QGIS Python plugin. If you download the plugin, you have the Python code. Commented Apr 6, 2016 at 23:20

1 Answer 1


NNjoin joins two vector layers based on nearest neighbour relationships and it use GEOS as Shapely.

In Python it is quicker to use a Spatial Index as Rtree (Nearest Neighbours). There are many examples of the use of this module with Shapely and Fiona on the Web and GIS SE.

A solution with Shapely (without a Spatial Index) is given in Nearest neighbor between point layer and line layer?

And a comparison of all the Python solutions is proposed in Benchmarking Nearest Neighbor Searches in Python

  • Thanks for the help gene, I was able to get the nearest neighbor based on your recommendation. I also tried and was able to find the nearest point using interpolation. This also gave me the coordinates of the nearest neighbor on the centerline.
    – webapp
    Commented Mar 14, 2016 at 0:30

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.