4

I'm using Distance Matrix to find the closest point between one set (4 points) and another set (4 points), detailed below. All results look okay except one, where the returned point is not the closest. Any ideas what is it that I'm doing wrong?

Set 1 (input)

Set 1 (input)

Set 2 (target)

Set 2 (target)

The output layer shows the following. However, TargetID for 1 should be 3, and not 2. By the way, I did another distance matrix layer where the "Use only the nearest (k) target point" was 0, giving an output of all distances between all points, and indeed the distance between 1 (bilbao) and 3 (oviedo) was smaller than the distance between 1 (bilbao) and 2 (alcala de henares), which suggests that qgis knows the correct distance between points but returned a wrong one when I'm looking for the nearest point only.

Distance Matrix layer

Distance Matrix layer

1 Answer 1

2

It looks like a bug and/or improper projection.

Using distances in long-lat degrees (which doesn't mean much), the result is the proper one. Using true ground distances (in meters), the reported distances are the proper ones.

with src as (select 1 as id, st_makepoint(-2.92391,43.25722) as geom),
dest (id, geom) as (values (2, st_makePoint(-3.36708, 40.48227)),(3,st_makePoint(-5.84372, 43.36232)))
SELECT src.id, dest.id, 
       ST_Distance(src.geom,dest.geom) d_degrees, 
       ST_Distance(src.geom::geography,dest.geom::geography) d_meters
from src, dest;

 id | id |    d_degrees     |    d_meters
----+----+------------------+-----------------
  1 |  2 | 2.81011514913535 | 310402.55232612
  1 |  3 | 2.92170095083326 | 237160.04795382

Looking at the code, we see that two different functions are used to get the nearest neighbor (using the spatial index) and to compute the distance. My guess is that the function computing the nearest neighbor does not consider the unit nor cast to geography.

I would recommend projecting your data to a CRS that use meters, like UTM.

You might also want to fill a bug report.

3
  • Many thanks! I'm curious. If my results were in meters, doesn't it mean that my CRS uses meters too? The CRS I used was ETRS89 / UTM zone 30N.
    – GeoTrip
    Commented May 21, 2019 at 21:24
  • @GeoTrip No, the reported distances will always be in meters, even if the layer is not projected. The coordinates that are shown in the question are not in UTM but in lat-long. Make sure the layer is projected in UTM, that you are not doing projection on the fly by setting the project CRS to UTM
    – JGH
    Commented May 22, 2019 at 11:43
  • Okay, got it. I was able to fix this problem. Apparently, I was doing projection on the fly, so I saved my point files as shape files with the CRS set to UTM, and used the saved files. I'm not a 100% sure that that's what I had to do in the first place, but anyway, now distance matrix returns the correct shortest distance for all points. Thanks again @JGH. Your help is much appreciated!
    – GeoTrip
    Commented May 23, 2019 at 11:31

Your Answer

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

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