Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

2 Answers 2

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!

[1] https://www.gaia-gis.it/fossil/libspatialite/index

[2] http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.1.0.html

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.