R使用时间戳将GPS转换为GPX

时间:2018-06-27 16:15:32

标签: r gps gpx

这是示例数据的样子

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

我有多个设备和数百万个数据点 使用gpsvisualizer来转换文件,所以我知道输出是什么样的

<?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>

想知道是否有人可以使用writeOGR(或任何其他包)在R中执行相同的操作,并且时间和日期包含在输出gpx文件中。谢谢。

1 个答案:

答案 0 :(得分:1)

您真的需要外部包装吗?

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>

(您可能应该保存到文件中,而不仅仅是显示cat(...),我只是想显示它。)