我已经读取了不同gpx文件到R的行,并将它们组合成一个SpatialLines对象。现在我想将gpx-names(chr-vector" names")作为属性应用于空间数据集" tracksSL"但是我被困在这里..
library(sp)
library(gdal)
setwd("D:/WEB/gardaweb")
files <- dir(pattern="*.gpx$", recursive = T, include.dirs = T)
spl <- lapply(files, function(x) {readOGR(x,"tracks")@lines[[1]]} )
lines <- lapply( spl , function(x) `@`(x , "Lines"))
tracks <- Lines(unlist(lines), ID = "Tracks")
tracksSL <- SpatialLines(list(tracks))
names <- basename(files)
str(tracksSL)
>>
Formal class 'SpatialLines' [package "sp"] with 3 slots
..@ lines :List of 1
.. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
.. .. .. ..@ Lines:List of 15
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:2322, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:2961, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:2201, 1:2] 10.7 10.7 10.7 10.7 10.7 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:3329, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:2976, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:3333, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:2484, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:746, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:3373, 1:2] 11 11 11 11 11 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:2286, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:1612, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:1315, 1:2] 10.7 10.7 10.7 10.7 10.7 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:4342, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:4168, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:2366, 1:2] 10.9 10.9 10.9 10.9 10.9 ...
.. .. .. ..@ ID : chr "Tracks"
..@ bbox : num [1:2, 1:2] 10.6 45.7 11.1 46
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr NA
str(names)
>>
chr [1:15] "601er RifugiGraziani.gpx" "Bocca dei Fortini.gpx" ...
答案 0 :(得分:0)
解决:
library(sp)
library(gdal)
library(plotKML)
setwd("D:/WEB/gardaweb")
files <- dir(pattern="*.gpx$", recursive = T, include.dirs = T)
spl <- lapply(files, function(x) {readOGR(x, "tracks")@lines[[1]]} )
str(spl)
for(i in 1:length(spl)) {slot(spl[[i]], "ID") <- as.character(i)}
tracksSL <- SpatialLines(spl, proj4string = CRS("+proj=longlat +datum=WGS84"))
summary(tracksSL)
plot(tracksSL)
df <- data.frame(names = basename(files), row.names = sapply(slot(tracksSL, "lines"), function(x) slot(x, "ID")))
tracksSLDF <- SpatialLinesDataFrame(tracksSL, data = df)
plotKML(tracksSLDF, "tracks_collection")