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

In Arcgis IProximityOperator.QueryNearestPoint Method was available to get the nearest point. Similarly Is there any function available in pyQgis?

Thanks in Advance.

share|improve this question

There is no ready-made solution, you need to program but it is a simple problem of analytical geometry (Nearest neighbor) and the solution was given by Paul Bourke in 1998 (Points, lines, and planes).

Several versions of his algorithm have been proposed in various languages ​​including Python:

The script with shapely can be easily converted to PyQGIS

1) The solution of IProximityOperator.QueryNearestPoint Method) with ArcGIS (figure from IProximityOperator.QueryNearestPoint Method)

enter image description here

2) one solution with PyQGIS adapting the script of scw to PyQGIS;

enter image description here

share|improve this answer
Thanks for your reply. I checked that reference links. But all these were seems to applicable for a line with two vertex. I am having a polyline with more vertex. If I create a loop(in programming) for each segment, then it consumes more execution time. Is there anyother option available? – Dharmarajan Dec 3 '13 at 8:59

You can use Spatialite [1] and its ClosestPoint( geom1 Geometry , geom2 Geometry ) : Point. As the docs [2] say:

Returns the Point on geom1 that is closest to geom2.

You can try it in QGIS DB Manager connected to any Spatialite database:

SELECT AsText(ClosestPoint(GeomFromText('LINESTRING(0 0, 0 4)') , GeomFromText('POINT(2 2)') ))

which returns:

POINT(0 2)

Or use it in QGIS Python console:

from pyspatialite import dbapi2
conn = dbapi2.connect('path_to_your_database/db.sqlite')
cur = conn.cursor()
qry = "SELECT AsText(ClosestPoint(GeomFromText('LINESTRING(0 0, 0 4)'), GeomFromText('POINT(2 2)') ))"

Good luck!



share|improve this answer

By using shapely, I accomplish this []

code snippet:

dist = lineStr.project(refPt)

np = lineStr.interpolate(dist) # using shapely
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.