R语言--KML格式转换为GPX
install.packages("maptools")
library(maptools)
library(stringr)
library(XML)
kmlfile = "220200903.kml"#文件名
read_html(kmlfile)
web <- kmlfile%>%read_html(encoding = "UTF-8")
Time <- web %>% html_node("description") %>% html_text()
start.time=substr(Time,14,32)#获取开始时间
end.time=substr(Time,37,55)#获取结束时间
kmlfile = "20200903.kml"
start.time = "2020-09-03 08:30:41"
end.time = "2020-09-03 15:02:45"
require("maptools")
Sys.setlocale("LC_TIME", "English")
data <- 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(data[[1]]))
points.time2 <- format(points.time, "%Y-%m-%dT%H:%M:%SZ")
data <- cbind(data[[1]], points.time2)
colnames(data) <- c("longitude", "latitude", "altitude", "time")
Trackfile <- c('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>',"<gpx>"," <trk>"," <name>Track written by zfx</name>"," <trkseg>")
for(i in 1:nrow(data)){
Trackfile <- c(Trackfile,
paste(' <trkpt lat="',data[i,2], '" lon="',
data[i,1], '">', collapse = "", sep = ""),
paste(" <ele>", data[i,3], "</ele>", sep = ""),
paste(" <time>", data[i,4], "</time>", sep = ""),
" </trkpt>")
}
Trackfile <- c(Trackfile," </trkseg>"," </trk>","</gpx>")
writeLines(Trackfile, "20200904.gpx")
#导出文件为20200904.gpx的文件
采用Notepad++软件打开gpx文件可见: