如何使用 R Package Trip 过滤 GPS 跟踪数据?

How to filter GPS tracking data using R Package Trip?

目的是通过设置最大“企鹅”速度过滤在生物学上不切实际的 GPS 轨迹(使用 R 中的旅行包),删除点,然后编写一个删除所有错误点的 csv。

我一直在按照以前学生的脚本进行操作,但一直无法正常工作。将 Lat/Long 分配给对象(坐标)后问题开始。 IE。这行没有报错:

coordinates(GPSdataalltrips.obj)<-c("Lat", "Long")

但是,我在下一行看到一条警告消息,然后是错误消息(请参阅下面 MRE 中的警告 + 错误)

有什么问题吗?

最小可重现示例

设置:

library(trip)  
library(sp)    
library(rgdal)   

GPSdataalltrips.obj <- structure(list(Deployment = c(1, 1, 1, 1, 1, 1), Device = c(2, 
2, 2, 2, 2, 2), `Box no.` = c("C23", "C23", "C23", "C23", "C23", 
"C23"), `Bird no.` = c(10825325, 10825325, 10825325, 10825325, 
10825325, 10825325), `Breeding Stage` = c(1, 1, 1, 1, 1, 1), 
    Lat = c(-41.09482, -41.09491, -41.09484, -41.09491, -41.09496, 
    -41.09564), Long = c(174.78327, 174.78326, 174.78332, 174.78323, 
    174.78325, 174.78277), Time = c("18/09/2020,4:13:44 AM", 
    "18/09/2020,4:14:02 AM", "18/09/2020,4:15:01 AM", "18/09/2020,4:16:01 AM", 
    "18/09/2020,4:17:02 AM", "18/09/2020,4:18:02 AM"), Tripid = c(1, 
    1, 1, 1, 1, 1), `Trip no.` = c(1, 1, 1, 1, 1, 1), Complete = c(0, 
    0, 0, 0, 0, 0)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

警告信息:

coordinates(GPSdataalltrips.obj)<-c("Lat", "Long")  
proj4string(GPSdataalltrips.obj)<- CRS("+init=epsg:3857")
Warning messages:
1: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
  Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs
2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
  Discarded datum WGS_1984 in Proj4 definition

错误:

trip.obj<-trip(GPSdataalltrips.obj,c("Time", "Tripid")) 
Error in unclass(adjusted) - unclass(x[[tor[1]]]) : 
  non-numeric argument to binary operator
In addition: Warning messages:
1: In sp::proj4string(x) : CRS object has comment, which is lost in output
2: In force_internal(obj, TORnames) :
  ordering input records by trip ID, then time

首先我必须诚实,我只喜欢 tidyverse 的几个工具。那么让我以传统的方式进行。

allTrips <- structure(list(Deployment = c(1, 1, 1, 1, 1, 1), Device = c(2, 
2, 2, 2, 2, 2), `Box no.` = c("C23", "C23", "C23", "C23", "C23", 
"C23"), `Bird no.` = c(10825325, 10825325, 10825325, 10825325, 
10825325, 10825325), `Breeding Stage` = c(1, 1, 1, 1, 1, 1), 
    Lat = c(-41.09482, -41.09491, -41.09484, -41.09491, -41.09496, 
    -41.09564), Long = c(174.78327, 174.78326, 174.78332, 174.78323, 
    174.78325, 174.78277), Time = c("18/09/2020,4:13:44 AM", 
    "18/09/2020,4:14:02 AM", "18/09/2020,4:15:01 AM", "18/09/2020,4:16:01 AM", 
    "18/09/2020,4:17:02 AM", "18/09/2020,4:18:02 AM"), Tripid = c(1, 
    1, 1, 1, 1, 1), `Trip no.` = c(1, 1, 1, 1, 1, 1), Complete = c(0, 
    0, 0, 0, 0, 0)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

#到目前为止一切顺利。但是,我会将其用作简单的 data.frame.

allTripsDf = as.data.frame(allTrips)

#接下来我会正确定义时间戳

allTripsDf$Time = as.POSIXct(allTripsDf$Time, format = c("%d/%m/%Y,%I:%M:%S %p"))

#接下来,我将按照您的CRS定义,以不同的方式定义坐标和CRS。

sp::coordinates(allTripsDf) <- ~Long+Lat
sp::proj4string(allTripsDf) <- sp::CRS("+init=epsg:3857", doCheckCRSArgs = FALSE)

#最终创建对象“trip”。

tr <- trip(allTripsDf, c("Time", "Tripid"))
tr

#输出:

Object of class trip
  tripID ("Tripid") No.Records  startTime ("Time")    endTime ("Time") tripDuration
1                 1          6 2020-09-17 19:13:44 2020-09-17 19:18:02     4.3 mins

    data.columns data.class                  
1     Deployment    numeric                  
2         Device    numeric                  
3        Box no.  character                  
4       Bird no.    numeric                  
5 Breeding Stage    numeric                  
6           Time    POSIXct **trip DateTime**
7         Tripid    numeric **trip ID**      
8       Trip no.    numeric                  
9       Complete    numeric          

供您参考,我 运行 Microsoft R-Open V.3.5(64 位)上的此代码

library(readxl)
library(tidyverse)
library(trip)
library(sp)    
library(rgdal)  

setwd("D:/Courtney/Courtney Thesis Desktop/Foraging data/GPS data for all trips")

allTripsDf = read_xlsx("GPS data for all trips.xlsx")
allTripsDf = allTripsDf %>% rename(TripID=Tripid, Time=time, Lat=y,Long=x) %>% mutate(Deployment=as.factor(Deployment), Time = as.POSIXct(Time, format = c("%d/%m/%Y,%I:%M:%S %p"))) %>% arrange(TripID, Time)

sp::coordinates(allTripsDf) <- ~Long+Lat
sp::proj4string(allTripsDf) <- sp::CRS("+init=epsg:3857", doCheckCRSArgs = FALSE)

tr <- trip(allTripsDf, c("Time", "TripID"))
tr

filter<-speedfilter(tr,max.speed=7.2)
tracks.filtered<-subset(tr,filter==TRUE)

write.csv(tracks.filtered,file= "alltracksfiltered.csv")
tracks.filtered<-trip(tracks.filtered,c("Time","TripID"))
summary(tracks.filtered)

Object of class trip
   tripID ("TripID") No.Records  startTime ("Time")    endTime ("Time")   tripDuration tripDistance  meanSpeed   maxSpeed
1                  1       1800 2020-09-17 16:13:44 2020-09-19 21:51:46  2.234745 days   1.17660003 0.02345576 0.26928410
2                  2        940 2020-09-17 16:24:51 2020-09-19 06:30:51    1.5875 days   1.17241745 0.02549042 0.13454383
3                  3       1943 2020-09-17 16:37:01 2020-09-19 18:37:02  2.083345 days   1.60473172 0.02590134 0.33962331
4                  4        875 2020-09-20 15:44:01 2020-09-21 22:40:35  1.289282 days   0.94451710 0.02219599 0.25349928
5                  5        938 2020-09-18 14:27:02 2020-09-20 04:00:00   1.56456 days   0.99592446 0.02253615 0.56183868
6                  6        858 2020-09-21 15:58:00 2020-09-22 17:55:00   1.08125 days   0.79782599 0.02319607 0.35638181
7                  7       1908 2020-09-17 14:44:00 2020-09-19 21:57:41  2.301169 days   1.59492851 0.02289163 0.18317205
8                  8        897 2020-09-20 16:27:01 2020-09-21 21:15:32  1.200359 days   0.83761965 0.02135898 0.35455887
9                  9        259 2020-10-02 16:20:59 2020-10-03 20:19:11  1.165417 days   0.91318834 0.01612964 0.06638808
10                10        165 2020-09-29 15:25:14 2020-09-30 08:24:03 16.98028 hours   0.56592496 0.03436422 0.10410670
11                11         21 2020-10-06 13:50:34 2020-10-06 14:51:07 1.009167 hours   0.01794772 0.01710078 0.04587768
12                12         66 2020-10-06 15:46:55 2020-10-06 22:33:54 6.783056 hours   0.29428118 0.03939214 0.07306933
13                13         67 2020-09-28 15:51:02 2020-09-29 07:21:24 15.50611 hours   0.48236092 0.03167416 0.11855739
14                14         56 2020-09-30 15:06:02 2020-09-30 22:54:34 7.808889 hours   0.33093319 0.03686727 0.07233184
15                15        175 2020-09-29 16:00:15 2020-10-01 01:41:31  1.403657 days   1.04897563 0.01982784 0.05564086
16                16        360 2020-10-02 13:43:34 2020-10-04 17:50:18  2.171343 days   1.27621209 0.01760442 0.24219294
17                17        245 2020-10-07 15:12:00 2020-10-08 08:02:26 16.84056 hours   0.31151573 0.02172707 0.06500334
18                18        458 2020-10-09 12:09:03 2020-10-10 07:47:04 19.63361 hours   0.23487030 0.02115819 0.08490136
19                19        306 2020-10-11 12:12:01 2020-10-11 17:22:09 5.168889 hours   0.10528737 0.01986299 0.14923605
20                20        804 2020-10-07 14:27:01 2020-10-09 00:20:44  1.412303 days   0.99639926 0.01798060 0.09781349
21                21        177 2020-10-08 16:16:00 2020-10-09 07:42:00 15.43333 hours   0.20802642 0.02354949 0.09661910
22                22        153 2020-10-14 16:40:09 2020-10-15 11:17:02 18.61472 hours   0.48583514 0.04204171 0.09885848
23                23        247 2020-10-16 15:25:00 2020-10-17 01:59:29 10.57472 hours   0.52001738 0.03624685 0.24219288
24                24       4761 2020-10-16 16:26:24 2020-10-25 07:55:17  8.645058 days   4.13800059 0.01851673 0.26508868
25                25         63 2020-10-26 16:23:03 2020-10-27 07:15:11 14.86889 hours   0.08537073 0.02760054 0.07437073
26                26        128 2020-10-27 04:00:01 2020-10-29 07:26:51  2.143634 days   0.29954487 0.02714203 0.08512836

Total trip duration: 3153705 seconds (876 hours, 105 seconds)

Derived from Spatial data:

Object of class SpatialPointsDataFrame
Coordinates:
           min       max
Long 174.34849 175.03964
Lat  -41.18152 -40.11535
Is projected: TRUE 
proj4string : [+init=epsg:3857]
Number of points: 18670
Data attributes:
 Deployment     Device         Box.no.             Bird.no.        Breeding.Stage       Time                    
 1:10159    Min.   : 2.000   Length:18670       Min.   :10824948   Min.   :1.000   Min.   :2020-09-18 02:44:00  
 2: 1169    1st Qu.: 4.000   Class :character   1st Qu.:10825123   1st Qu.:1.000   1st Qu.:2020-09-19 19:02:00  
 3: 1990    Median : 5.000   Mode  :character   Median :10825325   Median :2.000   Median :2020-09-22 05:51:31  
 4: 5352    Mean   : 5.575                      Mean   :11250465   Mean   :1.703   Mean   :2020-10-01 23:05:27  
            3rd Qu.: 7.000                      3rd Qu.:12125033   3rd Qu.:2.000   3rd Qu.:2020-10-17 23:46:47  
            Max.   :10.000                      Max.   :12211624   Max.   :4.000   Max.   :2020-10-29 20:26:51  
     TripID         Trip.no.        Complete    
 Min.   : 1.00   Min.   :1.000   Min.   :0.000  
 1st Qu.: 3.00   1st Qu.:1.000   1st Qu.:0.000  
 Median : 8.00   Median :1.000   Median :0.000  
 Mean   :12.03   Mean   :1.092   Mean   :0.333  
 3rd Qu.:24.00   3rd Qu.:1.000   3rd Qu.:1.000  
 Max.   :26.00   Max.   :3.000   Max.   :1.000