1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| library(maptools) kml2timetag <- function(kmlfile = "ttt.kml", start.time = "2015-02-04 21:39:36", end.time = "2015-02-05 23:40:50", csv.file = NULL)
{ require("maptools") Sys.setlocale("LC_TIME", "English") dat <- getKMLcoordinates(kmlfile = kmlfile, ignoreAltitude=FALSE) start.time <- as.POSIXlt(start.time) end.time <- as.POSIXlt(end.time) time.zone = 8 points.time <- seq(start.time, end.time, length.out = nrow(dat[[1]])) points.time2 <- format(points.time, "%Y-%m-%dT%H:%M:%SZ") dat <- cbind(dat[[1]], points.time2) colnames(dat) <- c("longitude", "latitude", "altitude", "time") if(!is.null(csv.file)){ write.csv(dat, csv.file) } return(as.data.frame(dat)) }
tagkml2gpx <- function(dat, output = "res.gpx"){ res <- c( "<?xml version="1.0" encoding="UTF-8" standalone="no" ?>", "<gpx>", " <trk>", " <name>Track written by tagkml2gpx</name>", " <trkseg>") for(i in 1:nrow(dat)){ res <- c(res, paste(" <trkpt lat="",dat$latitude[i], "\" lon=\"", dat$longitude[i], "\">", collapse = "", sep = ""), paste(" <ele>", dat$altitude[i], "</ele>", sep = ""), paste(" <time>", dat$time[i], "</time>", sep = ""), " </trkpt>") } res <- c(res, " </trkseg>", " </trk>", "</gpx>") writeLines(res, output) }
dat2 <- kml2timetag(kmlfile = "ttt.kml", start.time = "2015-02-04 21:39:36", end.time = "2015-02-05 23:40:50") tagkml2gpx(dat2, output = "res.gpx")
|