简体   繁体   中英

R SpatialPointsDataFrame to SpatialLinesDataFrame

I've imported some GPS points from my Sports watch into R:

library(plotKML)
route <-  readGPX("Move_Cycling.gpx")
str(route)

The data looks like this:

List of 5
 $ metadata : NULL
 $ bounds   : NULL
 $ waypoints: NULL
 $ tracks   :List of 1
  ..$ :List of 1
  .. ..$ Move:'data.frame': 677 obs. of  5 variables:
  .. .. ..$ lon       : num [1:677] -3.8 -3.8 -3.8 -3.8 -3.8 ...
  .. .. ..$ lat       : num [1:677] 52.1 52.1 52.1 52.1 52.1 ...
  .. .. ..$ ele       : chr [1:677] "152" "151" "153" "153" ...
  .. .. ..$ time      : chr [1:677] "2014-06-08T09:17:08.050Z" "2014-06-08T09:17:18.680Z" "2014-06-08T09:17:23.680Z" "2014-06-08T09:17:29.680Z" ...
  .. .. ..$ extensions: chr [1:677] "7627.7999992370605141521101800" "7427.6000003814697141511.7000000476837210180.8490009442642210" "9127.523.13003521531.7000000476837210181.799999952316280" "10027.534.96003841534.1999998092651410181.88300029210510" ...
 $ routes   : NULL

I've managed to transform to get the data points into a SpatialPointsDataFrame and to plot it over Google Earth with:

SPDF <- SpatialPointsDataFrame(coords=route$tracks[[1]]$Move[1:2], 
                               data=route$tracks[[1]]$Move[1:2],
                               proj4string = CRS("+init=epsg:4326"))

plotKML(SPDF)

What I really want is the cycling track, ie a SpatialLinesDataFrame , but I can't work out how to set the ID field correctly to match the SpatialLines object with the data.

This is how far I've got:

tmp <- Line(coords=route$tracks[[1]]$Move[1:2])
tmp2 <- Lines(list(tmp), ID=c("coord"))
tmp3 <- SpatialLines(list(tmp2), proj4string = CRS("+init=epsg:4326"))

# result should be something like, 
# but the ID of tmp3 and data don't match at the moment

SPDF <- SpatialLinesDataFrame(tmp3, data)

You can read the GPX file straight into a SpatialLinesDataFrame object with readOGR from the rgdal package. A GPX file can contain tracks, waypoints, etc and these are seen by OGR as layers in the file. So simply:

> track = readOGR("myfile.gpx","tracks")
> plot(track)

should work. You should see lines.

In your last line you've not said what your data is, but it needs to be a data frame with one row per track if you are trying to construct a SpatialLinesDataFrame from some SpatialLines and a data frame, and you can tell it not to bother matching the IDs because you don't actually have any real per-track data you are merging. So:

> SPDF = SpatialLinesDataFrame(tmp3, data.frame(who="me"),match=FALSE)
> plot(SPDF)

But if you use readOGR you don't need to go through all that. It will also read in a bit of per-track metadata from the GPX file.

Happy cycling!

As an update, here's my final solution

library(rgdal)
library(plotKML)
track <- readOGR("Move_Cycling.gpx","tracks")
plotKML(track, colour='red', width=2, labels="Cwm Rhaeadr Trail")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM