Search code examples
rgpsr-sprgdal

How to filter GPS tracking data using R Package Trip?


Aim is to filter GPS tracks (Using the Trip Package in R) that are biologically unrealistic by setting a max "penguin" speed, remove points, then write a csv with all erroneous points removed.

I have been following script from a previous student and have been unable to get it to work. The issue starts after assigning Lat/Long to object (coordinates). I.e. this line doesn't error:

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

However, I see a warning message on the next line, and error messages thereafter (see warning + error in MRE below)

Any ideas on what's going wrong?

Minimal Reproducible Example

Setup:

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"))

The warning message:

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

And the error:

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

Solution

  • First of all I have to be honest and I like just few tools of tidyverse. So let me proceed in a traditional way.

    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"))
    

    #So far so good. However, I will use it as a simple data.frame.

    allTripsDf = as.data.frame(allTrips)
    

    #Next, I will define the timestamp properly

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

    #Next, I will define coordinates and CRS in a different way following your CRS' definition.

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

    #Finally create object "trip".

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

    #Output:

    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          
    

    For your information, I run this code on Microsoft R-Open V.3.5 (64-bit)