首页 > 解决方案 > 在R中找到4个坐标的中点坐标

问题描述

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

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

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

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

先感谢您!

标签: rgeolocationcoordinateslatitude-longitudecentroid

解决方案


这是一种方法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

推荐阅读