简体   繁体   English

R 的坐标对 (UTM) 之间的距离,单位为公里

[英]Distance between pairs of coordinastes (UTM) in km with R

I have a dataframe with 2 pairs of UTM (32N) coordinates and I need to compute the differences in km between each of them, from origin to destination.我有一个带有 2 对 UTM (32N) 坐标的 dataframe,我需要计算它们之间从起点到终点的公里差。 I'm trying with sf library, using "by_element" but I obtain an error message "Error in st_distance(data, by_element = TRUE): .missing_y non è TRUE"?我正在尝试使用 sf 库,使用“by_element”,但我收到一条错误消息“st_distance(data, by_element = TRUE): .missing_y non è TRUE”错误消息? What's wrong, If I use it without the "by_element" option, it works and the distance matrix between all coordinates is created.出了什么问题,如果我在没有“by_element”选项的情况下使用它,它会起作用并创建所有坐标之间的距离矩阵。 but this is not what I need.但这不是我需要的。

library(sf)
df <- data.frame(id = c(1,2,3), x_origin = c(642683.2, 373775,383881 ), y_origin = c(5082920, 4997274,4994504), x_dest =c(642683.3, 1126050,942763.9 ), y_dest=c(5082920, 4374481,4534235  )) 

data <- st_as_sf(df, coords = c("x_origin", "y_origin"), crs="4326" )

distances <- st_distance(data, by_element = TRUE  )

You have provided x (origin) to st_distance() but no y (destination).您已向st_distance()提供x (起点)但未提供y (终点)。 And that CRS can't be right, sf doesn't recognise EPSG code if it's a string and 4326 would suggest coordinates are in WGS84 lat/long.而且 CRS 不可能是正确的,sf 无法识别 EPSG 代码(如果它是字符串)并且 4326 会建议坐标在 WGS84 lat/long 中。 Assuming 32632, WGS 84 / UTM zone 32N.假设 32632,WGS 84 / UTM zone 32N。

library(sf)
df <- data.frame(id = c(1,2,3), x_origin = c(642683.2, 373775,383881 ), y_origin = c(5082920, 4997274,4994504), x_dest =c(642683.3, 1126050,942763.9 ), y_dest=c(5082920, 4374481,4534235  )) 
origin <- st_as_sf(df, coords = c("x_origin", "y_origin"), crs=32632 )
dest <- st_as_sf(df, coords = c("x_dest", "y_dest"), crs=32632 )

(distances <- st_distance(origin, dest,  by_element = TRUE ))
#> Units: [m]
#>        1        2        3 
#>      0.1 976621.1 724015.0

Created on 2023-01-25 with reprex v2.0.2创建于 2023-01-25,使用reprex v2.0.2

First, sf can't know that you are calculating distances between origin and destination by the way you have input the data.首先, sf无法通过输入数据的方式知道您正在计算起点和终点之间的距离。 Second, the EPSG code for UTM Zone 32N is not 4326 .其次,UTM Zone 32N 的 EPSG 代码不是4326 Third, you should have used crs = st_crs(4326) instead of crs = "4326" .第三,您应该使用crs = st_crs(4326)而不是crs = "4326"

Use the following piece of code to create the objects needed to calculate the distances you are interested in使用以下代码创建计算您感兴趣的距离所需的对象

library(sf)
df <- data.frame(id = c(1, 2, 3),
                 x_origin = c(642683.2, 373775, 383881),
                 y_origin = c(5082920, 4997274, 4994504),
                 x_dest = c(642683.3, 1126050, 942763.9),
                 y_dest = c(5082920, 4374481, 4534235))

origin <- st_as_sf(df, coords = c("x_origin", "y_origin"),
                   crs = st_crs(32632))
dest <- st_as_sf(df, coords = c("x_dest", "y_dest"),
                 crs = st_crs(32632))

Note the different EPSG code for the CRS.请注意 CRS 的不同 EPSG 代码。

Next, we calculate the distances (the default of this projection is in meters)接下来,我们计算距离(此投影的默认单位为米)

distances <- st_distance(origin, dest, by_element = TRUE)

If you use by_element = FALSE , you get all the pairwise distances.如果您使用by_element = FALSE ,您将获得所有成对距离。

Lastly, we can use the package units to convert the distances to km (or we can simply divide them by 1000).最后,我们可以使用 package units将距离转换为km (或者我们可以简单地将它们除以 1000)。

units::set_units(distances, "km")
> Units: [km]
> [1]   0.0001 976.6211 724.0150

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM