Search code examples
rgpsgpx

R Convert GPS to GPX with timestamp


This is what the sample data looks like

DEVICE_ID   LAT         LONGITUDE   DATE        TIME
150211559   12.920818   77.600197   02-01-17    0:00:00
150211559   12.914159   77.600037   02-01-17    0:01:39
150211559   12.919819   77.600189   02-01-17    0:00:10
150211559   12.919434   77.600174   02-01-17    0:00:20
150211559   12.918937   77.60009    02-01-17    0:00:29
150211559   12.914159   77.600037   02-01-17    0:01:49
150211559   12.918482   77.600136   02-01-17    0:00:39
150211559   12.917423   77.60009    02-01-17    0:00:49

I have multiple devices and millions of data points Used gpsvisualizer to convert the file so I have an idea what the output looks like

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <gpx version="1.1" creator="GPS Visualizer http://www.gpsvisualizer.com/" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"> <trk>   <name>test1</name> <trkseg>
    <trkpt lat="12.872757" lon="77.586197">
      <time>2017-02-01T00:00:01Z</time>
    </trkpt>
    <trkpt lat="12.872756" lon="77.586205">
      <time>2017-02-01T00:00:11Z</time>
    </trkpt>
    <trkpt lat="12.872757" lon="77.586212">
      <time>2017-02-01T00:00:21Z</time>
    </trkpt>    </trkseg>  </trk>  </gpx>

Was wondering if anyone could help to do the same in R using writeOGR (or any other package) with the time and date being included in the output gpx file. Thank you.


Solution

  • Do you really need an external package?

    dat <- read.table(header=TRUE, stringsAsFactors=FALSE, text='
    DEVICE_ID   LAT         LONGITUDE   DATE        TIME
    150211559   12.920818   77.600197   02-01-17    0:00:00
    150211559   12.914159   77.600037   02-01-17    0:01:39
    150211559   12.919819   77.600189   02-01-17    0:00:10
    150211559   12.919434   77.600174   02-01-17    0:00:20
    150211559   12.918937   77.60009    02-01-17    0:00:29
    150211559   12.914159   77.600037   02-01-17    0:01:49
    150211559   12.918482   77.600136   02-01-17    0:00:39
    150211559   12.917423   77.60009    02-01-17    0:00:49')
    
    pre <- '<?xml version="1.0" encoding="utf-8" standalone="yes"?> <gpx version="1.1" creator="GPS Visualizer http://www.gpsvisualizer.com/" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"> <trk>   <name>test1</name> <trkseg>'
    post <- '</trkseg>  </trk>  </gpx>'
    
    dat$dt <- format(as.POSIXct(paste(dat$DATE, dat$TIME), format="%m-%d-%y %H:%M:%S"),
                     format="%Y-%m-%dT%H:%M:%SZ", tz="UTC")
    
    cat(paste(
      c(pre,
        mapply(function(lat, lon, datetime) {
          sprintf('<trkpt lat="%f" lon="%f">
            <time>%s</time>
          </trkpt>', lat, lon, datetime)
        }, dat$LAT, dat$LONGITUDE, dat$dt),
        post, "\n"),
      collapse="\n"))
    # <?xml version="1.0" encoding="utf-8" standalone="yes"?> <gpx version="1.1" creator="GPS Visualizer http://www.gpsvisualizer.com/" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"> <trk>   <name>test1</name> <trkseg>
    # <trkpt lat="12.920818" lon="77.600197">
    #       <time>2017-02-01T08:00:00Z</time>
    #     </trkpt>
    # <trkpt lat="12.914159" lon="77.600037">
    #       <time>2017-02-01T08:01:39Z</time>
    #     </trkpt>
    # <trkpt lat="12.919819" lon="77.600189">
    #       <time>2017-02-01T08:00:10Z</time>
    #     </trkpt>
    # <trkpt lat="12.919434" lon="77.600174">
    #       <time>2017-02-01T08:00:20Z</time>
    #     </trkpt>
    # <trkpt lat="12.918937" lon="77.600090">
    #       <time>2017-02-01T08:00:29Z</time>
    #     </trkpt>
    # <trkpt lat="12.914159" lon="77.600037">
    #       <time>2017-02-01T08:01:49Z</time>
    #     </trkpt>
    # <trkpt lat="12.918482" lon="77.600136">
    #       <time>2017-02-01T08:00:39Z</time>
    #     </trkpt>
    # <trkpt lat="12.917423" lon="77.600090">
    #       <time>2017-02-01T08:00:49Z</time>
    #     </trkpt>
    # </trkseg>  </trk>  </gpx>
    

    (You should probably save to a file instead of just cat(...), I just wanted to show it.)