【问题标题】:Find the coordinates of mid point of 4 coordinates in R在R中找到4个坐标的中点坐标
【发布时间】:2020-07-05 23:44:36
【问题描述】:

首先感谢大家对我之前的问题提供的所有帮助。

我试图在下面寻找一个可以找到我的数据集中心坐标的函数:

df <- read.table(sep=",", col.names=c("Start_Latitude","Start_Longitude","End_Latitude", "End_Longitude"),text="43.9567343,  -78.8571382, 43.9399364, -78.8497342")

Start_Latitude    Start_Longitude   End_Latitude    End_Longitude      
  43.9567343       -78.8571382       43.9399364      -78.8497342

我试过这段代码,但它只计算从起始坐标到结束坐标的距离,我想找到中点坐标而不仅仅是距离。我的意思是我需要找到中点的经纬度。

#dd the distance as column in the dataframe
df1$dist <- distm(x = df[, c('Start_Longitude', 'Start_Longitude')], 
                y = df[, c('End_Longitude', 'End_Latitude')],
                fun = distHaversine
)

我有这个数据集:

 Start_Latitude    Start_Longitude   End_Latitude    End_Longitude       dist
      43.9567343       -78.8571382       43.9399364      -78.8497342     13669708

有没有办法找到中点的纬度和经度?另外,我如何计算以公里为单位的距离。

提前谢谢你!

【问题讨论】:

    标签: r geolocation coordinates latitude-longitude centroid


    【解决方案1】:

    这是geosphere::midPoint 的一种方法:

    library(geosphere)
    data.frame(df,
               dist = distHaversine(df[, c('Start_Longitude', 'Start_Latitude')],
                                    df[, c('End_Longitude', 'End_Latitude')]) / 1000, 
               midPoint(p1 = df[, c('Start_Longitude', 'Start_Latitude')],
                        p2 = df[, c('End_Longitude', 'End_Latitude')]))
    #  Start_Latitude Start_Longitude End_Latitude End_Longitude    dist       lon      lat
    #1       43.95673       -78.85714     43.93994     -78.84973 1.96183 -78.85344 43.94834
    

    【讨论】:

    • 我想知道为什么您在此答案的第一篇文章中对latlong 的结果与我的答案大不相同。您的答案的第一次和第二次迭代之间发生了什么?
    • @LenGreski 我犯了复制 OP 代码的错误。它们的起始坐标重复。
    • SO 系统有效。这种差异让我们俩都仔细检查了我们的答案。赞成。
    • 确实如此。我还了解到地球的曲率在短距离内造成的差异有多么小。
    【解决方案2】:

    可以通过平均纬度/经度组合来计算中点。使用原始帖子中的数据,我们使用dplyr::mutate() 来计算中点。

    textFile <- "Start_Latitude    Start_Longitude   End_Latitude    End_Longitude      
      43.9567343       -78.8571382       43.9399364      -78.8497342
    41.947460   -87.654730   41.890620   -87.624480"
    
    data <- read.table(text=textFile,header=TRUE)
    
    library(dplyr)
    data %>% rowwise(.) %>% 
        mutate(midpoint_lat = sum(Start_Latitude,End_Latitude)/2,
               midpoint_long = sum(Start_Longitude,End_Longitude)/2) %>%
        as.data.frame(.) # print as df, not tibble
    

    ...和输出:

      Start_Latitude Start_Longitude End_Latitude End_Longitude id midpoint_lat
    1       43.95673       -78.85714     43.93994     -78.84973  1     43.94834
    2       41.94746       -87.65473     41.89062     -87.62448  2     41.91904
      midpoint_long
    1     -78.85344
    2     -87.63961
    

    以公里为单位计算距离

    正如geosphere 包中的distHaversine() 函数的帮助中所述,结果中的测量单位是根据为地球半径给出的参数返回的。默认值为r = 6378137 米,因此以米为单位返回测量单位。

    要换算成公里,有两种选择:

    1. 将结果除以 1,000(如本问题的其他答案所示),或
    2. 将参数r更改为6378.137

    【讨论】:

    • 这种方法的一个限制是它没有考虑地球的曲率。当然,如果不需要高水平的准确度,那就没问题了。
    • 其实在这个例子中,两种方法只相差4.2cm。
    • @IanCampbell - 明白了。在谷歌地图上,这些坐标相距约 650 米,或米,因为地点位于加拿大安大略省。
    • 我尝试了 Len Greski 的答案,但是当运行命令时: data %>% mutate(midpoint_lat = sum(Start_Latitude,End_Latitude)/2, midpoint_long = sum(Start_Longitude,End_Longitude)/2) 它给出不同的中点 lat 和 long ...我得到中点 lat : 8525.977 和中点 long -15297.57 .. 顺便说一句,我没有将数据框更改为表格,因为我不断收到错误消息
    • @Reta - 看起来mutate() 中的sum() 函数正在跨行聚合。我将更新我的解决方案以缓解这种情况。
    猜你喜欢
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    相关资源
    最近更新 更多