2

In ArcGIS the IProximityOperator.QueryNearestPoint method is available to get the nearest point.

Is there any similar function available in PyQGIS?

1

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

  • 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
1

By using shapely, I accomplish this [http://toblerity.org/shapely/manual.html]

code snippet:

dist = lineStr.project(refPt)

np = lineStr.interpolate(dist) # using shapely
0

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)') ))"
cur.execute(qry)
cur.fetchone()[0]

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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