[Qgis-user] nearest point selection without reuse

Mike Toews mwtoews at gmail.com
Sat Mar 27 15:26:55 PDT 2010


On 26 March 2010 09:01, M.E.Dodd <m.e.dodd at open.ac.uk> wrote:
> Is it possible to find the nearest point in one table compared to points in another table BUT without using the same point in the second table more than once.
> I have one table of points and want to find the nearest point in another table, the trouble is that some points in the second table are used many times and others are not used at all even though they are only slightly further away.  There are different numbers of points in each table, each table has hundreds or thousands of points.  The multiple use of points in the second table like this might be biasing the analysis.

This is slightly complicated to determine. There are two tables A and
B, where the number of points in A is less than or equal to B. There
is exactly 1 solution, unless there are more than 1 set of points with
the same nearest-distance.

You will need to write some code up in R, PL/pgSQL, or any computer
environment with loops. Here is the pseudocode for the function:

Make an empty result set M for matches between A and B
While numrow(M) < numrow(A):
    point A0 := Select any point A not in M order by x-coord, y-coord
    point B1 := Find closest B point from point A0 not in M
    point A1 := Find closest A point from point B1 not in M
    Insert into M(A,B) values(A1, B1)
Return set M


-Mike



More information about the Qgis-user mailing list