R语言--KML格式转换为GPX
Posted 实验室科研笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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文件可见:
以上是关于R语言--KML格式转换为GPX的主要内容,如果未能解决你的问题,请参考以下文章