【问题标题】:Calculating distance between two GPS locations in a data frame using distm () in R使用R中的distm()计算数据帧中两个GPS位置之间的距离
【发布时间】:2019-03-18 08:31:57
【问题描述】:

之前有人问过这个问题,但从未使用以下数据排列。下面是它的一个示例:

> head(datagps)
   Date & Time [Local]  Latitude Longitude
1: 2018-06-18 03:01:00 -2.434901  34.85359
2: 2018-06-18 03:06:00 -2.434598  34.85387
3: 2018-06-18 03:08:00 -2.434726  34.85382
4: 2018-06-18 03:12:00 -2.434816  34.85371
5: 2018-06-18 03:16:00 -2.434613  34.85372
6: 2018-06-18 03:20:00 -2.434511  34.85376

如您所见,我有一个Date & Time [Local] 列,其中平均每 4 分钟记录一次 GPS 位置。我想计算两个连续记录之间的距离(以米为单位)并将此度量存储在新列 Step 中。我一直在尝试对我的数据实施distm()

> datagps$Step<-distm(c(datagps$Longitude, datagps$Latitude), c(datagps$Longitude+1, datagps$Latitude+1), fun = distHaversine)
Error in .pointsToMatrix(x) : Wrong length for a vector, should be 2

虽然我非常不确定语法以及这是否是填充函数参数的正确方法。我对 R 很陌生,所以希望能得到一些帮助。

感谢任何输入!

【问题讨论】:

    标签: r geosphere


    【解决方案1】:

    我想你已经快到了。假设您要将上一个记录 (n) 和当前记录 (n+1) 之间的距离存储在 n+1,您可以使用:

    library(geosphere)
    date <- c("2018-06-18 03:01.00","2018-06-18 03:06.00","2018-06-18 03:08.00","2018-06-18 03:12.00","2018-06-18 03:16.00","2018-06-18 03:20.00")
    latitude <- c(-2.434901,-2.434598,-2.434726,-2.434816,-2.434613,-2.434511)  
    longitude <- c(34.85359,34.85387,34.85382,34.85371,34.85372,34.85376)
    datagps <- data.frame(date,lat,lon)
    
    datagps$length <- distm(x=datagps[,2:3], fun = distHaversine)[,1]
    

    第一个结果为0,其余为连续点之间的距离

    【讨论】:

      【解决方案2】:

      如果您查看函数的文档,您会看到:

      library(geosphere)
      ?distm
      

      x 点的经度/纬度。可以是两个数字的向量、2 列的矩阵(第一个是经度,第二个是纬度)或 SpatialPoints* 对象

      y 与 x 相同。如果缺失,y 与 x 相同

      这意味着您可以同时使用矩阵或向量。

      一种方法可能是:

      res <- distm(as.matrix(df1[,c("Longitude","Latitude")]), fun = distHaversine)
      
      res
      
      #         [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
      #[1,]  0.00000 45.90731 32.15371 16.36018 35.16947 47.35305
      #[2,] 45.90731  0.00000 15.29559 30.09289 16.76621 15.60347
      #[3,] 32.15371 15.29559  0.00000 15.81292 16.79079 24.84658
      #[4,] 16.36018 30.09289 15.81292  0.00000 22.62521 34.40483
      #[5,] 35.16947 16.76621 16.79079 22.62521  0.00000 12.19500
      #[6,] 47.35305 15.60347 24.84658 34.40483 12.19500  0.00000
      

      【讨论】:

        【解决方案3】:

        使用sf-package 的解决方案

        样本数据

        library(data.table)
        dt1 <- data.table::fread( 'DateTime, Latitude, Longitude
        2018-06-18 03:01:00, -2.434901,  34.85359
        2018-06-18 03:06:00, -2.434598,  34.85387
        2018-06-18 03:08:00, -2.434726,  34.85382
        2018-06-18 03:12:00, -2.434816,  34.85371
        2018-06-18 03:16:00, -2.434613,  34.85372
        2018-06-18 03:20:00, -2.434511,  34.85376')
        
        setDF(dt1)
        

        代码

        library(sf)
        #create spatial points object
        dt1.sf <- st_as_sf( x= dt1, 
                            coords = c("Longitude", "Latitude"),
                            crs = "+proj=longlat +datum=WGS84")
        #calculate distances
        st_distance(dt1.sf)
        

        输出

        # Units: [m]
        #          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
        # [1,]  0.00000 45.74224 32.07520 16.32379 34.97450 47.08749
        # [2,] 45.74224  0.00000 15.20702 29.96245 16.76520 15.56348
        # [3,] 32.07520 15.20702  0.00000 15.77068 16.72801 24.69270
        # [4,] 16.32379 29.96245 15.77068  0.00000 22.47452 34.18116
        # [5,] 34.97450 16.76520 16.72801 22.47452  0.00000 12.12446
        # [6,] 47.08749 15.56348 24.69270 34.18116 12.12446  0.00000
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-26
          • 2018-11-14
          • 1970-01-01
          • 1970-01-01
          • 2017-04-11
          • 1970-01-01
          • 2015-10-26
          • 2020-11-23
          相关资源
          最近更新 更多