Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit argument of distance is not working properly. #1545

Closed
LuisLauM opened this issue Jun 14, 2024 · 1 comment
Closed

unit argument of distance is not working properly. #1545

LuisLauM opened this issue Jun 14, 2024 · 1 comment

Comments

@LuisLauM
Copy link

LuisLauM commented Jun 14, 2024

I have discovered that the argument unit of distance function works well only in certain specific situations.

My data:

df <- data.frame(Lat = c(42.1234, 41.0192, 40.9988, 51.0175, 50.6523, 50.9214), 
                 Lon = c(26.5462, 25.9967, 27.0001, 31.1542, 31.8924, 32.1025))

Example 1: Good behavior.

distance(x = as.matrix(df[,c("Lon", "Lat")]), lonlat = TRUE, unit = "m")
distance(x = as.matrix(df[,c("Lon", "Lat")]), lonlat = TRUE, unit = "km")

image

Example 2: Bad behavior.

distance(x = as.matrix(df), y = as.matrix(df), lonlat = TRUE, unit = "m")
distance(x = as.matrix(df), y = as.matrix(df), lonlat = TRUE, unit = "km")

image

I think it requires an urgent patch because it is an invisible issue that could not be noticed.

My platform:
OS: Windows 10
RStudio 2024.04.2 Build 764
R 4.4.0
terra 1.7-78

@rhijmans
Copy link
Member

Thank you. I now get:

distance(x = as.matrix(df), y = as.matrix(df), lonlat = TRUE, unit = "km")
#          [,1]      [,2]      [,3]      [,4]       [,5]       [,6]
#[1,]    0.0000  125.9805  122.6313 1006.4391 1018.61992 1052.68131
#[2,]  125.9805    0.0000  111.1910 1132.1578 1143.24154 1177.20483
#[3,]  122.6313  111.1910    0.0000 1078.0487 1081.50932 1114.86600
#[4,] 1006.4391 1132.1578 1078.0487    0.0000   88.89660  105.54234
#[5,] 1018.6199 1143.2415 1081.5093   88.8966    0.00000   34.48749
#[6,] 1052.6813 1177.2048 1114.8660  105.5423   34.48749    0.00000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants