3

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.

Name;ID;X;Y;X_2;Y_2;Distance

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".

6
  • 2
    Which software are you using?
    – fatih_dur
    Dec 4, 2018 at 7:29
  • The software is QGIS Dec 4, 2018 at 7:42
  • This topic Calculate distance between two points with QGIS 2.14.0 can also help you
    – Taras
    Dec 4, 2018 at 7:55
  • Yes, thanks. Ive seen it. I cant install "Connect Points". I got QGIS 3.0.3. I cant find the Plug-In in the menu plus I cant install the download. As I understood the post (i read before asking my question) its not matching my problem perfectly. Im modifying a few 100k features. And Im looking for a quick and stable solution. So maybe a short python script could help me? Dec 4, 2018 at 8:00
  • Thank you for posting another helpful topic! But I already read this topic (I read a lot of topics before posting here). Its not fixing my problem :( Dec 4, 2018 at 8:04

1 Answer 1

2

In the Field Calculator, please try:

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

The function distance():

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


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.

Measure_length_tool


References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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