繁体   English   中英

R-如何计算两组坐标点之间的距离?

[英]R - How to calculate distance between two sets of coordinate points?

我有一个非常类似于此SO帖子的问题:

2个经纬度点列表(坐标)之间的地理/地理空间距离

这是一组经过编辑的示例坐标,用于说明我的情况:

require(tidyverse)

list1 <- data.frame(longitude = c(72, 74, 76, 78, 79, 82), 
                    latitude = c(20.5, 19, 19.5, 20, 22, 21),
                    area = "A")
list2 <- data.frame(longitude = c(71, 73, 75, 77, 79, 78.5, 72), 
                    latitude = c(21.5, 22, 20.5, 23, 23.5, 24, 24), 
                    area = "B")

df <- bind_rows(list1, list2)

ggplot(data = df) +
    geom_point(aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list1, aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list1[c(2,6),], aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list1[c(1,4),], aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list2[c(1,7),], aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list2[c(7,6),], aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list2[c(6,5),], aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list2[c(5,3),], aes(x = longitude, y = latitude, color = area)) +
    geom_line(data = list2[c(3,1),], aes(x = longitude, y = latitude, color = area))

ggplot2输出

因此,我需要计算两个坐标点列表之间的最小距离。 我已经能够完成这项工作,但是我注意到我需要更高效的东西-数据太大。

我考虑过的一种可能性是,在这些区域形成不重叠的多边形,并计算从一组点到相邻多边形的距离。 有没有办法形成这些多边形? 凸包不是选项,因为这些区域参差不齐。

另一种选择是在区域之间形成一条线。

编辑:我在图中添加了一些线条以说明多边形。

您可以计算出我改变数据集的欧几里德距离。 我删除最后一列。

x1 <- data.frame(longitude = c(72, 74, 76, 78, 79, 82), 
                    latitude = c(20.5, 19, 19.5, 20, 22, 21))
x2 <- data.frame(longitude = c(71, 73, 75, 77, 79, 78.5, 72), 
                    latitude = c(21.5, 22, 20.5, 23, 23.5, 24, 24))

euc.dist <- function(x1, x2) sqrt(sum((x1 - x2) ^ 2))


dist <- NULL
for(i in 1:nrow(x1)) dist[i] <- euc.dist(x1[i,],x2[i,])
dist

也许这就是您想要的?

#load libraries
library(dplyr)
library(sf)

#create row_id's, and make it a simple (spatial) feature
list1.sf <- list1 %>% 
  mutate( id = row_number() ) %>% 
  st_as_sf( coords = c("longitude", "latitude"), crs = 4326 )
list2.sf <- list2 %>% 
  mutate( id = row_number() ) %>% 
  st_as_sf( coords = c("longitude", "latitude"), crs = 4326 )

#find nearest points in list2 for each id in list1, and as a bonus, calculate the distance to this point
list1.sf %>% 
  dplyr::group_by( id ) %>%
  dplyr::mutate( np = sf::st_nearest_feature( geometry, list2.sf ),
                 dist_np = as.numeric( sf::st_distance( geometry, list2.sf[np,] ) ) )


# Simple feature collection with 6 features and 4 fields
# geometry type:  POINT
# dimension:      XY
# bbox:           xmin: 72 ymin: 19 xmax: 82 ymax: 22
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
# # A tibble: 6 x 5
# # Groups:   id [6]
# area     id    geometry    np dist_np
# * <fct> <int> <POINT [°]> <int>   <dbl>
# 1 A         1   (72 20.5)     1 151880.
# 2 A         2     (74 19)     3 196361.
# 3 A         3   (76 19.5)     3 152335.
# 4 A         4     (78 20)     3 318287.
# 5 A         5     (79 22)     5 166111.
# 6 A         6     (82 21)     5 415019.

暂无
暂无

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

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