2

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

Is there any similar function available in PyQGIS?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Dharmarajan
  • 447
  • 5
  • 9

3 Answers3

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
Dharmarajan
  • 447
  • 5
  • 9
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

gene
  • 54,868
  • 3
  • 110
  • 187
  • 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 03 '13 at 08:59
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

Erpas
  • 11
  • 1