简体   繁体   English

如何使用R计算点与参考点之间的距离?

[英]How to Calculate Distance between points from a reference point using R?

I have this data frame individual_dets which has some lat and long values in it.我有这个数据框individual_dets ,其中有一些经纬度值。 This is the dataframe这是数据框

individual_dets = structure(list(location = c("ARB-04", "BIRCHY HEAD", "Boca1", 
"BON-AR-S2", "BON-AR-S2", "BON-W-S5"), month = structure(c(12L, 
10L, 10L, 8L, 11L, 2L), .Label = c("Jan", "Feb", "Mar", "Apr", 
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", 
"factor")), year = c(2018, 2018, 2018, 2018, 2018, 2018), detection_count = c(3L, 
256L, 2L, 4L, 2L, 2L), num_unique_tags = c(1L, 1L, 1L, 1L, 1L, 
1L), total_res_time_in_seconds = c(0, 1182040, 0, 2732221, 0, 
0), latitude = c(24.94808, 44.5713, 26.32559, -49.27732, -49.27732, 
-49.27985), longitude = c(-80.45412, -64.03512, -80.07108, 69.48038, 
69.48038, 69.47853)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -6L), groups = structure(list(
    location = c("ARB-04", "BIRCHY HEAD", "Boca1", "BON-AR-S2", 
    "BON-AR-S2", "BON-W-S5"), month = structure(c(12L, 10L, 10L, 
    8L, 11L, 2L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", 
    "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", 
    "factor")), .rows = list(1L, 2L, 3L, 4L, 5L, 6L)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

However, my dataframe has 4247 observations.但是,我的数据框有 4247 个观察值。

The lat and long values are for coordinates across the ocean in the NW Atlantic. lat 和 long 值用于横跨大西洋西北部的坐标。 I am trying to calculate the distance from Halifax of these lat and long values.我正在尝试计算这些经纬度值与哈利法克斯的距离。

I have been told to use this function snap points to line and then use lappy but I am looking at the structure and I am so lost.有人告诉我使用此功能snap points to line然后使用lappy但我正在查看结构并且我很迷茫。 Does anyone understand how to code using the function snap points to line and lappy to get approximate distance?有没有人了解如何使用函数snap points to line和 lappy 进行编码以获得近似距离?

You can use st_distance() from the sf package:您可以使用sf包中的st_distance()

library(sf)
library(dplyr)

individual_dets_sf <- st_as_sf(individual_dets, coords = c("longitude", "latitude"),
                               crs = 4326) %>% 
  ungroup()
halifax <- data.frame("longitude" = -63.5752, "latitude" = 44.6488)
halifax_sf <- st_as_sf(halifax, 
                       coords = c("longitude", "latitude"),
                       crs = 4326,
                       remove = FALSE) 

individual_dets_sf_2 <- individual_dets_sf %>% 
  mutate(distances = st_distance(., halifax_sf, by_element = TRUE))

individual_dets_sf_2$distances

Units: [m]
[1]  2664402.84    37510.33  2513963.08 16466708.65 16466708.65 16466623.17

If you want to visualize it (Halifax in blue):如果你想形象化它(蓝色的哈利法克斯):

library(tmap)

tmap_mode("view")

tm_shape(individual_dets_sf_2 %>% mutate(distances = units::drop_units(distances))) + tm_dots(col = "distances") +
  tm_shape(halifax_sf) + tm_dots(col = "blue")

在此处输入图片说明

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

相关问题 如何使用带有R的OSM计算从某个点到POI的距离 - How to calculate distance from a certain point to a POI using OSM with R 使用R,如何计算从一点到一条线的距离? - Using R, how to calculate the distance from one point to a line? 如何计算R中缓冲距离内点到多边形的距离 - How to calculate distance from points to multipolygons within a buffer distance in R 如何用R计算参考线(或点)与长/纬点数据框之间的道路网络距离? - How to calculate road network distances between a reference line(or point) and a dataframe of long/lat points with R? 如何计算R中沿线的两个点之间的地理距离? - How to calculate geographic distance between two points along a line in R? 如何在 R Studio 中计算点与固定位置之间的距离 - How to calculate the distance between points and a fixed location in R Studio 如何使用 leaflet、geosphere 和 R shiny 计算 2 点之间的距离 - How to calculate distance between 2 points with leaflet, geosphere, and R shiny 如何计算带有正负坐标的R中的点之间的距离 - How to calculate distance between points in R with negative and positive coordinate R-如何计算两组坐标点之间的距离? - R - How to calculate distance between two sets of coordinate points? 如何计算椭球体与R中的点之间的最小距离 - how to calculate minimum distance between an ellipsoid hull and a point in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM