I got two sets of points.

  1. The first set has a unique ID
  2. The second set references to this ID (one-to-many).

So I merged the two sets. Now every point in the joined table has the X and Y coordinates from the related first set.


Every X and Y is unique but X_2 and Y_2 repeats from a given set.

Now I would like to calculate the distance between (X,Y) and (X_2,Y_2) in the new column for each row.

I tried distance() but I'm way too new in Python to get it working.

I also tried some tools like Distance Matrix, but I got results for all distances (or to the number defined) but not only to the distances in one row. Or between Points sharing the same ID.


Executed in QGIS 2.18 and QGIS 3.0.3

In the Field Calculator/Expression dialog, please try

distance(make_point("X", "Y"), make_point("X_2", "Y_2"))

Function distance() returns the minimum distance (based on spatial ref) between two geometries in projected units.

Syntax: distance(geometry a, geometry b)

Another formula which can lead to the answer is

distance(geom_from_wkt('POINT(' + "X1" || ' ' || "Y1" + ')'), geom_from_wkt('POINT(' + "X2" || ' ' || "Y2" +')'))

P.S. To check whether the calculated distances are correct, you can apply the Measure length tool, see the image below.



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.